Merge commit 'fb7413cc1310571f97f9378aceed5e82561a6378'

This commit is contained in:
Pascal Serrarens 2025-01-26 10:20:17 +01:00
commit f2d761fa69
2 changed files with 22 additions and 7 deletions

View File

@ -2,6 +2,8 @@
#include "Sensors/TemperatureSensor.h"
#include <functional>
namespace Passer {
namespace Control {
@ -14,6 +16,8 @@ SiteServer::SiteServer(int port) {
this->participants.push_back(this);
SetupUDP(port, ipAddress, 0);
Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor);
}
void SiteServer::Update(unsigned long currentTimeMs) {
@ -33,19 +37,28 @@ void SiteServer::Process(Participant *sender, ClientMsg *msg) {
void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {}
using ThingConstructor = std::function<std::unique_ptr<Thing>(
unsigned char networkId, unsigned char thingId)>;
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
template <typename ThingClass>
void SiteServer::Register(unsigned char thingType) {
thingMsgProcessors[thingType] = [](unsigned char networkId,
unsigned char thingId) {
return std::make_unique<ThingClass>(networkId, thingId);
};
}
void SiteServer::Process(ThingMsg *msg) {
Thing *thing = Thing::Get(msg->networkId, msg->thingId);
if (thing == nullptr) {
switch ((Thing::Type)msg->thingType) {
case Thing::Type::TemperatureSensor:
new TemperatureSensor(msg->networkId, msg->thingId);
break;
default:
auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
if (thingMsgProcessor != thingMsgProcessors.end()) // found item
thingMsgProcessor->second(msg->networkId, msg->thingId);
else
new Thing(this, msg->networkId, msg->thingId,
(Thing::Type)msg->thingType);
break;
}
}
}

View File

@ -12,6 +12,8 @@ public:
virtual void Update(unsigned long currentTimeMs) override;
template <typename ThingClass> void Register(unsigned char thingType);
protected:
unsigned long nextPublishMe = 0;