From ecad013d0930e6d742ec855e13537072993a1ff3 Mon Sep 17 00:00:00 2001 From: andreas Date: Sat, 6 Nov 2021 20:41:27 +0100 Subject: [PATCH] provide formatter info to the UI --- lib/boatData/GwBoatData.cpp | 2 +- lib/boatData/GwBoatData.h | 13 ++++++++----- web/index.html | 32 +++++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lib/boatData/GwBoatData.cpp b/lib/boatData/GwBoatData.cpp index 7d9ca6e..dfb8b39 100644 --- a/lib/boatData/GwBoatData.cpp +++ b/lib/boatData/GwBoatData.cpp @@ -19,7 +19,7 @@ String GwBoatData::toJson() const { count++; elementSizes+=(*it)->getJsonSize(); } - DynamicJsonDocument json(JSON_OBJECT_SIZE(count)+elementSizes+4); + DynamicJsonDocument json(JSON_OBJECT_SIZE(count)+elementSizes+10); for (it=values.begin() ; it != values.end();it++){ (*it)->toJsonDoc(&json,minTime); } diff --git a/lib/boatData/GwBoatData.h b/lib/boatData/GwBoatData.h index 5ffab7a..dcde248 100644 --- a/lib/boatData/GwBoatData.h +++ b/lib/boatData/GwBoatData.h @@ -36,7 +36,7 @@ class GwBoatItemBase{ lastSet=0; } virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime)=0; - virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(5);} + virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(15);} virtual int getLastSource()=0; }; class GwBoatData; @@ -47,10 +47,12 @@ template class GwBoatItem : public GwBoatItemBase{ T data; Formatter fmt; int lastUpdateSource; + String formatInfo; public: - GwBoatItem(String name,unsigned long invalidTime=INVALID_TIME,Formatter fmt=NULL,GwBoatItemMap *map=NULL): + GwBoatItem(String name,String formatInfo,unsigned long invalidTime=INVALID_TIME,Formatter fmt=NULL,GwBoatItemMap *map=NULL): GwBoatItemBase(name,invalidTime){ this->fmt=fmt; + this->formatInfo=formatInfo; if (map){ map->push_back(this); } @@ -86,6 +88,7 @@ template class GwBoatItem : public GwBoatItemBase{ o[F("update")]=minTime-lastSet; o[F("source")]=lastUpdateSource; o[F("valid")]=isValid(minTime); + o[F("format")]=formatInfo; } virtual int getLastSource(){return lastUpdateSource;} }; @@ -129,7 +132,7 @@ static double kelvinToC(double v) } #define GWBOATDATA(type,name,time,fmt) \ - GwBoatItem *name=new GwBoatItem(F(#name),time,fmt,&values) ; + GwBoatItem *name=new GwBoatItem(F(#name),F(#fmt),time,fmt,&values) ; class GwBoatData{ private: GwLog *logger; @@ -143,8 +146,8 @@ class GwBoatData{ GWBOATDATA(double,STW,4000,&formatKnots) GWBOATDATA(double,TWS,4000,&formatKnots) GWBOATDATA(double,AWS,4000,&formatKnots) - GWBOATDATA(double,MaxTws,4000,&formatKnots) - GWBOATDATA(double,MaxAws,4000,&formatKnots) + GWBOATDATA(double,MaxTws,0,&formatKnots) + GWBOATDATA(double,MaxAws,0,&formatKnots) GWBOATDATA(double,AWA,4000,&formatWind) GWBOATDATA(double,Heading,4000,&formatCourse) //true GWBOATDATA(double,MagneticHeading,4000,&formatCourse) diff --git a/web/index.html b/web/index.html index 74c74b9..c621333 100644 --- a/web/index.html +++ b/web/index.html @@ -399,24 +399,42 @@ updateDashboard(json); }); } + let valueFormatters={ + formatCourse: function(v){let x=parseFloat(v); return x.toFixed(0);}, + formatKnots: function(v){let x=parseFloat(v); return x.toFixed(2);}, + formatWind: function(v){let x=parseFloat(v); return x.toFixed(0);}, + mtr2nm: function(v){let x=parseFloat(v); return x.toFixed(2);}, + kelvinToC: function(v){let x=parseFloat(v); return x.toFixed(0);}, + } function updateDashboard(data){ for (let n in data){ let de=document.getElementById('data_'+n); if (de){ if (data[n].valid){ - let v=parseFloat(data[n].value); - if (! isNaN(v)){ - v=v.toFixed(3) - de.textContent=v; + let formatter; + if (data[n].format && data[n].format != "NULL"){ + let key=data[n].format.replace(/^\&/,''); + formatter=valueFormatters[key]; } - else{ - de.textContent=data[n].value; + if (formatter){ + de.textContent=formatter(data[n].value); + } + else { + let v = parseFloat(data[n].value); + if (!isNaN(v)) { + v = v.toFixed(3) + de.textContent = v; + } + else { + de.textContent = data[n].value; + } } } else de.textContent="---"; } } - } + } + window.setInterval(update,1000); window.setInterval(function(){ let dp=document.getElementById('dashboardPage');