Browse Source

implemented #336 set time manually or resync NTP

pull/317/head
lumapu 2 years ago
parent
commit
e3532f4464
  1. 17
      tools/esp8266/app.cpp
  2. 10
      tools/esp8266/app.h
  3. 2
      tools/esp8266/defines.h
  4. 28
      tools/esp8266/html/setup.html
  5. 3
      tools/esp8266/html/style.css
  6. 23
      tools/esp8266/webApi.cpp
  7. 1
      tools/esp8266/webApi.h

17
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]

10
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

2
tools/esp8266/defines.h

@ -13,7 +13,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 5
#define VERSION_PATCH 18
#define VERSION_PATCH 19
//-------------------------------------

28
tools/esp8266/html/setup.html

@ -64,6 +64,10 @@
<input type="text" class="text" name="ntpAddr"/>
<label for="ntpPort">NTP Port</label>
<input type="text" class="text" name="ntpPort"/>
<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>
</fieldset>
</div>
@ -106,7 +110,7 @@
<label for="reboot">Reboot device after successful save</label>
<input type="checkbox" class="cb" name="reboot"/>
<input type="submit" value="save" class="btn"/>
<input type="submit" value="save" class="btn right"/>
</form>
</div>
</div>
@ -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))

3
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;
}

23
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;
}

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

Loading…
Cancel
Save