From 810e16df6ed5661f662721606c1d60637d9e3268 Mon Sep 17 00:00:00 2001 From: lumapu Date: Mon, 5 Sep 2022 21:27:48 +0200 Subject: [PATCH] * start creation of visualisation api contents * removed /json part -> moved to API * added missing update.html --- tools/esp8266/app.cpp | 26 ------------ tools/esp8266/app.h | 1 - tools/esp8266/html/index.html | 2 +- tools/esp8266/html/update.html | 25 +++++++++++ tools/esp8266/web.cpp | 8 ---- tools/esp8266/web.h | 1 - tools/esp8266/webApi.cpp | 78 +++++++++++++++++++++++++++++----- tools/esp8266/webApi.h | 1 + 8 files changed, 95 insertions(+), 47 deletions(-) create mode 100644 tools/esp8266/html/update.html diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index 84fa4ae6..a14f7ae0 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -498,32 +498,6 @@ void app::cbMqtt(char* topic, byte* payload, unsigned int length) { } -//----------------------------------------------------------------------------- -String app::getJson(void) { - DPRINTLN(DBG_VERBOSE, F("app::showJson")); - String modJson; - - modJson = F("{\n"); - for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { - Inverter<> *iv = mSys->getInverterByPos(id); - if(NULL != iv) { - char topic[40], val[25]; - snprintf(topic, 30, "\"%s\": {\n", iv->name); - modJson += String(topic); - for(uint8_t i = 0; i < iv->listLen; i++) { - snprintf(topic, 40, "\t\"ch%d/%s\"", iv->assign[i].ch, iv->getFieldName(i)); - snprintf(val, 25, "[%.3f, \"%s\"]", iv->getValue(i), iv->getUnit(i)); - modJson += String(topic) + ": " + String(val) + F(",\n"); - } - modJson += F("\t\"last_msg\": \"") + getDateTimeStr(iv->ts) + F("\"\n\t},\n"); - } - } - modJson += F("\"json_ts\": \"") + String(getDateTimeStr(mTimestamp)) + F("\"\n}\n"); - - return modJson; -} - - //----------------------------------------------------------------------------- bool app::getWifiApActive(void) { return mWifi->getApActive(); diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 2d728be5..08d6b199 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -62,7 +62,6 @@ class app { void cbMqtt(char* topic, byte* payload, unsigned int length); void saveValues(void); void resetPayload(Inverter<>* iv); - String getJson(void); bool getWifiApActive(void); uint8_t getIrqPin(void) { diff --git a/tools/esp8266/html/index.html b/tools/esp8266/html/index.html index f8a06f6c..627b3b03 100644 --- a/tools/esp8266/html/index.html +++ b/tools/esp8266/html/index.html @@ -80,7 +80,7 @@ html += "producing\n"; if(false == i["is_avail"]) { - var date = new Date(i["ts_last_succes"] * 1000); + var date = new Date(i["ts_last_success"] * 1000); html += "-> last successful transmission: " + date.toLocaleString('de-DE', {timeZone: 'UTC'}); } diff --git a/tools/esp8266/html/update.html b/tools/esp8266/html/update.html new file mode 100644 index 00000000..4da28459 --- /dev/null +++ b/tools/esp8266/html/update.html @@ -0,0 +1,25 @@ + + + + Update + + + {#HEAD} + + +

Update

+
+
+ Make sure that you have noted all or settings before starting an update. New versions maybe changed their memory layout which remains in default settings. +
+

+ {#CONTENT} +
+ + + diff --git a/tools/esp8266/web.cpp b/tools/esp8266/web.cpp index ffd676c7..c638e255 100644 --- a/tools/esp8266/web.cpp +++ b/tools/esp8266/web.cpp @@ -53,7 +53,6 @@ void web::setup(void) { mWeb->on("/visualization", HTTP_ANY, std::bind(&web::showVisualization, this, std::placeholders::_1)); mWeb->on("/livedata", HTTP_ANY, std::bind(&web::showLiveData, this, std::placeholders::_1)); - mWeb->on("/json", HTTP_ANY, std::bind(&web::showJson, this, std::placeholders::_1)); mWeb->on("/api1", HTTP_POST, std::bind(&web::showWebApi, this, std::placeholders::_1)); @@ -413,13 +412,6 @@ void web::showLiveData(AsyncWebServerRequest *request) { } -//----------------------------------------------------------------------------- -void web::showJson(AsyncWebServerRequest *request) { - DPRINTLN(DBG_VERBOSE, F("web::showJson")); - request->send(200, F("application/json"), mMain->getJson()); -} - - //----------------------------------------------------------------------------- void web::showWebApi(AsyncWebServerRequest *request) { diff --git a/tools/esp8266/web.h b/tools/esp8266/web.h index 652bf677..f8e46ad4 100644 --- a/tools/esp8266/web.h +++ b/tools/esp8266/web.h @@ -39,7 +39,6 @@ class web { void showVisualization(AsyncWebServerRequest *request); void showLiveData(AsyncWebServerRequest *request); - void showJson(AsyncWebServerRequest *request); void showWebApi(AsyncWebServerRequest *request); void showUpdateForm(AsyncWebServerRequest *request); diff --git a/tools/esp8266/webApi.cpp b/tools/esp8266/webApi.cpp index 60d1ed2d..1e3ce713 100644 --- a/tools/esp8266/webApi.cpp +++ b/tools/esp8266/webApi.cpp @@ -48,6 +48,7 @@ void webApi::onApi(AsyncWebServerRequest *request) { else if(path == "serial") getSerial(root); else if(path == "index") getIndex(root); else if(path == "setup") getSetup(root); + else if(path == "live") getLive(root); else getNotFound(root, F("http://") + request->host() + F("/api/")); @@ -86,20 +87,20 @@ void webApi::getInverterList(JsonObject obj) { for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { iv = mApp->mSys->getInverterByPos(i); if(NULL != iv) { - JsonObject obj = invArr.createNestedObject(); - obj[F("id")] = i; - obj[F("name")] = String(iv->name); - obj[F("serial")] = String(iv->serial.u64, HEX); - obj[F("channels")] = iv->channels; - obj[F("version")] = String(iv->fwVersion); + JsonObject obj2 = invArr.createNestedObject(); + obj2[F("id")] = i; + obj2[F("name")] = String(iv->name); + obj2[F("serial")] = String(iv->serial.u64, HEX); + obj2[F("channels")] = iv->channels; + obj2[F("version")] = String(iv->fwVersion); for(uint8_t j = 0; j < iv->channels; j ++) { - obj[F("ch_max_power")][j] = iv->chMaxPwr[j]; - obj[F("ch_name")][j] = iv->chName[j]; + obj2[F("ch_max_power")][j] = iv->chMaxPwr[j]; + obj2[F("ch_name")][j] = iv->chName[j]; } - obj[F("power_limit")] = iv->powerLimit[0]; - obj[F("power_limit_option")] = iv->powerLimit[1]; + obj2[F("power_limit")] = iv->powerLimit[0]; + obj2[F("power_limit_option")] = iv->powerLimit[1]; } } obj[F("interval")] = String(mConfig->sendInterval); @@ -160,6 +161,7 @@ void webApi::getNotFound(JsonObject obj, String url) { ep[F("serial")] = url + F("serial"); ep[F("index")] = url + F("index"); ep[F("setup")] = url + F("setup"); + ep[F("live")] = url + F("live"); } @@ -210,3 +212,59 @@ void webApi::getSetup(JsonObject obj) { getRadio(obj.createNestedObject(F("radio"))); getSerial(obj.createNestedObject(F("serial"))); } + + +//----------------------------------------------------------------------------- +void webApi::getLive(JsonObject obj) { + JsonArray invArr = obj.createNestedArray(F("inverter")); + + uint8_t list[] = {FLD_UAC, FLD_IAC, FLD_PAC, FLD_F, FLD_PCT, FLD_T, FLD_YT, FLD_YD, FLD_PDC, FLD_EFF, FLD_PRA, FLD_ALARM_MES_ID}; + + Inverter<> *iv; + uint8_t pos; + for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { + iv = mApp->mSys->getInverterByPos(i); + if(NULL != iv) { + JsonObject obj2 = invArr.createNestedObject(); + obj2[F("id")] = i; + obj2[F("name")] = String(iv->name); + obj2[F("channels")] = iv->channels; + obj2[F("power_limit_read")] = iv->actPowerLimit; + obj2[F("power_limit_active")] = NoPowerLimit != iv->powerLimit[1]; + obj2[F("last_alarm")] = String(iv->lastAlarmMsg); + obj2[F("ts_last_success")] = iv->ts; + + JsonArray ch = obj2.createNestedArray("ch"); + JsonArray ch0 = ch.createNestedArray(); + for (uint8_t fld = 0; fld < 11; fld++) { + pos = (iv->getPosByChFld(CH0, list[fld])); + if (0xff != pos) { + JsonObject dat = ch0.createNestedObject(); + dat[F("value")] = iv->getValue(pos); + dat[F("unit")] = String(iv->getUnit(pos)); + dat[F("name")] = String(iv->getFieldName(pos)); + } + } + + for(uint8_t j = 0; j < iv->channels; j ++) { + obj2[F("ch_names")][j] = iv->chName[j]; + JsonArray cur = ch.createNestedArray(); + for (uint8_t k = 0; k < 6; k++) { + switch(k) { + default: pos = (iv->getPosByChFld(j, FLD_UDC)); break; + case 1: pos = (iv->getPosByChFld(j, FLD_IDC)); break; + case 2: pos = (iv->getPosByChFld(j, FLD_PDC)); break; + case 3: pos = (iv->getPosByChFld(j, FLD_YD)); break; + case 4: pos = (iv->getPosByChFld(j, FLD_YT)); break; + case 5: pos = (iv->getPosByChFld(j, FLD_IRR)); break; + } + cur[k] = (0xff != pos) ? iv->getValue(pos) : 0; + if(0xff != pos) { + obj2[F("fld_units")][k] = String(iv->getUnit(pos)); + obj2[F("fld_names")][k] = String(iv->getFieldName(pos)); + } + } + } + } + } +} diff --git a/tools/esp8266/webApi.h b/tools/esp8266/webApi.h index 1ad91d05..9a92519a 100644 --- a/tools/esp8266/webApi.h +++ b/tools/esp8266/webApi.h @@ -32,6 +32,7 @@ class webApi { void getNotFound(JsonObject obj, String url); void getIndex(JsonObject obj); void getSetup(JsonObject obj); + void getLive(JsonObject obj); AsyncWebServer *mSrv; app *mApp;