handle priorities in boat data

This commit is contained in:
andreas 2021-11-01 11:41:41 +01:00
parent 14b04fb4a0
commit 0bdcc8fff2
4 changed files with 20 additions and 10 deletions

View File

@ -19,7 +19,7 @@ String GwBoatData::toJson() const {
count++; count++;
elementSizes+=(*it)->getJsonSize(); 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++){ for (it=values.begin() ; it != values.end();it++){
(*it)->toJsonDoc(&json,minTime); (*it)->toJsonDoc(&json,minTime);
} }

View File

@ -14,8 +14,9 @@ class GwBoatItemBase{
unsigned long lastSet=0; unsigned long lastSet=0;
unsigned long invalidTime=INVALID_TIME; unsigned long invalidTime=INVALID_TIME;
String name; String name;
void uls(){ void uls(unsigned long ts=0){
lastSet=millis(); if (ts) lastSet=ts;
else lastSet=millis();
} }
public: public:
unsigned long getLastSet() const {return lastSet;} unsigned long getLastSet() const {return lastSet;}
@ -35,7 +36,7 @@ class GwBoatItemBase{
lastSet=0; lastSet=0;
} }
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime)=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; virtual int getLastSource()=0;
}; };
class GwBoatData; class GwBoatData;
@ -56,10 +57,20 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
lastUpdateSource=-1; lastUpdateSource=-1;
} }
virtual ~GwBoatItem(){} 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; data=nv;
lastUpdateSource=source; lastUpdateSource=source;
uls(); uls(now);
return true;
} }
T getData(bool useFormatter=false){ T getData(bool useFormatter=false){
if (! useFormatter || fmt == NULL) return data; if (! useFormatter || fmt == NULL) return data;

View File

@ -66,15 +66,13 @@ private:
} }
bool updateDouble(GwBoatItem<double> *target,double v, int sourceId){ bool updateDouble(GwBoatItem<double> *target,double v, int sourceId){
if (v != NMEA0183DoubleNA){ if (v != NMEA0183DoubleNA){
target->update(v,sourceId); return target->update(v,sourceId);
return true;
} }
return false; return false;
} }
bool updateUint32(GwBoatItem<uint32_t> *target,uint32_t v, int sourceId){ bool updateUint32(GwBoatItem<uint32_t> *target,uint32_t v, int sourceId){
if (v != NMEA0183UInt32NA){ if (v != NMEA0183UInt32NA){
target->update(v,sourceId); return target->update(v,sourceId);
return true;
} }
return v; return v;
} }

View File

@ -467,6 +467,7 @@ void loop() {
//read channels //read channels
socketServer.readMessages(&receiver); socketServer.readMessages(&receiver);
receiver.id=USB_CHANNEL_ID;
usbSerial.readMessages(&receiver); usbSerial.readMessages(&receiver);
//handle message requests //handle message requests