show uptime on overview page, moved labels from firmware to Web UI (#1543)

* show uptime on overview page, moved labels from firmware to Web UI

* show uptime on info page

* also use formated time in log

Co-authored-by: CaCO3 <caco@ruinelli.ch>
This commit is contained in:
CaCO3
2022-12-12 08:54:46 +01:00
committed by GitHub
parent b85e3b11a9
commit 6863e637aa
6 changed files with 101 additions and 15 deletions

View File

@@ -11,6 +11,7 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <math.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -792,6 +793,30 @@ string getResetReason(void) {
return reasonText; return reasonText;
} }
/**
* Returns the current uptime formated ad xxf xxh xxm [xxs]
*/
std::string getFormatedUptime(bool compact) {
char buf[20];
#pragma GCC diagnostic ignored "-Wformat-truncation"
int uptime = (uint32_t)(esp_timer_get_time()/1000/1000); // in seconds
int days = int(floor(uptime / (3600*24)));
int hours = int(floor((uptime - days * 3600*24) / (3600)));
int minutes = int(floor((uptime - days * 3600*24 - hours * 3600) / (60)));
int seconds = uptime - days * 3600*24 - hours * 3600 - minutes * 60;
if (compact) {
snprintf(buf, sizeof(buf), "%dd%02dh%02dm%02ds", days, hours, minutes, seconds);
}
else {
snprintf(buf, sizeof(buf), "%3dd %02dh %02dm %02ds", days, hours, minutes, seconds);
}
return std::string(buf);
}
const char* get404(void) { const char* get404(void) {
return return
"<pre>\n\n\n\n" "<pre>\n\n\n\n"

View File

@@ -64,5 +64,6 @@ string getSDCardSectorSize();
string getMac(void); string getMac(void);
string getResetReason(void); string getResetReason(void);
std::string getFormatedUptime(bool compact);
const char* get404(void); const char* get404(void);

View File

@@ -115,7 +115,7 @@ void ClassLogFile::WriteToDedicatedFile(std::string _fn, esp_log_level_t level,
{ {
FILE* pFile; FILE* pFile;
std::string zwtime; std::string zwtime;
std::string logline = ""; std::string ntpTime = "";
if (level > loglevel) {// Only write to file if loglevel is below threshold if (level > loglevel) {// Only write to file if loglevel is below threshold
return; return;
@@ -138,7 +138,7 @@ void ClassLogFile::WriteToDedicatedFile(std::string _fn, esp_log_level_t level,
strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo); strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
zwtime = std::string(buffer); zwtime = std::string(buffer);
logline = zwtime; ntpTime = zwtime;
} }
std::string loglevelString; std::string loglevelString;
@@ -163,11 +163,11 @@ void ClassLogFile::WriteToDedicatedFile(std::string _fn, esp_log_level_t level,
loglevelString = "NONE"; loglevelString = "NONE";
break; break;
} }
char uptime[20]; std::string formatedUptime = getFormatedUptime(true);
snprintf(uptime, sizeof(uptime), "%8d", (uint32_t)(esp_timer_get_time()/1000/1000)); // in seconds
logline = "[" + std::string(uptime) + "] " + logline + "\t<" + loglevelString + ">\t" + message + "\n"; ntpTime = "[" + formatedUptime + "] " + ntpTime + "\t<" + loglevelString + ">\t" + message + "\n";
fputs(logline.c_str(), pFile); fputs(ntpTime.c_str(), pFile);
fclose(pFile); fclose(pFile);
} else { } else {
ESP_LOGE(TAG, "Can't open log file %s", _fn.c_str()); ESP_LOGE(TAG, "Can't open log file %s", _fn.c_str());

View File

@@ -614,7 +614,7 @@ esp_err_t handler_cputemp(httpd_req_t *req)
const char* resp_str; const char* resp_str;
char cputemp[20]; char cputemp[20];
sprintf(cputemp, "CPU Temp: %4.1f°C", temperatureRead()); sprintf(cputemp, "%4.1f°C", temperatureRead());
resp_str = cputemp; resp_str = cputemp;
@@ -639,7 +639,7 @@ esp_err_t handler_rssi(httpd_req_t *req)
const char* resp_str; const char* resp_str;
char rssi[20]; char rssi[20];
sprintf(rssi, "RSSI: %idBm", get_WIFI_RSSI()); sprintf(rssi, "%idBm", get_WIFI_RSSI());
resp_str = rssi; resp_str = rssi;
@@ -655,6 +655,34 @@ esp_err_t handler_rssi(httpd_req_t *req)
return ESP_OK; return ESP_OK;
}; };
esp_err_t handler_uptime(httpd_req_t *req)
{
#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_uptime - Start");
#endif
const char* resp_str;
std::string formatedUptime = getFormatedUptime(false);
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, formatedUptime.c_str(), strlen(formatedUptime.c_str()));
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_uptime - End");
#endif
return ESP_OK;
}
esp_err_t handler_prevalue(httpd_req_t *req) esp_err_t handler_prevalue(httpd_req_t *req)
{ {
#ifdef DEBUG_DETAIL_ON #ifdef DEBUG_DETAIL_ON
@@ -721,6 +749,7 @@ void task_autodoFlow(void *pvParameter)
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Restarted due to an Exception/panic! Postponing first round start by 5 minutes to allow for an OTA or to fetch the log!"); LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Restarted due to an Exception/panic! Postponing first round start by 5 minutes to allow for an OTA or to fetch the log!");
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Setting logfile level to DEBUG until the next reboot!"); LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Setting logfile level to DEBUG until the next reboot!");
LogFile.setLogLevel(ESP_LOG_DEBUG); LogFile.setLogLevel(ESP_LOG_DEBUG);
//MQTTPublish(GetMQTTMainTopic() + "/" + "status", "Postponing first round", false);
vTaskDelay(60*5000 / portTICK_RATE_MS); // Wait 5 minutes to give time to do an OTA or fetch the log vTaskDelay(60*5000 / portTICK_RATE_MS); // Wait 5 minutes to give time to do an OTA or fetch the log
} }
@@ -873,6 +902,11 @@ void register_server_tflite_uri(httpd_handle_t server)
camuri.user_ctx = (void*) "Light Off"; camuri.user_ctx = (void*) "Light Off";
httpd_register_uri_handler(server, &camuri); httpd_register_uri_handler(server, &camuri);
camuri.uri = "/uptime";
camuri.handler = handler_uptime;
camuri.user_ctx = (void*) "Light Off";
httpd_register_uri_handler(server, &camuri);
camuri.uri = "/editflow"; camuri.uri = "/editflow";
camuri.handler = handler_editflow; camuri.handler = handler_editflow;
camuri.user_ctx = (void*) "EditFlow"; camuri.user_ctx = (void*) "EditFlow";

View File

@@ -37,6 +37,16 @@ div {
</div> </div>
</td> </td>
</tr> </tr>
<tr>
<td>
Uptime:
</td>
<td>
<div id="uptime">
<object data="/uptime"></object>
</div>
</td>
</tr>
</table> </table>
<h3>Build Info</h3> <h3>Build Info</h3>

View File

@@ -46,7 +46,7 @@
</td> </td>
</tr> </tr>
<tr> <tr>
<th class="th">Error:</th> <th class="th">Status:</th>
</tr> </tr>
<tr> <tr>
<td class="tg-2"> <td class="tg-2">
@@ -59,6 +59,7 @@
<div id="statusflow" ></div> <div id="statusflow" ></div>
<div id="cputemp" ></div> <div id="cputemp" ></div>
<div id="rssi" ></div> <div id="rssi" ></div>
<div id="uptime" ></div>
</td> </td>
</tr> </tr>
</table> </table>
@@ -87,6 +88,7 @@ function addZero(i) {
loadStatus(); loadStatus();
loadCPUTemp(); loadCPUTemp();
loadRSSI(); loadRSSI();
loadUptime();
refresh(); refresh();
}); });
@@ -117,11 +119,11 @@ function refresh() {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
var _rsp = xhttp.responseText; var _rsp = xhttp.responseText;
_rsp = "Status: " + _rsp; _rsp = "Status: " + _rsp;
$('#statusflow').html(_rsp); $('#statusflow').html("Status: " + _rsp);
} }
} }
xhttp.open("GET", url, true); xhttp.open("GET", url, true);
xhttp.send(); xhttp.send();
} }
function loadCPUTemp() { function loadCPUTemp() {
@@ -130,11 +132,11 @@ function refresh() {
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
var _rsp = xhttp.responseText; var _rsp = xhttp.responseText;
$('#cputemp').html(_rsp); $('#cputemp').html("CPU Temperature: " +_rsp);
} }
} }
xhttp.open("GET", url, true); xhttp.open("GET", url, true);
xhttp.send(); xhttp.send();
} }
function loadRSSI() { function loadRSSI() {
@@ -143,12 +145,25 @@ function refresh() {
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
var _rsp = xhttp.responseText; var _rsp = xhttp.responseText;
$('#rssi').html(_rsp); $('#rssi').html("RSSI: " + _rsp);
} }
} }
xhttp.open("GET", url, true); xhttp.open("GET", url, true);
xhttp.send(); xhttp.send();
} }
function loadUptime() {
url = basepath + '/uptime';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var _rsp = xhttp.responseText;
$('#uptime').html("Uptime: " + _rsp);
}
}
xhttp.open("GET", url, true);
xhttp.send();
}
function loadValue(_type, _div, _style) { function loadValue(_type, _div, _style) {
url = basepath + '/value?all=true&type=' + _type; url = basepath + '/value?all=true&type=' + _type;
@@ -199,6 +214,7 @@ function refresh() {
loadStatus(); loadStatus();
loadCPUTemp(); loadCPUTemp();
loadRSSI(); loadRSSI();
loadUptime();
} }
init(); init();