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:
61
lib/counter/GwCounter.h
Normal file
61
lib/counter/GwCounter.h
Normal 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
|
||||
Reference in New Issue
Block a user