Refactoring

This commit is contained in:
Pascal Serrarens 2025-01-31 11:45:22 +01:00
parent bf5d949992
commit 61740913fe
23 changed files with 243 additions and 165 deletions

View File

@ -109,4 +109,48 @@ bool StartWifi(const char *wifiSsid, const char *wifiPassword,
return (!hotSpotEnabled);
}
void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION) {
Serial.println("Checking for firmware updates.");
WiFiClient client;
HTTPClient httpClient;
String versionURL = url + FIRMWARE_NAME + ".version";
httpClient.begin(client, versionURL);
int httpCode = httpClient.GET();
if (httpCode == 200) {
String newFWVersion = httpClient.getString();
Serial.print("Current firmware version: ");
Serial.println(FIRMWARE_VERSION);
Serial.print("Available firmware version: ");
Serial.println(newFWVersion);
int newVersion = newFWVersion.toInt();
if (newVersion > FIRMWARE_VERSION) {
Serial.println("Preparing to update firmware.");
String firmwareURL = url + FIRMWARE_NAME + ".bin";
t_httpUpdate_return ret = ESPhttpUpdate.update(client, firmwareURL);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s",
ESPhttpUpdate.getLastError(),
ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
break;
}
} else {
Serial.println("No Firmware update necessary.");
}
} else {
Serial.print("Http Error: ");
Serial.println(httpCode);
}
}
#endif

View File

@ -33,94 +33,3 @@ unsigned char *IMessage::ReceiveMsg(unsigned char packetSize) {
// IMessage
#pragma endregion
#pragma region Investigate
InvestigateMsg::InvestigateMsg(char *buffer) {
unsigned ix = 1; // first byte is msgId
this->networkId = buffer[ix++];
this->thingId = buffer[ix++];
}
InvestigateMsg::InvestigateMsg(unsigned char networkId, unsigned char thingId) {
this->networkId = networkId;
this->thingId = thingId;
}
unsigned char InvestigateMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
return ix;
}
// bool InvestigateMsg::Send(Participant *participant, unsigned char networkId,
// unsigned char thingId) {
// InvestigateMsg msg = InvestigateMsg(networkId, thingId);
// return msg.Send(participant);
// }
// Investigate
#pragma endregion
#pragma region PoseMsg
PoseMsg::PoseMsg(unsigned char networkId, unsigned char thingId,
unsigned char poseType, Spherical16 position,
SwingTwist16 orientation, Spherical16 linearVelocity,
Spherical16 angularVelocity) {
this->networkId = networkId;
this->thingId = thingId;
this->poseType = poseType;
this->position = position;
this->orientation = orientation;
this->linearVelocity = linearVelocity;
this->angularVelocity = angularVelocity;
}
PoseMsg::PoseMsg(const char *buffer) {
unsigned char ix = 1; // First byte is msg id
this->networkId = buffer[ix++];
this->thingId = buffer[ix++];
this->poseType = buffer[ix++];
this->position = LowLevelMessages::ReceiveSpherical16(buffer, &ix);
this->orientation = LowLevelMessages::ReceiveQuat32(buffer, &ix);
}
unsigned char PoseMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = PoseMsg::id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
buffer[ix++] = this->poseType;
if ((this->poseType & Pose_Position) != 0)
LowLevelMessages::SendSpherical16(buffer, &ix, this->position);
if ((this->poseType & Pose_Orientation) != 0)
LowLevelMessages::SendQuat32(buffer, &ix, this->orientation);
if ((this->poseType & Pose_LinearVelocity) != 0)
LowLevelMessages::SendSpherical16(buffer, &ix, this->linearVelocity);
if ((this->poseType & Pose_AngularVelocity) != 0)
LowLevelMessages::SendSpherical16(buffer, &ix, this->angularVelocity);
return ix;
}
// Pose
#pragma endregion
#pragma region DestroyMsg
DestroyMsg::DestroyMsg(unsigned char networkId, Thing *thing) {
this->networkId = networkId;
this->thingId = thing->id;
}
unsigned char DestroyMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
return ix;
}
// DestroyMsg
#pragma endregion

View File

@ -21,59 +21,6 @@ public:
// bool SendTo(Participant *participant);
};
class InvestigateMsg : public IMessage {
public:
static const unsigned char id = 0x81;
static const unsigned char length = 3;
unsigned char networkId;
unsigned char thingId;
InvestigateMsg(char *buffer);
InvestigateMsg(unsigned char networkId, unsigned char thingId);
virtual unsigned char Serialize(char *buffer) override;
};
class PoseMsg : public IMessage {
public:
static const unsigned char id = 0x10;
unsigned char length = 4 + 4 + 4;
unsigned char networkId;
unsigned char thingId;
unsigned char poseType;
static const unsigned char Pose_Position = 0x01;
static const unsigned char Pose_Orientation = 0x02;
static const unsigned char Pose_LinearVelocity = 0x04; // For future use
static const unsigned char Pose_AngularVelocity = 0x08; // For future use
Spherical16 position;
SwingTwist16 orientation;
Spherical16 linearVelocity;
Spherical16 angularVelocity;
PoseMsg(unsigned char networkId, unsigned char thingId,
unsigned char poseType, Spherical16 position,
SwingTwist16 orientation, Spherical16 linearVelocity = Spherical16(),
Spherical16 angularVelocity = Spherical16());
PoseMsg(const char *buffer);
virtual unsigned char Serialize(char *buffer) override;
};
class DestroyMsg : public IMessage {
public:
static const unsigned char id = 0x20;
static const unsigned length = 3;
unsigned char networkId;
unsigned char thingId;
DestroyMsg(unsigned char networkId, Thing *thing);
virtual unsigned char Serialize(char *buffer) override;
};
} // namespace Control
} // namespace Passer

View File

@ -6,6 +6,8 @@ ClientMsg::ClientMsg(char networkId) { this->networkId = networkId; }
ClientMsg::ClientMsg(const char *buffer) { this->networkId = buffer[1]; }
ClientMsg::~ClientMsg() {}
unsigned char ClientMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;

View File

@ -16,6 +16,7 @@ public:
ClientMsg(char networkId);
ClientMsg(const char *buffer);
virtual ~ClientMsg();
virtual unsigned char Serialize(char *buffer) override;
};

View File

@ -19,7 +19,7 @@ public:
CustomMsg(char *buffer);
CustomMsg(unsigned char networkId, Thing *thing);
~CustomMsg();
virtual ~CustomMsg();
virtual unsigned char Serialize(char *buffer) override;

22
Messages/DestroyMsg.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "DestroyMsg.h"
namespace Passer {
namespace Control {
DestroyMsg::DestroyMsg(unsigned char networkId, Thing *thing) {
this->networkId = networkId;
this->thingId = thing->id;
}
DestroyMsg::~DestroyMsg() {}
unsigned char DestroyMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
return ix;
}
} // namespace Control
} // namespace Passer

19
Messages/DestroyMsg.h Normal file
View File

@ -0,0 +1,19 @@
#include "Messages.h"
namespace Passer {
namespace Control {
class DestroyMsg : public IMessage {
public:
static const unsigned char id = 0x20;
static const unsigned length = 3;
unsigned char networkId;
unsigned char thingId;
DestroyMsg(unsigned char networkId, Thing *thing);
virtual ~DestroyMsg();
virtual unsigned char Serialize(char *buffer) override;
};
} // namespace Control
} // namespace Passer

View File

@ -0,0 +1,30 @@
#include "InvestigateMsg.h"
#pragma region Investigate
InvestigateMsg::InvestigateMsg(char *buffer) {
unsigned ix = 1; // first byte is msgId
this->networkId = buffer[ix++];
this->thingId = buffer[ix++];
}
InvestigateMsg::InvestigateMsg(unsigned char networkId, unsigned char thingId) {
this->networkId = networkId;
this->thingId = thingId;
}
InvestigateMsg::~InvestigateMsg() {}
unsigned char InvestigateMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
return ix;
}
// bool InvestigateMsg::Send(Participant *participant, unsigned char networkId,
// unsigned char thingId) {
// InvestigateMsg msg = InvestigateMsg(networkId, thingId);
// return msg.Send(participant);
// }
// Investigate
#pragma endregion

15
Messages/InvestigateMsg.h Normal file
View File

@ -0,0 +1,15 @@
#include "Messages.h"
class InvestigateMsg : public IMessage {
public:
static const unsigned char id = 0x81;
static const unsigned char length = 3;
unsigned char networkId;
unsigned char thingId;
InvestigateMsg(char *buffer);
InvestigateMsg(unsigned char networkId, unsigned char thingId);
virtual ~InvestigateMsg();
virtual unsigned char Serialize(char *buffer) override;
};

View File

@ -35,6 +35,8 @@ ModelUrlMsg::ModelUrlMsg(unsigned char networkId, Thing *thing) {
this->url = thing->modelUrl; // dangerous!
}
ModelUrlMsg::~ModelUrlMsg() {}
unsigned char ModelUrlMsg::Serialize(char *buffer) {
if (this->urlLength == 0 || this->url == nullptr)
return 0;

View File

@ -18,6 +18,7 @@ public:
ModelUrlMsg(unsigned char networkId, Thing *thing);
// ModelUrlMsg(unsigned char networkId, unsigned char thingId,
// unsigned char urlLegth, const char *url, float scale = 1);
virtual ~ModelUrlMsg();
virtual unsigned char Serialize(char *buffer) override;
};

View File

@ -32,6 +32,8 @@ NameMsg::NameMsg(unsigned char networkId, Thing *thing) {
// this->nameLength = nameLength;
// }
NameMsg::~NameMsg() {}
unsigned char NameMsg::Serialize(char *buffer) {
if (this->nameLength == 0 || this->name == nullptr)
return 0;

View File

@ -16,6 +16,7 @@ public:
NameMsg(unsigned char networkId, Thing *thing);
// NameMsg(unsigned char networkId, unsigned char thingId, const char *name,
// unsigned char nameLength);
virtual ~NameMsg();
virtual unsigned char Serialize(char *buffer) override;
};

View File

@ -9,6 +9,8 @@ NetworkIdMsg::NetworkIdMsg(unsigned char networkId) {
this->networkId = networkId;
}
NetworkIdMsg::~NetworkIdMsg() {}
unsigned char NetworkIdMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;

View File

@ -11,6 +11,7 @@ public:
NetworkIdMsg(const char *buffer);
NetworkIdMsg(unsigned char networkId);
virtual ~NetworkIdMsg();
virtual unsigned char Serialize(char *buffer) override;
// static NetworkIdMsg Receive(char *buffer, unsigned char bufferSize);

43
Messages/PoseMsg.cpp Normal file
View File

@ -0,0 +1,43 @@
#include "PoseMsg.h"
#include "LowLevelMessages.h"
PoseMsg::PoseMsg(unsigned char networkId, unsigned char thingId,
unsigned char poseType, Spherical16 position,
SwingTwist16 orientation, Spherical16 linearVelocity,
Spherical16 angularVelocity) {
this->networkId = networkId;
this->thingId = thingId;
this->poseType = poseType;
this->position = position;
this->orientation = orientation;
this->linearVelocity = linearVelocity;
this->angularVelocity = angularVelocity;
}
PoseMsg::PoseMsg(const char *buffer) {
unsigned char ix = 1; // First byte is msg id
this->networkId = buffer[ix++];
this->thingId = buffer[ix++];
this->poseType = buffer[ix++];
this->position = LowLevelMessages::ReceiveSpherical16(buffer, &ix);
this->orientation = LowLevelMessages::ReceiveQuat32(buffer, &ix);
}
PoseMsg::~PoseMsg() {}
unsigned char PoseMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = PoseMsg::id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
buffer[ix++] = this->poseType;
if ((this->poseType & Pose_Position) != 0)
LowLevelMessages::SendSpherical16(buffer, &ix, this->position);
if ((this->poseType & Pose_Orientation) != 0)
LowLevelMessages::SendQuat32(buffer, &ix, this->orientation);
if ((this->poseType & Pose_LinearVelocity) != 0)
LowLevelMessages::SendSpherical16(buffer, &ix, this->linearVelocity);
if ((this->poseType & Pose_AngularVelocity) != 0)
LowLevelMessages::SendSpherical16(buffer, &ix, this->angularVelocity);
return ix;
}

30
Messages/PoseMsg.h Normal file
View File

@ -0,0 +1,30 @@
#include "Messages.h"
class PoseMsg : public IMessage {
public:
static const unsigned char id = 0x10;
unsigned char length = 4 + 4 + 4;
unsigned char networkId;
unsigned char thingId;
unsigned char poseType;
static const unsigned char Pose_Position = 0x01;
static const unsigned char Pose_Orientation = 0x02;
static const unsigned char Pose_LinearVelocity = 0x04; // For future use
static const unsigned char Pose_AngularVelocity = 0x08; // For future use
Spherical16 position;
SwingTwist16 orientation;
Spherical16 linearVelocity;
Spherical16 angularVelocity;
PoseMsg(unsigned char networkId, unsigned char thingId,
unsigned char poseType, Spherical16 position,
SwingTwist16 orientation, Spherical16 linearVelocity = Spherical16(),
Spherical16 angularVelocity = Spherical16());
PoseMsg(const char *buffer);
virtual ~PoseMsg();
virtual unsigned char Serialize(char *buffer) override;
};

View File

@ -30,6 +30,8 @@ ThingMsg::ThingMsg(unsigned char networkId, Thing *thing) {
// this->parentId = parentId;
// }
ThingMsg::~ThingMsg() {}
unsigned char ThingMsg::Serialize(char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;

View File

@ -16,6 +16,7 @@ public:
ThingMsg(unsigned char networkId, Thing *thing);
// ThingMsg(unsigned char networkId, unsigned char thingId,
// unsigned char thingType, unsigned char parentId);
virtual ~ThingMsg();
virtual unsigned char Serialize(char *buffer) override;
};

View File

@ -49,7 +49,8 @@ void Participant::begin() {
SetupUDP(this->localPort, this->ipAddress, this->port);
}
void Participant::SetupUDP(int localPort, const char *remoteIpAddress, int remotePort) {
void Participant::SetupUDP(int localPort, const char *remoteIpAddress,
int remotePort) {
#if defined(_WIN32) || defined(_WIN64)
UdpWindows *thisWindows = static_cast<UdpWindows *>(this);
thisWindows->Setup(localPort, remoteIpAddress, remotePort);
@ -70,7 +71,7 @@ void Participant::Update() { this->Update(millis()); }
void Participant::Update(unsigned long currentTimeMs) {
if (this->connected == false)
begin();
if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) {
ClientMsg *msg = new ClientMsg(this->networkId);
this->Publish(msg);
@ -115,15 +116,15 @@ Participant *Participant::AddParticipant(const char *ipAddress, int port) {
void Participant::SendThingInfo(Thing *thing) {
std::cout << "Send thing info\n";
IMessage *msg = new ThingMsg(this->networkId, thing);
this->Send(msg);
delete msg;
msg = new NameMsg(this->networkId, thing);
this->Send(msg);
delete msg;
msg = new ModelUrlMsg(this->networkId, thing);
this->Send(msg);
delete msg;
ThingMsg *thingMsg = new ThingMsg(this->networkId, thing);
this->Send(thingMsg);
delete thingMsg;
NameMsg *nameMsg = new NameMsg(this->networkId, thing);
this->Send(nameMsg);
delete nameMsg;
ModelUrlMsg *modelMsg = new ModelUrlMsg(this->networkId, thing);
this->Send(modelMsg);
delete modelMsg;
}
void Passer::Control::Participant::PublishThingInfo(Thing *thing) {
@ -245,9 +246,10 @@ void Participant::Process(ThingMsg *msg) {}
void Participant::Process(NameMsg *msg) {
Thing *thing = Thing::Get(msg->networkId, msg->thingId);
if (thing != nullptr) {
thing->name = new char[strlen(msg->name)];
strcpy(thing->name, msg->name);
std::cout << "thing name = " << thing->name << "\n";
char *thingName = new char[strlen(msg->name)];
strcpy(thingName, msg->name);
thing->name = thingName;
std::cout << "thing name = " << thing->name << "\n";
}
}

View File

@ -1,12 +1,14 @@
#pragma once
#include "ClientMsg.h"
#include "Messages.h"
#include "Messages/ClientMsg.h"
#include "Messages/CustomMsg.h"
#include "ModelUrlMsg.h"
#include "NameMsg.h"
#include "NetworkIdMsg.h"
#include "ThingMsg.h"
#include "Messages/InvestigateMsg.h"
#include "Messages/ModelUrlMsg.h"
#include "Messages/NameMsg.h"
#include "Messages/NetworkIdMsg.h"
#include "Messages/PoseMsg.h"
#include "Messages/ThingMsg.h"
#include <list>

View File

@ -73,7 +73,7 @@ protected:
public:
/// @brief The type of Thing
unsigned char type = 0;
char *name = nullptr;
const char *name = nullptr;
const char *modelUrl = nullptr;
float modelScale = 1;
// protected Sensor sensor;