allow for dynamic user task registration

This commit is contained in:
andreas 2023-10-27 19:55:22 +02:00
parent 682bbb5bb4
commit 9e05a86820
6 changed files with 42 additions and 7 deletions

View File

@ -7,6 +7,8 @@
#include "GwBoatData.h"
#include "GwXDRMappings.h"
#include <map>
class GwApi;
typedef void (*GwUserTaskFunction)(GwApi *);
//API to be used for additional tasks
class GwApi{
public:
@ -158,6 +160,13 @@ class GwApi{
virtual bool addXdrMapping(const GwXDRMappingDef &)=0;
virtual void addCapability(const String &name, const String &value)=0;
/**
* add a user task
* this allows you decide based on defines/config if a user task really should be added
* so this is the preferred solution over DECLARE_USERTASK
* The name should be similar to the function name of the user task (although not mandatory)
*/
virtual bool addUserTask(GwUserTaskFunction task,const String Name, int stackSize=2000)=0;
/**
* not thread safe methods

View File

@ -36,12 +36,13 @@ class SHT3XConfig{
tempSource=(tN2kTempSource)(config->getInt(GwConfigDefinitions::SHT3XTempSource));
}
};
void runIicTask(GwApi *api);
void initIicTask(GwApi *api){
GwLog *logger=api->getLogger();
#ifndef _GWIIC
return;
#endif
bool addTask=false;
#ifdef GWSHT3X
api->addCapability("SHT3X","true");
LOG_DEBUG(GwLog::LOG,"GWSHT3X configured, adding capability and xdr mappings");
@ -70,7 +71,11 @@ void initIicTask(GwApi *api){
xdr.xdrName=sht3xConfig.tempTransducer;
api->addXdrMapping(xdr);
}
if (sht3xConfig.tempActive || sht3xConfig.humidActive) addTask=true;
#endif
if (addTask){
api->addUserTask(runIicTask,"iicTask",3000);
}
}
void runIicTask(GwApi *api){
GwLog *logger=api->getLogger();

View File

@ -1,8 +1,6 @@
#ifndef _GWIICTASK_H
#define _GWIICTASK_H
#include "GwApi.h"
void runIicTask(GwApi *api);
void initIicTask(GwApi *api);
DECLARE_USERTASK_PARAM(runIicTask,3000);
DECLARE_INITFUNCTION(initIicTask);
#endif

View File

@ -3,6 +3,9 @@
#define DECLARE_INITFUNCTION(task) GwInitTask __Init##task##__(task,#task);
#define DECLARE_CAPABILITY(name,value) GwUserCapability __CAP##name##__(#name,#value);
#define DECLARE_STRING_CAPABILITY(name,value) GwUserCapability __CAP##name##__(#name,value);
#define DECLARE_TASKIF(task,type) \
DECLARE_TASKIF_IMPL(task,type) \
GwIreg __register##type(#task,__FILE__,#type)
#include "GwUserCode.h"
#include "GwSynchronized.h"
@ -17,6 +20,14 @@
std::vector<GwUserTask> userTasks;
std::vector<GwUserTask> initTasks;
GwUserCode::Capabilities userCapabilities;
template <typename V>
bool taskExists(V &list, const String &name){
for (auto it=list.begin();it!=list.end();it++){
if (it->name == name) return true;
}
return false;
}
class RegEntry{
public:
String file;
@ -54,9 +65,6 @@ class GwIreg{
}
};
#define DECLARE_TASKIF(task,type) \
DECLARE_TASKIF_IMPL(task,type) \
GwIreg __register##type(#task,__FILE__,#type)
class GwUserTaskDef{
@ -290,6 +298,19 @@ public:
if (! isInit) return;
userCapabilities[name]=value;
}
virtual bool addUserTask(GwUserTaskFunction task,const String tname, int stackSize=2000){
if (! isInit){
api->getLogger()->logDebug(GwLog::ERROR,"trying to add a user task %s outside init",tname.c_str());
return false;
}
if (taskExists(userTasks,name)){
api->getLogger()->logDebug(GwLog::ERROR,"trying to add a user task %s that already exists",tname.c_str());
return false;
}
userTasks.push_back(GwUserTask(tname,task,stackSize));
api->getLogger()->logDebug(GwLog::LOG,"adding user task %s",tname.c_str());
return true;
}
};

View File

@ -5,7 +5,6 @@
#include "GwApi.h"
#include "GwJsonDocument.h"
class GwLog;
typedef void (*GwUserTaskFunction)(GwApi *);
class GwApiInternal : public GwApi{
public:

View File

@ -328,6 +328,9 @@ public:
return xdrMappings.addFixedMapping(mapping);
}
virtual void addCapability(const String &name, const String &value){}
virtual bool addUserTask(GwUserTaskFunction task,const String Name, int stackSize=2000){
return false;
}
};
bool delayedRestart(){