handle priorities in boat data
This commit is contained in:
parent
14b04fb4a0
commit
0bdcc8fff2
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 T> 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;
|
||||
|
|
|
@ -66,15 +66,13 @@ private:
|
|||
}
|
||||
bool updateDouble(GwBoatItem<double> *target,double v, int sourceId){
|
||||
if (v != NMEA0183DoubleNA){
|
||||
target->update(v,sourceId);
|
||||
return true;
|
||||
return target->update(v,sourceId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool updateUint32(GwBoatItem<uint32_t> *target,uint32_t v, int sourceId){
|
||||
if (v != NMEA0183UInt32NA){
|
||||
target->update(v,sourceId);
|
||||
return true;
|
||||
return target->update(v,sourceId);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
|
|
@ -467,6 +467,7 @@ void loop() {
|
|||
|
||||
//read channels
|
||||
socketServer.readMessages(&receiver);
|
||||
receiver.id=USB_CHANNEL_ID;
|
||||
usbSerial.readMessages(&receiver);
|
||||
|
||||
//handle message requests
|
||||
|
|
Loading…
Reference in New Issue