cleanup boatData handling

This commit is contained in:
andreas 2021-10-20 16:42:12 +02:00
parent 68769368f2
commit e1e3928062
3 changed files with 29 additions and 23 deletions

View File

@ -23,7 +23,3 @@ String GwBoatData::toJson() const {
serializeJson(json,buf); serializeJson(json,buf);
return buf; return buf;
} }
GwBoatItemBase::GwBoatItemMap * GwBoatData::getValues(){
return &values;
}

View File

@ -31,17 +31,7 @@ class GwBoatItemBase{
} }
virtual void toJsonDoc(DynamicJsonDocument *doc,String name)=0; virtual void toJsonDoc(DynamicJsonDocument *doc,String name)=0;
}; };
class GwBoatData{ class GwBoatData;
private:
GwLog *logger;
GwBoatItemBase::GwBoatItemMap values;
public:
GwBoatData(GwLog *logger);
~GwBoatData();
GwBoatItemBase::GwBoatItemMap * getValues();
String toJson() const;
friend class GwBoatItemBase;
};
template<class T> class GwBoatItem : public GwBoatItemBase{ template<class T> class GwBoatItem : public GwBoatItemBase{
public: public:
typedef T (*Formatter)(T); typedef T (*Formatter)(T);
@ -64,9 +54,9 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
virtual void toJsonDoc(DynamicJsonDocument *doc,String name){ virtual void toJsonDoc(DynamicJsonDocument *doc,String name){
(*doc)[name]=getData(true); (*doc)[name]=getData(true);
} }
static GwBoatItem<T> *findOrCreate(GwBoatData *handler, String name,bool doCreate=true, private:
static GwBoatItem<T> *findOrCreate(GwBoatItemMap *values, String name,bool doCreate=true,
long invalidTime=GwBoatItemBase::INVALID_TIME, Formatter fmt=NULL){ long invalidTime=GwBoatItemBase::INVALID_TIME, Formatter fmt=NULL){
GwBoatItemMap *values=handler->getValues();
GwBoatItemMap::iterator it; GwBoatItemMap::iterator it;
if ((it=values->find(name)) != values->end()){ if ((it=values->find(name)) != values->end()){
return (GwBoatItem<T> *)it->second; return (GwBoatItem<T> *)it->second;
@ -76,6 +66,26 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
(*values)[name]=ni; (*values)[name]=ni;
return ni; return ni;
} }
friend class GwBoatData;
}; };
#define GWBOATDATA_IMPL_ITEM(type,name) GwBoatItem<type> *get##name##Item(String iname,bool doCreate=true, \
long invalidTime=GwBoatItemBase::INVALID_TIME, GwBoatItem<type>::Formatter fmt=NULL){ \
return GwBoatItem<type>::findOrCreate(&values,iname, doCreate, \
invalidTime,fmt);\
}
class GwBoatData{
private:
GwLog *logger;
GwBoatItemBase::GwBoatItemMap values;
public:
GwBoatData(GwLog *logger);
~GwBoatData();
String toJson() const;
GWBOATDATA_IMPL_ITEM(double,Double)
GWBOATDATA_IMPL_ITEM(long,Long)
};
#endif #endif

View File

@ -51,12 +51,12 @@ N2kDataToNMEA0183::N2kDataToNMEA0183(GwLog * logger, GwBoatData *boatData, tNMEA
LastWindTime=0; LastWindTime=0;
this->logger=logger; this->logger=logger;
this->boatData=boatData; this->boatData=boatData;
heading=GwBoatItem<double>::findOrCreate(boatData,F("Heading"),true,2000,&formatCourse); heading=boatData->getDoubleItem(F("Heading"),true,2000,&formatCourse);
latitude=GwBoatItem<double>::findOrCreate(boatData,F("Latitude"),true,4000); latitude=boatData->getDoubleItem(F("Latitude"),true,4000);
longitude=GwBoatItem<double>::findOrCreate(boatData,F("Longitude"),true,4000); longitude=boatData->getDoubleItem(F("Longitude"),true,4000);
altitude=GwBoatItem<double>::findOrCreate(boatData,F("Altitude"),true,4000); altitude=boatData->getDoubleItem(F("Altitude"),true,4000);
cog=GwBoatItem<double>::findOrCreate(boatData,F("COG"),true,2000,&formatCourse); cog=boatData->getDoubleItem(F("COG"),true,2000,&formatCourse);
sog=GwBoatItem<double>::findOrCreate(boatData,F("SOG"),true,2000,&formatKnots); sog=boatData->getDoubleItem(F("SOG"),true,2000,&formatKnots);
} }
//***************************************************************************** //*****************************************************************************