make changing timeouts working correctly

This commit is contained in:
andreas 2024-11-13 17:05:17 +01:00
parent 7ea500bad6
commit 2c87be78db
3 changed files with 52 additions and 30 deletions

View File

@ -48,12 +48,36 @@ GwBoatItemBase::GwBoatItemBase(String name, String format, GwBoatItemBase::TOTyp
this->type = 0; this->type = 0;
this->lastUpdateSource = -1; this->lastUpdateSource = -1;
} }
void GwBoatItemBase::setInvalidTime(unsigned long it, bool force){ void GwBoatItemBase::setInvalidTime(GwConfigHandler *cfg){
if (toType != TOType::user || force ){ if (toType != TOType::user){
invalidTime=it; 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); } 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 #define STRING_SIZE 40
GwBoatItemBase::StringWriter::StringWriter() GwBoatItemBase::StringWriter::StringWriter()
{ {
@ -127,7 +151,7 @@ GwBoatItem<T>::GwBoatItem(String name, String formatInfo, unsigned long invalidT
this->type = GwBoatItemTypes::getType(dummy); this->type = GwBoatItemTypes::getType(dummy);
if (map) if (map)
{ {
(*map)[name] = this; map->add(name,this);
} }
} }
template <class T> template <class T>
@ -137,7 +161,7 @@ GwBoatItem<T>::GwBoatItem(String name, String formatInfo, GwBoatItemBase::TOType
this->type = GwBoatItemTypes::getType(dummy); this->type = GwBoatItemTypes::getType(dummy);
if (map) 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) GwBoatData::GwBoatData(GwLog *logger, GwConfigHandler *cfg)
{ {
this->logger = logger; this->logger = logger;
this->config = cfg;
}
void GwBoatData::begin(){
for (auto &&it : values){ for (auto &&it : values){
unsigned long timeout=GwBoatItemBase::INVALID_TIME; it.second->setInvalidTime(config);
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);
} }
} }
GwBoatData::~GwBoatData() GwBoatData::~GwBoatData()
{ {
@ -456,6 +464,10 @@ double GwBoatData::getDoubleValue(String name, double defaultv)
return defaultv; return defaultv;
return it->second->getDoubleValue(); return it->second->getDoubleValue();
} }
void GwBoatData::setInvalidTime(GwBoatItemBase *item){
if (config != nullptr) item->setInvalidTime(config);
}
double formatCourse(double cv) double formatCourse(double cv)
{ {
double rt = cv * 180.0 / M_PI; double rt = cv * 180.0 / M_PI;

View File

@ -14,6 +14,8 @@
#define ROT_WA_FACTOR 60 #define ROT_WA_FACTOR 60
class GwJsonDocument; class GwJsonDocument;
class GwBoatData;
class GwBoatItemBase{ class GwBoatItemBase{
public: public:
using TOType=enum{ using TOType=enum{
@ -56,7 +58,6 @@ class GwBoatItemBase{
GWSC(formatRot); GWSC(formatRot);
GWSC(formatDate); GWSC(formatDate);
GWSC(formatTime); GWSC(formatTime);
typedef std::map<String,GwBoatItemBase*> GwBoatItemMap;
protected: protected:
int type; int type;
unsigned long lastSet=0; unsigned long lastSet=0;
@ -93,10 +94,15 @@ class GwBoatItemBase{
virtual double getDoubleValue()=0; virtual double getDoubleValue()=0;
String getName(){return name;} String getName(){return name;}
const String & getFormat() const{return format;} const String & getFormat() const{return format;}
virtual void setInvalidTime(unsigned long it, bool force=true); virtual void setInvalidTime(GwConfigHandler *cfg);
TOType getToType(){return toType;} TOType getToType(){return toType;}
class GwBoatItemMap : public std::map<String,GwBoatItemBase*>{
GwBoatData *boatData;
public:
GwBoatItemMap(GwBoatData *bd):boatData(bd){}
void add(const String &name,GwBoatItemBase *item);
};
}; };
class GwBoatData;
template<class T> class GwBoatItem : public GwBoatItemBase{ template<class T> class GwBoatItem : public GwBoatItemBase{
protected: protected:
T data; T data;
@ -186,8 +192,9 @@ public:
clazz *name=new clazz(#name,GwBoatItemBase::fmt,toType,&values) ; clazz *name=new clazz(#name,GwBoatItemBase::fmt,toType,&values) ;
class GwBoatData{ class GwBoatData{
private: private:
GwLog *logger; GwLog *logger=nullptr;
GwBoatItemBase::GwBoatItemMap values; GwConfigHandler *config=nullptr;
GwBoatItemBase::GwBoatItemMap values{this};
public: public:
GWBOATDATA(double,COG,formatCourse) // course over ground GWBOATDATA(double,COG,formatCourse) // course over ground
@ -231,9 +238,11 @@ class GwBoatData{
public: public:
GwBoatData(GwLog *logger, GwConfigHandler *cfg); GwBoatData(GwLog *logger, GwConfigHandler *cfg);
~GwBoatData(); ~GwBoatData();
void begin();
template<class T> GwBoatItem<T> *getOrCreate(T initial,GwBoatItemNameProvider *provider); template<class T> GwBoatItem<T> *getOrCreate(T initial,GwBoatItemNameProvider *provider);
template<class T> bool update(T value,int source,GwBoatItemNameProvider *provider); template<class T> bool update(T value,int source,GwBoatItemNameProvider *provider);
template<class T> T getDataWithDefault(T defaultv, GwBoatItemNameProvider *provider); template<class T> T getDataWithDefault(T defaultv, GwBoatItemNameProvider *provider);
void setInvalidTime(GwBoatItemBase *item);
bool isValid(String name); bool isValid(String name);
double getDoubleValue(String name,double defaultv); double getDoubleValue(String name,double defaultv);
GwBoatItemBase *getBase(String name); GwBoatItemBase *getBase(String name);

View File

@ -784,6 +784,7 @@ void setup() {
logger.prefix="FALLBACK:"; logger.prefix="FALLBACK:";
logger.setWriter(new DefaultLogWriter()); logger.setWriter(new DefaultLogWriter());
#endif #endif
boatData.begin();
userCodeHandler.startInitTasks(MIN_USER_TASK); userCodeHandler.startInitTasks(MIN_USER_TASK);
channels.preinit(); channels.preinit();
config.stopChanges(); config.stopChanges();