Simulated Accelerometer can be received

This commit is contained in:
Pascal Serrarens 2025-04-03 17:57:08 +02:00
parent 7b17d8459a
commit b32ac68966
5 changed files with 87 additions and 42 deletions

View File

@ -271,7 +271,7 @@ void LocalParticipant::ReceiveData(unsigned char packetSize,
void LocalParticipant::ReceiveData(unsigned char bufferSize,
Participant* remoteParticipant) {
unsigned char msgId = this->buffer[0];
//std::cout << "receive msg " << (int)msgId << "\n";
// std::cout << "receive msg " << (int)msgId << "\n";
switch (msgId) {
case ParticipantMsg::id: {
ParticipantMsg* msg = new ParticipantMsg(this->buffer);
@ -314,8 +314,8 @@ void LocalParticipant::ReceiveData(unsigned char bufferSize,
void LocalParticipant::Process(Participant* sender, ParticipantMsg* msg) {}
void LocalParticipant::Process(Participant* sender, SiteMsg* msg) {
std::cout << this->name << ": process NetworkId [" << (int)this->networkId
<< "/" << (int)msg->networkId << "]\n";
std::cout << this->name << ": process Site Id " << (int)this->networkId
<< "->" << (int)msg->networkId << "\n";
if (this->networkId != msg->networkId) {
this->networkId = msg->networkId;
// std::cout << this->things.size() << " things\n";
@ -327,12 +327,24 @@ void LocalParticipant::Process(Participant* sender, SiteMsg* msg) {
void LocalParticipant::Process(Participant* sender, InvestigateMsg* msg) {}
void LocalParticipant::Process(Participant* sender, ThingMsg* msg) {
std::cout << this->name << ": process Thing [" << (int)this->networkId << "/" << (int)msg->networkId << "]\n";
std::cout << this->name << ": process Thing [" << (int)msg->networkId << "/"
<< (int)msg->thingId << "]\n";
#if !defined(NO_STD)
auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
//Thing* newThing;
if (thingMsgProcessor != thingMsgProcessors.end()) { // found item
//newThing =
thingMsgProcessor->second(sender, msg->networkId, msg->thingId);
} else
//newThing =
new Thing(sender, msg->networkId, msg->thingId,
(Thing::Type)msg->thingType);
#endif
}
void LocalParticipant::Process(Participant* sender, NameMsg* msg) {
std::cout << this->name << ": process Name [" << (int)this->networkId
<< "/" << (int)msg->networkId << "]\n";
std::cout << this->name << ": process Name [" << (int)msg->networkId << "/"
<< (int)msg->thingId << "]\n";
Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr) {
@ -351,17 +363,20 @@ void LocalParticipant::Process(Participant* sender, NameMsg* msg) {
#endif
thingName[nameLength] = '\0';
thing->name = thingName;
// std::cout << "thing name = " << thing->name << " length = " << nameLength
// std::cout << "thing name = " << thing->name << " length = " <<
// nameLength
// << "\n";
}
}
void LocalParticipant::Process(Participant* sender, PoseMsg* msg) {
std::cout << this->name << ": process Pose [" << (int)this->networkId << "/" << (int)msg->networkId << "]\n";
std::cout << this->name << ": process Pose [" << (int)this->networkId << "/"
<< (int)msg->networkId << "]\n";
}
void LocalParticipant::Process(Participant* sender, BinaryMsg* msg) {
std::cout << this->name << ": process Binary [" << (int)this->networkId << "/" << (int)msg->networkId << "]\n";
std::cout << this->name << ": process Binary [" << (int)this->networkId << "/"
<< (int)msg->networkId << "]\n";
Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr)
thing->ProcessBinary(msg->bytes);

View File

@ -11,7 +11,9 @@
#include "Participant.h"
#if !defined(NO_STD)
#include <functional>
#include <list>
// #include <unordered_map>
#endif
#if defined(_WIN32) || defined(_WIN64)
@ -51,7 +53,9 @@ class LocalParticipant : public Participant {
/// @brief Create a participant which will try to connect to a site.
/// @param ipAddress The IP address of the site
/// @param port The port used by the site
LocalParticipant(const char* ipAddress, int port = 7681, int localPort = 7681);
LocalParticipant(const char* ipAddress,
int port = 7681,
int localPort = 7681);
// Note to self: one cannot specify the port used by the local participant
// now!!
@ -136,6 +140,35 @@ class LocalParticipant : public Participant {
virtual void Process(Participant* sender, NameMsg* msg);
virtual void Process(Participant* sender, PoseMsg* msg);
virtual void Process(Participant* sender, BinaryMsg* msg);
#if !defined(NO_STD)
public:
using ThingConstructor = std::function<Thing*(Participant* participant,
unsigned char networkId,
unsigned char thingId)>;
template <typename ThingClass>
void Register(unsigned char thingType) {
thingMsgProcessors[thingType] = [](Participant* participant,
unsigned char networkId,
unsigned char thingId) {
return new ThingClass(participant, networkId, thingId);
};
};
template <typename ThingClass>
void Register2(unsigned char thingType, ThingConstructor f) {
thingMsgProcessors[thingType] = [f](Participant* participant,
unsigned char networkId,
unsigned char thingId) {
return f(participant, networkId, thingId);
};
};
protected:
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
#endif
};
} // namespace RoboidControl

View File

@ -25,7 +25,7 @@ SiteServer::SiteServer(int port) {
SetupUDP(port, ipAddress, 0);
#if !defined(NO_STD)
Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor);
//Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor);
#endif
}
@ -45,19 +45,20 @@ void SiteServer::Process(Participant* sender, SiteMsg* msg) {}
void SiteServer::Process(Participant* sender, ThingMsg* msg) {
Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing == nullptr) {
#if defined(NO_STD)
// #if defined(NO_STD)
new Thing(sender, msg->networkId, msg->thingId,
(Thing::Type)msg->thingType);
#else
auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
Thing* newThing;
if (thingMsgProcessor != thingMsgProcessors.end()) // found item
newThing =
thingMsgProcessor->second(sender, msg->networkId, msg->thingId);
else
newThing = new Thing(sender, msg->networkId, msg->thingId,
(Thing::Type)msg->thingType);
#endif
// #else
// auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
// //Thing* newThing;
// if (thingMsgProcessor != thingMsgProcessors.end()) // found item
// //newThing =
// thingMsgProcessor->second(sender, msg->networkId, msg->thingId);
// else
// //newThing =
// new Thing(sender, msg->networkId, msg->thingId,
// (Thing::Type)msg->thingType);
// #endif
}
}

View File

@ -17,16 +17,16 @@ class SiteServer : public LocalParticipant {
// virtual void Update(unsigned long currentTimeMs = 0) override;
#if !defined(NO_STD)
template <typename ThingClass>
void Register(unsigned char thingType) {
thingMsgProcessors[thingType] = [](Participant* participant,
unsigned char networkId,
unsigned char thingId) {
return new ThingClass(participant, networkId, thingId);
};
};
#endif
// #if !defined(NO_STD)
// template <typename ThingClass>
// void Register(unsigned char thingType) {
// thingMsgProcessors[thingType] = [](Participant* participant,
// unsigned char networkId,
// unsigned char thingId) {
// return new ThingClass(participant, networkId, thingId);
// };
// };
// #endif
protected:
unsigned long nextPublishMe = 0;
@ -35,12 +35,12 @@ class SiteServer : public LocalParticipant {
virtual void Process(Participant* sender, SiteMsg* msg) override;
virtual void Process(Participant* sender, ThingMsg* msg) override;
#if !defined(NO_STD)
using ThingConstructor = std::function<Thing*(Participant* participant,
unsigned char networkId,
unsigned char thingId)>;
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
#endif
// #if !defined(NO_STD)
// using ThingConstructor = std::function<Thing*(Participant* participant,
// unsigned char networkId,
// unsigned char thingId)>;
// std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
// #endif
};
} // namespace RoboidControl

View File

@ -4,10 +4,6 @@
namespace RoboidControl {
// TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {}
// TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {}
TemperatureSensor::TemperatureSensor(Participant* participant,
unsigned char networkId,
unsigned char thingId)