ADD: added controlmessage

This commit is contained in:
Henry Winkel
2023-08-15 15:03:11 +02:00
parent 03b6b79878
commit ff894bdfe4
5 changed files with 139 additions and 1 deletions

View File

@@ -42,7 +42,9 @@ add_library(SimCore STATIC
include/SimCore/Messages/Emission.hpp
src/SimCore/Messages/Emission.cpp
include/SimCore/Messages/Control.hpp
src/SimCore/Messages/Control.cpp
include/SimCore/Messages/Protos/Control.pb.cc
include/SimCore/Messages/Protos/GeocentricPosition.pb.cc
include/SimCore/Messages/Protos/Identifier.pb.cc

View File

@@ -0,0 +1,46 @@
#pragma once
#include <SimCore/Identifier.hpp>
#include <WHISPER/Messages/Message.hpp>
#include <cstdint>
#include <memory>
#include <string>
#include <SimCore/Messages/Protos/Control.pb.h>
namespace SimCore
{
class Control
{
public:
Control(const SimCore::Identifier SenderID,ControlType type,std::string data);
const ControlType Type;
std::string Data;
uint64_t Timestamp;
/// @brief return a WHISPER Message out of a simtrack object
/// @param parentID
/// @return WHISPER::Message
WHISPER::Message buildMessage();
/// @brief creates a SimTrack out of a whisper message
/// @param msg
/// @return SimTrack Oject
static std::unique_ptr<SimCore::Control> unpack(WHISPER::Message msg);
/// @brief creates a SimTrack out of a string message
/// @param msg
/// @return SimTrack Oject
static std::unique_ptr<SimCore::Control> unpack(std::string msgString);
private:
const SimCore::Identifier ID_;
};
}

View File

@@ -0,0 +1,9 @@
syntax = "proto3";
package messages.control;
message control{
uint32 type = 1;
string data = 2;
optional uint64 timestamp = 3;
}

View File

@@ -78,6 +78,16 @@ enum TrackKind : std::uint8_t {
};
enum ControlType : std::uint32_t
{
IDENTIFY,
START,
STOP,
CREATE_ENTITY,
DELETE_ENTITY,
GET_CONTROL
};

View File

@@ -0,0 +1,71 @@
#include "SimCore/Identifier.hpp"
#include "SimCore/Messages/Protos/Control.pb.h"
#include "SimCore/SimCore.hpp"
#include <SimCore/Messages/Control.hpp>
#include <string>
namespace SimCore
{
Control::Control(const SimCore::Identifier SenderID,ControlType type,std::string data):ID_(SenderID),Type(type),Data(data)
{
}
WHISPER::Message Control::buildMessage()
{
WHISPER::Message msg(ID_.getUUID(),WHISPER::MsgTopics::MANAGEMENT , WHISPER::MsgType::COMMAND);
messages::control::control control;
control.set_type(Type);
control.set_data(Data);
control.set_timestamp(Timestamp);
auto any = std::make_shared<google::protobuf::Any>();
any->PackFrom(control);
msg.addPayLoad(any);
return msg;
}
std::unique_ptr<SimCore::Control> Control::unpack(WHISPER::Message msg)
{
auto m = msg.getProtoMessage();
auto controlMsg = messages::control::control();
if(m.payload().Is<messages::control::control>())
{
m.payload().UnpackTo(&controlMsg);
SimCore::Identifier id(msg.senderUUID_);
ControlType kind = (SimCore::ControlType)controlMsg.type();
std::string data = controlMsg.data();
auto control = std::make_unique<Control>(id,kind,data);
if (controlMsg.has_timestamp()) control->Timestamp = controlMsg.timestamp();
// SimCore::Control control(id,kind,data);
return control;
}
return nullptr;
}
std::unique_ptr<SimCore::Control> Control::unpack(std::string msgString)
{
if (std::empty(msgString) == false && msgString != "NULL")
{
WHISPER::Message whisperMessage(msgString);
return std::move(unpack(whisperMessage));
}
return nullptr;
}
}