From 516e588b3863cd34a583c85b41d01de5189aeaf5 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 31 Jan 2025 11:48:00 +0100 Subject: [PATCH] update without millis --- Participant.cpp | 21 ++++++++++++++++----- Participant.h | 5 +---- SiteServer.cpp | 20 ++++++++++++++++---- SiteServer.h | 16 ++++++++-------- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/Participant.cpp b/Participant.cpp index 3e5cd05..677bc14 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -12,6 +12,7 @@ #pragma comment(lib, "ws2_32.lib") #elif defined(__unix__) || defined(__APPLE__) #include +#include #include // For fcntl #include #include @@ -58,11 +59,18 @@ void Passer::Control::Participant::SetupUDP(int localPort, #endif } -#if defined(ARDUINO) -void Participant::Update() { this->Update(millis()); } -#endif - 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( + now.time_since_epoch()); + currentTimeMs = static_cast(ms.count()); +#endif + } + if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) { ClientMsg *msg = new ClientMsg(this->networkId); this->Publish(msg); @@ -239,7 +247,7 @@ void Participant::Process(NameMsg *msg) { if (thing != nullptr) { thing->name = new char[strlen(msg->name)]; strcpy(thing->name, msg->name); - std::cout << "thing name = " << thing->name << "\n"; + std::cout << "thing name = " << thing->name << "\n"; } } @@ -249,6 +257,9 @@ void Participant::Process(CustomMsg *msg) { Thing *thing = Thing::Get(msg->networkId, msg->thingId); if (thing != nullptr) thing->ProcessBytes(msg->bytes); + else + std::cout << "custom msg for unknown thing " << (int)msg->networkId << ":" + << (int)msg->thingId << "\n"; } // Receive diff --git a/Participant.h b/Participant.h index 3feaccc..a2b9c14 100644 --- a/Participant.h +++ b/Participant.h @@ -62,10 +62,7 @@ public: // i.e. // Participant p = Participant("127.0.0.1", 8000); -#if defined(ARDUINO) - virtual void Update(); -#endif - virtual void Update(unsigned long currentTimeMs); + virtual void Update(unsigned long currentTimeMs = 0); void SendThingInfo(Thing *thing); void PublishThingInfo(Thing *thing); diff --git a/SiteServer.cpp b/SiteServer.cpp index 87ea5d6..2fc9c48 100644 --- a/SiteServer.cpp +++ b/SiteServer.cpp @@ -10,6 +10,7 @@ namespace Control { SiteServer::SiteServer(int port) { this->name = "Site Server"; + this->publishInterval = 0; this->ipAddress = "0.0.0.0"; this->port = port; @@ -21,10 +22,21 @@ SiteServer::SiteServer(int port) { Register((unsigned char)Thing::Type::TemperatureSensor); } -void SiteServer::Update(unsigned long currentTimeMs) { - this->ReceiveUDP(); - Thing::UpdateAll(currentTimeMs); -} +// void SiteServer::Update(unsigned long currentTimeMs) { +// if (currentTimeMs == 0) { +// #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( +// now.time_since_epoch()); +// currentTimeMs = static_cast(ms.count()); +// #endif +// } +// this->ReceiveUDP(); +// Thing::UpdateAll(currentTimeMs); +// } void SiteServer::Process(Participant *sender, ClientMsg *msg) { if (msg->networkId == 0) { diff --git a/SiteServer.h b/SiteServer.h index 507c8b9..dbe6ff3 100644 --- a/SiteServer.h +++ b/SiteServer.h @@ -2,9 +2,9 @@ #include "Participant.h" -#include #include #include +#include namespace Passer { namespace Control { @@ -14,13 +14,13 @@ class SiteServer : public Participant { public: SiteServer(int port = 7681); - virtual void Update(unsigned long currentTimeMs) override; + // virtual void Update(unsigned long currentTimeMs = 0) override; template void Register(unsigned char thingType) { thingMsgProcessors[thingType] = [](unsigned char networkId, - unsigned char thingId) { - return std::make_unique(networkId, thingId); - }; + unsigned char thingId) { + return std::make_unique(networkId, thingId); + }; }; protected: @@ -30,9 +30,9 @@ protected: virtual void Process(Participant *sender, NetworkIdMsg *msg) override; virtual void Process(ThingMsg *msg) override; -using ThingConstructor = std::function( - unsigned char networkId, unsigned char thingId)>; - std::unordered_map thingMsgProcessors; + using ThingConstructor = std::function( + unsigned char networkId, unsigned char thingId)>; + std::unordered_map thingMsgProcessors; }; } // namespace Control