Added addLog(const char *) to SpanWebLog()

Also included logic so that clockTime is set to "Unknown" is addLog() is called prior to WiFi being established.

Next up: replace addLog(const char *) with a variadic set of parameters with dynamic storage allocation.
This commit is contained in:
Gregg 2022-03-05 10:40:01 -06:00
parent 168be05586
commit acc64f863a
3 changed files with 29 additions and 6 deletions

View File

@ -1256,6 +1256,16 @@ int HAPClient::putPrepareURL(char *json){
int HAPClient::getStatusURL(){ int HAPClient::getStatusURL(){
char clocktime[33];
if(homeSpan.webLog->timeInit){
struct tm timeinfo;
getLocalTime(&timeinfo,10);
strftime(clocktime,sizeof(clocktime),"%c",&timeinfo);
} else {
sprintf(clocktime,"Unknown");
}
char uptime[16]; char uptime[16];
int seconds=esp_timer_get_time()/1e6; int seconds=esp_timer_get_time()/1e6;
int secs=seconds%60; int secs=seconds%60;
@ -1268,13 +1278,14 @@ int HAPClient::getStatusURL(){
String response="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n"; String response="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
response+="<html><head><title>HomeSpan Status</title>\n"; response+="<html><head><title>HomeSpan Status</title>\n";
response+="<style>th, td {padding-right: 10px; border:1px solid black;}"; response+="<style>th, td {padding-right: 10px; padding-left: 10px; border:1px solid black;}";
response+="</style></head>\n"; response+="</style></head>\n";
response+="<body style=\"background-color:lightblue;\">\n"; response+="<body style=\"background-color:lightblue;\">\n";
response+="<p><b>" + String(homeSpan.displayName) + "</b></p>\n"; response+="<p><b>" + String(homeSpan.displayName) + "</b></p>\n";
response+="<table>\n"; response+="<table>\n";
response+="<tr><td>Up Time:</td><td>" + String(uptime) + "</td></tr>\n"; response+="<tr><td>Up Time:</td><td>" + String(uptime) + "</td></tr>\n";
response+="<tr><td>Current Time:</td><td>" + String(clocktime) + "</td></tr>\n";
response+="<tr><td>Boot Time:</td><td>" + String(homeSpan.webLog->bootTime) + "</td></tr>\n"; response+="<tr><td>Boot Time:</td><td>" + String(homeSpan.webLog->bootTime) + "</td></tr>\n";
response+="<tr><td>ESP32 Board:</td><td>" + String(ARDUINO_BOARD) + "</td></tr>\n"; response+="<tr><td>ESP32 Board:</td><td>" + String(ARDUINO_BOARD) + "</td></tr>\n";
response+="<tr><td>Arduino-ESP Version:</td><td>" + String(ARDUINO_ESP_VERSION) + "</td></tr>\n"; response+="<tr><td>Arduino-ESP Version:</td><td>" + String(ARDUINO_ESP_VERSION) + "</td></tr>\n";
@ -1292,9 +1303,21 @@ int HAPClient::getStatusURL(){
lastIndex=0; lastIndex=0;
for(int i=homeSpan.webLog->nEntries-1;i>=lastIndex;i--){ for(int i=homeSpan.webLog->nEntries-1;i>=lastIndex;i--){
response+="<tr><td>" + String(i+1) + "</td><td>" + "TBD1" "</td><td>" + "TBD2" + "</td><td>" + String(homeSpan.webLog->log[i%homeSpan.webLog->maxEntries].message) + "</td/tr>"; int index=i%homeSpan.webLog->maxEntries;
} seconds=homeSpan.webLog->log[index].upTime/1e6;
secs=seconds%60;
mins=(seconds/=60)%60;
hours=(seconds/=60)%24;
days=(seconds/=24);
sprintf(uptime,"%d:%02d:%02d:%02d",days,hours,mins,secs);
if(homeSpan.webLog->log[index].clockTime.tm_year>0)
strftime(clocktime,sizeof(clocktime),"%c",&homeSpan.webLog->log[index].clockTime);
else
sprintf(clocktime,"Unknown");
response+="<tr><td>" + String(i+1) + "</td><td>" + String(uptime) + "</td><td>" + String(clocktime) + "</td><td>" + String(homeSpan.webLog->log[index].message) + "</td/tr>";
}
response+="</table>\n"; response+="</table>\n";
} }

View File

@ -1965,9 +1965,9 @@ void SpanWebLog::addLog(const char *m){
int index=nEntries%maxEntries; int index=nEntries%maxEntries;
log[index].upTime=esp_timer_get_time()/1e6; log[index].upTime=esp_timer_get_time();
if(timeInit) if(timeInit)
getLocalTime(&log[index].clockTime); getLocalTime(&log[index].clockTime,10);
else else
log[index].clockTime.tm_year=0; log[index].clockTime.tm_year=0;
log[index].message=m; log[index].message=m;

View File

@ -105,7 +105,7 @@ struct SpanWebLog{ // optional web status/log data
String statusURL; // URL of status log String statusURL; // URL of status log
struct log_t { // log entry type struct log_t { // log entry type
uint32_t upTime; // number of seconds since booting uint64_t upTime; // number of seconds since booting
struct tm clockTime; // clock time struct tm clockTime; // clock time
const char *message; // pointers to log entries of arbitrary size const char *message; // pointers to log entries of arbitrary size
} *log=NULL; // array of log entries } *log=NULL; // array of log entries