Browse Source

fix #338, needs verifcation from homeassistant users

added download link for settings on setup.html and update.html
pull/357/head
lumapu 2 years ago
parent
commit
a595950d7d
  1. 45
      tools/esp8266/app.cpp
  2. 5
      tools/esp8266/app.h
  3. 31
      tools/esp8266/html/setup.html
  4. 4
      tools/esp8266/html/update.html
  5. 19
      tools/esp8266/webApi.cpp
  6. 2
      tools/esp8266/webApi.h

45
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<JsonObject>();
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]

5
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);

31
tools/esp8266/html/setup.html

@ -67,7 +67,7 @@
<label for="ntpBtn">set system time</label>
<input type="button" name="ntpBtn" id="ntpBtn" class="btn" value="from browser" onclick="setTime()"/>
<input type="button" name="ntpSync" id="ntpSync" class="btn" value="sync NTP" onclick="syncTime()"/>
<span id="apiResult">n/a</span>
<span id="apiResultNtp"></span>
</fieldset>
</div>
@ -85,6 +85,9 @@
<input type="password" class="text" name="mqttPwd"/>
<label for="mqttTopic">Topic</label>
<input type="text" class="text" name="mqttTopic"/>
<label for="mqttBtn">Discovery Config (homeassistant)</label>
<input type="button" name="mqttDiscovery" id="mqttDiscovery" class="btn" value="send" onclick="sendDiscoveryConfig()"/>
<span id="apiResultMqtt"></span>
</fieldset>
</div>
@ -110,7 +113,9 @@
<label for="reboot">Reboot device after successful save</label>
<input type="checkbox" class="cb" name="reboot"/>
<input type="submit" value="save" class="btn right"/>
<input type="submit" value="save" class="btn right"/><br/>
<br/>
<a href="/get_setup" target="_blank">Download your settings (JSON file)</a> (only saved values)
</form>
</div>
</div>
@ -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) {

4
tools/esp8266/html/update.html

@ -10,7 +10,9 @@
<h1>Update</h1>
<div id="content" class="content">
<div>
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.<br/>
<br/>
<a href="/get_setup" target="_blank">Download your settings (JSON file)</a>
</div>
<br/><br/>
<form method="POST" action="/update" enctype="multipart/form-data" accept-charset="utf-8">

19
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;

2
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);

Loading…
Cancel
Save