Update to WebLog TimeServer Logic

Acquiring time from a timeserver is now spawned as a separate task running in the background.  This avoids blocking HomeSpan while setting the time.

Default wait time has been extended from 10 seconds to 2 minutes, since there are no problems with blocking.
This commit is contained in:
Gregg 2023-04-27 19:29:05 -05:00
parent 1f675f390f
commit beb79538e9
2 changed files with 18 additions and 15 deletions

View File

@ -543,10 +543,11 @@ void Span::checkConnect(){
mdns_service_txt_item_set("_hap","_tcp","logURL",webLog.statusURL.c_str()+4); // Web Log status (info only - NOT used by HAP) mdns_service_txt_item_set("_hap","_tcp","logURL",webLog.statusURL.c_str()+4); // Web Log status (info only - NOT used by HAP)
Serial.printf("Web Logging enabled at http://%s.local:%d%swith max number of entries=%d\n\n",hostName,tcpPortNum,webLog.statusURL.c_str()+4,webLog.maxEntries); Serial.printf("Web Logging enabled at http://%s.local:%d%swith max number of entries=%d\n\n",hostName,tcpPortNum,webLog.statusURL.c_str()+4,webLog.maxEntries);
webLog.initTime(); if(webLog.timeServer)
xTaskCreateUniversal(webLog.initTime, "timeSeverTaskHandle", 8096, &webLog, 1, NULL, 0);
} }
Serial.printf("Starting HAP Server on port %d supporting %d simultaneous HomeKit Controller Connections...\n",tcpPortNum,maxConnections); Serial.printf("Starting HAP Server on port %d supporting %d simultaneous HomeKit Controller Connections...\n\n",tcpPortNum,maxConnections);
hapServer->begin(); hapServer->begin();
@ -2182,20 +2183,22 @@ void SpanWebLog::init(uint16_t maxEntries, const char *serv, const char *tz, con
/////////////////////////////// ///////////////////////////////
void SpanWebLog::initTime(){ void SpanWebLog::initTime(void *args){
if(!timeServer) SpanWebLog *wLog = (SpanWebLog *)args;
return;
Serial.printf("Acquiring Time from %s (%s). Waiting %d second(s) for response... ",timeServer,timeZone,waitTime/1000); WEBLOG("Acquiring Time from %s (%s)",wLog->timeServer,wLog->timeZone,wLog->waitTime/1000);
configTzTime(timeZone,timeServer); configTzTime(wLog->timeZone,wLog->timeServer);
struct tm timeinfo; struct tm timeinfo;
if(getLocalTime(&timeinfo,waitTime)){ if(getLocalTime(&timeinfo,wLog->waitTime)){
strftime(bootTime,sizeof(bootTime),"%c",&timeinfo); strftime(wLog->bootTime,sizeof(wLog->bootTime),"%c",&timeinfo);
Serial.printf("%s\n\n",bootTime); wLog->timeInit=true;
timeInit=true; WEBLOG("Time Acquired: %s",wLog->bootTime);
} else { } else {
Serial.printf("Can't access Time Server - time-keeping not initialized!\n\n"); WEBLOG("Can't access Time Server after %d seconds",wLog->waitTime/1000);
} }
vTaskDelete(NULL);
} }
/////////////////////////////// ///////////////////////////////

View File

@ -150,7 +150,7 @@ struct SpanWebLog{ // optional web status/log data
boolean timeInit=false; // flag to indicate time has been initialized boolean timeInit=false; // flag to indicate time has been initialized
char bootTime[33]="Unknown"; // boot time char bootTime[33]="Unknown"; // boot time
String statusURL; // URL of status log String statusURL; // URL of status log
uint32_t waitTime=10000; // number of milliseconds to wait for initial connection to time server uint32_t waitTime=120000; // number of milliseconds to wait for initial connection to time server
String css=""; // optional user-defined style sheet for web log String css=""; // optional user-defined style sheet for web log
struct log_t { // log entry type struct log_t { // log entry type
@ -161,7 +161,7 @@ struct SpanWebLog{ // optional web status/log data
} *log=NULL; // array of log entries } *log=NULL; // array of log entries
void init(uint16_t maxEntries, const char *serv, const char *tz, const char *url); void init(uint16_t maxEntries, const char *serv, const char *tz, const char *url);
void initTime(); static void initTime(void *args);
void vLog(boolean sysMsg, const char *fmr, va_list ap); void vLog(boolean sysMsg, const char *fmr, va_list ap);
}; };