free GwApi from ArduinoJson
This commit is contained in:
parent
d9af772a57
commit
5195c3be88
|
@ -1,10 +1,13 @@
|
|||
#include "GwBoatData.h"
|
||||
#include <GwJsonDocument.h>
|
||||
#include <ArduinoJson/Json/TextFormatter.hpp>
|
||||
#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<T>::updateMax(T nv, int sourceId)
|
|||
return false;
|
||||
}
|
||||
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);
|
||||
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<GwSatInfoList>::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);
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
#define _GWBOATDATA_H
|
||||
|
||||
#include "GwLog.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <Arduino.h>
|
||||
#include <map>
|
||||
#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 T> 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<GwSatInfoList>
|
|||
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);
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#define _NMEA0183CONVERTERLIST_H
|
||||
|
||||
#include "WString.h"
|
||||
#include "ArduinoJson.h"
|
||||
#include "GwJsonDocument.h"
|
||||
#include <map>
|
||||
|
||||
template <class T, class Msg>
|
||||
|
@ -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(){
|
||||
|
|
|
@ -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 "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);
|
||||
}
|
||||
|
|
|
@ -25,12 +25,12 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#include <NMEA0183.h>
|
||||
#include <NMEA2000.h>
|
||||
|
||||
|
||||
#include <GwLog.h>
|
||||
#include <GwBoatData.h>
|
||||
#include <GwXDRMappings.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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
|
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 <ESPAsyncWebServer.h>
|
||||
#include <Preferences.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#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);
|
||||
}
|
||||
|
|
|
@ -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__),'..')
|
||||
|
|
Loading…
Reference in New Issue