intermediate: prepare for dynamic boat data

This commit is contained in:
wellenvogel 2021-11-23 20:26:59 +01:00
parent a9e1357c7f
commit 3ef74581de
3 changed files with 57 additions and 2 deletions

View File

@ -10,6 +10,16 @@ GwBoatData::~GwBoatData(){
}
}
template<class T> GwBoatItem<T> *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<T>(name,format,invalidTime,&values);
}
String GwBoatData::toJson() const {
unsigned long minTime=millis();
GwBoatItemBase::GwBoatItemMap::const_iterator it;

View File

@ -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 T> class GwBoatItem : public GwBoatItemBase{
@ -253,8 +254,43 @@ class GwBoatData{
public:
GwBoatData(GwLog *logger);
~GwBoatData();
template<class T> GwBoatItem<T> *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 T> class GwBoatItemHolder{
private:
GwBoatItem<T> *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<T> *getItem(GwBoatItemNameProvider *provider){
if (item) return item;
T dummy;
item=data->getOrCreate(dummy,
provider->getBoatItemName() ,
provider->getBoatItemFormat(),
invalidTime);
return item;
}
GwBoatItem<T> *getItem(){return item;}
};
#endif

View File

@ -2,6 +2,7 @@
#define _GWXDRMAPPINGS_H
#include "GwLog.h"
#include "GWConfig.h"
#include "GwBoatData.h"
#include <WString.h>
#include <vector>
#include <map>
@ -141,7 +142,7 @@ class GwXDRMapping{
typedef std::map<String,MappingList> N138Map;
typedef std::map<unsigned long,MappingList> 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