diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index d8f6321e..61897e25 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -61,10 +61,14 @@ void app::loop(void) { } if(checkTicker(&mNtpRefreshTicker, mNtpRefreshInterval)) { - if(!apActive) { - mTimestamp = mWifi->getNtpTime(); - DPRINTLN(DBG_INFO, "[NTP]: " + getDateTimeStr(mTimestamp)); - } + if(!apActive) + mUpdateNtp = true; + } + + if(mUpdateNtp) { + mUpdateNtp = false; + mTimestamp = mWifi->getNtpTime(); + DPRINTLN(DBG_INFO, "[NTP]: " + getDateTimeStr(mTimestamp)); } if(mShouldReboot) { @@ -685,8 +689,9 @@ const char* app::getFieldStateClass(uint8_t fieldId) { //----------------------------------------------------------------------------- void app::resetSystem(void) { - mUptimeSecs = 0; - mPrevMillis = 0; + mUptimeSecs = 0; + mPrevMillis = 0; + mUpdateNtp = false; mNtpRefreshTicker = 0; mNtpRefreshInterval = NTP_REFRESH_INTERVAL; // [ms] diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 012c710b..fe92bb5e 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -112,6 +112,14 @@ class app { return mTimestamp; } + inline void setTimestamp(uint32_t newTime) { + DPRINTLN(DBG_DEBUG, F("setTimestamp: ") + String(newTime)); + if(0 == newTime) + mUpdateNtp = true; + else + mTimestamp = newTime; + } + void eraseSettings(bool all = false) { //DPRINTLN(DBG_VERBOSE, F("main.h:eraseSettings")); uint8_t buf[64]; @@ -239,6 +247,7 @@ class app { eep *mEep; uint32_t mTimestamp; + bool mUpdateNtp; bool mShowRebootRequest; @@ -257,7 +266,6 @@ class app { // timer uint32_t mTicker; - uint32_t mRxTicker; // mqtt diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index f866dad0..14aa1ac0 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 18 +#define VERSION_PATCH 19 //------------------------------------- diff --git a/tools/esp8266/html/setup.html b/tools/esp8266/html/setup.html index 7e67bfb9..0381e3fc 100644 --- a/tools/esp8266/html/setup.html +++ b/tools/esp8266/html/setup.html @@ -64,6 +64,10 @@ + + + + n/a @@ -106,7 +110,7 @@ - + @@ -128,6 +132,28 @@ ivHtml(JSON.parse('{"name":"","serial":"","channels":4,"ch_max_power":[0,0,0,0],"ch_name":["","","",""],"power_limit":1500,"power_limit_option":65535}'), highestId + 1); }); + function apiCb(obj) { + var e = document.getElementById("apiResult"); + if(obj["success"]) + e.innerHTML = "ok"; + else + e.innerHTML = "Error: " + obj["error"]; + } + + function setTime() { + var date = new Date(); + var obj = new Object(); + obj.cmd = "set_time"; + obj.ts = parseInt(date.getTime() / 1000); + getAjax("/api/setup", apiCb, "POST", JSON.stringify(obj)); + } + + function syncTime() { + var obj = new Object(); + obj.cmd = "sync_ntp"; + getAjax("/api/setup", apiCb, "POST", JSON.stringify(obj)); + } + function ivHtml(obj, id) { highestId = id; if(highestId == (maxInv - 1)) diff --git a/tools/esp8266/html/style.css b/tools/esp8266/html/style.css index 2998f3f8..258309bf 100644 --- a/tools/esp8266/html/style.css +++ b/tools/esp8266/html/style.css @@ -133,9 +133,8 @@ input.btn { background-color: #006ec0; color: #fff; border: 0px; - float: right; - margin: 10px 0px 30px 10px; padding: 7px 20px 7px 20px; + margin-bottom: 10px; text-transform: uppercase; cursor: pointer; } diff --git a/tools/esp8266/webApi.cpp b/tools/esp8266/webApi.cpp index 5461ab2a..398ce659 100644 --- a/tools/esp8266/webApi.cpp +++ b/tools/esp8266/webApi.cpp @@ -80,6 +80,8 @@ void webApi::onApiPostBody(AsyncWebServerRequest *request, uint8_t *data, size_t String path = request->url().substring(5); if(path == "ctrl") root[F("success")] = setCtrl(json, root); + else if(path == "setup") + root[F("success")] = setSetup(json, root); else { root[F("success")] = false; root[F("error")] = "Path not found: " + path; @@ -381,7 +383,24 @@ bool webApi::setCtrl(DynamicJsonDocument jsonIn, JsonObject jsonOut) { } } else { - jsonOut["error"] = "unknown 'tx_request'"; + jsonOut[F("error")] = F("unknown 'tx_request'"); + return false; + } + + return true; +} + + +//----------------------------------------------------------------------------- +bool webApi::setSetup(DynamicJsonDocument jsonIn, JsonObject jsonOut) { + if(F("set_time") == jsonIn[F("cmd")]) { + mApp->setTimestamp(jsonIn[F("ts")]); + } + else if(F("sync_ntp") == jsonIn[F("cmd")]) { + mApp->setTimestamp(0); // 0: update ntp flag + } + else { + jsonOut[F("error")] = F("unknown cmd"); return false; } @@ -394,6 +413,6 @@ Inverter<> *webApi::getInverter(DynamicJsonDocument jsonIn, JsonObject jsonOut) uint8_t id = jsonIn[F("inverter")]; Inverter<> *iv = mApp->mSys->getInverterByPos(id); if(NULL == iv) - jsonOut["error"] = F("inverter index to high: ") + String(id); + jsonOut[F("error")] = F("inverter index to high: ") + String(id); return iv; } diff --git a/tools/esp8266/webApi.h b/tools/esp8266/webApi.h index cd42ab30..e07e7d53 100644 --- a/tools/esp8266/webApi.h +++ b/tools/esp8266/webApi.h @@ -42,6 +42,7 @@ class webApi { void getRecord(JsonObject obj, record_t<> *rec); bool setCtrl(DynamicJsonDocument jsonIn, JsonObject jsonOut); + bool setSetup(DynamicJsonDocument jsonIn, JsonObject jsonOut); Inverter<> *getInverter(DynamicJsonDocument jsonIn, JsonObject jsonOut);