mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-13 05:53:06 +01:00
return status info in boatData request
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user