Add property Downtime

Add property Downtime to state and status 11 message representing the duration of wifi connection loss
This commit is contained in:
Theo Arends 2019-02-18 18:02:22 +01:00
parent 4884a94b6d
commit 3c3b5bb8ca
5 changed files with 42 additions and 27 deletions

View File

@ -7,6 +7,7 @@
* Remove command SetOption62 as it's functionality is replaced by user changing the device template (#5255)
* Add property LinkCount to state and status 11 message representing number of Wifi Link re-connections
* Add property MqttCount to status 6 message representing number of Mqtt re-connections
* Add property Downtime to state and status 11 message representing the duration of wifi connection loss
*
* 6.4.1.16 20190211
* Initial support for online template change using command Template or GUI Configure Other (#5177)

View File

@ -54,6 +54,7 @@
#define D_JSON_DISTANCE "Distance"
#define D_JSON_DNSSERVER "DNSServer"
#define D_JSON_DONE "Done"
#define D_JSON_DOWNTIME "Downtime"
#define D_JSON_ECO2 "eCO2"
#define D_JSON_EMPTY "Empty"
#define D_JSON_ENDDST "EndDST" // End Daylight Savings Time

View File

@ -1775,8 +1775,8 @@ void MqttShowState(void)
MqttShowPWMState();
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_WIFI "\":{\"" D_JSON_AP "\":%d,\"" D_JSON_SSID "\":\"%s\",\"" D_JSON_BSSID "\":\"%s\",\"" D_JSON_CHANNEL "\":%d,\"" D_JSON_RSSI "\":%d,\"" D_JSON_LINK_COUNT "\":%d}}"),
mqtt_data, Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active], WiFi.BSSIDstr().c_str(), WiFi.channel(), WifiGetRssiAsQuality(WiFi.RSSI()), WifiLinkCount());
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_WIFI "\":{\"" D_JSON_AP "\":%d,\"" D_JSON_SSID "\":\"%s\",\"" D_JSON_BSSID "\":\"%s\",\"" D_JSON_CHANNEL "\":%d,\"" D_JSON_RSSI "\":%d,\"" D_JSON_LINK_COUNT "\":%d,\"" D_JSON_DOWNTIME "\":\"%s\"}}"),
mqtt_data, Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active], WiFi.BSSIDstr().c_str(), WiFi.channel(), WifiGetRssiAsQuality(WiFi.RSSI()), WifiLinkCount(), WifiDowntime().c_str());
}
bool MqttShowSensor(void)

View File

@ -87,6 +87,23 @@ String GetTimeZone(void)
return String(tz); // -03:45
}
String GetDuration(uint32_t time)
{
char dt[16];
TIME_T ut;
BreakTime(time, ut);
// "P128DT14H35M44S" - ISO8601:2004 - https://en.wikipedia.org/wiki/ISO_8601 Durations
// snprintf_P(dt, sizeof(dt), PSTR("P%dDT%02dH%02dM%02dS"), ut.days, ut.hour, ut.minute, ut.second);
// "128 14:35:44" - OpenVMS
// "128T14:35:44" - Tasmota
snprintf_P(dt, sizeof(dt), PSTR("%dT%02d:%02d:%02d"), ut.days, ut.hour, ut.minute, ut.second);
return String(dt); // 128T14:35:44
}
String GetDT(uint32_t time)
{
// "2017-03-07T11:08:02" - ISO8601:2004
@ -155,37 +172,24 @@ String GetTime(int type)
return String(stime); // Thu Nov 01 11:41:02 2018
}
uint32_t UpTime(void)
{
if (restart_time) {
return utc_time - restart_time;
} else {
return uptime;
}
}
String GetUptime(void)
{
char dt[16];
TIME_T ut;
if (restart_time) {
BreakTime(utc_time - restart_time, ut);
} else {
BreakTime(uptime, ut);
}
// "P128DT14H35M44S" - ISO8601:2004 - https://en.wikipedia.org/wiki/ISO_8601 Durations
// snprintf_P(dt, sizeof(dt), PSTR("P%dDT%02dH%02dM%02dS"), ut.days, ut.hour, ut.minute, ut.second);
// "128 14:35:44" - OpenVMS
// "128T14:35:44" - Tasmota
snprintf_P(dt, sizeof(dt), PSTR("%dT%02d:%02d:%02d"), ut.days, ut.hour, ut.minute, ut.second);
return String(dt); // 128T14:35:44
return GetDuration(UpTime());
}
uint32_t GetMinutesUptime(void)
{
TIME_T ut;
if (restart_time) {
BreakTime(utc_time - restart_time, ut);
} else {
BreakTime(uptime, ut);
}
BreakTime(UpTime(), ut);
return (ut.days *1440) + (ut.hour *60) + ut.minute;
}

View File

@ -49,7 +49,9 @@ using namespace axTLS;
*/
#include <ESP8266WiFi.h> // Wifi, MQTT, Ota, WifiManager
uint16_t wifi_link_count = 0;
uint32_t wifi_last_event = 0; // Last wifi connection event
uint32_t wifi_downtime = 0; // Wifi down duration
uint16_t wifi_link_count = 0; // Number of wifi re-connect
uint8_t wifi_counter;
uint8_t wifi_retry_init;
uint8_t wifi_retry;
@ -346,14 +348,21 @@ uint16_t WifiLinkCount()
return wifi_link_count;
}
String WifiDowntime()
{
return GetDuration(wifi_downtime);
}
void WifiSetState(uint8_t state)
{
if (state == global_state.wifi_down) {
if (state) {
rules_flag.wifi_connected = 1;
wifi_link_count++;
wifi_downtime += UpTime() - wifi_last_event;
} else {
rules_flag.wifi_disconnected = 1;
wifi_last_event = UpTime();
}
}
global_state.wifi_down = state ^1;