provide formatter info to the UI
This commit is contained in:
		
							parent
							
								
									616ca1348b
								
							
						
					
					
						commit
						ecad013d09
					
				| 
						 | 
					@ -19,7 +19,7 @@ String GwBoatData::toJson() const {
 | 
				
			||||||
        count++;
 | 
					        count++;
 | 
				
			||||||
        elementSizes+=(*it)->getJsonSize();
 | 
					        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++){
 | 
					    for (it=values.begin() ; it != values.end();it++){
 | 
				
			||||||
        (*it)->toJsonDoc(&json,minTime);
 | 
					        (*it)->toJsonDoc(&json,minTime);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,7 +36,7 @@ class GwBoatItemBase{
 | 
				
			||||||
            lastSet=0;
 | 
					            lastSet=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(5);}
 | 
					        virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(15);}
 | 
				
			||||||
        virtual int getLastSource()=0;
 | 
					        virtual int getLastSource()=0;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
class GwBoatData;
 | 
					class GwBoatData;
 | 
				
			||||||
| 
						 | 
					@ -47,10 +47,12 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
 | 
				
			||||||
        T data;
 | 
					        T data;
 | 
				
			||||||
        Formatter fmt;
 | 
					        Formatter fmt;
 | 
				
			||||||
        int lastUpdateSource;
 | 
					        int lastUpdateSource;
 | 
				
			||||||
 | 
					        String formatInfo;
 | 
				
			||||||
    public:
 | 
					    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){
 | 
					            GwBoatItemBase(name,invalidTime){
 | 
				
			||||||
            this->fmt=fmt;
 | 
					            this->fmt=fmt;
 | 
				
			||||||
 | 
					            this->formatInfo=formatInfo;
 | 
				
			||||||
            if (map){
 | 
					            if (map){
 | 
				
			||||||
                map->push_back(this);
 | 
					                map->push_back(this);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -86,6 +88,7 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
 | 
				
			||||||
            o[F("update")]=minTime-lastSet;
 | 
					            o[F("update")]=minTime-lastSet;
 | 
				
			||||||
            o[F("source")]=lastUpdateSource;
 | 
					            o[F("source")]=lastUpdateSource;
 | 
				
			||||||
            o[F("valid")]=isValid(minTime);
 | 
					            o[F("valid")]=isValid(minTime);
 | 
				
			||||||
 | 
					            o[F("format")]=formatInfo;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        virtual int getLastSource(){return lastUpdateSource;}
 | 
					        virtual int getLastSource(){return lastUpdateSource;}
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					@ -129,7 +132,7 @@ static double kelvinToC(double v)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define GWBOATDATA(type,name,time,fmt)  \
 | 
					#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{
 | 
					class GwBoatData{
 | 
				
			||||||
    private:
 | 
					    private:
 | 
				
			||||||
        GwLog *logger;
 | 
					        GwLog *logger;
 | 
				
			||||||
| 
						 | 
					@ -143,8 +146,8 @@ class GwBoatData{
 | 
				
			||||||
    GWBOATDATA(double,STW,4000,&formatKnots)
 | 
					    GWBOATDATA(double,STW,4000,&formatKnots)
 | 
				
			||||||
    GWBOATDATA(double,TWS,4000,&formatKnots)
 | 
					    GWBOATDATA(double,TWS,4000,&formatKnots)
 | 
				
			||||||
    GWBOATDATA(double,AWS,4000,&formatKnots)
 | 
					    GWBOATDATA(double,AWS,4000,&formatKnots)
 | 
				
			||||||
    GWBOATDATA(double,MaxTws,4000,&formatKnots)
 | 
					    GWBOATDATA(double,MaxTws,0,&formatKnots)
 | 
				
			||||||
    GWBOATDATA(double,MaxAws,4000,&formatKnots)
 | 
					    GWBOATDATA(double,MaxAws,0,&formatKnots)
 | 
				
			||||||
    GWBOATDATA(double,AWA,4000,&formatWind)
 | 
					    GWBOATDATA(double,AWA,4000,&formatWind)
 | 
				
			||||||
    GWBOATDATA(double,Heading,4000,&formatCourse) //true
 | 
					    GWBOATDATA(double,Heading,4000,&formatCourse) //true
 | 
				
			||||||
    GWBOATDATA(double,MagneticHeading,4000,&formatCourse)
 | 
					    GWBOATDATA(double,MagneticHeading,4000,&formatCourse)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -399,24 +399,42 @@
 | 
				
			||||||
      updateDashboard(json);
 | 
					      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){
 | 
					  function updateDashboard(data){
 | 
				
			||||||
    for (let n in data){
 | 
					    for (let n in data){
 | 
				
			||||||
      let de=document.getElementById('data_'+n);
 | 
					      let de=document.getElementById('data_'+n);
 | 
				
			||||||
      if (de){
 | 
					      if (de){
 | 
				
			||||||
        if (data[n].valid){
 | 
					        if (data[n].valid){
 | 
				
			||||||
          let v=parseFloat(data[n].value);
 | 
					          let formatter;
 | 
				
			||||||
          if (! isNaN(v)){
 | 
					          if (data[n].format && data[n].format != "NULL"){
 | 
				
			||||||
            v=v.toFixed(3)
 | 
					            let key=data[n].format.replace(/^\&/,'');
 | 
				
			||||||
            de.textContent=v;
 | 
					            formatter=valueFormatters[key];
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          else{
 | 
					          if (formatter){
 | 
				
			||||||
            de.textContent=data[n].value;
 | 
					            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="---";
 | 
					        else de.textContent="---";
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }  
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  window.setInterval(update,1000);
 | 
					  window.setInterval(update,1000);
 | 
				
			||||||
  window.setInterval(function(){
 | 
					  window.setInterval(function(){
 | 
				
			||||||
    let dp=document.getElementById('dashboardPage');
 | 
					    let dp=document.getElementById('dashboardPage');
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue