provide formatter info to the UI

This commit is contained in:
andreas 2021-11-06 20:41:27 +01:00
parent 616ca1348b
commit ecad013d09
3 changed files with 34 additions and 13 deletions

View File

@ -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);
}

View File

@ -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 T> 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 T> 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<type> *name=new GwBoatItem<type>(F(#name),time,fmt,&values) ;
GwBoatItem<type> *name=new GwBoatItem<type>(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)

View File

@ -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];
}
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=data[n].value;
}
}
else de.textContent="---";
}
}
}
window.setInterval(update,1000);
window.setInterval(function(){
let dp=document.getElementById('dashboardPage');