diff --git a/lib/boatData/GwBoatData.cpp b/lib/boatData/GwBoatData.cpp index 4be1450..39d22c8 100644 --- a/lib/boatData/GwBoatData.cpp +++ b/lib/boatData/GwBoatData.cpp @@ -48,12 +48,36 @@ GwBoatItemBase::GwBoatItemBase(String name, String format, GwBoatItemBase::TOTyp this->type = 0; this->lastUpdateSource = -1; } -void GwBoatItemBase::setInvalidTime(unsigned long it, bool force){ - if (toType != TOType::user || force ){ - invalidTime=it; +void GwBoatItemBase::setInvalidTime(GwConfigHandler *cfg){ + if (toType != TOType::user){ + unsigned long timeout=GwBoatItemBase::INVALID_TIME; + switch(getToType()){ + case GwBoatItemBase::TOType::ais: + timeout=cfg->getInt(GwConfigDefinitions::timoAis); + break; + case GwBoatItemBase::TOType::def: + timeout=cfg->getInt(GwConfigDefinitions::timoDefault); + break; + case GwBoatItemBase::TOType::lng: + timeout=cfg->getInt(GwConfigDefinitions::timoLong); + break; + case GwBoatItemBase::TOType::sensor: + timeout=cfg->getInt(GwConfigDefinitions::timoSensor); + break; + case GwBoatItemBase::TOType::keep: + timeout=0; + break; + } + invalidTime=timeout; } } size_t GwBoatItemBase::getJsonSize() { return JSON_OBJECT_SIZE(10); } + +void GwBoatItemBase::GwBoatItemMap::add(const String &name,GwBoatItemBase *item){ + boatData->setInvalidTime(item); + (*this)[name]=item; +} + #define STRING_SIZE 40 GwBoatItemBase::StringWriter::StringWriter() { @@ -127,7 +151,7 @@ GwBoatItem::GwBoatItem(String name, String formatInfo, unsigned long invalidT this->type = GwBoatItemTypes::getType(dummy); if (map) { - (*map)[name] = this; + map->add(name,this); } } template @@ -137,7 +161,7 @@ GwBoatItem::GwBoatItem(String name, String formatInfo, GwBoatItemBase::TOType this->type = GwBoatItemTypes::getType(dummy); if (map) { - (*map)[name] = this; + map->add(name,this); } } @@ -322,28 +346,12 @@ void GwBoatDataSatList::toJsonDoc(GwJsonDocument *doc, unsigned long minTime) GwBoatData::GwBoatData(GwLog *logger, GwConfigHandler *cfg) { this->logger = logger; + this->config = cfg; +} +void GwBoatData::begin(){ for (auto &&it : values){ - unsigned long timeout=GwBoatItemBase::INVALID_TIME; - switch(it.second->getToType()){ - case GwBoatItemBase::TOType::ais: - timeout=cfg->getInt(GwConfigDefinitions::timoAis); - break; - case GwBoatItemBase::TOType::def: - timeout=cfg->getInt(GwConfigDefinitions::timoDefault); - break; - case GwBoatItemBase::TOType::lng: - timeout=cfg->getInt(GwConfigDefinitions::timoLong); - break; - case GwBoatItemBase::TOType::sensor: - timeout=cfg->getInt(GwConfigDefinitions::timoSensor); - break; - case GwBoatItemBase::TOType::keep: - timeout=0; - break; - } - it.second->setInvalidTime(timeout); + it.second->setInvalidTime(config); } - } GwBoatData::~GwBoatData() { @@ -456,6 +464,10 @@ double GwBoatData::getDoubleValue(String name, double defaultv) return defaultv; return it->second->getDoubleValue(); } + +void GwBoatData::setInvalidTime(GwBoatItemBase *item){ + if (config != nullptr) item->setInvalidTime(config); +} double formatCourse(double cv) { double rt = cv * 180.0 / M_PI; diff --git a/lib/boatData/GwBoatData.h b/lib/boatData/GwBoatData.h index 18a5f08..a4df3b4 100644 --- a/lib/boatData/GwBoatData.h +++ b/lib/boatData/GwBoatData.h @@ -14,6 +14,8 @@ #define ROT_WA_FACTOR 60 class GwJsonDocument; +class GwBoatData; + class GwBoatItemBase{ public: using TOType=enum{ @@ -56,7 +58,6 @@ class GwBoatItemBase{ GWSC(formatRot); GWSC(formatDate); GWSC(formatTime); - typedef std::map GwBoatItemMap; protected: int type; unsigned long lastSet=0; @@ -93,10 +94,15 @@ class GwBoatItemBase{ virtual double getDoubleValue()=0; String getName(){return name;} const String & getFormat() const{return format;} - virtual void setInvalidTime(unsigned long it, bool force=true); + virtual void setInvalidTime(GwConfigHandler *cfg); TOType getToType(){return toType;} + class GwBoatItemMap : public std::map{ + GwBoatData *boatData; + public: + GwBoatItemMap(GwBoatData *bd):boatData(bd){} + void add(const String &name,GwBoatItemBase *item); + }; }; -class GwBoatData; template class GwBoatItem : public GwBoatItemBase{ protected: T data; @@ -186,8 +192,9 @@ public: clazz *name=new clazz(#name,GwBoatItemBase::fmt,toType,&values) ; class GwBoatData{ private: - GwLog *logger; - GwBoatItemBase::GwBoatItemMap values; + GwLog *logger=nullptr; + GwConfigHandler *config=nullptr; + GwBoatItemBase::GwBoatItemMap values{this}; public: GWBOATDATA(double,COG,formatCourse) // course over ground @@ -231,9 +238,11 @@ class GwBoatData{ public: GwBoatData(GwLog *logger, GwConfigHandler *cfg); ~GwBoatData(); + void begin(); template GwBoatItem *getOrCreate(T initial,GwBoatItemNameProvider *provider); template bool update(T value,int source,GwBoatItemNameProvider *provider); template T getDataWithDefault(T defaultv, GwBoatItemNameProvider *provider); + void setInvalidTime(GwBoatItemBase *item); bool isValid(String name); double getDoubleValue(String name,double defaultv); GwBoatItemBase *getBase(String name); diff --git a/src/main.cpp b/src/main.cpp index 714c0a5..90e5691 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -784,6 +784,7 @@ void setup() { logger.prefix="FALLBACK:"; logger.setWriter(new DefaultLogWriter()); #endif + boatData.begin(); userCodeHandler.startInitTasks(MIN_USER_TASK); channels.preinit(); config.stopChanges();