Added thing type registration
This commit is contained in:
parent
54ab547d5d
commit
fb7413cc13
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user