From c14ed4cfe9cb66afe829f1a950aef87ca4c865bd Mon Sep 17 00:00:00 2001 From: andreas Date: Sun, 31 Oct 2021 16:31:22 +0100 Subject: [PATCH] return status info in boatData request --- lib/boatData/GwBoatData.cpp | 16 +++++++---- lib/boatData/GwBoatData.h | 38 ++++++++++++++++---------- lib/nmea2kto0183/N2kDataToNMEA0183.cpp | 18 ++++++------ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/lib/boatData/GwBoatData.cpp b/lib/boatData/GwBoatData.cpp index 47cffbf..474520d 100644 --- a/lib/boatData/GwBoatData.cpp +++ b/lib/boatData/GwBoatData.cpp @@ -6,18 +6,22 @@ GwBoatData::GwBoatData(GwLog *logger){ GwBoatData::~GwBoatData(){ GwBoatItemBase::GwBoatItemMap::iterator it; for (it=values.begin() ; it != values.end();it++){ - delete it->second; + delete *it; } } String GwBoatData::toJson() const { - long minTime=millis(); - DynamicJsonDocument json(800); + unsigned long minTime=millis(); GwBoatItemBase::GwBoatItemMap::const_iterator it; + size_t count=0; + size_t elementSizes=0; for (it=values.begin() ; it != values.end();it++){ - if (it->second->isValid(minTime)){ - it->second->toJsonDoc(&json,it->first); - } + count++; + elementSizes+=(*it)->getJsonSize(); + } + DynamicJsonDocument json(JSON_OBJECT_SIZE(count)+elementSizes); + for (it=values.begin() ; it != values.end();it++){ + (*it)->toJsonDoc(&json,minTime); } String buf; serializeJson(json,buf); diff --git a/lib/boatData/GwBoatData.h b/lib/boatData/GwBoatData.h index 2babe1c..1a60abc 100644 --- a/lib/boatData/GwBoatData.h +++ b/lib/boatData/GwBoatData.h @@ -4,15 +4,16 @@ #include "GwLog.h" #include #include -#include +#include #define GW_BOAT_VALUE_LEN 32 class GwBoatItemBase{ public: static const unsigned long INVALID_TIME=60000; - typedef std::map GwBoatItemMap; + typedef std::vector GwBoatItemMap; protected: unsigned long lastSet=0; unsigned long invalidTime=INVALID_TIME; + String name; void uls(){ lastSet=millis(); } @@ -24,15 +25,17 @@ class GwBoatItemBase{ if (now == 0) now=millis(); return (lastSet + invalidTime) >= now; } - GwBoatItemBase(unsigned long invalidTime=INVALID_TIME){ + GwBoatItemBase(String name,unsigned long invalidTime=INVALID_TIME){ lastSet=0; this->invalidTime=invalidTime; + this->name=name; } virtual ~GwBoatItemBase(){} void invalidate(){ lastSet=0; } - virtual void toJsonDoc(DynamicJsonDocument *doc,String name)=0; + virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime)=0; + virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(4);} }; class GwBoatData; template class GwBoatItem : public GwBoatItemBase{ @@ -41,19 +44,20 @@ template class GwBoatItem : public GwBoatItemBase{ private: T data; Formatter fmt; + int lastUpdateSource; public: - GwBoatItem(unsigned long invalidTime=INVALID_TIME,Formatter fmt=NULL): - GwBoatItemBase(invalidTime){ + GwBoatItem(String name,unsigned long invalidTime=INVALID_TIME,Formatter fmt=NULL,GwBoatItemMap *map=NULL): + GwBoatItemBase(name,invalidTime){ this->fmt=fmt; - } - GwBoatItem(GwBoatItemMap *map,String name,unsigned long invalidTime=INVALID_TIME,Formatter fmt=NULL): - GwBoatItemBase(invalidTime){ - this->fmt=fmt; - (*map)[name]=this; + if (map){ + map->push_back(this); + } + lastUpdateSource=-1; } virtual ~GwBoatItem(){} - void update(T nv){ + void update(T nv, int source=-1){ data=nv; + lastUpdateSource=source; uls(); } T getData(bool useFormatter=false){ @@ -64,8 +68,12 @@ template class GwBoatItem : public GwBoatItemBase{ if (! isValid(millis())) return defaultv; return data; } - virtual void toJsonDoc(DynamicJsonDocument *doc,String name){ - (*doc)[name]=getData(true); + virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime){ + JsonObject o=doc->createNestedObject(name); + o[F("value")]=getData(true); + o[F("update")]=minTime-lastSet; + o[F("source")]=lastUpdateSource; + o[F("valid")]=isValid(minTime); } }; @@ -101,7 +109,7 @@ static double formatCourse(double cv) #define GWBOATDATA(type,name,time,fmt) \ - GwBoatItem *name=new GwBoatItem(&values,F(#name),time,fmt) ; + GwBoatItem *name=new GwBoatItem(F(#name),time,fmt,&values) ; class GwBoatData{ private: GwLog *logger; diff --git a/lib/nmea2kto0183/N2kDataToNMEA0183.cpp b/lib/nmea2kto0183/N2kDataToNMEA0183.cpp index 18af2bd..c97b3e6 100644 --- a/lib/nmea2kto0183/N2kDataToNMEA0183.cpp +++ b/lib/nmea2kto0183/N2kDataToNMEA0183.cpp @@ -65,23 +65,23 @@ class N2kToNMEA0183Functions : public N2kDataToNMEA0183 private: ConverterList converters; static const unsigned long RMCPeriod = 500; - static void setMax(GwBoatItem *maxItem, GwBoatItem *item) + void setMax(GwBoatItem *maxItem, GwBoatItem *item) { unsigned long now = millis(); if (!item->isValid(now)) return; if (item->getData() > maxItem->getData() || !maxItem->isValid(now)) { - maxItem->update(item->getData()); + maxItem->update(item->getData(),sourceId); } } - static void updateDouble(GwBoatItem *item, double value) + void updateDouble(GwBoatItem *item, double value) { if (value == N2kDoubleNA) return; if (!item) return; - item->update(value); + item->update(value,sourceId); } unsigned long LastPosSend; @@ -146,7 +146,7 @@ private: ParseN2kMagneticVariation(N2kMsg, SID, Source, DaysSince1970, Variation); updateDouble(boatData->Variation, Variation); if (DaysSince1970 != N2kUInt16NA && DaysSince1970 != 0) - boatData->DaysSince1970->update(DaysSince1970); + boatData->DaysSince1970->update(DaysSince1970,sourceId); } //***************************************************************************** @@ -264,7 +264,7 @@ private: updateDouble(boatData->Altitude, Altitude); updateDouble(boatData->SecondsSinceMidnight, SecondsSinceMidnight); if (DaysSince1970 != N2kUInt16NA && DaysSince1970 != 0) - boatData->DaysSince1970->update(DaysSince1970); + boatData->DaysSince1970->update(DaysSince1970,sourceId); } } @@ -368,11 +368,11 @@ private: if (ParseN2kDistanceLog(N2kMsg, DaysSince1970, SecondsSinceMidnight, Log, TripLog)) { if (Log != N2kUInt32NA) - boatData->Log->update(Log); + boatData->Log->update(Log,sourceId); if (TripLog != N2kUInt32NA) - boatData->TripLog->update(TripLog); + boatData->TripLog->update(TripLog,sourceId); if (DaysSince1970 != N2kUInt16NA && DaysSince1970 != 0) - boatData->DaysSince1970->update(DaysSince1970); + boatData->DaysSince1970->update(DaysSince1970,sourceId); tNMEA0183Msg NMEA0183Msg; if (!NMEA0183Msg.Init("VLW", "GP"))