1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

add NMEA and NSk counter

This commit is contained in:
andreas
2021-11-07 13:12:38 +01:00
parent 2425006d7c
commit 3c4920d104
3 changed files with 252 additions and 60 deletions

61
lib/counter/GwCounter.h Normal file
View File

@@ -0,0 +1,61 @@
#ifndef _GWCOUNTER_H
#define _GWCOUNTER_H
#include <map>
#include "ArduinoJson.h"
template<class T> class GwCounter{
private:
typedef std::map<T,unsigned long> CounterMap;
CounterMap okCounter;
CounterMap failCounter;
unsigned long globalOk=0;
unsigned long globalFail=0;
String name;
public:
GwCounter(String name){
this->name=name;
};
void reset(){
okCounter.clear();
failCounter.clear();
globalFail=0;
globalOk=0;
}
void add(T key){
globalOk++;
auto it=okCounter.find(key);
if (it == okCounter.end()){
okCounter[key]=1;
}
else{
it->second++;
}
}
void addFail(T key){
globalFail++;
auto it=failCounter.find(key);
if (it == failCounter.end()){
failCounter[key]=1;
}
else{
it->second++;
}
}
int getJsonSize(){
return JSON_OBJECT_SIZE(4)+JSON_OBJECT_SIZE(okCounter.size()+1)+
JSON_OBJECT_SIZE(failCounter.size()+1);
}
void toJson(JsonDocument &json){
JsonObject jo=json.createNestedObject(name);
jo["sumOk"]=globalOk;
jo["sumFail"]=globalFail;
JsonObject jok=jo.createNestedObject("ok");
for (auto it=okCounter.begin();it!=okCounter.end();it++){
jok[String(it->first)]=it->second;
}
JsonObject jfail=jo.createNestedObject("fail");
for (auto it=failCounter.begin();it!=failCounter.end();it++){
jfail[String(it->first)]=it->second;
}
}
};
#endif