allow for dynamic user task registration
This commit is contained in:
parent
682bbb5bb4
commit
9e05a86820
|
@ -7,6 +7,8 @@
|
||||||
#include "GwBoatData.h"
|
#include "GwBoatData.h"
|
||||||
#include "GwXDRMappings.h"
|
#include "GwXDRMappings.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
|
class GwApi;
|
||||||
|
typedef void (*GwUserTaskFunction)(GwApi *);
|
||||||
//API to be used for additional tasks
|
//API to be used for additional tasks
|
||||||
class GwApi{
|
class GwApi{
|
||||||
public:
|
public:
|
||||||
|
@ -158,6 +160,13 @@ class GwApi{
|
||||||
virtual bool addXdrMapping(const GwXDRMappingDef &)=0;
|
virtual bool addXdrMapping(const GwXDRMappingDef &)=0;
|
||||||
|
|
||||||
virtual void addCapability(const String &name, const String &value)=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
|
* not thread safe methods
|
||||||
|
|
|
@ -36,12 +36,13 @@ class SHT3XConfig{
|
||||||
tempSource=(tN2kTempSource)(config->getInt(GwConfigDefinitions::SHT3XTempSource));
|
tempSource=(tN2kTempSource)(config->getInt(GwConfigDefinitions::SHT3XTempSource));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
void runIicTask(GwApi *api);
|
||||||
void initIicTask(GwApi *api){
|
void initIicTask(GwApi *api){
|
||||||
GwLog *logger=api->getLogger();
|
GwLog *logger=api->getLogger();
|
||||||
#ifndef _GWIIC
|
#ifndef _GWIIC
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
|
bool addTask=false;
|
||||||
#ifdef GWSHT3X
|
#ifdef GWSHT3X
|
||||||
api->addCapability("SHT3X","true");
|
api->addCapability("SHT3X","true");
|
||||||
LOG_DEBUG(GwLog::LOG,"GWSHT3X configured, adding capability and xdr mappings");
|
LOG_DEBUG(GwLog::LOG,"GWSHT3X configured, adding capability and xdr mappings");
|
||||||
|
@ -70,7 +71,11 @@ void initIicTask(GwApi *api){
|
||||||
xdr.xdrName=sht3xConfig.tempTransducer;
|
xdr.xdrName=sht3xConfig.tempTransducer;
|
||||||
api->addXdrMapping(xdr);
|
api->addXdrMapping(xdr);
|
||||||
}
|
}
|
||||||
|
if (sht3xConfig.tempActive || sht3xConfig.humidActive) addTask=true;
|
||||||
#endif
|
#endif
|
||||||
|
if (addTask){
|
||||||
|
api->addUserTask(runIicTask,"iicTask",3000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void runIicTask(GwApi *api){
|
void runIicTask(GwApi *api){
|
||||||
GwLog *logger=api->getLogger();
|
GwLog *logger=api->getLogger();
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
#ifndef _GWIICTASK_H
|
#ifndef _GWIICTASK_H
|
||||||
#define _GWIICTASK_H
|
#define _GWIICTASK_H
|
||||||
#include "GwApi.h"
|
#include "GwApi.h"
|
||||||
void runIicTask(GwApi *api);
|
|
||||||
void initIicTask(GwApi *api);
|
void initIicTask(GwApi *api);
|
||||||
DECLARE_USERTASK_PARAM(runIicTask,3000);
|
|
||||||
DECLARE_INITFUNCTION(initIicTask);
|
DECLARE_INITFUNCTION(initIicTask);
|
||||||
#endif
|
#endif
|
|
@ -3,6 +3,9 @@
|
||||||
#define DECLARE_INITFUNCTION(task) GwInitTask __Init##task##__(task,#task);
|
#define DECLARE_INITFUNCTION(task) GwInitTask __Init##task##__(task,#task);
|
||||||
#define DECLARE_CAPABILITY(name,value) GwUserCapability __CAP##name##__(#name,#value);
|
#define DECLARE_CAPABILITY(name,value) GwUserCapability __CAP##name##__(#name,#value);
|
||||||
#define DECLARE_STRING_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 "GwUserCode.h"
|
||||||
#include "GwSynchronized.h"
|
#include "GwSynchronized.h"
|
||||||
|
@ -17,6 +20,14 @@
|
||||||
std::vector<GwUserTask> userTasks;
|
std::vector<GwUserTask> userTasks;
|
||||||
std::vector<GwUserTask> initTasks;
|
std::vector<GwUserTask> initTasks;
|
||||||
GwUserCode::Capabilities userCapabilities;
|
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{
|
class RegEntry{
|
||||||
public:
|
public:
|
||||||
String file;
|
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{
|
class GwUserTaskDef{
|
||||||
|
@ -290,6 +298,19 @@ public:
|
||||||
if (! isInit) return;
|
if (! isInit) return;
|
||||||
userCapabilities[name]=value;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
#include "GwApi.h"
|
#include "GwApi.h"
|
||||||
#include "GwJsonDocument.h"
|
#include "GwJsonDocument.h"
|
||||||
class GwLog;
|
class GwLog;
|
||||||
typedef void (*GwUserTaskFunction)(GwApi *);
|
|
||||||
|
|
||||||
class GwApiInternal : public GwApi{
|
class GwApiInternal : public GwApi{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -328,6 +328,9 @@ public:
|
||||||
return xdrMappings.addFixedMapping(mapping);
|
return xdrMappings.addFixedMapping(mapping);
|
||||||
}
|
}
|
||||||
virtual void addCapability(const String &name, const String &value){}
|
virtual void addCapability(const String &name, const String &value){}
|
||||||
|
virtual bool addUserTask(GwUserTaskFunction task,const String Name, int stackSize=2000){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bool delayedRestart(){
|
bool delayedRestart(){
|
||||||
|
|
Loading…
Reference in New Issue