update without millis
This commit is contained in:
parent
830b418e48
commit
516e588b38
@ -12,6 +12,7 @@
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
#include <arpa/inet.h>
|
||||
#include <chrono>
|
||||
#include <fcntl.h> // For fcntl
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
@ -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<std::chrono::milliseconds>(
|
||||
now.time_since_epoch());
|
||||
currentTimeMs = static_cast<unsigned long>(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
|
||||
|
@ -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);
|
||||
|
@ -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<TemperatureSensor>((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<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) {
|
||||
if (msg->networkId == 0) {
|
||||
|
16
SiteServer.h
16
SiteServer.h
@ -2,9 +2,9 @@
|
||||
|
||||
#include "Participant.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
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 <typename ThingClass> void Register(unsigned char thingType) {
|
||||
thingMsgProcessors[thingType] = [](unsigned char networkId,
|
||||
unsigned char thingId) {
|
||||
return std::make_unique<ThingClass>(networkId, thingId);
|
||||
};
|
||||
unsigned char thingId) {
|
||||
return std::make_unique<ThingClass>(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<std::unique_ptr<Thing>(
|
||||
unsigned char networkId, unsigned char thingId)>;
|
||||
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
|
||||
using ThingConstructor = std::function<std::unique_ptr<Thing>(
|
||||
unsigned char networkId, unsigned char thingId)>;
|
||||
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
|
||||
};
|
||||
|
||||
} // namespace Control
|
||||
|
Loading…
x
Reference in New Issue
Block a user