Files
SimCore/src/SimCore/Messages/Control.cpp
2023-08-15 15:03:11 +02:00

72 lines
1.8 KiB
C++

#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;
}
}