allow for multiple counters in user task

This commit is contained in:
andreas 2023-10-14 19:43:50 +02:00
parent 6cdaab4d60
commit 015b4762f8
3 changed files with 39 additions and 17 deletions

View File

@ -127,10 +127,12 @@ class GwApi{
/** /**
* access to counters for a task * access to counters for a task
* thread safe * thread safe
* use the value returned from addCounter for the other operations
*/ */
virtual void setCounterDisplayName(const String &){} virtual int addCounter(const String &){return -1;}
virtual void increment(const String &name,bool failed=false){} virtual void increment(int idx,const String &name,bool failed=false){}
virtual void reset(){} virtual void reset(int idx){}
virtual void remove(int idx){}
/** /**
* not thread safe methods * not thread safe methods
* accessing boat data must only be executed from within the main thread * accessing boat data must only be executed from within the main thread

View File

@ -98,7 +98,7 @@ void exampleTask(GwApi *api){
GwApi::BoatValue *testValue=new GwApi::BoatValue(boatItemName); GwApi::BoatValue *testValue=new GwApi::BoatValue(boatItemName);
GwApi::BoatValue *valueList[]={longitude,latitude,testValue}; GwApi::BoatValue *valueList[]={longitude,latitude,testValue};
GwApi::Status status; GwApi::Status status;
api->setCounterDisplayName("usertest"); int counter=api->addCounter("usertest");
while(true){ while(true){
delay(1000); delay(1000);
/* /*
@ -197,7 +197,7 @@ void exampleTask(GwApi *api){
status.n2kRx, status.n2kRx,
status.n2kTx); status.n2kTx);
//increment some counter //increment some counter
api->increment("Test"); api->increment(counter,"Test");
} }
vTaskDelete(NULL); vTaskDelete(NULL);

View File

@ -52,8 +52,9 @@ class TaskApi : public GwApiInternal
int sourceId; int sourceId;
SemaphoreHandle_t *mainLock; SemaphoreHandle_t *mainLock;
SemaphoreHandle_t localLock; SemaphoreHandle_t localLock;
GwCounter<String> *counter=NULL; std::map<int,GwCounter<String>> counter;
bool counterUsed=false; bool counterUsed=false;
int counterIdx=0;
public: public:
TaskApi(GwApiInternal *api, int sourceId, SemaphoreHandle_t *mainLock, const String &name) TaskApi(GwApiInternal *api, int sourceId, SemaphoreHandle_t *mainLock, const String &name)
@ -62,7 +63,6 @@ public:
this->api = api; this->api = api;
this->mainLock=mainLock; this->mainLock=mainLock;
localLock=xSemaphoreCreateMutex(); localLock=xSemaphoreCreateMutex();
counter=new GwCounter<String>("count"+name);
} }
virtual GwRequestQueue *getQueue() virtual GwRequestQueue *getQueue()
{ {
@ -112,33 +112,53 @@ public:
} }
virtual ~TaskApi(){ virtual ~TaskApi(){
vSemaphoreDelete(localLock); vSemaphoreDelete(localLock);
delete counter;
}; };
virtual void fillStatus(GwJsonDocument &status){ virtual void fillStatus(GwJsonDocument &status){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(&localLock);
if (! counterUsed) return; if (! counterUsed) return;
return counter->toJson(status); for (auto it=counter.begin();it != counter.end();it++){
it->second.toJson(status);
}
}; };
virtual int getJsonSize(){ virtual int getJsonSize(){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(&localLock);
if (! counterUsed) return 0; if (! counterUsed) return 0;
return counter->getJsonSize(); int rt=0;
for (auto it=counter.begin();it != counter.end();it++){
rt+=it->second.getJsonSize();
}
return rt;
}; };
virtual void increment(const String &name,bool failed=false){ virtual void increment(int idx,const String &name,bool failed=false){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(&localLock);
counterUsed=true; counterUsed=true;
if (failed) counter->addFail(name); auto it=counter.find(idx);
else (counter->add(name)); if (it == counter.end()) return;
if (failed) it->second.addFail(name);
else (it->second.add(name));
}; };
virtual void reset(){ virtual void reset(int idx){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(&localLock);
counterUsed=true; counterUsed=true;
counter->reset(); auto it=counter.find(idx);
if (it == counter.end()) return;
it->second.reset();
}; };
virtual void setCounterDisplayName(const String &name){ virtual void remove(int idx){
GWSYNCHRONIZED(&localLock);
counter.erase(idx);
}
virtual int addCounter(const String &name){
GWSYNCHRONIZED(&localLock); GWSYNCHRONIZED(&localLock);
counterUsed=true; counterUsed=true;
counter->setName("count"+name); counterIdx++;
//avoid the need for an empty counter constructor
auto it=counter.find(counterIdx);
if (it == counter.end()){
counter.insert(std::make_pair(counterIdx,GwCounter<String>("count"+name)));
}
else it->second=GwCounter<String>("count"+name);
return counterIdx;
} }
}; };