free GwApi from ArduinoJson
This commit is contained in:
parent
d9af772a57
commit
5195c3be88
|
@ -1,10 +1,13 @@
|
||||||
#include "GwBoatData.h"
|
#include "GwBoatData.h"
|
||||||
|
#include <GwJsonDocument.h>
|
||||||
#include <ArduinoJson/Json/TextFormatter.hpp>
|
#include <ArduinoJson/Json/TextFormatter.hpp>
|
||||||
#define GWTYPE_DOUBLE 1
|
#define GWTYPE_DOUBLE 1
|
||||||
#define GWTYPE_UINT32 2
|
#define GWTYPE_UINT32 2
|
||||||
#define GWTYPE_UINT16 3
|
#define GWTYPE_UINT16 3
|
||||||
#define GWTYPE_INT16 4
|
#define GWTYPE_INT16 4
|
||||||
#define GWTYPE_USER 100
|
#define GWTYPE_USER 100
|
||||||
|
|
||||||
|
|
||||||
class GwBoatItemTypes{
|
class GwBoatItemTypes{
|
||||||
public:
|
public:
|
||||||
static int getType(const uint32_t &x){return GWTYPE_UINT32;}
|
static int getType(const uint32_t &x){return GWTYPE_UINT32;}
|
||||||
|
@ -33,6 +36,7 @@ GwBoatItemBase::GwBoatItemBase(String name, String format, unsigned long invalid
|
||||||
this->type = 0;
|
this->type = 0;
|
||||||
this->lastUpdateSource=-1;
|
this->lastUpdateSource=-1;
|
||||||
}
|
}
|
||||||
|
size_t GwBoatItemBase::getJsonSize(){return JSON_OBJECT_SIZE(10);}
|
||||||
#define STRING_SIZE 40
|
#define STRING_SIZE 40
|
||||||
GwBoatItemBase::StringWriter::StringWriter(){
|
GwBoatItemBase::StringWriter::StringWriter(){
|
||||||
buffer=new uint8_t[STRING_SIZE];
|
buffer=new uint8_t[STRING_SIZE];
|
||||||
|
@ -126,7 +130,7 @@ bool GwBoatItem<T>::updateMax(T nv, int sourceId)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
template <class T>
|
template <class T>
|
||||||
void GwBoatItem<T>::toJsonDoc(JsonDocument *doc, unsigned long minTime)
|
void GwBoatItem<T>::toJsonDoc(GwJsonDocument *doc, unsigned long minTime)
|
||||||
{
|
{
|
||||||
JsonObject o = doc->createNestedObject(name);
|
JsonObject o = doc->createNestedObject(name);
|
||||||
o[F("value")] = getData();
|
o[F("value")] = getData();
|
||||||
|
@ -246,7 +250,7 @@ bool GwBoatDataSatList::update(GwSatInfo info, int source)
|
||||||
data.update(info);
|
data.update(info);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void GwBoatDataSatList::toJsonDoc(JsonDocument *doc, unsigned long minTime)
|
void GwBoatDataSatList::toJsonDoc(GwJsonDocument *doc, unsigned long minTime)
|
||||||
{
|
{
|
||||||
data.houseKeeping();
|
data.houseKeeping();
|
||||||
GwBoatItem<GwSatInfoList>::toJsonDoc(doc, minTime);
|
GwBoatItem<GwSatInfoList>::toJsonDoc(doc, minTime);
|
||||||
|
@ -309,7 +313,7 @@ String GwBoatData::toJson() const {
|
||||||
}
|
}
|
||||||
int sz=JSON_OBJECT_SIZE(count)+elementSizes+10;
|
int sz=JSON_OBJECT_SIZE(count)+elementSizes+10;
|
||||||
LOG_DEBUG(GwLog::DEBUG,"size for boatData: %d",sz);
|
LOG_DEBUG(GwLog::DEBUG,"size for boatData: %d",sz);
|
||||||
DynamicJsonDocument json(sz);
|
GwJsonDocument json(sz);
|
||||||
for (it=values.begin() ; it != values.end();it++){
|
for (it=values.begin() ; it != values.end();it++){
|
||||||
it->second->toJsonDoc(&json,minTime);
|
it->second->toJsonDoc(&json,minTime);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
#define _GWBOATDATA_H
|
#define _GWBOATDATA_H
|
||||||
|
|
||||||
#include "GwLog.h"
|
#include "GwLog.h"
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#define GW_BOAT_VALUE_LEN 32
|
#define GW_BOAT_VALUE_LEN 32
|
||||||
#define GWSC(name) static constexpr const __FlashStringHelper* name=F(#name)
|
#define GWSC(name) static constexpr const __FlashStringHelper* name=F(#name)
|
||||||
|
|
||||||
|
class GwJsonDocument;
|
||||||
class GwBoatItemBase{
|
class GwBoatItemBase{
|
||||||
public:
|
public:
|
||||||
class StringWriter{
|
class StringWriter{
|
||||||
|
@ -65,8 +65,8 @@ class GwBoatItemBase{
|
||||||
return writer.c_str();
|
return writer.c_str();
|
||||||
}
|
}
|
||||||
virtual void fillString()=0;
|
virtual void fillString()=0;
|
||||||
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime)=0;
|
virtual void toJsonDoc(GwJsonDocument *doc, unsigned long minTime)=0;
|
||||||
virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(10);}
|
virtual size_t getJsonSize();
|
||||||
virtual int getLastSource(){return lastUpdateSource;}
|
virtual int getLastSource(){return lastUpdateSource;}
|
||||||
virtual void refresh(unsigned long ts=0){uls(ts);}
|
virtual void refresh(unsigned long ts=0){uls(ts);}
|
||||||
String getName(){return name;}
|
String getName(){return name;}
|
||||||
|
@ -89,7 +89,7 @@ template<class T> class GwBoatItem : public GwBoatItemBase{
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
virtual void fillString();
|
virtual void fillString();
|
||||||
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime);
|
virtual void toJsonDoc(GwJsonDocument *doc, unsigned long minTime);
|
||||||
virtual int getLastSource(){return lastUpdateSource;}
|
virtual int getLastSource(){return lastUpdateSource;}
|
||||||
};
|
};
|
||||||
double formatCourse(double cv);
|
double formatCourse(double cv);
|
||||||
|
@ -127,7 +127,7 @@ class GwBoatDataSatList : public GwBoatItem<GwSatInfoList>
|
||||||
public:
|
public:
|
||||||
GwBoatDataSatList(String name, String formatInfo, unsigned long invalidTime = INVALID_TIME, GwBoatItemMap *map = NULL);
|
GwBoatDataSatList(String name, String formatInfo, unsigned long invalidTime = INVALID_TIME, GwBoatItemMap *map = NULL);
|
||||||
bool update(GwSatInfo info, int source);
|
bool update(GwSatInfo info, int source);
|
||||||
virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime);
|
virtual void toJsonDoc(GwJsonDocument *doc, unsigned long minTime);
|
||||||
GwSatInfo *getAt(int idx){
|
GwSatInfo *getAt(int idx){
|
||||||
if (! isValid()) return NULL;
|
if (! isValid()) return NULL;
|
||||||
return data.getAt(idx);
|
return data.getAt(idx);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#define _NMEA0183CONVERTERLIST_H
|
#define _NMEA0183CONVERTERLIST_H
|
||||||
|
|
||||||
#include "WString.h"
|
#include "WString.h"
|
||||||
#include "ArduinoJson.h"
|
#include "GwJsonDocument.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
template <class T, class Msg>
|
template <class T, class Msg>
|
||||||
|
@ -189,10 +189,10 @@ private:
|
||||||
int numConverters(){
|
int numConverters(){
|
||||||
return converters.size();
|
return converters.size();
|
||||||
}
|
}
|
||||||
void toJson(String prefix, JsonDocument &json){
|
void toJson(String prefix, GwJsonDocument *json){
|
||||||
for (auto it = converters.begin(); it != converters.end(); it++)
|
for (auto it = converters.begin(); it != converters.end(); it++)
|
||||||
{
|
{
|
||||||
json[prefix][String(it->first)] = it->second.count;
|
(*json)[prefix][String(it->first)] = it->second.count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String handledKeys(){
|
String handledKeys(){
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef _GWJSONDOCUMENT_H
|
||||||
|
#define _GWJSONDOCUMENT_H
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
class GwJsonDocument : public DynamicJsonDocument{
|
||||||
|
public:
|
||||||
|
GwJsonDocument(int sz): DynamicJsonDocument(sz){}
|
||||||
|
};
|
||||||
|
#endif
|
|
@ -28,6 +28,7 @@
|
||||||
#include "N2kDataToNMEA0183.h"
|
#include "N2kDataToNMEA0183.h"
|
||||||
#include "NMEA0183AISMessages.h"
|
#include "NMEA0183AISMessages.h"
|
||||||
#include "ConverterList.h"
|
#include "ConverterList.h"
|
||||||
|
#include "GwJsonDocument.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -141,7 +142,7 @@ private:
|
||||||
LOG_DEBUG(GwLog::DEBUG+1,"handled %ld",N2kMsg.PGN);
|
LOG_DEBUG(GwLog::DEBUG+1,"handled %ld",N2kMsg.PGN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
virtual void toJson(JsonDocument &json)
|
virtual void toJson(GwJsonDocument *json)
|
||||||
{
|
{
|
||||||
converters.toJson(String(F("cnv")),json);
|
converters.toJson(String(F("cnv")),json);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,12 +25,12 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#include <NMEA0183.h>
|
#include <NMEA0183.h>
|
||||||
#include <NMEA2000.h>
|
#include <NMEA2000.h>
|
||||||
|
|
||||||
|
|
||||||
#include <GwLog.h>
|
#include <GwLog.h>
|
||||||
#include <GwBoatData.h>
|
#include <GwBoatData.h>
|
||||||
#include <GwXDRMappings.h>
|
#include <GwXDRMappings.h>
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
class GwJsonDocument;
|
||||||
class N2kDataToNMEA0183
|
class N2kDataToNMEA0183
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -54,7 +54,7 @@ public:
|
||||||
virtual ~N2kDataToNMEA0183(){}
|
virtual ~N2kDataToNMEA0183(){}
|
||||||
virtual unsigned long* handledPgns()=0;
|
virtual unsigned long* handledPgns()=0;
|
||||||
virtual int numPgns()=0;
|
virtual int numPgns()=0;
|
||||||
virtual void toJson(JsonDocument &json)=0;
|
virtual void toJson(GwJsonDocument *json)=0;
|
||||||
virtual String handledKeys()=0;
|
virtual String handledKeys()=0;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
11
src/main.cpp
11
src/main.cpp
|
@ -40,12 +40,11 @@ const unsigned long HEAP_REPORT_TIME=2000; //set to 0 to disable heap reporting
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "esp_heap_caps.h"
|
#include "esp_heap_caps.h"
|
||||||
|
#include "GwJsonDocument.h"
|
||||||
#include "N2kDataToNMEA0183.h"
|
#include "N2kDataToNMEA0183.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -297,7 +296,7 @@ protected:
|
||||||
virtual void processRequest()
|
virtual void processRequest()
|
||||||
{
|
{
|
||||||
int numPgns = nmea0183Converter->numPgns();
|
int numPgns = nmea0183Converter->numPgns();
|
||||||
DynamicJsonDocument status(256 +
|
GwJsonDocument status(256 +
|
||||||
countNMEA2KIn.getJsonSize()+
|
countNMEA2KIn.getJsonSize()+
|
||||||
countNMEA2KOut.getJsonSize() +
|
countNMEA2KOut.getJsonSize() +
|
||||||
countUSBIn.getJsonSize()+
|
countUSBIn.getJsonSize()+
|
||||||
|
@ -331,7 +330,7 @@ class CapabilitiesRequest : public GwRequestMessage{
|
||||||
protected:
|
protected:
|
||||||
virtual void processRequest(){
|
virtual void processRequest(){
|
||||||
int numCapabilities=userCodeHandler.getCapabilities()->size();
|
int numCapabilities=userCodeHandler.getCapabilities()->size();
|
||||||
DynamicJsonDocument json(JSON_OBJECT_SIZE(numCapabilities*3+6));
|
GwJsonDocument json(JSON_OBJECT_SIZE(numCapabilities*3+6));
|
||||||
for (auto it=userCodeHandler.getCapabilities()->begin();
|
for (auto it=userCodeHandler.getCapabilities()->begin();
|
||||||
it != userCodeHandler.getCapabilities()->end();it++){
|
it != userCodeHandler.getCapabilities()->end();it++){
|
||||||
json[it->first]=it->second;
|
json[it->first]=it->second;
|
||||||
|
@ -353,7 +352,7 @@ class ConverterInfoRequest : public GwRequestMessage{
|
||||||
ConverterInfoRequest() : GwRequestMessage(F("application/json"),F("converterInfo")){};
|
ConverterInfoRequest() : GwRequestMessage(F("application/json"),F("converterInfo")){};
|
||||||
protected:
|
protected:
|
||||||
virtual void processRequest(){
|
virtual void processRequest(){
|
||||||
DynamicJsonDocument json(512);
|
GwJsonDocument json(512);
|
||||||
String keys=toN2KConverter->handledKeys();
|
String keys=toN2KConverter->handledKeys();
|
||||||
logger.logDebug(GwLog::DEBUG,"handled nmea0183: %s",keys.c_str());
|
logger.logDebug(GwLog::DEBUG,"handled nmea0183: %s",keys.c_str());
|
||||||
json["nmea0183"]=keys;
|
json["nmea0183"]=keys;
|
||||||
|
@ -408,7 +407,7 @@ protected:
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
DynamicJsonDocument rt(100);
|
GwJsonDocument rt(100);
|
||||||
rt["status"] = error;
|
rt["status"] = error;
|
||||||
serializeJson(rt, result);
|
serializeJson(rt, result);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,7 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
path = posixpath.normpath(path)
|
path = posixpath.normpath(path)
|
||||||
words = path.split('/')
|
words = path.split('/')
|
||||||
words = list(filter(None, words))
|
words = list(filter(None, words))
|
||||||
|
isSecond=False
|
||||||
for baseDir in [
|
for baseDir in [
|
||||||
os.path.join(self.server.baseDir,'lib','generated'),
|
os.path.join(self.server.baseDir,'lib','generated'),
|
||||||
os.path.join(self.server.baseDir,'web')]:
|
os.path.join(self.server.baseDir,'web')]:
|
||||||
|
@ -63,6 +64,9 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
rpath += '/'
|
rpath += '/'
|
||||||
if os.path.exists(rpath):
|
if os.path.exists(rpath):
|
||||||
return rpath
|
return rpath
|
||||||
|
if isSecond:
|
||||||
|
return rpath
|
||||||
|
isSecond=True
|
||||||
|
|
||||||
def run(port,apiUrl,server_class=http.server.HTTPServer, handler_class=RequestHandler):
|
def run(port,apiUrl,server_class=http.server.HTTPServer, handler_class=RequestHandler):
|
||||||
basedir=os.path.join(os.path.dirname(__file__),'..')
|
basedir=os.path.join(os.path.dirname(__file__),'..')
|
||||||
|
|
Loading…
Reference in New Issue