Merge branch 'master' of https://github.com/wellenvogel/esp32-nmea2000 into feature/env2
This commit is contained in:
commit
48a4142438
|
@ -1,4 +1,256 @@
|
||||||
#include "GwBoatData.h"
|
#include "GwBoatData.h"
|
||||||
|
#include <ArduinoJson/Json/TextFormatter.hpp>
|
||||||
|
#define GWTYPE_DOUBLE 1
|
||||||
|
#define GWTYPE_UINT32 2
|
||||||
|
#define GWTYPE_UINT16 3
|
||||||
|
#define GWTYPE_INT16 4
|
||||||
|
#define GWTYPE_USER 100
|
||||||
|
class GwBoatItemTypes{
|
||||||
|
public:
|
||||||
|
static int getType(const uint32_t &x){return GWTYPE_UINT32;}
|
||||||
|
static int getType(const uint16_t &x){return GWTYPE_UINT16;}
|
||||||
|
static int getType(const int16_t &x){return GWTYPE_INT16;}
|
||||||
|
static int getType(const double &x){return GWTYPE_DOUBLE;}
|
||||||
|
static int getType(const GwSatInfoList &x){ return GWTYPE_USER+1;}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool GwBoatItemBase::isValid(unsigned long now) const
|
||||||
|
{
|
||||||
|
if (lastSet == 0)
|
||||||
|
return false;
|
||||||
|
if (invalidTime == 0)
|
||||||
|
return true;
|
||||||
|
if (now == 0)
|
||||||
|
now = millis();
|
||||||
|
return (lastSet + invalidTime) >= now;
|
||||||
|
}
|
||||||
|
GwBoatItemBase::GwBoatItemBase(String name, String format, unsigned long invalidTime)
|
||||||
|
{
|
||||||
|
lastSet = 0;
|
||||||
|
this->invalidTime = invalidTime;
|
||||||
|
this->name = name;
|
||||||
|
this->format = format;
|
||||||
|
this->type = 0;
|
||||||
|
this->lastUpdateSource=-1;
|
||||||
|
}
|
||||||
|
#define STRING_SIZE 40
|
||||||
|
GwBoatItemBase::StringWriter::StringWriter(){
|
||||||
|
buffer=new uint8_t[STRING_SIZE];
|
||||||
|
wp=buffer;
|
||||||
|
bufferSize=STRING_SIZE;
|
||||||
|
buffer [0]=0;
|
||||||
|
};
|
||||||
|
const char *GwBoatItemBase::StringWriter::c_str() const{
|
||||||
|
return (const char *)buffer;
|
||||||
|
}
|
||||||
|
int GwBoatItemBase::StringWriter::getSize() const{
|
||||||
|
return wp-buffer;
|
||||||
|
}
|
||||||
|
void GwBoatItemBase::StringWriter::reset(){
|
||||||
|
wp=buffer;
|
||||||
|
*wp=0;
|
||||||
|
}
|
||||||
|
void GwBoatItemBase::StringWriter::ensure(size_t size){
|
||||||
|
size_t fill=wp-buffer;
|
||||||
|
size_t newSize=bufferSize;
|
||||||
|
while ((fill+size) >= (newSize-1) ){
|
||||||
|
newSize+=STRING_SIZE;
|
||||||
|
}
|
||||||
|
if (newSize != bufferSize){
|
||||||
|
uint8_t *newBuffer=new uint8_t[newSize];
|
||||||
|
memcpy(newBuffer,buffer,fill);
|
||||||
|
newBuffer[fill]=0;
|
||||||
|
delete buffer;
|
||||||
|
buffer=newBuffer;
|
||||||
|
wp=newBuffer+fill;
|
||||||
|
bufferSize=newSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size_t GwBoatItemBase::StringWriter::write(uint8_t c){
|
||||||
|
ensure(1);
|
||||||
|
*wp=c;
|
||||||
|
wp++;
|
||||||
|
*wp=0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
size_t GwBoatItemBase::StringWriter::write(const uint8_t* s, size_t n){
|
||||||
|
ensure(n);
|
||||||
|
memcpy(wp,s,n);
|
||||||
|
wp+=n;
|
||||||
|
*wp=0;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
template<class T> GwBoatItem<T>::GwBoatItem(String name,String formatInfo,unsigned long invalidTime,GwBoatItemMap *map):
|
||||||
|
GwBoatItemBase(name,formatInfo,invalidTime){
|
||||||
|
T dummy;
|
||||||
|
this->type=GwBoatItemTypes::getType(dummy);
|
||||||
|
if (map){
|
||||||
|
(*map)[name]=this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
bool GwBoatItem<T>::update(T nv, int source)
|
||||||
|
{
|
||||||
|
unsigned long now = millis();
|
||||||
|
if (isValid(now))
|
||||||
|
{
|
||||||
|
//priority handling
|
||||||
|
//sources with lower ids will win
|
||||||
|
//and we will not overwrite their value
|
||||||
|
if (lastUpdateSource < source && lastUpdateSource >= 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data = nv;
|
||||||
|
lastUpdateSource = source;
|
||||||
|
uls(now);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
template <class T>
|
||||||
|
bool GwBoatItem<T>::updateMax(T nv, int sourceId)
|
||||||
|
{
|
||||||
|
unsigned long now = millis();
|
||||||
|
if (!isValid(now))
|
||||||
|
{
|
||||||
|
return update(nv, sourceId);
|
||||||
|
}
|
||||||
|
if (getData() < nv)
|
||||||
|
{
|
||||||
|
data = nv;
|
||||||
|
lastUpdateSource = sourceId;
|
||||||
|
uls(now);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
template <class T>
|
||||||
|
void GwBoatItem<T>::toJsonDoc(JsonDocument *doc, unsigned long minTime)
|
||||||
|
{
|
||||||
|
JsonObject o = doc->createNestedObject(name);
|
||||||
|
o[F("value")] = getData();
|
||||||
|
o[F("update")] = minTime - lastSet;
|
||||||
|
o[F("source")] = lastUpdateSource;
|
||||||
|
o[F("valid")] = isValid(minTime);
|
||||||
|
o[F("format")] = format;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class WriterWrapper{
|
||||||
|
GwBoatItemBase::StringWriter *writer=NULL;
|
||||||
|
public:
|
||||||
|
WriterWrapper(GwBoatItemBase::StringWriter *w){
|
||||||
|
writer=w;
|
||||||
|
}
|
||||||
|
size_t write(uint8_t c){
|
||||||
|
if (! writer) return 0;
|
||||||
|
return writer->write(c);
|
||||||
|
}
|
||||||
|
size_t write(const uint8_t* s, size_t n){
|
||||||
|
if (! writer) return 0;
|
||||||
|
return writer->write(s,n);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
typedef ARDUINOJSON_NAMESPACE::TextFormatter<WriterWrapper> GwTextWriter;
|
||||||
|
|
||||||
|
static void writeToString(GwTextWriter *writer,const double &value){
|
||||||
|
writer->writeFloat(value);
|
||||||
|
}
|
||||||
|
static void writeToString(GwTextWriter *writer,const uint16_t &value){
|
||||||
|
writer->writeInteger(value);
|
||||||
|
}
|
||||||
|
static void writeToString(GwTextWriter *writer,const uint32_t &value){
|
||||||
|
writer->writeInteger(value);
|
||||||
|
}
|
||||||
|
static void writeToString(GwTextWriter *writer,const int16_t &value){
|
||||||
|
writer->writeInteger(value);
|
||||||
|
}
|
||||||
|
static void writeToString(GwTextWriter *writer,GwSatInfoList &value){
|
||||||
|
writer->writeInteger(value.getNumSats());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void GwBoatItem<T>::fillString(){
|
||||||
|
bool valid=isValid();
|
||||||
|
if (writer.getSize() && (valid == lastStringValid)) return;
|
||||||
|
lastStringValid=valid;
|
||||||
|
writer.reset();
|
||||||
|
WriterWrapper wrapper(&writer);
|
||||||
|
GwTextWriter stringWriter(wrapper);
|
||||||
|
stringWriter.writeRaw(name.c_str());
|
||||||
|
stringWriter.writeChar(',');
|
||||||
|
stringWriter.writeInteger(valid?1:0);
|
||||||
|
stringWriter.writeChar(',');
|
||||||
|
stringWriter.writeInteger(lastSet);
|
||||||
|
stringWriter.writeChar(',');
|
||||||
|
stringWriter.writeInteger(lastUpdateSource);
|
||||||
|
stringWriter.writeChar(',');
|
||||||
|
stringWriter.writeRaw(format.c_str());
|
||||||
|
stringWriter.writeChar(',');
|
||||||
|
writeToString(&stringWriter,data);
|
||||||
|
}
|
||||||
|
|
||||||
|
template class GwBoatItem<double>;
|
||||||
|
template class GwBoatItem<uint32_t>;
|
||||||
|
template class GwBoatItem<uint16_t>;
|
||||||
|
template class GwBoatItem<int16_t>;
|
||||||
|
void GwSatInfoList::houseKeeping(unsigned long ts)
|
||||||
|
{
|
||||||
|
if (ts == 0)
|
||||||
|
ts = millis();
|
||||||
|
sats.erase(std::remove_if(
|
||||||
|
sats.begin(),
|
||||||
|
sats.end(),
|
||||||
|
[ts, this](const GwSatInfo &info)
|
||||||
|
{
|
||||||
|
return (info.timestamp + lifeTime) < ts;
|
||||||
|
}),
|
||||||
|
sats.end());
|
||||||
|
}
|
||||||
|
void GwSatInfoList::update(GwSatInfo entry)
|
||||||
|
{
|
||||||
|
unsigned long now = millis();
|
||||||
|
entry.timestamp = now;
|
||||||
|
for (auto it = sats.begin(); it != sats.end(); it++)
|
||||||
|
{
|
||||||
|
if (it->PRN == entry.PRN)
|
||||||
|
{
|
||||||
|
*it = entry;
|
||||||
|
houseKeeping();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
houseKeeping();
|
||||||
|
sats.push_back(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
GwBoatDataSatList::GwBoatDataSatList(String name, String formatInfo, unsigned long invalidTime , GwBoatItemMap *map) :
|
||||||
|
GwBoatItem<GwSatInfoList>(name, formatInfo, invalidTime, map) {}
|
||||||
|
|
||||||
|
bool GwBoatDataSatList::update(GwSatInfo info, int source)
|
||||||
|
{
|
||||||
|
unsigned long now = millis();
|
||||||
|
if (isValid(now))
|
||||||
|
{
|
||||||
|
//priority handling
|
||||||
|
//sources with lower ids will win
|
||||||
|
//and we will not overwrite their value
|
||||||
|
if (lastUpdateSource < source)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastUpdateSource = source;
|
||||||
|
uls(now);
|
||||||
|
data.update(info);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
void GwBoatDataSatList::toJsonDoc(JsonDocument *doc, unsigned long minTime)
|
||||||
|
{
|
||||||
|
data.houseKeeping();
|
||||||
|
GwBoatItem<GwSatInfoList>::toJsonDoc(doc, minTime);
|
||||||
|
}
|
||||||
|
|
||||||
GwBoatData::GwBoatData(GwLog *logger){
|
GwBoatData::GwBoatData(GwLog *logger){
|
||||||
this->logger=logger;
|
this->logger=logger;
|
||||||
|
@ -23,7 +275,7 @@ template<class T> GwBoatItem<T> *GwBoatData::getOrCreate(T initial, GwBoatItemNa
|
||||||
}
|
}
|
||||||
return (GwBoatItem<T>*)(it->second);
|
return (GwBoatItem<T>*)(it->second);
|
||||||
}
|
}
|
||||||
GwBoatItem<T> *rt=new GwBoatItem<T>(GwBoatItemTypes::getType(initial), name,
|
GwBoatItem<T> *rt=new GwBoatItem<T>(name,
|
||||||
provider->getBoatItemFormat(),
|
provider->getBoatItemFormat(),
|
||||||
provider->getInvalidTime(),
|
provider->getInvalidTime(),
|
||||||
&values);
|
&values);
|
||||||
|
@ -65,7 +317,14 @@ String GwBoatData::toJson() const {
|
||||||
serializeJson(json,buf);
|
serializeJson(json,buf);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
String GwBoatData::toString(){
|
||||||
|
String rt;
|
||||||
|
for (auto it=values.begin() ; it != values.end();it++){
|
||||||
|
rt+=it->second->getDataString();
|
||||||
|
rt+="\n";
|
||||||
|
}
|
||||||
|
return rt;
|
||||||
|
}
|
||||||
double formatCourse(double cv)
|
double formatCourse(double cv)
|
||||||
{
|
{
|
||||||
double rt = cv * 180.0 / M_PI;
|
double rt = cv * 180.0 / M_PI;
|
||||||
|
@ -102,3 +361,18 @@ double mtr2nm(double m)
|
||||||
bool convertToJson(const GwSatInfoList &si,JsonVariant &variant){
|
bool convertToJson(const GwSatInfoList &si,JsonVariant &variant){
|
||||||
return variant.set(si.getNumSats());
|
return variant.set(si.getNumSats());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _UNDEF
|
||||||
|
#include <ArduinoJson/Json/TextFormatter.hpp>
|
||||||
|
|
||||||
|
class XWriter{
|
||||||
|
public:
|
||||||
|
void write(uint8_t c) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(const uint8_t* s, size_t n) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
static XWriter xwriter;
|
||||||
|
ARDUINOJSON_NAMESPACE::TextFormatter<XWriter> testWriter(xwriter);
|
||||||
|
#endif
|
|
@ -7,20 +7,22 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
#define GW_BOAT_VALUE_LEN 32
|
#define GW_BOAT_VALUE_LEN 32
|
||||||
#define GWSC(name) static constexpr const __FlashStringHelper* name=F(#name)
|
#define GWSC(name) static constexpr const __FlashStringHelper* name=F(#name)
|
||||||
#define GWTYPE_DOUBLE 1
|
|
||||||
#define GWTYPE_UINT32 2
|
|
||||||
#define GWTYPE_UINT16 3
|
|
||||||
#define GWTYPE_INT16 4
|
|
||||||
#define GWTYPE_USER 100
|
|
||||||
class GwBoatItemTypes{
|
|
||||||
public:
|
|
||||||
static int getType(const uint32_t &x){return GWTYPE_UINT32;}
|
|
||||||
static int getType(const uint16_t &x){return GWTYPE_UINT16;}
|
|
||||||
static int getType(const int16_t &x){return GWTYPE_INT16;}
|
|
||||||
static int getType(const double &x){return GWTYPE_DOUBLE;}
|
|
||||||
};
|
|
||||||
class GwBoatItemBase{
|
class GwBoatItemBase{
|
||||||
public:
|
public:
|
||||||
|
class StringWriter{
|
||||||
|
uint8_t *buffer =NULL;
|
||||||
|
uint8_t *wp=NULL;
|
||||||
|
size_t bufferSize=0;
|
||||||
|
void ensure(size_t size);
|
||||||
|
public:
|
||||||
|
StringWriter();
|
||||||
|
size_t write(uint8_t c);
|
||||||
|
size_t write(const uint8_t* s, size_t n);
|
||||||
|
const char * c_str() const;
|
||||||
|
int getSize() const;
|
||||||
|
void reset();
|
||||||
|
};
|
||||||
static const unsigned long INVALID_TIME=60000;
|
static const unsigned long INVALID_TIME=60000;
|
||||||
//the formatter names that must be known in js
|
//the formatter names that must be known in js
|
||||||
GWSC(formatCourse);
|
GWSC(formatCourse);
|
||||||
|
@ -42,33 +44,30 @@ class GwBoatItemBase{
|
||||||
unsigned long invalidTime=INVALID_TIME;
|
unsigned long invalidTime=INVALID_TIME;
|
||||||
String name;
|
String name;
|
||||||
String format;
|
String format;
|
||||||
|
StringWriter writer;
|
||||||
void uls(unsigned long ts=0){
|
void uls(unsigned long ts=0){
|
||||||
if (ts) lastSet=ts;
|
if (ts) lastSet=ts;
|
||||||
else lastSet=millis();
|
else lastSet=millis();
|
||||||
|
writer.reset(); //value has changed
|
||||||
}
|
}
|
||||||
|
int lastUpdateSource;
|
||||||
public:
|
public:
|
||||||
int getCurrentType(){return type;}
|
int getCurrentType(){return type;}
|
||||||
unsigned long getLastSet() const {return lastSet;}
|
unsigned long getLastSet() const {return lastSet;}
|
||||||
bool isValid(unsigned long now=0) const {
|
bool isValid(unsigned long now=0) const ;
|
||||||
if (lastSet == 0) return false;
|
GwBoatItemBase(String name,String format,unsigned long invalidTime=INVALID_TIME);
|
||||||
if (invalidTime == 0) return true;
|
|
||||||
if (now == 0) now=millis();
|
|
||||||
return (lastSet + invalidTime) >= now;
|
|
||||||
}
|
|
||||||
GwBoatItemBase(String name,String format,unsigned long invalidTime=INVALID_TIME){
|
|
||||||
lastSet=0;
|
|
||||||
this->invalidTime=invalidTime;
|
|
||||||
this->name=name;
|
|
||||||
this->format=format;
|
|
||||||
this->type=0;
|
|
||||||
}
|
|
||||||
virtual ~GwBoatItemBase(){}
|
virtual ~GwBoatItemBase(){}
|
||||||
void invalidate(){
|
void invalidate(){
|
||||||
lastSet=0;
|
lastSet=0;
|
||||||
}
|
}
|
||||||
|
const char *getDataString(){
|
||||||
|
fillString();
|
||||||
|
return writer.c_str();
|
||||||
|
}
|
||||||
|
virtual void fillString()=0;
|
||||||
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime)=0;
|
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime)=0;
|
||||||
virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(10);}
|
virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(10);}
|
||||||
virtual int getLastSource()=0;
|
virtual int getLastSource(){return lastUpdateSource;}
|
||||||
virtual void refresh(unsigned long ts=0){uls(ts);}
|
virtual void refresh(unsigned long ts=0){uls(ts);}
|
||||||
String getName(){return name;}
|
String getName(){return name;}
|
||||||
};
|
};
|
||||||
|
@ -76,45 +75,12 @@ class GwBoatData;
|
||||||
template<class T> class GwBoatItem : public GwBoatItemBase{
|
template<class T> class GwBoatItem : public GwBoatItemBase{
|
||||||
protected:
|
protected:
|
||||||
T data;
|
T data;
|
||||||
int lastUpdateSource;
|
bool lastStringValid=false;
|
||||||
public:
|
public:
|
||||||
GwBoatItem(int type,String name,String formatInfo,unsigned long invalidTime=INVALID_TIME,GwBoatItemMap *map=NULL):
|
GwBoatItem(String name,String formatInfo,unsigned long invalidTime=INVALID_TIME,GwBoatItemMap *map=NULL);
|
||||||
GwBoatItemBase(name,formatInfo,invalidTime){
|
|
||||||
this->type=type;
|
|
||||||
if (map){
|
|
||||||
(*map)[name]=this;
|
|
||||||
}
|
|
||||||
lastUpdateSource=-1;
|
|
||||||
}
|
|
||||||
virtual ~GwBoatItem(){}
|
virtual ~GwBoatItem(){}
|
||||||
bool update(T nv, int source=-1){
|
bool update(T nv, int source=-1);
|
||||||
unsigned long now=millis();
|
bool updateMax(T nv,int sourceId=-1);
|
||||||
if (isValid(now)){
|
|
||||||
//priority handling
|
|
||||||
//sources with lower ids will win
|
|
||||||
//and we will not overwrite their value
|
|
||||||
if (lastUpdateSource < source && lastUpdateSource >= 0){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
data=nv;
|
|
||||||
lastUpdateSource=source;
|
|
||||||
uls(now);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool updateMax(T nv,int sourceId=-1){
|
|
||||||
unsigned long now=millis();
|
|
||||||
if (! isValid(now)){
|
|
||||||
return update(nv,sourceId);
|
|
||||||
}
|
|
||||||
if (getData() < nv){
|
|
||||||
data=nv;
|
|
||||||
lastUpdateSource=sourceId;
|
|
||||||
uls(now);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
T getData(){
|
T getData(){
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
@ -122,14 +88,8 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
|
||||||
if (! isValid(millis())) return defaultv;
|
if (! isValid(millis())) return defaultv;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime){
|
virtual void fillString();
|
||||||
JsonObject o=doc->createNestedObject(name);
|
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime);
|
||||||
o[F("value")]=getData();
|
|
||||||
o[F("update")]=minTime-lastSet;
|
|
||||||
o[F("source")]=lastUpdateSource;
|
|
||||||
o[F("valid")]=isValid(minTime);
|
|
||||||
o[F("format")]=format;
|
|
||||||
}
|
|
||||||
virtual int getLastSource(){return lastUpdateSource;}
|
virtual int getLastSource(){return lastUpdateSource;}
|
||||||
};
|
};
|
||||||
double formatCourse(double cv);
|
double formatCourse(double cv);
|
||||||
|
@ -151,29 +111,8 @@ class GwSatInfoList{
|
||||||
public:
|
public:
|
||||||
static const unsigned long lifeTime=32000;
|
static const unsigned long lifeTime=32000;
|
||||||
std::vector<GwSatInfo> sats;
|
std::vector<GwSatInfo> sats;
|
||||||
void houseKeeping(unsigned long ts=0){
|
void houseKeeping(unsigned long ts=0);
|
||||||
if (ts == 0) ts=millis();
|
void update(GwSatInfo entry);
|
||||||
sats.erase(std::remove_if(
|
|
||||||
sats.begin(),
|
|
||||||
sats.end(),
|
|
||||||
[ts,this](const GwSatInfo &info){
|
|
||||||
return (info.timestamp + lifeTime) < ts;
|
|
||||||
}
|
|
||||||
),sats.end());
|
|
||||||
}
|
|
||||||
void update(GwSatInfo entry){
|
|
||||||
unsigned long now=millis();
|
|
||||||
entry.timestamp=now;
|
|
||||||
for (auto it=sats.begin();it!=sats.end();it++){
|
|
||||||
if (it->PRN == entry.PRN){
|
|
||||||
*it=entry;
|
|
||||||
houseKeeping();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
houseKeeping();
|
|
||||||
sats.push_back(entry);
|
|
||||||
}
|
|
||||||
int getNumSats() const{
|
int getNumSats() const{
|
||||||
return sats.size();
|
return sats.size();
|
||||||
}
|
}
|
||||||
|
@ -183,34 +122,12 @@ class GwSatInfoList{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bool convertToJson(const GwSatInfoList &si,JsonVariant &variant);
|
|
||||||
class GwBoatDataSatList : public GwBoatItem<GwSatInfoList>
|
class GwBoatDataSatList : public GwBoatItem<GwSatInfoList>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GwBoatDataSatList(String name, String formatInfo, unsigned long invalidTime = INVALID_TIME, GwBoatItemMap *map = NULL) :
|
GwBoatDataSatList(String name, String formatInfo, unsigned long invalidTime = INVALID_TIME, GwBoatItemMap *map = NULL);
|
||||||
GwBoatItem<GwSatInfoList>(GWTYPE_USER+1, name, formatInfo, invalidTime, map) {}
|
bool update(GwSatInfo info, int source);
|
||||||
bool update(GwSatInfo info, int source)
|
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime);
|
||||||
{
|
|
||||||
unsigned long now = millis();
|
|
||||||
if (isValid(now))
|
|
||||||
{
|
|
||||||
//priority handling
|
|
||||||
//sources with lower ids will win
|
|
||||||
//and we will not overwrite their value
|
|
||||||
if (lastUpdateSource < source)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lastUpdateSource = source;
|
|
||||||
uls(now);
|
|
||||||
data.update(info);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime){
|
|
||||||
data.houseKeeping();
|
|
||||||
GwBoatItem<GwSatInfoList>::toJsonDoc(doc,minTime);
|
|
||||||
}
|
|
||||||
GwSatInfo *getAt(int idx){
|
GwSatInfo *getAt(int idx){
|
||||||
if (! isValid()) return NULL;
|
if (! isValid()) return NULL;
|
||||||
return data.getAt(idx);
|
return data.getAt(idx);
|
||||||
|
@ -231,7 +148,7 @@ public:
|
||||||
virtual ~GwBoatItemNameProvider() {}
|
virtual ~GwBoatItemNameProvider() {}
|
||||||
};
|
};
|
||||||
#define GWBOATDATA(type,name,time,fmt) \
|
#define GWBOATDATA(type,name,time,fmt) \
|
||||||
GwBoatItem<type> *name=new GwBoatItem<type>(GwBoatItemTypes::getType((type)0),F(#name),GwBoatItemBase::fmt,time,&values) ;
|
GwBoatItem<type> *name=new GwBoatItem<type>(F(#name),GwBoatItemBase::fmt,time,&values) ;
|
||||||
#define GWSPECBOATDATA(clazz,name,time,fmt) \
|
#define GWSPECBOATDATA(clazz,name,time,fmt) \
|
||||||
clazz *name=new clazz(F(#name),GwBoatItemBase::fmt,time,&values) ;
|
clazz *name=new clazz(F(#name),GwBoatItemBase::fmt,time,&values) ;
|
||||||
class GwBoatData{
|
class GwBoatData{
|
||||||
|
@ -283,6 +200,7 @@ class GwBoatData{
|
||||||
template<class T> bool update(T value,int source,GwBoatItemNameProvider *provider);
|
template<class T> bool update(T value,int source,GwBoatItemNameProvider *provider);
|
||||||
template<class T> T getDataWithDefault(T defaultv, GwBoatItemNameProvider *provider);
|
template<class T> T getDataWithDefault(T defaultv, GwBoatItemNameProvider *provider);
|
||||||
String toJson() const;
|
String toJson() const;
|
||||||
|
String toString();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
13
src/main.cpp
13
src/main.cpp
|
@ -420,6 +420,17 @@ protected:
|
||||||
result = boatData.toJson();
|
result = boatData.toJson();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
class BoatDataStringRequest : public GwRequestMessage
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BoatDataStringRequest() : GwRequestMessage(F("text/plain"),F("boatDataString")){};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void processRequest()
|
||||||
|
{
|
||||||
|
result = boatData.toString();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class XdrExampleRequest : public GwRequestMessage
|
class XdrExampleRequest : public GwRequestMessage
|
||||||
{
|
{
|
||||||
|
@ -562,6 +573,8 @@ void setup() {
|
||||||
{ return new ResetConfigRequest(); });
|
{ return new ResetConfigRequest(); });
|
||||||
webserver.registerMainHandler("/api/boatData", [](AsyncWebServerRequest *request)->GwRequestMessage *
|
webserver.registerMainHandler("/api/boatData", [](AsyncWebServerRequest *request)->GwRequestMessage *
|
||||||
{ return new BoatDataRequest(); });
|
{ return new BoatDataRequest(); });
|
||||||
|
webserver.registerMainHandler("/api/boatDataString", [](AsyncWebServerRequest *request)->GwRequestMessage *
|
||||||
|
{ return new BoatDataStringRequest(); });
|
||||||
webserver.registerMainHandler("/api/xdrExample", [](AsyncWebServerRequest *request)->GwRequestMessage *
|
webserver.registerMainHandler("/api/xdrExample", [](AsyncWebServerRequest *request)->GwRequestMessage *
|
||||||
{
|
{
|
||||||
String mapping=request->arg("mapping");
|
String mapping=request->arg("mapping");
|
||||||
|
|
57
web/index.js
57
web/index.js
|
@ -1035,6 +1035,7 @@ function resizeFont(el,reset,maxIt){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function createDashboardItem(name, def, parent) {
|
function createDashboardItem(name, def, parent) {
|
||||||
|
if (! def.name) return;
|
||||||
let frame = addEl('div', 'dash', parent);
|
let frame = addEl('div', 'dash', parent);
|
||||||
let title = addEl('span', 'dashTitle', frame, name);
|
let title = addEl('span', 'dashTitle', frame, name);
|
||||||
let value = addEl('span', 'dashValue', frame);
|
let value = addEl('span', 'dashValue', frame);
|
||||||
|
@ -1044,22 +1045,35 @@ function createDashboardItem(name, def, parent) {
|
||||||
let footer = addEl('div','footer',frame);
|
let footer = addEl('div','footer',frame);
|
||||||
let src= addEl('span','source',footer);
|
let src= addEl('span','source',footer);
|
||||||
src.setAttribute('id','source_'+name);
|
src.setAttribute('id','source_'+name);
|
||||||
let u=fmt?fmt.u:'';
|
let u=fmt?fmt.u:' ';
|
||||||
if (! fmt && def.format.match(/formatXdr/)){
|
if (! fmt && def.format && def.format.match(/formatXdr/)){
|
||||||
u=def.format.replace(/formatXdr/,'');
|
u=def.format.replace(/formatXdr/,'');
|
||||||
}
|
}
|
||||||
addEl('span','unit',footer,u);
|
addEl('span','unit',footer,u);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
function parseBoatDataLine(line){
|
||||||
|
let rt={};
|
||||||
|
let parts=line.split(',');
|
||||||
|
rt.name=parts[0];
|
||||||
|
rt.valid=parts[1] === '1';
|
||||||
|
rt.update=parseInt(parts[2]);
|
||||||
|
rt.source=parseInt(parts[3]);
|
||||||
|
rt.format=parts[4];
|
||||||
|
rt.value=parts[5];
|
||||||
|
return rt;
|
||||||
|
}
|
||||||
function createDashboard() {
|
function createDashboard() {
|
||||||
let frame = document.getElementById('dashboardPage');
|
let frame = document.getElementById('dashboardPage');
|
||||||
if (!frame) return;
|
if (!frame) return;
|
||||||
getJson("api/boatData").then(function (json) {
|
getText("api/boatDataString").then(function (txt) {
|
||||||
frame.innerHTML = '';
|
frame.innerHTML = '';
|
||||||
for (let n in json) {
|
let values=txt.split('\n');
|
||||||
createDashboardItem(n, json[n], frame);
|
for (let n in values) {
|
||||||
|
let def=parseBoatDataLine(values[n]);
|
||||||
|
createDashboardItem(def.name, def, frame);
|
||||||
}
|
}
|
||||||
updateDashboard(json);
|
updateDashboard(values);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function sourceName(v){
|
function sourceName(v){
|
||||||
|
@ -1071,30 +1085,34 @@ function sourceName(v){
|
||||||
}
|
}
|
||||||
function updateDashboard(data) {
|
function updateDashboard(data) {
|
||||||
let frame = document.getElementById('dashboardPage');
|
let frame = document.getElementById('dashboardPage');
|
||||||
|
let names={};
|
||||||
for (let n in data) {
|
for (let n in data) {
|
||||||
let de = document.getElementById('data_' + n);
|
let current=parseBoatDataLine(data[n]);
|
||||||
|
if (! current.name) continue;
|
||||||
|
names[current.name]=true;
|
||||||
|
let de = document.getElementById('data_' + current.name);
|
||||||
if (! de && frame){
|
if (! de && frame){
|
||||||
de=createDashboardItem(n,data[n],frame);
|
de=createDashboardItem(current.name,current,frame);
|
||||||
}
|
}
|
||||||
if (de) {
|
if (de) {
|
||||||
let newContent='----';
|
let newContent='----';
|
||||||
if (data[n].valid) {
|
if (current.valid) {
|
||||||
let formatter;
|
let formatter;
|
||||||
if (data[n].format && data[n].format != "NULL") {
|
if (current.format && current.format != "NULL") {
|
||||||
let key = data[n].format.replace(/^\&/, '');
|
let key = current.format.replace(/^\&/, '');
|
||||||
formatter = valueFormatters[key];
|
formatter = valueFormatters[key];
|
||||||
}
|
}
|
||||||
if (formatter) {
|
if (formatter) {
|
||||||
newContent = formatter.f(data[n].value);
|
newContent = formatter.f(current.value);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let v = parseFloat(data[n].value);
|
let v = parseFloat(current.value);
|
||||||
if (!isNaN(v)) {
|
if (!isNaN(v)) {
|
||||||
v = v.toFixed(3)
|
v = v.toFixed(3)
|
||||||
newContent = v;
|
newContent = v;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
newContent = data[n].value;
|
newContent = current.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1104,15 +1122,16 @@ function updateDashboard(data) {
|
||||||
resizeFont(de,true);
|
resizeFont(de,true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let src=document.getElementById('source_'+n);
|
let src=document.getElementById('source_'+current.name);
|
||||||
if (src){
|
if (src){
|
||||||
src.textContent=sourceName(data[n].source);
|
src.textContent=sourceName(current.source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log("update");
|
||||||
forEl('.dashValue',function(el){
|
forEl('.dashValue',function(el){
|
||||||
let id=el.getAttribute('id');
|
let id=el.getAttribute('id');
|
||||||
if (id){
|
if (id){
|
||||||
if (! data[id.replace(/^data_/,'')]){
|
if (! names[id.replace(/^data_/,'')]){
|
||||||
el.parentElement.remove();
|
el.parentElement.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1123,8 +1142,8 @@ window.setInterval(update, 1000);
|
||||||
window.setInterval(function () {
|
window.setInterval(function () {
|
||||||
let dp = document.getElementById('dashboardPage');
|
let dp = document.getElementById('dashboardPage');
|
||||||
if (dp.classList.contains('hidden')) return;
|
if (dp.classList.contains('hidden')) return;
|
||||||
getJson('api/boatData').then(function (data) {
|
getText('api/boatDataString').then(function (data) {
|
||||||
updateDashboard(data);
|
updateDashboard(data.split('\n'));
|
||||||
});
|
});
|
||||||
}, 1000);
|
}, 1000);
|
||||||
window.addEventListener('load', function () {
|
window.addEventListener('load', function () {
|
||||||
|
|
Loading…
Reference in New Issue