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(){
|
GwBoatData::~GwBoatData(){
|
||||||
GwBoatItemBase::GwBoatItemMap::iterator it;
|
GwBoatItemBase::GwBoatItemMap::iterator it;
|
||||||
for (it=values.begin() ; it != values.end();it++){
|
for (it=values.begin() ; it != values.end();it++){
|
||||||
delete it->second;
|
delete *it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String GwBoatData::toJson() const {
|
String GwBoatData::toJson() const {
|
||||||
long minTime=millis();
|
unsigned long minTime=millis();
|
||||||
DynamicJsonDocument json(800);
|
|
||||||
GwBoatItemBase::GwBoatItemMap::const_iterator it;
|
GwBoatItemBase::GwBoatItemMap::const_iterator it;
|
||||||
|
size_t count=0;
|
||||||
|
size_t elementSizes=0;
|
||||||
for (it=values.begin() ; it != values.end();it++){
|
for (it=values.begin() ; it != values.end();it++){
|
||||||
if (it->second->isValid(minTime)){
|
count++;
|
||||||
it->second->toJsonDoc(&json,it->first);
|
elementSizes+=(*it)->getJsonSize();
|
||||||
}
|
}
|
||||||
|
DynamicJsonDocument json(JSON_OBJECT_SIZE(count)+elementSizes);
|
||||||
|
for (it=values.begin() ; it != values.end();it++){
|
||||||
|
(*it)->toJsonDoc(&json,minTime);
|
||||||
}
|
}
|
||||||
String buf;
|
String buf;
|
||||||
serializeJson(json,buf);
|
serializeJson(json,buf);
|
||||||
|
|
|
@ -4,15 +4,16 @@
|
||||||
#include "GwLog.h"
|
#include "GwLog.h"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <map>
|
#include <vector>
|
||||||
#define GW_BOAT_VALUE_LEN 32
|
#define GW_BOAT_VALUE_LEN 32
|
||||||
class GwBoatItemBase{
|
class GwBoatItemBase{
|
||||||
public:
|
public:
|
||||||
static const unsigned long INVALID_TIME=60000;
|
static const unsigned long INVALID_TIME=60000;
|
||||||
typedef std::map<String,GwBoatItemBase*> GwBoatItemMap;
|
typedef std::vector<GwBoatItemBase*> GwBoatItemMap;
|
||||||
protected:
|
protected:
|
||||||
unsigned long lastSet=0;
|
unsigned long lastSet=0;
|
||||||
unsigned long invalidTime=INVALID_TIME;
|
unsigned long invalidTime=INVALID_TIME;
|
||||||
|
String name;
|
||||||
void uls(){
|
void uls(){
|
||||||
lastSet=millis();
|
lastSet=millis();
|
||||||
}
|
}
|
||||||
|
@ -24,15 +25,17 @@ class GwBoatItemBase{
|
||||||
if (now == 0) now=millis();
|
if (now == 0) now=millis();
|
||||||
return (lastSet + invalidTime) >= now;
|
return (lastSet + invalidTime) >= now;
|
||||||
}
|
}
|
||||||
GwBoatItemBase(unsigned long invalidTime=INVALID_TIME){
|
GwBoatItemBase(String name,unsigned long invalidTime=INVALID_TIME){
|
||||||
lastSet=0;
|
lastSet=0;
|
||||||
this->invalidTime=invalidTime;
|
this->invalidTime=invalidTime;
|
||||||
|
this->name=name;
|
||||||
}
|
}
|
||||||
virtual ~GwBoatItemBase(){}
|
virtual ~GwBoatItemBase(){}
|
||||||
void invalidate(){
|
void invalidate(){
|
||||||
lastSet=0;
|
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;
|
class GwBoatData;
|
||||||
template<class T> class GwBoatItem : public GwBoatItemBase{
|
template<class T> class GwBoatItem : public GwBoatItemBase{
|
||||||
|
@ -41,19 +44,20 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
|
||||||
private:
|
private:
|
||||||
T data;
|
T data;
|
||||||
Formatter fmt;
|
Formatter fmt;
|
||||||
|
int lastUpdateSource;
|
||||||
public:
|
public:
|
||||||
GwBoatItem(unsigned long invalidTime=INVALID_TIME,Formatter fmt=NULL):
|
GwBoatItem(String name,unsigned long invalidTime=INVALID_TIME,Formatter fmt=NULL,GwBoatItemMap *map=NULL):
|
||||||
GwBoatItemBase(invalidTime){
|
GwBoatItemBase(name,invalidTime){
|
||||||
this->fmt=fmt;
|
this->fmt=fmt;
|
||||||
}
|
if (map){
|
||||||
GwBoatItem(GwBoatItemMap *map,String name,unsigned long invalidTime=INVALID_TIME,Formatter fmt=NULL):
|
map->push_back(this);
|
||||||
GwBoatItemBase(invalidTime){
|
}
|
||||||
this->fmt=fmt;
|
lastUpdateSource=-1;
|
||||||
(*map)[name]=this;
|
|
||||||
}
|
}
|
||||||
virtual ~GwBoatItem(){}
|
virtual ~GwBoatItem(){}
|
||||||
void update(T nv){
|
void update(T nv, int source=-1){
|
||||||
data=nv;
|
data=nv;
|
||||||
|
lastUpdateSource=source;
|
||||||
uls();
|
uls();
|
||||||
}
|
}
|
||||||
T getData(bool useFormatter=false){
|
T getData(bool useFormatter=false){
|
||||||
|
@ -64,8 +68,12 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
|
||||||
if (! isValid(millis())) return defaultv;
|
if (! isValid(millis())) return defaultv;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
virtual void toJsonDoc(DynamicJsonDocument *doc,String name){
|
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime){
|
||||||
(*doc)[name]=getData(true);
|
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) \
|
#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{
|
class GwBoatData{
|
||||||
private:
|
private:
|
||||||
GwLog *logger;
|
GwLog *logger;
|
||||||
|
|
|
@ -65,23 +65,23 @@ class N2kToNMEA0183Functions : public N2kDataToNMEA0183
|
||||||
private:
|
private:
|
||||||
ConverterList<N2kToNMEA0183Functions,tN2kMsg> converters;
|
ConverterList<N2kToNMEA0183Functions,tN2kMsg> converters;
|
||||||
static const unsigned long RMCPeriod = 500;
|
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();
|
unsigned long now = millis();
|
||||||
if (!item->isValid(now))
|
if (!item->isValid(now))
|
||||||
return;
|
return;
|
||||||
if (item->getData() > maxItem->getData() || !maxItem->isValid(now))
|
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)
|
if (value == N2kDoubleNA)
|
||||||
return;
|
return;
|
||||||
if (!item)
|
if (!item)
|
||||||
return;
|
return;
|
||||||
item->update(value);
|
item->update(value,sourceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long LastPosSend;
|
unsigned long LastPosSend;
|
||||||
|
@ -146,7 +146,7 @@ private:
|
||||||
ParseN2kMagneticVariation(N2kMsg, SID, Source, DaysSince1970, Variation);
|
ParseN2kMagneticVariation(N2kMsg, SID, Source, DaysSince1970, Variation);
|
||||||
updateDouble(boatData->Variation, Variation);
|
updateDouble(boatData->Variation, Variation);
|
||||||
if (DaysSince1970 != N2kUInt16NA && DaysSince1970 != 0)
|
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->Altitude, Altitude);
|
||||||
updateDouble(boatData->SecondsSinceMidnight, SecondsSinceMidnight);
|
updateDouble(boatData->SecondsSinceMidnight, SecondsSinceMidnight);
|
||||||
if (DaysSince1970 != N2kUInt16NA && DaysSince1970 != 0)
|
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 (ParseN2kDistanceLog(N2kMsg, DaysSince1970, SecondsSinceMidnight, Log, TripLog))
|
||||||
{
|
{
|
||||||
if (Log != N2kUInt32NA)
|
if (Log != N2kUInt32NA)
|
||||||
boatData->Log->update(Log);
|
boatData->Log->update(Log,sourceId);
|
||||||
if (TripLog != N2kUInt32NA)
|
if (TripLog != N2kUInt32NA)
|
||||||
boatData->TripLog->update(TripLog);
|
boatData->TripLog->update(TripLog,sourceId);
|
||||||
if (DaysSince1970 != N2kUInt16NA && DaysSince1970 != 0)
|
if (DaysSince1970 != N2kUInt16NA && DaysSince1970 != 0)
|
||||||
boatData->DaysSince1970->update(DaysSince1970);
|
boatData->DaysSince1970->update(DaysSince1970,sourceId);
|
||||||
tNMEA0183Msg NMEA0183Msg;
|
tNMEA0183Msg NMEA0183Msg;
|
||||||
|
|
||||||
if (!NMEA0183Msg.Init("VLW", "GP"))
|
if (!NMEA0183Msg.Init("VLW", "GP"))
|
||||||
|
|
Loading…
Reference in New Issue