xdrExample and xdrUnmapped requests

This commit is contained in:
wellenvogel 2021-11-22 12:16:19 +01:00
parent 769cf33e67
commit 8610d94382
3 changed files with 91 additions and 2 deletions

View File

@ -201,6 +201,23 @@ String GwXDRMappingDef::getTransducerName(int instance)
return name; return name;
} }
String GwXDRFoundMapping::buildXdrEntry(double value)
{
char buffer[40];
String name = getTransducerName();
if (type->tonmea)
{
value = (*(type->tonmea))(value);
}
snprintf(buffer, 39, "%s,%.3f,%s,%s",
type->xdrtype.c_str(),
value,
type->xdrunit.c_str(),
name.c_str());
buffer[39] = 0;
return String(buffer);
}
GwXDRMappings::GwXDRMappings(GwLog *logger, GwConfigHandler *config) GwXDRMappings::GwXDRMappings(GwLog *logger, GwConfigHandler *config)
{ {
this->logger = logger; this->logger = logger;
@ -393,9 +410,33 @@ const char * GwXDRMappings::getUnMapped(){
*unknowAsString=0; *unknowAsString=0;
char *ptr=unknowAsString; char *ptr=unknowAsString;
for (auto it=unknown.begin();it!=unknown.end();it++){ for (auto it=unknown.begin();it!=unknown.end();it++){
snprintf(ptr,ESIZE-1,"%lu,",*it); snprintf(ptr,ESIZE-1,"%lu\n",*it);
*(ptr+ESIZE-1)=0; *(ptr+ESIZE-1)=0;
while (*ptr != 0) ptr++; while (*ptr != 0) ptr++;
} }
return unknowAsString; return unknowAsString;
} }
String GwXDRMappings::getXdrEntry(String mapping, double value,int instance){
String rt;
GwXDRMappingDef *def=GwXDRMappingDef::fromString(mapping);
if (! def) return rt;
int typeIndex=0;
GwXDRType::TypeCode code = findTypeMapping(def->category, def->field);
if (code == GwXDRType::UNKNOWN)
{
return rt;
}
GwXDRType *type = findType(code, &typeIndex);
bool first=true;
while (type){
GwXDRFoundMapping found(def,type);
found.instanceId=instance;
if (first) first=false;
else rt+=",";
rt+=found.buildXdrEntry(value);
type = findType(code, &typeIndex);
}
delete def;
return rt;
}

View File

@ -162,6 +162,7 @@ class GwXDRFoundMapping{
String getTransducerName(){ String getTransducerName(){
return definition->getTransducerName(instanceId); return definition->getTransducerName(instanceId);
} }
String buildXdrEntry(double value);
}; };
//the class GwXDRMappings is not intended to be deleted //the class GwXDRMappings is not intended to be deleted
@ -185,6 +186,7 @@ class GwXDRMappings{
//the returned mapping will exactly contain one mapping def //the returned mapping will exactly contain one mapping def
GwXDRFoundMapping getMapping(String xName,String xType,String xUnit); GwXDRFoundMapping getMapping(String xName,String xType,String xUnit);
GwXDRFoundMapping getMapping(GwXDRCategory category,int selector,int field=0,int instance=-1); GwXDRFoundMapping getMapping(GwXDRCategory category,int selector,int field=0,int instance=-1);
String getXdrEntry(String mapping, double value,int instance=0);
const char * getUnMapped(); const char * getUnMapped();
}; };

View File

@ -421,6 +421,44 @@ protected:
} }
}; };
class XdrExampleRequest : public GwRequestMessage
{
public:
String mapping;
double value;
XdrExampleRequest(String mapping, double value) : GwRequestMessage(F("text/plain"),F("xdrExample")){
this->mapping=mapping;
this->value=value;
};
protected:
virtual void processRequest()
{
String val=xdrMappings.getXdrEntry(mapping,value);
if (val == "") {
result=val;
return;
}
tNMEA0183Msg msg;
msg.Init("XDR",config.getString(config.talkerId,String("GP")).c_str());
msg.AddStrField(val.c_str());
char buf[MAX_NMEA0183_MSG_BUF_LEN];
msg.GetMessage(buf,MAX_NMEA0183_MSG_BUF_LEN);
result=buf;
}
};
class XdrUnMappedRequest : public GwRequestMessage
{
public:
XdrUnMappedRequest() : GwRequestMessage(F("text/plain"),F("boatData")){};
protected:
virtual void processRequest()
{
result = xdrMappings.getUnMapped();
}
};
void setup() { void setup() {
uint8_t chipid[6]; uint8_t chipid[6];
@ -524,6 +562,14 @@ void setup() {
{ return new ResetConfigRequest(); }); { return new ResetConfigRequest(); });
webserver.registerMainHandler("/api/boatData", [](AsyncWebServerRequest *request)->GwRequestMessage * webserver.registerMainHandler("/api/boatData", [](AsyncWebServerRequest *request)->GwRequestMessage *
{ return new BoatDataRequest(); }); { return new BoatDataRequest(); });
webserver.registerMainHandler("/api/xdrExample", [](AsyncWebServerRequest *request)->GwRequestMessage *
{
String mapping=request->arg("mapping");
double value=atof(request->arg("value").c_str());
return new XdrExampleRequest(mapping,value);
});
webserver.registerMainHandler("/api/xdrUnmapped", [](AsyncWebServerRequest *request)->GwRequestMessage *
{ return new XdrUnMappedRequest(); });
webserver.begin(); webserver.begin();
xdrMappings.begin(); xdrMappings.begin();