From 0bdcc8fff2908c0be66ea15eacb775ab71d1e88a Mon Sep 17 00:00:00 2001 From: andreas Date: Mon, 1 Nov 2021 11:41:41 +0100 Subject: [PATCH] handle priorities in boat data --- lib/boatData/GwBoatData.cpp | 2 +- lib/boatData/GwBoatData.h | 21 ++++++++++++++++----- lib/nmea0183ton2k/NMEA0183DataToN2K.cpp | 6 ++---- src/main.cpp | 1 + 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/boatData/GwBoatData.cpp b/lib/boatData/GwBoatData.cpp index 474520d..7d9ca6e 100644 --- a/lib/boatData/GwBoatData.cpp +++ b/lib/boatData/GwBoatData.cpp @@ -19,7 +19,7 @@ String GwBoatData::toJson() const { count++; elementSizes+=(*it)->getJsonSize(); } - DynamicJsonDocument json(JSON_OBJECT_SIZE(count)+elementSizes); + DynamicJsonDocument json(JSON_OBJECT_SIZE(count)+elementSizes+4); for (it=values.begin() ; it != values.end();it++){ (*it)->toJsonDoc(&json,minTime); } diff --git a/lib/boatData/GwBoatData.h b/lib/boatData/GwBoatData.h index 0c01b3f..6747cf1 100644 --- a/lib/boatData/GwBoatData.h +++ b/lib/boatData/GwBoatData.h @@ -14,8 +14,9 @@ class GwBoatItemBase{ unsigned long lastSet=0; unsigned long invalidTime=INVALID_TIME; String name; - void uls(){ - lastSet=millis(); + void uls(unsigned long ts=0){ + if (ts) lastSet=ts; + else lastSet=millis(); } public: unsigned long getLastSet() const {return lastSet;} @@ -35,7 +36,7 @@ class GwBoatItemBase{ lastSet=0; } virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime)=0; - virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(4);} + virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(5);} virtual int getLastSource()=0; }; class GwBoatData; @@ -56,10 +57,20 @@ template class GwBoatItem : public GwBoatItemBase{ lastUpdateSource=-1; } virtual ~GwBoatItem(){} - void update(T nv, int source=-1){ + bool update(T nv, int source=-1){ + unsigned long now=millis(); + if (isValid(now)){ + //priority handling + //sources with lower ids will win + //and we will not overwrite their value + if (lastUpdateSource < source){ + return false; + } + } data=nv; lastUpdateSource=source; - uls(); + uls(now); + return true; } T getData(bool useFormatter=false){ if (! useFormatter || fmt == NULL) return data; diff --git a/lib/nmea0183ton2k/NMEA0183DataToN2K.cpp b/lib/nmea0183ton2k/NMEA0183DataToN2K.cpp index 3927357..f6d6008 100644 --- a/lib/nmea0183ton2k/NMEA0183DataToN2K.cpp +++ b/lib/nmea0183ton2k/NMEA0183DataToN2K.cpp @@ -66,15 +66,13 @@ private: } bool updateDouble(GwBoatItem *target,double v, int sourceId){ if (v != NMEA0183DoubleNA){ - target->update(v,sourceId); - return true; + return target->update(v,sourceId); } return false; } bool updateUint32(GwBoatItem *target,uint32_t v, int sourceId){ if (v != NMEA0183UInt32NA){ - target->update(v,sourceId); - return true; + return target->update(v,sourceId); } return v; } diff --git a/src/main.cpp b/src/main.cpp index e5a4a52..136f051 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -467,6 +467,7 @@ void loop() { //read channels socketServer.readMessages(&receiver); + receiver.id=USB_CHANNEL_ID; usbSerial.readMessages(&receiver); //handle message requests