From 015b4762f81ab0666177793550600ebf5b1aa708 Mon Sep 17 00:00:00 2001 From: andreas Date: Sat, 14 Oct 2023 19:43:50 +0200 Subject: [PATCH] allow for multiple counters in user task --- lib/api/GwApi.h | 8 +++--- lib/exampletask/GwExampleTask.cpp | 4 +-- lib/usercode/GwUserCode.cpp | 44 ++++++++++++++++++++++--------- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/lib/api/GwApi.h b/lib/api/GwApi.h index 8021ac0..c6d679e 100644 --- a/lib/api/GwApi.h +++ b/lib/api/GwApi.h @@ -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 diff --git a/lib/exampletask/GwExampleTask.cpp b/lib/exampletask/GwExampleTask.cpp index 883be56..ea2598e 100644 --- a/lib/exampletask/GwExampleTask.cpp +++ b/lib/exampletask/GwExampleTask.cpp @@ -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); diff --git a/lib/usercode/GwUserCode.cpp b/lib/usercode/GwUserCode.cpp index 9b4bc04..7c47a55 100644 --- a/lib/usercode/GwUserCode.cpp +++ b/lib/usercode/GwUserCode.cpp @@ -52,8 +52,9 @@ class TaskApi : public GwApiInternal int sourceId; SemaphoreHandle_t *mainLock; SemaphoreHandle_t localLock; - GwCounter *counter=NULL; + std::map> 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("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("count"+name))); + } + else it->second=GwCounter("count"+name); + return counterIdx; } };