mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-13 05:53:06 +01:00
store all values in boatData
This commit is contained in:
@@ -1,54 +1,29 @@
|
||||
#include "GwBoatData.h"
|
||||
|
||||
GwBoatItem *GwBoatData::find(String name,bool doCreate){
|
||||
GwBoatItemMap::iterator it;
|
||||
if ((it=values.find(name)) != values.end()){
|
||||
return it->second;
|
||||
}
|
||||
if (! doCreate) return NULL;
|
||||
GwBoatItem *ni=new GwBoatItem();
|
||||
values[name]=ni;
|
||||
return ni;
|
||||
}
|
||||
GwBoatData::GwBoatData(GwLog *logger){
|
||||
|
||||
}
|
||||
GwBoatData::~GwBoatData(){
|
||||
GwBoatItemMap::iterator it;
|
||||
GwBoatItemBase::GwBoatItemMap::iterator it;
|
||||
for (it=values.begin() ; it != values.end();it++){
|
||||
delete it->second;
|
||||
}
|
||||
}
|
||||
void GwBoatData::update(String name,const char *value){
|
||||
GwBoatItem *i=find(name);
|
||||
i->update(value);
|
||||
}
|
||||
void GwBoatData::update(String name, String value){
|
||||
GwBoatItem *i=find(name);
|
||||
i->update(value);
|
||||
}
|
||||
void GwBoatData::update(String name, long value){
|
||||
GwBoatItem *i=find(name);
|
||||
i->update(value);
|
||||
}
|
||||
void GwBoatData::update(String name, double value){
|
||||
GwBoatItem *i=find(name);
|
||||
i->update(value);
|
||||
}
|
||||
void GwBoatData::update(String name, bool value){
|
||||
GwBoatItem *i=find(name);
|
||||
i->update(value);
|
||||
}
|
||||
|
||||
String GwBoatData::toJson() const {
|
||||
long minTime=millis() - maxAge;
|
||||
long minTime=millis();
|
||||
DynamicJsonDocument json(800);
|
||||
GwBoatItemMap::const_iterator it;
|
||||
GwBoatItemBase::GwBoatItemMap::const_iterator it;
|
||||
for (it=values.begin() ; it != values.end();it++){
|
||||
if (it->second->isValid(minTime)){
|
||||
json[it->first]=it->second->getValue();
|
||||
it->second->toJsonDoc(&json,it->first);
|
||||
}
|
||||
}
|
||||
String buf;
|
||||
serializeJson(json,buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
GwBoatItemBase::GwBoatItemMap * GwBoatData::getValues(){
|
||||
return &values;
|
||||
}
|
||||
@@ -6,62 +6,70 @@
|
||||
#include <Arduino.h>
|
||||
#include <map>
|
||||
#define GW_BOAT_VALUE_LEN 32
|
||||
class GwBoatItem{
|
||||
private:
|
||||
char value[GW_BOAT_VALUE_LEN];
|
||||
class GwBoatItemBase{
|
||||
public:
|
||||
static const long INVALID_TIME=60000;
|
||||
typedef std::map<String,GwBoatItemBase*> GwBoatItemMap;
|
||||
protected:
|
||||
long lastSet;
|
||||
long invalidTime=INVALID_TIME;
|
||||
void uls(){
|
||||
lastSet=millis();
|
||||
}
|
||||
public:
|
||||
const char * getValue() const{return value;}
|
||||
long getLastSet() const {return lastSet;}
|
||||
bool isValid(long minTime) const {return lastSet > minTime;}
|
||||
GwBoatItem(){
|
||||
value[0]=0;
|
||||
bool isValid(long now) const {
|
||||
return (lastSet + invalidTime) >= now;
|
||||
}
|
||||
GwBoatItemBase(long invalidTime=INVALID_TIME){
|
||||
lastSet=-1;
|
||||
this->invalidTime=invalidTime;
|
||||
}
|
||||
void update(String nv){
|
||||
strncpy(value,nv.c_str(),GW_BOAT_VALUE_LEN-1);
|
||||
uls();
|
||||
}
|
||||
void update(const char * nv){
|
||||
strncpy(value,nv,GW_BOAT_VALUE_LEN-1);
|
||||
}
|
||||
void update(long nv){
|
||||
ltoa(nv,value,10);
|
||||
uls();
|
||||
}
|
||||
void update(bool nv){
|
||||
if (nv) strcpy_P(value,PSTR("true"));
|
||||
else strcpy_P(value,PSTR("false"));
|
||||
uls();
|
||||
}
|
||||
void update(double nv){
|
||||
dtostrf(nv,3,7,value);
|
||||
uls();
|
||||
}
|
||||
virtual ~GwBoatItemBase(){}
|
||||
void invalidate(){
|
||||
lastSet=0;
|
||||
}
|
||||
|
||||
virtual void toJsonDoc(DynamicJsonDocument *doc,String name)=0;
|
||||
};
|
||||
class GwBoatData{
|
||||
typedef std::map<String,GwBoatItem*> GwBoatItemMap;
|
||||
private:
|
||||
const long maxAge=60000; //max age for valid data in ms
|
||||
GwLog *logger;
|
||||
GwBoatItemMap values;
|
||||
GwBoatItem *find(String name, bool doCreate=true);
|
||||
GwBoatItemBase::GwBoatItemMap values;
|
||||
public:
|
||||
GwBoatData(GwLog *logger);
|
||||
~GwBoatData();
|
||||
void update(String name,const char *value);
|
||||
void update(String name, String value);
|
||||
void update(String name, long value);
|
||||
void update(String name, bool value);
|
||||
void update(String name, double value);
|
||||
~GwBoatData();
|
||||
GwBoatItemBase::GwBoatItemMap * getValues();
|
||||
String toJson() const;
|
||||
|
||||
friend class GwBoatItemBase;
|
||||
};
|
||||
template<class T> class GwBoatItem : public GwBoatItemBase{
|
||||
private:
|
||||
T data;
|
||||
public:
|
||||
GwBoatItem(long invalidTime=INVALID_TIME): GwBoatItemBase(invalidTime){}
|
||||
virtual ~GwBoatItem(){}
|
||||
void update(T nv){
|
||||
data=nv;
|
||||
uls();
|
||||
}
|
||||
T getData(){
|
||||
return data;
|
||||
}
|
||||
virtual void toJsonDoc(DynamicJsonDocument *doc,String name){
|
||||
(*doc)[name]=data;
|
||||
}
|
||||
static GwBoatItem<T> *findOrCreate(GwBoatData *handler, String name,bool doCreate=true,
|
||||
long invalidTime=GwBoatItemBase::INVALID_TIME){
|
||||
GwBoatItemMap *values=handler->getValues();
|
||||
GwBoatItemMap::iterator it;
|
||||
if ((it=values->find(name)) != values->end()){
|
||||
return (GwBoatItem<T> *)it->second;
|
||||
}
|
||||
if (! doCreate) return NULL;
|
||||
GwBoatItem<T> *ni=new GwBoatItem<T>(invalidTime);
|
||||
(*values)[name]=ni;
|
||||
return ni;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user