diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index c151cef0..3d7e8b11 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -71,6 +71,11 @@ void app::loop(void) { DPRINTLN(DBG_INFO, "[NTP]: " + getDateTimeStr(mTimestamp)); } + if(mFlagSendDiscoveryConfig) { + mFlagSendDiscoveryConfig = false; + sendMqttDiscoveryConfig(); + } + if(mShouldReboot) { DPRINTLN(DBG_INFO, F("Rebooting...")); ESP.restart(); @@ -155,10 +160,6 @@ void app::loop(void) { char val[10]; snprintf(val, 10, "%ld", millis()/1000); -#ifndef __MQTT_NO_DISCOVERCONFIG__ - // MQTTDiscoveryConfig nur wenn nicht abgeschaltet. - sendMqttDiscoveryConfig(); -#endif mMqtt.sendMsg("uptime", val); #ifdef __MQTT_TEST__ @@ -598,13 +599,14 @@ void app::sendMqttDiscoveryConfig(void) { Inverter<> *iv = mSys->getInverterByPos(id); if(NULL != iv) { record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug); - if(iv->isAvailable(mTimestamp, rec) && mMqttConfigSendState[id] != true) { + // TODO: next line makes no sense if discovery config is send manually by button + //if(iv->isAvailable(mTimestamp, rec) && mMqttConfigSendState[id] != true) { DynamicJsonDocument deviceDoc(128); deviceDoc["name"] = iv->name; - deviceDoc["ids"] = String(iv->serial.u64, HEX); - deviceDoc["cu"] = F("http://") + String(WiFi.localIP().toString()); - deviceDoc["mf"] = "Hoymiles"; - deviceDoc["mdl"] = iv->name; + deviceDoc["ids"] = String(iv->serial.u64, HEX); + deviceDoc["cu"] = F("http://") + String(WiFi.localIP().toString()); + deviceDoc["mf"] = "Hoymiles"; + deviceDoc["mdl"] = iv->name; JsonObject deviceObj = deviceDoc.as(); DynamicJsonDocument doc(384); @@ -620,28 +622,28 @@ void app::sendMqttDiscoveryConfig(void) { const char* devCls = getFieldDeviceClass(rec->assign[i].fieldId); const char* stateCls = getFieldStateClass(rec->assign[i].fieldId); - doc["name"] = name; - doc["stat_t"] = stateTopic; + doc["name"] = name; + doc["stat_t"] = stateTopic; doc["unit_of_meas"] = iv->getUnit(i, rec); - doc["uniq_id"] = String(iv->serial.u64, HEX) + "_" + uniq_id; - doc["dev"] = deviceObj; - doc["exp_aft"] = mMqttInterval + 5; // add 5 sec if connection is bad or ESP too slow - if (devCls != NULL) { + doc["uniq_id"] = String(iv->serial.u64, HEX) + "_" + uniq_id; + doc["dev"] = deviceObj; + doc["exp_aft"] = mMqttInterval + 5; // add 5 sec if connection is bad or ESP too slow + if (devCls != NULL) doc["dev_cla"] = devCls; - } - if (stateCls != NULL) { + if (stateCls != NULL) doc["stat_cla"] = stateCls; - } serializeJson(doc, buffer); mMqtt.sendMsg2(discoveryTopic, buffer, true); + //DPRINTLN(DBG_INFO, F("mqtt sent")); doc.clear(); - - yield(); } + // TODO: remove this field, obsolete? mMqttConfigSendState[id] = true; - } + + yield(); + //} } } } @@ -674,6 +676,7 @@ void app::resetSystem(void) { mUptimeSecs = 0; mPrevMillis = 0; mUpdateNtp = false; + mFlagSendDiscoveryConfig = false; mNtpRefreshTicker = 0; mNtpRefreshInterval = NTP_REFRESH_INTERVAL; // [ms] diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index fe92bb5e..f02d85a1 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -156,20 +156,23 @@ class app { inline bool getSettingsValid(void) { return mSettingsValid; } inline bool getRebootRequestState(void) { return mShowRebootRequest; } + HmSystemType *mSys; bool mShouldReboot; + bool mFlagSendDiscoveryConfig; private: void resetSystem(void); void loadDefaultConfig(void); void loadEEpconfig(void); void setupMqtt(void); + + void sendMqttDiscoveryConfig(void); bool buildPayload(uint8_t id); void processPayload(bool retransmit); void processPayload(bool retransmit, uint8_t cmd); - void sendMqttDiscoveryConfig(void); const char* getFieldDeviceClass(uint8_t fieldId); const char* getFieldStateClass(uint8_t fieldId); diff --git a/tools/esp8266/html/setup.html b/tools/esp8266/html/setup.html index 3c9269b1..adfddea7 100644 --- a/tools/esp8266/html/setup.html +++ b/tools/esp8266/html/setup.html @@ -67,7 +67,7 @@ - n/a + @@ -85,6 +85,9 @@ + + + @@ -110,7 +113,9 @@ - +
+
+ Download your settings (JSON file) (only saved values) @@ -132,8 +137,16 @@ ivHtml(JSON.parse('{"name":"","serial":"","channels":4,"ch_max_power":[0,0,0,0],"ch_name":["","","",""]}'), highestId + 1); }); - function apiCb(obj) { - var e = document.getElementById("apiResult"); + function apiCbNtp(obj) { + var e = document.getElementById("apiResultNtp"); + if(obj["success"]) + e.innerHTML = "command excuted"; + else + e.innerHTML = "Error: " + obj["error"]; + } + + function apiCbMqtt(obj) { + var e = document.getElementById("apiResultMqtt"); if(obj["success"]) e.innerHTML = "command excuted"; else @@ -146,13 +159,19 @@ var obj = new Object(); obj.cmd = "set_time"; obj.ts = parseInt(offset + (date.getTime() / 1000)); - getAjax("/api/setup", apiCb, "POST", JSON.stringify(obj)); + getAjax("/api/setup", apiCbNtp, "POST", JSON.stringify(obj)); } function syncTime() { var obj = new Object(); obj.cmd = "sync_ntp"; - getAjax("/api/setup", apiCb, "POST", JSON.stringify(obj)); + getAjax("/api/setup", apiCbNtp, "POST", JSON.stringify(obj)); + } + + function sendDiscoveryConfig() { + var obj = new Object(); + obj.cmd = "discovery_cfg"; + getAjax("/api/setup", apiCbMqtt, "POST", JSON.stringify(obj)); } function ivHtml(obj, id) { diff --git a/tools/esp8266/html/update.html b/tools/esp8266/html/update.html index 55fd3e39..5f4f033a 100644 --- a/tools/esp8266/html/update.html +++ b/tools/esp8266/html/update.html @@ -10,7 +10,9 @@

Update

- Make sure that you have noted all your settings before starting an update. New versions may have changed their memory layout which can break your existing settings. + Make sure that you have noted all your settings before starting an update. New versions may have changed their memory layout which can break your existing settings.
+
+ Download your settings (JSON file)


diff --git a/tools/esp8266/webApi.cpp b/tools/esp8266/webApi.cpp index e7f2e4af..020eaf37 100644 --- a/tools/esp8266/webApi.cpp +++ b/tools/esp8266/webApi.cpp @@ -26,6 +26,8 @@ void webApi::setup(void) { mSrv->on("/api", HTTP_GET, std::bind(&webApi::onApi, this, std::placeholders::_1)); mSrv->on("/api", HTTP_POST, std::bind(&webApi::onApiPost, this, std::placeholders::_1)).onBody( std::bind(&webApi::onApiPostBody, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); + + mSrv->on("/get_setup", HTTP_GET, std::bind(&webApi::onDwnldSetup, this, std::placeholders::_1)); } @@ -117,6 +119,21 @@ void webApi::getNotFound(JsonObject obj, String url) { } +//----------------------------------------------------------------------------- +void webApi::onDwnldSetup(AsyncWebServerRequest *request) { + AsyncJsonResponse* response = new AsyncJsonResponse(false, 8192); + JsonObject root = response->getRoot(); + + getSetup(root); + + response->setLength(); + response->addHeader("Content-Type", "application/octet-stream"); + response->addHeader("Content-Description", "File Transfer"); + response->addHeader("Content-Disposition", "attachment; filename=ahoy_setup.json"); + request->send(response); +} + + //----------------------------------------------------------------------------- void webApi::getSystem(JsonObject obj) { obj[F("ssid")] = mSysCfg->stationSsid; @@ -396,6 +413,8 @@ bool webApi::setSetup(DynamicJsonDocument jsonIn, JsonObject jsonOut) { mApp->setTimestamp(jsonIn[F("ts")]); else if(F("sync_ntp") == jsonIn[F("cmd")]) mApp->setTimestamp(0); // 0: update ntp flag + else if(F("discovery_cfg") == jsonIn[F("cmd")]) + mApp->mFlagSendDiscoveryConfig = true; // for homeassistant else { jsonOut[F("error")] = F("unknown cmd"); return false; diff --git a/tools/esp8266/webApi.h b/tools/esp8266/webApi.h index e07e7d53..1d3caa17 100644 --- a/tools/esp8266/webApi.h +++ b/tools/esp8266/webApi.h @@ -26,6 +26,7 @@ class webApi { void onApiPost(AsyncWebServerRequest *request); void onApiPostBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total); void getNotFound(JsonObject obj, String url); + void onDwnldSetup(AsyncWebServerRequest *request); void getSystem(JsonObject obj); void getStatistics(JsonObject obj); @@ -36,6 +37,7 @@ class webApi { void getRadio(JsonObject obj); void getSerial(JsonObject obj); + void getIndex(JsonObject obj); void getSetup(JsonObject obj); void getLive(JsonObject obj);