update without millis

This commit is contained in:
Pascal Serrarens 2025-01-31 11:48:00 +01:00
parent 830b418e48
commit 516e588b38
4 changed files with 41 additions and 21 deletions

View File

@ -12,6 +12,7 @@
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
#elif defined(__unix__) || defined(__APPLE__) #elif defined(__unix__) || defined(__APPLE__)
#include <arpa/inet.h> #include <arpa/inet.h>
#include <chrono>
#include <fcntl.h> // For fcntl #include <fcntl.h> // For fcntl
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -58,11 +59,18 @@ void Passer::Control::Participant::SetupUDP(int localPort,
#endif #endif
} }
#if defined(ARDUINO)
void Participant::Update() { this->Update(millis()); }
#endif
void Participant::Update(unsigned long currentTimeMs) { void Participant::Update(unsigned long currentTimeMs) {
if (currentTimeMs == 0) {
#if defined(ARDUINO)
currentTimeMs = millis();
#elif defined(__unix__) || defined(__APPLE__)
auto now = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch());
currentTimeMs = static_cast<unsigned long>(ms.count());
#endif
}
if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) { if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) {
ClientMsg *msg = new ClientMsg(this->networkId); ClientMsg *msg = new ClientMsg(this->networkId);
this->Publish(msg); this->Publish(msg);
@ -249,6 +257,9 @@ void Participant::Process(CustomMsg *msg) {
Thing *thing = Thing::Get(msg->networkId, msg->thingId); Thing *thing = Thing::Get(msg->networkId, msg->thingId);
if (thing != nullptr) if (thing != nullptr)
thing->ProcessBytes(msg->bytes); thing->ProcessBytes(msg->bytes);
else
std::cout << "custom msg for unknown thing " << (int)msg->networkId << ":"
<< (int)msg->thingId << "\n";
} }
// Receive // Receive

View File

@ -62,10 +62,7 @@ public:
// i.e. // i.e.
// Participant p = Participant("127.0.0.1", 8000); // Participant p = Participant("127.0.0.1", 8000);
#if defined(ARDUINO) virtual void Update(unsigned long currentTimeMs = 0);
virtual void Update();
#endif
virtual void Update(unsigned long currentTimeMs);
void SendThingInfo(Thing *thing); void SendThingInfo(Thing *thing);
void PublishThingInfo(Thing *thing); void PublishThingInfo(Thing *thing);

View File

@ -10,6 +10,7 @@ namespace Control {
SiteServer::SiteServer(int port) { SiteServer::SiteServer(int port) {
this->name = "Site Server"; this->name = "Site Server";
this->publishInterval = 0;
this->ipAddress = "0.0.0.0"; this->ipAddress = "0.0.0.0";
this->port = port; this->port = port;
@ -21,10 +22,21 @@ SiteServer::SiteServer(int port) {
Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor); Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor);
} }
void SiteServer::Update(unsigned long currentTimeMs) { // void SiteServer::Update(unsigned long currentTimeMs) {
this->ReceiveUDP(); // if (currentTimeMs == 0) {
Thing::UpdateAll(currentTimeMs); // #if defined(ARDUINO)
} // currentTimeMs = millis();
// #elif defined(__unix__) || defined(__APPLE__)
// #elif defined(__unix__) || defined(__APPLE__)
// auto now = std::chrono::steady_clock::now();
// auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
// now.time_since_epoch());
// currentTimeMs = static_cast<unsigned long>(ms.count());
// #endif
// }
// this->ReceiveUDP();
// Thing::UpdateAll(currentTimeMs);
// }
void SiteServer::Process(Participant *sender, ClientMsg *msg) { void SiteServer::Process(Participant *sender, ClientMsg *msg) {
if (msg->networkId == 0) { if (msg->networkId == 0) {

View File

@ -2,9 +2,9 @@
#include "Participant.h" #include "Participant.h"
#include <unordered_map>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <unordered_map>
namespace Passer { namespace Passer {
namespace Control { namespace Control {
@ -14,7 +14,7 @@ class SiteServer : public Participant {
public: public:
SiteServer(int port = 7681); SiteServer(int port = 7681);
virtual void Update(unsigned long currentTimeMs) override; // virtual void Update(unsigned long currentTimeMs = 0) override;
template <typename ThingClass> void Register(unsigned char thingType) { template <typename ThingClass> void Register(unsigned char thingType) {
thingMsgProcessors[thingType] = [](unsigned char networkId, thingMsgProcessors[thingType] = [](unsigned char networkId,
@ -30,7 +30,7 @@ protected:
virtual void Process(Participant *sender, NetworkIdMsg *msg) override; virtual void Process(Participant *sender, NetworkIdMsg *msg) override;
virtual void Process(ThingMsg *msg) override; virtual void Process(ThingMsg *msg) override;
using ThingConstructor = std::function<std::unique_ptr<Thing>( using ThingConstructor = std::function<std::unique_ptr<Thing>(
unsigned char networkId, unsigned char thingId)>; unsigned char networkId, unsigned char thingId)>;
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors; std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
}; };