diff --git a/lib/boatData/GwBoatData.cpp b/lib/boatData/GwBoatData.cpp index 2767dfc..fba0bee 100644 --- a/lib/boatData/GwBoatData.cpp +++ b/lib/boatData/GwBoatData.cpp @@ -23,7 +23,3 @@ String GwBoatData::toJson() const { serializeJson(json,buf); return buf; } - -GwBoatItemBase::GwBoatItemMap * GwBoatData::getValues(){ - return &values; -} \ No newline at end of file diff --git a/lib/boatData/GwBoatData.h b/lib/boatData/GwBoatData.h index f88fb16..a06541c 100644 --- a/lib/boatData/GwBoatData.h +++ b/lib/boatData/GwBoatData.h @@ -31,17 +31,7 @@ class GwBoatItemBase{ } virtual void toJsonDoc(DynamicJsonDocument *doc,String name)=0; }; -class GwBoatData{ - private: - GwLog *logger; - GwBoatItemBase::GwBoatItemMap values; - public: - GwBoatData(GwLog *logger); - ~GwBoatData(); - GwBoatItemBase::GwBoatItemMap * getValues(); - String toJson() const; - friend class GwBoatItemBase; -}; +class GwBoatData; template class GwBoatItem : public GwBoatItemBase{ public: typedef T (*Formatter)(T); @@ -64,9 +54,9 @@ template class GwBoatItem : public GwBoatItemBase{ virtual void toJsonDoc(DynamicJsonDocument *doc,String name){ (*doc)[name]=getData(true); } - static GwBoatItem *findOrCreate(GwBoatData *handler, String name,bool doCreate=true, + private: + static GwBoatItem *findOrCreate(GwBoatItemMap *values, String name,bool doCreate=true, long invalidTime=GwBoatItemBase::INVALID_TIME, Formatter fmt=NULL){ - GwBoatItemMap *values=handler->getValues(); GwBoatItemMap::iterator it; if ((it=values->find(name)) != values->end()){ return (GwBoatItem *)it->second; @@ -76,6 +66,26 @@ template class GwBoatItem : public GwBoatItemBase{ (*values)[name]=ni; return ni; } + friend class GwBoatData; }; +#define GWBOATDATA_IMPL_ITEM(type,name) GwBoatItem *get##name##Item(String iname,bool doCreate=true, \ + long invalidTime=GwBoatItemBase::INVALID_TIME, GwBoatItem::Formatter fmt=NULL){ \ + return GwBoatItem::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 \ No newline at end of file diff --git a/lib/nmea2kto0183/N2kDataToNMEA0183.cpp b/lib/nmea2kto0183/N2kDataToNMEA0183.cpp index 8623e32..9337ded 100644 --- a/lib/nmea2kto0183/N2kDataToNMEA0183.cpp +++ b/lib/nmea2kto0183/N2kDataToNMEA0183.cpp @@ -51,12 +51,12 @@ N2kDataToNMEA0183::N2kDataToNMEA0183(GwLog * logger, GwBoatData *boatData, tNMEA LastWindTime=0; this->logger=logger; this->boatData=boatData; - heading=GwBoatItem::findOrCreate(boatData,F("Heading"),true,2000,&formatCourse); - latitude=GwBoatItem::findOrCreate(boatData,F("Latitude"),true,4000); - longitude=GwBoatItem::findOrCreate(boatData,F("Longitude"),true,4000); - altitude=GwBoatItem::findOrCreate(boatData,F("Altitude"),true,4000); - cog=GwBoatItem::findOrCreate(boatData,F("COG"),true,2000,&formatCourse); - sog=GwBoatItem::findOrCreate(boatData,F("SOG"),true,2000,&formatKnots); + heading=boatData->getDoubleItem(F("Heading"),true,2000,&formatCourse); + latitude=boatData->getDoubleItem(F("Latitude"),true,4000); + longitude=boatData->getDoubleItem(F("Longitude"),true,4000); + altitude=boatData->getDoubleItem(F("Altitude"),true,4000); + cog=boatData->getDoubleItem(F("COG"),true,2000,&formatCourse); + sog=boatData->getDoubleItem(F("SOG"),true,2000,&formatKnots); } //*****************************************************************************