return status info in boatData request
This commit is contained in:
parent
165aca2f61
commit
c14ed4cfe9
|
@ -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);
|
||||
|
|
|
@ -4,15 +4,16 @@
|
|||
#include "GwLog.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <Arduino.h>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#define GW_BOAT_VALUE_LEN 32
|
||||
class GwBoatItemBase{
|
||||
public:
|
||||
static const unsigned long INVALID_TIME=60000;
|
||||
typedef std::map<String,GwBoatItemBase*> GwBoatItemMap;
|
||||
typedef std::vector<GwBoatItemBase*> 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 T> class GwBoatItem : public GwBoatItemBase{
|
||||
|
@ -41,19 +44,20 @@ template<class T> 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 T> 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<type> *name=new GwBoatItem<type>(&values,F(#name),time,fmt) ;
|
||||
GwBoatItem<type> *name=new GwBoatItem<type>(F(#name),time,fmt,&values) ;
|
||||
class GwBoatData{
|
||||
private:
|
||||
GwLog *logger;
|
||||
|
|
|
@ -65,23 +65,23 @@ class N2kToNMEA0183Functions : public N2kDataToNMEA0183
|
|||
private:
|
||||
ConverterList<N2kToNMEA0183Functions,tN2kMsg> converters;
|
||||
static const unsigned long RMCPeriod = 500;
|
||||
static void setMax(GwBoatItem<double> *maxItem, GwBoatItem<double> *item)
|
||||
void setMax(GwBoatItem<double> *maxItem, GwBoatItem<double> *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<double> *item, double value)
|
||||
void updateDouble(GwBoatItem<double> *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"))
|
||||
|
|
Loading…
Reference in New Issue