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

@ -314,8 +314,8 @@ void LocalParticipant::ReceiveData(unsigned char bufferSize,
void LocalParticipant::Process(Participant* sender, ParticipantMsg* msg) {} void LocalParticipant::Process(Participant* sender, ParticipantMsg* msg) {}
void LocalParticipant::Process(Participant* sender, SiteMsg* msg) { void LocalParticipant::Process(Participant* sender, SiteMsg* msg) {
std::cout << this->name << ": process NetworkId [" << (int)this->networkId std::cout << this->name << ": process Site Id " << (int)this->networkId
<< "/" << (int)msg->networkId << "]\n"; << "->" << (int)msg->networkId << "\n";
if (this->networkId != msg->networkId) { if (this->networkId != msg->networkId) {
this->networkId = msg->networkId; this->networkId = msg->networkId;
// std::cout << this->things.size() << " things\n"; // 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, InvestigateMsg* msg) {}
void LocalParticipant::Process(Participant* sender, ThingMsg* 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) { void LocalParticipant::Process(Participant* sender, NameMsg* msg) {
std::cout << this->name << ": process Name [" << (int)this->networkId std::cout << this->name << ": process Name [" << (int)msg->networkId << "/"
<< "/" << (int)msg->networkId << "]\n"; << (int)msg->thingId << "]\n";
Thing* thing = sender->Get(msg->networkId, msg->thingId); Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr) { if (thing != nullptr) {
@ -351,17 +363,20 @@ void LocalParticipant::Process(Participant* sender, NameMsg* msg) {
#endif #endif
thingName[nameLength] = '\0'; thingName[nameLength] = '\0';
thing->name = thingName; thing->name = thingName;
// std::cout << "thing name = " << thing->name << " length = " << nameLength // std::cout << "thing name = " << thing->name << " length = " <<
// nameLength
// << "\n"; // << "\n";
} }
} }
void LocalParticipant::Process(Participant* sender, PoseMsg* msg) { 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) { 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); Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr) if (thing != nullptr)
thing->ProcessBinary(msg->bytes); thing->ProcessBinary(msg->bytes);

View File

@ -11,7 +11,9 @@
#include "Participant.h" #include "Participant.h"
#if !defined(NO_STD) #if !defined(NO_STD)
#include <functional>
#include <list> #include <list>
// #include <unordered_map>
#endif #endif
#if defined(_WIN32) || defined(_WIN64) #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. /// @brief Create a participant which will try to connect to a site.
/// @param ipAddress The IP address of the site /// @param ipAddress The IP address of the site
/// @param port The port used by 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 // Note to self: one cannot specify the port used by the local participant
// now!! // now!!
@ -136,6 +140,35 @@ class LocalParticipant : public Participant {
virtual void Process(Participant* sender, NameMsg* msg); virtual void Process(Participant* sender, NameMsg* msg);
virtual void Process(Participant* sender, PoseMsg* msg); virtual void Process(Participant* sender, PoseMsg* msg);
virtual void Process(Participant* sender, BinaryMsg* 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 } // namespace RoboidControl

View File

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

View File

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

View File

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