From 5195c3be8853dfe771c616705be96275a1f72caf Mon Sep 17 00:00:00 2001 From: wellenvogel Date: Sun, 28 Nov 2021 12:11:47 +0100 Subject: [PATCH] free GwApi from ArduinoJson --- lib/boatData/GwBoatData.cpp | 10 +++++++--- lib/boatData/GwBoatData.h | 10 +++++----- lib/converterlist/ConverterList.h | 6 +++--- lib/json/GwJsonDocument.h | 9 +++++++++ lib/nmea2kto0183/N2kDataToNMEA0183.cpp | 3 ++- lib/nmea2kto0183/N2kDataToNMEA0183.h | 4 ++-- src/main.cpp | 11 +++++------ tools/testServer.py | 4 ++++ 8 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 lib/json/GwJsonDocument.h diff --git a/lib/boatData/GwBoatData.cpp b/lib/boatData/GwBoatData.cpp index f360a67..fca4d4b 100644 --- a/lib/boatData/GwBoatData.cpp +++ b/lib/boatData/GwBoatData.cpp @@ -1,10 +1,13 @@ #include "GwBoatData.h" +#include #include #define GWTYPE_DOUBLE 1 #define GWTYPE_UINT32 2 #define GWTYPE_UINT16 3 #define GWTYPE_INT16 4 #define GWTYPE_USER 100 + + class GwBoatItemTypes{ public: 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->lastUpdateSource=-1; } +size_t GwBoatItemBase::getJsonSize(){return JSON_OBJECT_SIZE(10);} #define STRING_SIZE 40 GwBoatItemBase::StringWriter::StringWriter(){ buffer=new uint8_t[STRING_SIZE]; @@ -126,7 +130,7 @@ bool GwBoatItem::updateMax(T nv, int sourceId) return false; } template -void GwBoatItem::toJsonDoc(JsonDocument *doc, unsigned long minTime) +void GwBoatItem::toJsonDoc(GwJsonDocument *doc, unsigned long minTime) { JsonObject o = doc->createNestedObject(name); o[F("value")] = getData(); @@ -246,7 +250,7 @@ bool GwBoatDataSatList::update(GwSatInfo info, int source) data.update(info); return true; } -void GwBoatDataSatList::toJsonDoc(JsonDocument *doc, unsigned long minTime) +void GwBoatDataSatList::toJsonDoc(GwJsonDocument *doc, unsigned long minTime) { data.houseKeeping(); GwBoatItem::toJsonDoc(doc, minTime); @@ -309,7 +313,7 @@ String GwBoatData::toJson() const { } int sz=JSON_OBJECT_SIZE(count)+elementSizes+10; LOG_DEBUG(GwLog::DEBUG,"size for boatData: %d",sz); - DynamicJsonDocument json(sz); + GwJsonDocument json(sz); for (it=values.begin() ; it != values.end();it++){ it->second->toJsonDoc(&json,minTime); } diff --git a/lib/boatData/GwBoatData.h b/lib/boatData/GwBoatData.h index c2e68d6..8d5f7f1 100644 --- a/lib/boatData/GwBoatData.h +++ b/lib/boatData/GwBoatData.h @@ -2,12 +2,12 @@ #define _GWBOATDATA_H #include "GwLog.h" -#include #include #include #define GW_BOAT_VALUE_LEN 32 #define GWSC(name) static constexpr const __FlashStringHelper* name=F(#name) +class GwJsonDocument; class GwBoatItemBase{ public: class StringWriter{ @@ -65,8 +65,8 @@ class GwBoatItemBase{ return writer.c_str(); } virtual void fillString()=0; - virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime)=0; - virtual size_t getJsonSize(){return JSON_OBJECT_SIZE(10);} + virtual void toJsonDoc(GwJsonDocument *doc, unsigned long minTime)=0; + virtual size_t getJsonSize(); virtual int getLastSource(){return lastUpdateSource;} virtual void refresh(unsigned long ts=0){uls(ts);} String getName(){return name;} @@ -89,7 +89,7 @@ template class GwBoatItem : public GwBoatItemBase{ return data; } virtual void fillString(); - virtual void toJsonDoc(JsonDocument *doc, unsigned long minTime); + virtual void toJsonDoc(GwJsonDocument *doc, unsigned long minTime); virtual int getLastSource(){return lastUpdateSource;} }; double formatCourse(double cv); @@ -127,7 +127,7 @@ class GwBoatDataSatList : public GwBoatItem public: GwBoatDataSatList(String name, String formatInfo, unsigned long invalidTime = INVALID_TIME, GwBoatItemMap *map = NULL); 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){ if (! isValid()) return NULL; return data.getAt(idx); diff --git a/lib/converterlist/ConverterList.h b/lib/converterlist/ConverterList.h index afd776e..5e2a70a 100644 --- a/lib/converterlist/ConverterList.h +++ b/lib/converterlist/ConverterList.h @@ -2,7 +2,7 @@ #define _NMEA0183CONVERTERLIST_H #include "WString.h" -#include "ArduinoJson.h" +#include "GwJsonDocument.h" #include template @@ -189,10 +189,10 @@ private: int numConverters(){ return converters.size(); } - void toJson(String prefix, JsonDocument &json){ + void toJson(String prefix, GwJsonDocument *json){ 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(){ diff --git a/lib/json/GwJsonDocument.h b/lib/json/GwJsonDocument.h new file mode 100644 index 0000000..26d99e5 --- /dev/null +++ b/lib/json/GwJsonDocument.h @@ -0,0 +1,9 @@ +#ifndef _GWJSONDOCUMENT_H +#define _GWJSONDOCUMENT_H +#include + +class GwJsonDocument : public DynamicJsonDocument{ + public: + GwJsonDocument(int sz): DynamicJsonDocument(sz){} +}; +#endif \ No newline at end of file diff --git a/lib/nmea2kto0183/N2kDataToNMEA0183.cpp b/lib/nmea2kto0183/N2kDataToNMEA0183.cpp index 0cfb2a6..eb6b62d 100644 --- a/lib/nmea2kto0183/N2kDataToNMEA0183.cpp +++ b/lib/nmea2kto0183/N2kDataToNMEA0183.cpp @@ -28,6 +28,7 @@ #include "N2kDataToNMEA0183.h" #include "NMEA0183AISMessages.h" #include "ConverterList.h" +#include "GwJsonDocument.h" @@ -141,7 +142,7 @@ private: 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); } diff --git a/lib/nmea2kto0183/N2kDataToNMEA0183.h b/lib/nmea2kto0183/N2kDataToNMEA0183.h index d95e8a3..db1b704 100644 --- a/lib/nmea2kto0183/N2kDataToNMEA0183.h +++ b/lib/nmea2kto0183/N2kDataToNMEA0183.h @@ -25,12 +25,12 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include - #include #include #include //------------------------------------------------------------------------------ +class GwJsonDocument; class N2kDataToNMEA0183 { public: @@ -54,7 +54,7 @@ public: virtual ~N2kDataToNMEA0183(){} virtual unsigned long* handledPgns()=0; virtual int numPgns()=0; - virtual void toJson(JsonDocument &json)=0; + virtual void toJson(GwJsonDocument *json)=0; virtual String handledKeys()=0; }; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b02b42e..331680e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,12 +40,11 @@ const unsigned long HEAP_REPORT_TIME=2000; //set to 0 to disable heap reporting #include #include #include -#include #include #include #include #include "esp_heap_caps.h" - +#include "GwJsonDocument.h" #include "N2kDataToNMEA0183.h" @@ -297,7 +296,7 @@ protected: virtual void processRequest() { int numPgns = nmea0183Converter->numPgns(); - DynamicJsonDocument status(256 + + GwJsonDocument status(256 + countNMEA2KIn.getJsonSize()+ countNMEA2KOut.getJsonSize() + countUSBIn.getJsonSize()+ @@ -331,7 +330,7 @@ class CapabilitiesRequest : public GwRequestMessage{ protected: virtual void processRequest(){ 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(); it != userCodeHandler.getCapabilities()->end();it++){ json[it->first]=it->second; @@ -353,7 +352,7 @@ class ConverterInfoRequest : public GwRequestMessage{ ConverterInfoRequest() : GwRequestMessage(F("application/json"),F("converterInfo")){}; protected: virtual void processRequest(){ - DynamicJsonDocument json(512); + GwJsonDocument json(512); String keys=toN2KConverter->handledKeys(); logger.logDebug(GwLog::DEBUG,"handled nmea0183: %s",keys.c_str()); json["nmea0183"]=keys; @@ -408,7 +407,7 @@ protected: } else { - DynamicJsonDocument rt(100); + GwJsonDocument rt(100); rt["status"] = error; serializeJson(rt, result); } diff --git a/tools/testServer.py b/tools/testServer.py index 278fef8..8b73519 100755 --- a/tools/testServer.py +++ b/tools/testServer.py @@ -50,6 +50,7 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler): path = posixpath.normpath(path) words = path.split('/') words = list(filter(None, words)) + isSecond=False for baseDir in [ os.path.join(self.server.baseDir,'lib','generated'), os.path.join(self.server.baseDir,'web')]: @@ -63,6 +64,9 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler): rpath += '/' if os.path.exists(rpath): return rpath + if isSecond: + return rpath + isSecond=True def run(port,apiUrl,server_class=http.server.HTTPServer, handler_class=RequestHandler): basedir=os.path.join(os.path.dirname(__file__),'..')