Working register

This commit is contained in:
Pascal Serrarens 2025-01-20 14:49:27 +01:00
parent 5367cc1961
commit 7867c3c8cf
2 changed files with 14 additions and 24 deletions

View File

@ -2,10 +2,6 @@
#include "Sensors/TemperatureSensor.h"
#include <functional>
#include <unordered_map>
#include <memory>
namespace Passer {
namespace Control {
@ -39,24 +35,6 @@ 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);
};
}
// template <typename ThingClass>
// void SiteServer::Register(Thing::Type thingType)
// {
// Register<ThingClass>((unsigned char)thingType);
// }
void SiteServer::Process(ThingMsg *msg) {
Thing *thing = Thing::Get(msg->networkId, msg->thingId);

View File

@ -2,6 +2,10 @@
#include "Participant.h"
#include <unordered_map>
#include <functional>
#include <memory>
namespace Passer {
namespace Control {
@ -12,8 +16,12 @@ public:
virtual void Update(unsigned long currentTimeMs) override;
template <typename ThingClass> void Register(unsigned char thingType);
// template <typename ThingClass> void Register(Thing::Type thingType);
template <typename ThingClass> void Register(unsigned char thingType) {
thingMsgProcessors[thingType] = [](unsigned char networkId,
unsigned char thingId) {
return std::make_unique<ThingClass>(networkId, thingId);
};
};
protected:
unsigned long nextPublishMe = 0;
@ -21,6 +29,10 @@ protected:
virtual void Process(Participant *sender, ClientMsg *msg) override;
virtual void Process(Participant *sender, NetworkIdMsg *msg) override;
virtual void Process(ThingMsg *msg) override;
using ThingConstructor = std::function<std::unique_ptr<Thing>(
unsigned char networkId, unsigned char thingId)>;
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
};
} // namespace Control