Added temp sensor support

This commit is contained in:
Pascal Serrarens 2025-01-11 12:04:34 +01:00
parent f591d34d97
commit c092d029fc
12 changed files with 91 additions and 31 deletions

View File

@ -27,7 +27,7 @@ else()
.
LinearAlgebra
)
file(GLOB srcs *.cpp)
file(GLOB srcs *.cpp Sensors/*.cpp)
add_library(ControlCore STATIC ${srcs})
enable_testing()

View File

@ -1,3 +1,5 @@
#pragma once
#include "Messages.h"
namespace Passer {

View File

@ -112,7 +112,7 @@ CustomMsg::CustomMsg(char *buffer) {
unsigned char ix = 1;
this->networkId = buffer[ix++];
this->thingId = buffer[ix++];
this->data =
this->bytes =
buffer + ix; // This is only valid because the code ensures the the msg
// lifetime is shorter than the buffer lifetime...
}

View File

@ -71,8 +71,8 @@ public:
unsigned char thingId;
Thing *thing;
unsigned char dataSize;
char *data;
unsigned char bytesSize;
char *bytes;
CustomMsg(char *buffer);
CustomMsg(unsigned char networkId, Thing *thing);

View File

@ -174,20 +174,24 @@ void Participant::ReceiveData(unsigned char bufferSize,
delete msg;
} break;
case InvestigateMsg::id: {
InvestigateMsg msg = InvestigateMsg(this->buffer);
ProcessInvestigateMsg(msg);
InvestigateMsg *msg = new InvestigateMsg(this->buffer);
Process(msg);
delete msg;
} break;
case ThingMsg::id: {
ThingMsg msg = ThingMsg(this->buffer);
ProcessThingMsg(msg);
ThingMsg *msg = new ThingMsg(this->buffer);
Process(msg);
delete msg;
} break;
case PoseMsg::id: {
PoseMsg msg = PoseMsg(this->buffer);
ProcessPoseMsg(msg);
PoseMsg *msg = new PoseMsg(this->buffer);
Process(msg);
delete msg;
} break;
case CustomMsg::id: {
CustomMsg msg = CustomMsg(this->buffer);
ProcessCustomMsg(msg);
CustomMsg *msg = new CustomMsg(this->buffer);
Process(msg);
delete msg;
} break;
};
}
@ -213,13 +217,17 @@ void Participant::Process(Participant *sender, NetworkIdMsg *msg) {
}
}
void Participant::ProcessInvestigateMsg(InvestigateMsg msg) {}
void Participant::Process(InvestigateMsg *msg) {}
void Participant::ProcessThingMsg(ThingMsg msg) {}
void Participant::Process(ThingMsg *msg) {}
void Passer::Control::Participant::ProcessPoseMsg(PoseMsg msg) {}
void Participant::Process(PoseMsg *msg) {}
void Participant::ProcessCustomMsg(CustomMsg msg) {}
void Participant::Process(CustomMsg *msg) {
Thing *thing = Thing::Get(msg->networkId, msg->thingId);
if (thing != nullptr)
thing->ProcessBytes(msg->bytes);
}
// Receive
#pragma endregion

View File

@ -85,10 +85,10 @@ protected:
virtual void Process(Participant *sender, ClientMsg *msg);
virtual void Process(Participant *sender, NetworkIdMsg *msg);
virtual void ProcessInvestigateMsg(InvestigateMsg msg);
virtual void ProcessThingMsg(ThingMsg msg);
virtual void ProcessPoseMsg(PoseMsg msg);
virtual void ProcessCustomMsg(CustomMsg msg);
virtual void Process(InvestigateMsg *msg);
virtual void Process(ThingMsg *msg);
virtual void Process(PoseMsg *msg);
virtual void Process(CustomMsg *msg);
};
} // namespace Control

View File

@ -5,7 +5,11 @@
namespace Passer {
namespace Control {
TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {}
// TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {}
TemperatureSensor::TemperatureSensor(unsigned char networkId,
unsigned char thingId)
: Thing(nullptr, networkId, thingId, Type::TemperatureSensor) {}
void TemperatureSensor::SetTemperature(float temp) { this->temp = temp; }

View File

@ -7,7 +7,7 @@ namespace Control {
class TemperatureSensor : public Thing {
public:
TemperatureSensor();
TemperatureSensor(unsigned char networkId, unsigned char thingId);
virtual void SetTemperature(float temp);

View File

@ -1,6 +1,11 @@
#include "SiteServer.h"
Passer::Control::SiteServer::SiteServer(int port) {
#include "Sensors/TemperatureSensor.h"
namespace Passer {
namespace Control {
SiteServer::SiteServer(int port) {
this->name = "Site Server";
this->ipAddress = "0.0.0.0";
@ -11,21 +16,37 @@ Passer::Control::SiteServer::SiteServer(int port) {
SetupUDP(port, ipAddress, 0);
}
void Passer::Control::SiteServer::Update(unsigned long currentTimeMs) {
void SiteServer::Update(unsigned long currentTimeMs) {
this->ReceiveUDP();
Thing::UpdateAll(currentTimeMs);
}
void Passer::Control::SiteServer::Process(Participant *sender, ClientMsg *msg) {
void SiteServer::Process(Participant *sender, ClientMsg *msg) {
if (msg->networkId == 0) {
std::cout << this->name << " received New Client -> "
<< sender->ipAddress << " "
<< (int)sender->networkId << "\n";
NetworkIdMsg* msg = new NetworkIdMsg(sender->networkId);
std::cout << this->name << " received New Client -> " << sender->ipAddress
<< " " << (int)sender->networkId << "\n";
NetworkIdMsg *msg = new NetworkIdMsg(sender->networkId);
sender->Send(msg);
delete msg;
}
}
void Passer::Control::SiteServer::Process(Participant *sender,
NetworkIdMsg *msg) {}
void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {}
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:
new Thing(this, msg->networkId, msg->thingId,
(Thing::Type)msg->thingType);
break;
}
}
}
} // namespace Control
} // namespace Passer

View File

@ -17,6 +17,7 @@ protected:
virtual void Process(Participant *sender, ClientMsg *msg) override;
virtual void Process(Participant *sender, NetworkIdMsg *msg) override;
virtual void Process(ThingMsg *msg) override;
};
} // namespace Control

View File

@ -28,6 +28,26 @@ Thing::Thing(unsigned char thingType) {
this->angularVelocity = Spherical16::zero;
}
Passer::Control::Thing::Thing(Participant *participant, unsigned char networkId,
unsigned char thingId, Type thingType) {
// no participant reference yet..
this->networkId = networkId;
this->id = thingId;
this->type = (unsigned char)thingType;
this->Init();
thingId = Thing::Add(this);
if (thingId < 0) {
std::cout << "ERROR: Thing store is full\n";
this->id = 0; // what to do when we cannot store any more things?
} else
this->id = thingId;
this->linearVelocity = Spherical16::zero;
this->angularVelocity = Spherical16::zero;
}
void Thing::Terminate() { Thing::Remove(this); }
void Thing::Init() {}

View File

@ -7,6 +7,8 @@
namespace Passer {
namespace Control {
class Participant;
#define THING_STORE_SIZE 256
// IMPORTANT: values higher than 256 will need to change the Thing::id type
// to 16-bit or higher, breaking the networking protocol!
@ -39,6 +41,8 @@ public:
Thing(Type thingType = Type::Undetermined);
Thing(unsigned char thingType);
Thing(Participant *participant, unsigned char networkId,
unsigned char thingId, Type thingType = Type::Undetermined);
Thing *FindThing(const char *name);
// Thing *FindChild(unsigned char id);