1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

separate boat data, integrate with web server

This commit is contained in:
andreas
2021-10-18 19:20:00 +02:00
parent fb20135718
commit 3bbd9ef965
6 changed files with 190 additions and 178 deletions

View File

@@ -0,0 +1,54 @@
#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;
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;
DynamicJsonDocument json(800);
GwBoatItemMap::const_iterator it;
for (it=values.begin() ; it != values.end();it++){
if (it->second->isValid(minTime)){
json[it->first]=it->second->getValue();
}
}
String buf;
serializeJson(json,buf);
return buf;
}

67
lib/boatData/GwBoatData.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef _GWBOATDATA_H
#define _GWBOATDATA_H
#include "GwLog.h"
#include <ArduinoJson.h>
#include <Arduino.h>
#include <map>
#define GW_BOAT_VALUE_LEN 32
class GwBoatItem{
private:
char value[GW_BOAT_VALUE_LEN];
long lastSet;
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;
lastSet=-1;
}
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();
}
void invalidate(){
lastSet=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);
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);
String toJson() const;
};
#endif