diff --git a/lib/boatData/GwBoatData.cpp b/lib/boatData/GwBoatData.cpp index e05e0a4..533f931 100644 --- a/lib/boatData/GwBoatData.cpp +++ b/lib/boatData/GwBoatData.cpp @@ -10,6 +10,16 @@ GwBoatData::~GwBoatData(){ } } +template GwBoatItem *GwBoatData::getOrCreate(T dummy, String name, String format, + unsigned long invalidTime) +{ + for (auto it=values.begin();it != values.end();it++){ + if ((*it)->getName() == name){ + return *it; + } + } + return new GwBoatItem(name,format,invalidTime,&values); +} String GwBoatData::toJson() const { unsigned long minTime=millis(); GwBoatItemBase::GwBoatItemMap::const_iterator it; diff --git a/lib/boatData/GwBoatData.h b/lib/boatData/GwBoatData.h index f400241..d2e0db3 100644 --- a/lib/boatData/GwBoatData.h +++ b/lib/boatData/GwBoatData.h @@ -55,6 +55,7 @@ class GwBoatItemBase{ virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(15);} virtual int getLastSource()=0; virtual void refresh(unsigned long ts=0){uls(ts);} + String getName(){return name;} }; class GwBoatData; template class GwBoatItem : public GwBoatItemBase{ @@ -253,8 +254,43 @@ class GwBoatData{ public: GwBoatData(GwLog *logger); ~GwBoatData(); + template GwBoatItem *getOrCreate(T dummy,String name,String format, + unsigned long invalidTime=GwBoatItemBase::INVALID_TIME); String toJson() const; }; - +/** + * class for lazy creation of a boat item + * once we have someone that knows the name for it + * and providing fast access without searching all the time trough the map + * xdr mappings implement such a provider + */ +class GwBoatItemNameProvider +{ +public: + virtual String getBoatItemName() = 0; + virtual String getBoatItemFormat() = 0; + virtual ~GwBoatItemNameProvider() {} +}; +template class GwBoatItemHolder{ + private: + GwBoatItem *item=NULL; + GwBoatData *data; + unsigned long invalidTime=GwBoatItemBase::INVALID_TIME; + public: + GwBoatItemHolder(GwBoatData *data,unsigned long invalidTime=GwBoatItemBase::INVALID_TIME){ + this->data=data; + this->invalidTime=invalidTime; + } + GwBoatItem *getItem(GwBoatItemNameProvider *provider){ + if (item) return item; + T dummy; + item=data->getOrCreate(dummy, + provider->getBoatItemName() , + provider->getBoatItemFormat(), + invalidTime); + return item; + } + GwBoatItem *getItem(){return item;} +}; #endif \ No newline at end of file diff --git a/lib/xdrmappings/GwXDRMappings.h b/lib/xdrmappings/GwXDRMappings.h index e257953..8dffacc 100644 --- a/lib/xdrmappings/GwXDRMappings.h +++ b/lib/xdrmappings/GwXDRMappings.h @@ -2,6 +2,7 @@ #define _GWXDRMAPPINGS_H #include "GwLog.h" #include "GWConfig.h" +#include "GwBoatData.h" #include #include #include @@ -141,7 +142,7 @@ class GwXDRMapping{ typedef std::map N138Map; typedef std::map N2KMap; }; -class GwXDRFoundMapping{ +class GwXDRFoundMapping : public GwBoatItemNameProvider{ public: GwXDRMappingDef *definition=NULL; GwXDRType *type=NULL; @@ -163,6 +164,14 @@ class GwXDRFoundMapping{ return definition->getTransducerName(instanceId); } String buildXdrEntry(double value); + //boat Data info + virtual String getBoatItemName(){ + return getTransducerName(); + }; + virtual String getBoatItemFormat(){ + return "formatXdr"+type->xdrunit; //TODO: use the type def for the correct format + }; + virtual ~GwXDRFoundMapping(){} }; //the class GwXDRMappings is not intended to be deleted