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
* thread safe
* use the value returned from addCounter for the other operations
*/
virtual void setCounterDisplayName(const String &){}
virtual void increment(const String &name,bool failed=false){}
virtual void reset(){}
virtual int addCounter(const String &){return -1;}
virtual void increment(int idx,const String &name,bool failed=false){}
virtual void reset(int idx){}
virtual void remove(int idx){}
/**
* not thread safe methods
* 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 *valueList[]={longitude,latitude,testValue};
GwApi::Status status;
api->setCounterDisplayName("usertest");
int counter=api->addCounter("usertest");
while(true){
delay(1000);
/*
@ -197,7 +197,7 @@ void exampleTask(GwApi *api){
status.n2kRx,
status.n2kTx);
//increment some counter
api->increment("Test");
api->increment(counter,"Test");
}
vTaskDelete(NULL);

View File

@ -52,8 +52,9 @@ class TaskApi : public GwApiInternal
int sourceId;
SemaphoreHandle_t *mainLock;
SemaphoreHandle_t localLock;
GwCounter<String> *counter=NULL;
std::map<int,GwCounter<String>> counter;
bool counterUsed=false;
int counterIdx=0;
public:
TaskApi(GwApiInternal *api, int sourceId, SemaphoreHandle_t *mainLock, const String &name)
@ -62,7 +63,6 @@ public:
this->api = api;
this->mainLock=mainLock;
localLock=xSemaphoreCreateMutex();
counter=new GwCounter<String>("count"+name);
}
virtual GwRequestQueue *getQueue()
{
@ -112,33 +112,53 @@ public:
}
virtual ~TaskApi(){
vSemaphoreDelete(localLock);
delete counter;
};
virtual void fillStatus(GwJsonDocument &status){
GWSYNCHRONIZED(&localLock);
if (! counterUsed) return;
return counter->toJson(status);
for (auto it=counter.begin();it != counter.end();it++){
it->second.toJson(status);
}
};
virtual int getJsonSize(){
GWSYNCHRONIZED(&localLock);
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);
counterUsed=true;
if (failed) counter->addFail(name);
else (counter->add(name));
auto it=counter.find(idx);
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);
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);
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;
}
};