We are receiving temperatures

This commit is contained in:
Pascal Serrarens 2025-01-11 14:48:50 +01:00
parent c092d029fc
commit 54ab547d5d
6 changed files with 23 additions and 46 deletions

View File

@ -1,8 +1,6 @@
#include "Participant.h"
// #include <ws2tcpip.h>
#define BUF_SIZE 1024
#include "Thing.h"
#include "UdpArduino.h"
#include "UdpPosix.h"
@ -211,7 +209,7 @@ void Participant::Process(Participant *sender, NetworkIdMsg *msg) {
// sender->SendThingInfo(thing);
// }
for (Thing *thing : allThings) {
for (Thing *thing : Thing::allThings) {
sender->SendThingInfo(thing);
}
}

View File

@ -18,5 +18,10 @@ void TemperatureSensor::SendBytes(char *buffer, unsigned char *ix) {
LowLevelMessages::SendFloat16(buffer, ix, this->temp);
}
void TemperatureSensor::ProcessBytes(char *bytes) {
unsigned char ix = 0;
this->temp = LowLevelMessages::ReceiveFloat16(bytes, &ix);
}
} // namespace Control
} // namespace Passer

View File

@ -12,6 +12,7 @@ public:
virtual void SetTemperature(float temp);
void SendBytes(char *buffer, unsigned char *ix) override;
virtual void ProcessBytes(char *bytes) override;
protected:
float temp = 0;

View File

@ -34,6 +34,7 @@ void SiteServer::Process(Participant *sender, ClientMsg *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) {

View File

@ -46,6 +46,8 @@ Passer::Control::Thing::Thing(Participant *participant, unsigned char networkId,
this->linearVelocity = Spherical16::zero;
this->angularVelocity = Spherical16::zero;
std::cout << "Added thing " << (int)this->networkId << "/" << (int)this->id
<< "\n";
}
void Thing::Terminate() { Thing::Remove(this); }
@ -176,37 +178,19 @@ Spherical16 Thing::GetLinearVelocity() { return this->linearVelocity; }
Spherical16 Thing::GetAngularVelocity() { return this->angularVelocity; }
// All things
// Thing *Thing::allThings[THING_STORE_SIZE] = {nullptr};
// Thing **Passer::Control::Thing::GetAllThings() { return allThings; }
std::list<Thing *> Thing::allThings;
Thing *Thing::Get(unsigned char networkId, unsigned char thingId) {
// for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
// Thing *thing = allThings[ix];
// if (thing == nullptr)
// continue;
// if (thing->networkId == networkId && thing->id == thingId)
// return thing;
// }
// return nullptr;
std::find_if(allThings.begin(), allThings.end(),
[networkId, thingId](Thing *thing) {
return thing->networkId == networkId && thing->id == thingId;
});
for (auto &thing : allThings) {
if (thing->networkId == networkId && thing->id == thingId) {
return thing;
}
}
return nullptr;
}
int Thing::Add(Thing *newThing) {
// Exclude 0 because that value is reserved for 'no thing'
// for (uint16_t ix = 1; ix < THING_STORE_SIZE; ix++) {
// Thing *thing = allThings[ix];
// if (thing == nullptr) {
// allThings[ix] = newThing;
// std::cout << " Add new thing " << (int)ix << "\n";
// return ix;
// }
// }
for (Thing *thing : allThings) {
if (thing == newThing)
return thing->id;
@ -216,23 +200,13 @@ int Thing::Add(Thing *newThing) {
}
void Thing::Remove(Thing *thing) {
// std::cout << " remove " << (int)thing->id << "\n";
// allThings[thing->id] = nullptr;
allThings.remove_if([thing](Thing *obj) { return obj == thing; });
Thing::allThings.remove_if([thing](Thing *obj) { return obj == thing; });
}
void Thing::UpdateAll(unsigned long currentTimeMs) {
// Not very efficient, but it works for now.
// for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
// Thing *thing = allThings[ix];
// if (thing != nullptr &&
// thing->parent == nullptr) { // update all root things
// // std::cout << " update " << (int)ix << " thingid " << (int)thing->id
// // << "\n";
// thing->Update(currentTimeMs);
// }
// }
for (Thing *thing : allThings) {
for (Thing *thing : Thing::allThings) {
if (thing != nullptr &&
thing->parent == nullptr) { // update all root things
// std::cout << " update " << (int)ix << " thingid " << (int)thing->id

View File

@ -129,17 +129,15 @@ protected:
//------------ All things
public:
// static Thing **GetAllThings();
static Thing *Get(unsigned char networkId, unsigned char thingId);
static int Add(Thing *thing);
static void Remove(Thing *thing);
static void UpdateAll(unsigned long currentTimeMs);
// private:
// static Thing *allThings[];
static std::list<Thing *> allThings;
};
static std::list<Thing *> allThings;
// static std::list<Thing *> allThings;
} // namespace Control
} // namespace Passer