mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-13 05:53:06 +01:00
introduce channel config abstraction
This commit is contained in:
102
lib/channel/GwChannelConfig.cpp
Normal file
102
lib/channel/GwChannelConfig.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "GwChannelConfig.h"
|
||||
|
||||
GwChannelConfig::GwChannelConfig(GwLog *logger,String name){
|
||||
this->logger = logger;
|
||||
this->name=name;
|
||||
this->countIn=new GwCounter<String>(String("count")+name+String("in"));
|
||||
this->countOut=new GwCounter<String>(String("count")+name+String("out"));
|
||||
}
|
||||
void GwChannelConfig::begin(
|
||||
bool enabled,
|
||||
bool nmeaOut,
|
||||
bool nmeaIn,
|
||||
String readFilter,
|
||||
String writeFilter,
|
||||
bool seaSmartOut,
|
||||
bool toN2k)
|
||||
{
|
||||
this->enabled = enabled;
|
||||
this->NMEAout = nmeaOut;
|
||||
this->NMEAin = nmeaIn;
|
||||
this->readFilter=readFilter.isEmpty()?
|
||||
NULL:
|
||||
new GwNmeaFilter(readFilter);
|
||||
this->writeFilter=writeFilter.isEmpty()?
|
||||
NULL:
|
||||
new GwNmeaFilter(writeFilter);
|
||||
}
|
||||
void GwChannelConfig::updateCounter(const char *msg, bool out)
|
||||
{
|
||||
char key[6];
|
||||
if (msg[0] == '$')
|
||||
{
|
||||
strncpy(key, &msg[3], 3);
|
||||
key[3] = 0;
|
||||
}
|
||||
else if (msg[0] == '!')
|
||||
{
|
||||
strncpy(key, &msg[1], 5);
|
||||
key[5] = 0;
|
||||
}
|
||||
else{
|
||||
return;
|
||||
}
|
||||
if (out){
|
||||
countOut->add(key);
|
||||
}
|
||||
else{
|
||||
countIn->add(key);
|
||||
}
|
||||
}
|
||||
bool GwChannelConfig::canSendOut(unsigned long pgn){
|
||||
if (! enabled) return false;
|
||||
if (! NMEAout) return false;
|
||||
countOut->add(String(pgn));
|
||||
return true;
|
||||
}
|
||||
bool GwChannelConfig::canReceive(unsigned long pgn){
|
||||
if (!enabled) return false;
|
||||
if (!NMEAin) return false;
|
||||
countIn->add(String(pgn));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GwChannelConfig::canSendOut(const char *buffer){
|
||||
if (! enabled) return false;
|
||||
if (! NMEAout) return false;
|
||||
if (writeFilter && ! writeFilter->canPass(buffer)) return false;
|
||||
updateCounter(buffer,true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GwChannelConfig::canReceive(const char *buffer){
|
||||
if (! enabled) return false;
|
||||
if (! NMEAin) return false;
|
||||
if (readFilter && ! readFilter->canPass(buffer)) return false;
|
||||
updateCounter(buffer,false);
|
||||
return true;
|
||||
}
|
||||
|
||||
int GwChannelConfig::getJsonSize(){
|
||||
if (! enabled) return 0;
|
||||
int rt=2;
|
||||
if (countIn) rt+=countIn->getJsonSize();
|
||||
if (countOut) rt+=countOut->getJsonSize();
|
||||
return rt;
|
||||
}
|
||||
void GwChannelConfig::toJson(GwJsonDocument &doc){
|
||||
if (! enabled) return;
|
||||
if (countOut) countOut->toJson(doc);
|
||||
if (countIn) countIn->toJson(doc);
|
||||
}
|
||||
String GwChannelConfig::toString(){
|
||||
String rt="CH:"+name;
|
||||
rt+=enabled?"[ena]":"[dis]";
|
||||
rt+=NMEAin?"in,":"";
|
||||
rt+=NMEAout?"out,":"";
|
||||
if (readFilter) rt+="RF:"+ readFilter->toString();
|
||||
if (writeFilter) rt+="WF:"+ writeFilter->toString();
|
||||
rt+=","+ toN2k?"n2k":"";
|
||||
rt+=","+ seaSmartOut?"SM":"";
|
||||
return rt;
|
||||
}
|
||||
50
lib/channel/GwChannelConfig.h
Normal file
50
lib/channel/GwChannelConfig.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
#include "GwConfigItem.h"
|
||||
#include "GwLog.h"
|
||||
#include "GWConfig.h"
|
||||
#include "GwCounter.h"
|
||||
#include "GwJsonDocument.h"
|
||||
|
||||
class GwChannelConfig{
|
||||
bool enabled=false;
|
||||
bool NMEAout=false;
|
||||
bool NMEAin=false;
|
||||
GwNmeaFilter* readFilter=NULL;
|
||||
GwNmeaFilter* writeFilter=NULL;
|
||||
bool seaSmartOut=false;
|
||||
bool toN2k=false;
|
||||
GwLog *logger;
|
||||
String name;
|
||||
GwCounter<String> *countIn=NULL;
|
||||
GwCounter<String> *countOut=NULL;
|
||||
void updateCounter(const char *msg, bool out);
|
||||
public:
|
||||
GwChannelConfig(
|
||||
GwLog *logger,
|
||||
String name);
|
||||
void begin(
|
||||
bool enabled,
|
||||
bool nmeaOut,
|
||||
bool nmeaIn,
|
||||
String readFilter,
|
||||
String writeFilter,
|
||||
bool seaSmartOut,
|
||||
bool toN2k
|
||||
);
|
||||
|
||||
void enable(bool enabled){
|
||||
this->enabled=enabled;
|
||||
}
|
||||
bool isEnabled(){return enabled;}
|
||||
bool shouldRead(){return enabled && NMEAin;}
|
||||
bool canSendOut(unsigned long pgn);
|
||||
bool canReceive(unsigned long pgn);
|
||||
bool canSendOut(const char *buffer);
|
||||
bool canReceive(const char *buffer);
|
||||
bool sendSeaSmart(){ return seaSmartOut;}
|
||||
bool sendToN2K(){return toN2k;}
|
||||
int getJsonSize();
|
||||
void toJson(GwJsonDocument &doc);
|
||||
String toString();
|
||||
};
|
||||
|
||||
@@ -140,18 +140,21 @@ void GwNmeaFilter::parseFilter(){
|
||||
// "0:1:RMB,RMC"
|
||||
// 0: AIS off, 1:whitelist, list of sentences
|
||||
if (isReady) return;
|
||||
if (config.isEmpty()){
|
||||
isReady=true;
|
||||
return;
|
||||
}
|
||||
int found=0;
|
||||
int last=0;
|
||||
int index=0;
|
||||
String data=config->asString();
|
||||
while ((found = data.indexOf(':',last)) >= 0){
|
||||
String tok=data.substring(last,found);
|
||||
while ((found = config.indexOf(':',last)) >= 0){
|
||||
String tok=config.substring(last,found);
|
||||
handleToken(tok,index);
|
||||
last=found+1;
|
||||
index++;
|
||||
}
|
||||
if (last < data.length()){
|
||||
String tok=data.substring(last);
|
||||
if (last < config.length()){
|
||||
String tok=config.substring(last);
|
||||
handleToken(tok,index);
|
||||
}
|
||||
isReady=true;
|
||||
|
||||
@@ -46,7 +46,7 @@ class GwConfigInterface{
|
||||
|
||||
class GwNmeaFilter{
|
||||
private:
|
||||
GwConfigInterface *config=NULL;
|
||||
String config;
|
||||
bool isReady=false;
|
||||
bool ais=true;
|
||||
bool blacklist=true;
|
||||
@@ -54,7 +54,7 @@ class GwNmeaFilter{
|
||||
void handleToken(String token, int index);
|
||||
void parseFilter();
|
||||
public:
|
||||
GwNmeaFilter(GwConfigInterface *config){
|
||||
GwNmeaFilter(String config){
|
||||
this->config=config;
|
||||
isReady=false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user