Everything re-added?

This commit is contained in:
Pascal Serrarens 2024-12-13 17:12:06 +01:00
parent de288bcc14
commit 6518cb62be
8 changed files with 345 additions and 0 deletions

38
CoreThing.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "CoreThing.h"
CoreThing::CoreThing(
// Participant *client,
unsigned char networkId, unsigned char thingId, unsigned char thingType) {
// this->client = client;
this->id = thingId;
this->type = thingType;
this->networkId = networkId;
this->Init();
// CoreThings::Add(this);
}
void CoreThing::Init() {}
// CoreThing *CoreThings::allThings[256] = {nullptr};
// CoreThing *CoreThings::Get(unsigned char networkId, unsigned char thingId) {
// for (unsigned char ix = 0; ix < 256; ix++) {
// CoreThing *thing = allThings[ix];
// if (thing == nullptr)
// continue;
// if (thing->networkId == networkId && thing->id == thingId)
// return thing;
// }
// return nullptr;
// }
// bool CoreThings::Add(CoreThing *newThing) {
// for (unsigned char ix = 0; ix < 256; ix++) {
// CoreThing *thing = allThings[ix];
// if (thing == nullptr) {
// allThings[ix] = newThing;
// return true;
// }
// }
// return false;
// }

41
CoreThing.h Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include "Participant.h"
namespace Passer::Control {
class CoreThing {
public:
// Participant *client;
unsigned char networkId;
unsigned char id;
// CoreThing *parent;
unsigned char type;
const char *name;
const char *modelUrl;
// protected Sensor sensor;
public:
CoreThing(
// Participant *client,
unsigned char networkId, unsigned char thingId,
unsigned char thingType = 0);
protected:
virtual void Init();
// Things
};
// class CoreThings {
// friend class CoreThing;
// public:
// static CoreThing *allThings[];
// public:
// static CoreThing *Get(unsigned char networkId, unsigned char thingId);
// static bool Add(CoreThing *thing);
// };
} // namespace Passer::Control

12
LowLevelMessages.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "LowLevelMessages.h"
#include "../float16/float16.h"
void LowLevelMessages::SendFloat16(unsigned char *buffer, unsigned char *ix,
float value) {
float16 value16 = float16(value);
short binary = value16.getBinary();
buffer[(*ix)++] = (binary >> 8) & 0xFF;
buffer[(*ix)++] = binary & 0xFF;
}

12
LowLevelMessages.h Normal file
View File

@ -0,0 +1,12 @@
#include "../LinearAlgebra/Spherical.h"
namespace Passer::Control {
class LowLevelMessages {
public:
static void SendFloat16(unsigned char *buffer, unsigned char *ix,
float value);
};
} // namespace Passer::Control
using namespace Passer::Control;

133
Messages.cpp Normal file
View File

@ -0,0 +1,133 @@
#include "Messages.h"
#include "LowLevelMessages.h"
#include "string.h"
#pragma region IMessage
unsigned char IMessage::Serialize(unsigned char *buffer) { return 0; }
bool IMessage::SendMsg(Participant *client, IMessage msg) {
// return SendMsg(client, client.buffer, );nameLength
return client->SendBuffer(msg.Serialize(client->buffer));
}
#pragma endregion
#pragma region Investigate
InvestigateMsg::InvestigateMsg(unsigned char networkId, unsigned char thingId) {
this->networkId = networkId;
this->thingId = thingId;
}
unsigned char InvestigateMsg::Serialize(unsigned char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
return ix;
}
bool InvestigateMsg::Send(Participant *client, unsigned char networkId,
unsigned char thingId) {
InvestigateMsg msg = InvestigateMsg(networkId, thingId);
return SendMsg(client, msg);
}
// Investigate
#pragma endregion
#pragma region Thing
ThingMsg::ThingMsg(unsigned char networkId, unsigned char thingId,
unsigned char thingType, unsigned char parentId) {
this->networkId = networkId;
this->thingId = thingId;
this->thingType = thingType;
this->parentId = parentId;
}
unsigned char ThingMsg::Serialize(unsigned char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
buffer[ix++] = this->thingType;
buffer[ix++] = this->parentId;
return ix;
}
bool ThingMsg::Send(Participant *client, unsigned char networkId,
unsigned char thingId, unsigned char thingType,
unsigned char parentId) {
ThingMsg msg = ThingMsg(networkId, thingId, thingType, parentId);
return SendMsg(client, msg);
}
// Thing
#pragma endregion
#pragma region Name
NameMsg::NameMsg(unsigned char networkId, unsigned char thingId,
const char *name, unsigned char nameLength) {
this->networkId = networkId;
this->thingId = thingId;
this->name = name;
this->nameLength = nameLength;
}
unsigned char NameMsg::Serialize(unsigned char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
buffer[ix++] = this->nameLength;
for (int nameIx = 0; nameIx < this->nameLength; nameIx++)
buffer[ix++] = this->name[nameIx];
return ix;
}
bool NameMsg::Send(Participant *client, CoreThing *thing,
unsigned char nameLength) {
if (thing->name == nullptr)
return true; // nothing sent, but still a success!
if (strlen(thing->name) == 0)
return true;
NameMsg msg = NameMsg(thing->networkId, thing->id, thing->name, nameLength);
return SendMsg(client, msg);
}
// Name
#pragma endregion
#pragma region ModelUrl
ModelUrlMsg::ModelUrlMsg(unsigned char networkId, unsigned char thingId,
unsigned char urlLength, const char *url,
float scale) {
this->networkId = networkId;
this->thingId = thingId;
this->urlLength = urlLength;
this->url = url;
this->scale = scale;
}
unsigned char ModelUrlMsg::Serialize(unsigned char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
LowLevelMessages::SendFloat16(buffer, &ix, this->scale);
buffer[ix++] = this->urlLength;
for (int urlIx = 0; urlIx < this->urlLength; urlIx++)
buffer[ix++] = url[urlIx];
return ix;
}
// Model Url
#pragma endregion

83
Messages.h Normal file
View File

@ -0,0 +1,83 @@
#pragma once
#include "../float16/float16.h"
#include "CoreThing.h"
#include "Participant.h"
namespace Passer::Control {
class IMessage {
public:
virtual unsigned char Serialize(unsigned char *buffer);
static bool SendMsg(Participant *client, IMessage msg);
};
class InvestigateMsg : public IMessage {
public:
static const unsigned char id = 0x81;
static const unsigned char length = 3;
unsigned char networkId;
unsigned char thingId;
InvestigateMsg(unsigned char networkId, unsigned char thingId);
virtual unsigned char Serialize(unsigned char *buffer) override;
static bool Send(Participant *client, unsigned char networkId,
unsigned char thingId);
};
class ThingMsg : public IMessage {
public:
static const unsigned char id = 0x80;
static const unsigned char length = 5;
unsigned char networkId;
unsigned char thingId;
unsigned char thingType;
unsigned char parentId;
ThingMsg(unsigned char networkId, unsigned char thingId,
unsigned char thingType, unsigned char parentId);
virtual unsigned char Serialize(unsigned char *buffer) override;
static bool Send(Participant *client, unsigned char networkId,
unsigned char thingId, unsigned char thingType,
unsigned char parentId);
};
class NameMsg : public IMessage {
public:
static const unsigned char id = 0x91;
static const unsigned char length = 4;
unsigned char networkId;
unsigned char thingId;
unsigned char nameLength;
const char *name;
NameMsg(unsigned char networkId, unsigned char thingId, const char *name,
unsigned char nameLength);
virtual unsigned char Serialize(unsigned char *buffer) override;
static bool Send(Participant *client, CoreThing *thing,
unsigned char nameLength);
};
class ModelUrlMsg : public IMessage {
public:
static const unsigned char id = 0x90;
unsigned char networkId;
unsigned char thingId;
float scale;
unsigned char urlLength;
const char *url;
ModelUrlMsg(unsigned char networkId, unsigned char thingId,
unsigned char urlLegth, const char *url, float scale = 1);
virtual unsigned char Serialize(unsigned char *buffer) override;
};
} // namespace Passer::Control
using namespace Passer::Control;

10
Participant.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "Participant.h"
// bool Passer::Control::ControlClient::SendMsg(unsigned char *buffer,
// unsigned char bufferSize) {
// return false;
// }
bool Participant::SendBuffer(unsigned char bufferSize) { return false; }
bool Participant::PublishBuffer(unsigned char bufferSize) { return false; }

16
Participant.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
namespace Passer::Control {
class Participant {
public:
// unsigned char *buffer;
// bool SendMsg(unsigned char *buffer, unsigned char bufferSize);
unsigned char buffer[1024];
virtual bool SendBuffer(unsigned char bufferSize);
virtual bool PublishBuffer(unsigned char bufferSize);
};
} // namespace Passer::Control
using namespace Passer::Control;