Use iso-8601 to set time but time zone like suggested by @terjeio for [ESP800/140]

Simplify string parsing to set date/time using strptime
This commit is contained in:
Luc 2022-09-11 16:37:44 +08:00
parent 3eabce6204
commit e73b5e85c8
5 changed files with 16 additions and 74 deletions

View File

@ -71,7 +71,7 @@ The json format is {
`[ESP131]<port> json=<no> pwd=<admin password>` `[ESP131]<port> json=<no> pwd=<admin password>`
* Sync / Set / Get current time * Sync / Set / Get current time
`[ESP140]<SYNC> <srv1=XXXXX> <srv2=XXXXX> <srv3=XXXXX> <zone=xxx> <dst=YES/NO> <time=YYYY-MM-DD#H24:MM:SS> <NOW> json=<no> pwd=<admin password>` `[ESP140]<SYNC> <srv1=XXXXX> <srv2=XXXXX> <srv3=XXXXX> <zone=xxx> <dst=YES/NO> <time=YYYY-MM-DDTHH:mm:ss> <NOW> json=<no> pwd=<admin password>`
* Get/Set display/set boot delay in ms / Verbose boot * Get/Set display/set boot delay in ms / Verbose boot
`[ESP150]<delay=time in milliseconds><verbose=ON/OFF>pwd=<admin password>` `[ESP150]<delay=time in milliseconds><verbose=ON/OFF>pwd=<admin password>`
@ -284,7 +284,7 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling
`[ESP790]<Action>=<path> json=<no> pwd=<admin password>` `[ESP790]<Action>=<path> json=<no> pwd=<admin password>`
* FW Informations * FW Informations
`[ESP800]json=<no> pwd=<admin password>` `[ESP800]json=<no> pwd=<admin password> <time=YYYY-MM-DDTHH:mm:ss> <version=3.0.0-a11> <setup=0/1>`
* Get state / Set Enable / Disable Serial Communication * Get state / Set Enable / Disable Serial Communication
`[ESP900]<ENABLE/DISABLE> json=<no> pwd=<admin/user password>` `[ESP900]<ENABLE/DISABLE> json=<no> pwd=<admin/user password>`

View File

@ -59,7 +59,7 @@ const char * help[]= {"[ESP] (id) - display this help",
"[ESP131](Port) - display/set Telnet port", "[ESP131](Port) - display/set Telnet port",
#endif //TELNET_FEATURE #endif //TELNET_FEATURE
#if defined(TIMESTAMP_FEATURE) #if defined(TIMESTAMP_FEATURE)
"[ESP140](SYNC) (srv1=xxxx) (srv2=xxxx) (srv3=xxxx) (zone=xxx) (dst=YES/NO) (time=YYYY-MM-DD#H24:MM:SS) (SYNC) (NOW)- sync/display/set current time/time servers", "[ESP140](SYNC) (srv1=xxxx) (srv2=xxxx) (srv3=xxxx) (zone=xxx) (dst=YES/NO) (time=YYYY-MM-DDTHH:mm:ss) (SYNC) (NOW)- sync/display/set current time/time servers",
#endif //TIMESTAMP_FEATURE #endif //TIMESTAMP_FEATURE
"[ESP150](delay=time) (verbose=ON/OFF)- display/set boot delay in ms / Verbose boot", "[ESP150](delay=time) (verbose=ON/OFF)- display/set boot delay in ms / Verbose boot",
#if defined(WS_DATA_FEATURE) #if defined(WS_DATA_FEATURE)
@ -147,7 +147,7 @@ const char * help[]= {"[ESP] (id) - display this help",
"[ESP780](path) - List Global Filesystem", "[ESP780](path) - List Global Filesystem",
"[ESP790](Action)=(path) - rmdir / remove / mkdir / exists / create on Global Filesystem (path)", "[ESP790](Action)=(path) - rmdir / remove / mkdir / exists / create on Global Filesystem (path)",
#endif //GLOBAL_FILESYSTEM_FEATURE #endif //GLOBAL_FILESYSTEM_FEATURE
"[ESP800](time=YYYY-MM-DD-HH-MM-SS) - display FW Informations in plain/JSON", "[ESP800](time=YYYY-MM-DDTHH:mm:ss)(version=3.0.0-a11)(setup=0/1) - display FW Informations /set time",
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL #if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
"[ESP900](ENABLE/DISABLE) - display/set serial state", "[ESP900](ENABLE/DISABLE) - display/set serial state",
"[ESP901]<BAUD RATE> - display/set serial baud rate", "[ESP901]<BAUD RATE> - display/set serial baud rate",

View File

@ -26,12 +26,12 @@
#include "../../modules/time/time_server.h" #include "../../modules/time/time_server.h"
#define COMMANDID 140 #define COMMANDID 140
//Sync / Set / Get current time //Sync / Set / Get current time
//[ESP140]<SYNC> <srv1=XXXXX> <srv2=XXXXX> <srv3=XXXXX> <zone=xxx> <dst=YES/NO> <time=YYYY-MM-DD#H24:MM:SS> NOW json=<no> pwd=<admin password> //[ESP140]<SYNC> <srv1=XXXXX> <srv2=XXXXX> <srv3=XXXXX> <zone=xxx> <dst=YES/NO> <time=YYYY-MM-DDTHH:mm:ss> NOW json=<no> pwd=<admin password>
bool Commands::ESP140(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) bool Commands::ESP140(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
{ {
bool noError = true; bool noError = true;
bool json = has_tag (cmd_params, "json"); bool json = has_tag (cmd_params, "json");
String response; String response="ok";
String parameter; String parameter;
bool hasParam = false; bool hasParam = false;
int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead

View File

@ -46,7 +46,7 @@
//get fw version firmare target and fw version //get fw version firmare target and fw version
//eventually set time with pc time //eventually set time with pc time
//output is JSON or plain text according parameter //output is JSON or plain text according parameter
//[ESP800]json=<no><time=YYYY-MM-DD-HH-MM-SS> //[ESP800]json=<no><time=YYYY-MM-DDTHH:mm:ss> <version=3.0.0-a11> <setup=0/1>
bool Commands::ESP800(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) bool Commands::ESP800(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
{ {
bool noError = true; bool noError = true;

View File

@ -151,81 +151,23 @@ const char * TimeServer::current_time(time_t t)
return stmp.c_str(); return stmp.c_str();
} }
//the string date time need to be iso-8601
//the time zone part will be ignored
bool TimeServer::setTime(const char* stime) bool TimeServer::setTime(const char* stime)
{ {
String stmp = stime; String stmp = stime;
String substmp;
struct tm tmstruct; struct tm tmstruct;
struct timeval time_val = {0, 0}; struct timeval time_val = {0, 0};
int pos2; memset(&tmstruct, 0, sizeof(struct tm));
//make uniform separators if (strptime(stime,"%Y-%m-%dT%H:%M:%S", &tmstruct)==nullptr) {
stmp.replace("#","-"); //allow not to set seconds for lazy guys typing command line
stmp.replace(":","-"); if (strptime(stime,"%Y-%m-%dT%H:%M", &tmstruct)==nullptr) {
//Search Year return false;
int pos = stmp.indexOf("-"); }
if (pos == -1) {
return false;
}
substmp = stmp.substring(0,pos);
if (substmp.length()!=4) {
return false;
}
pos2=pos;
tmstruct.tm_year = substmp.toInt() - 1900;
//Search Month
pos = stmp.indexOf("-",pos2+1);
if (pos == -1) {
return false;
}
substmp = stmp.substring(pos2+1,pos);
if ((substmp.toInt() > 11) || (substmp.toInt() <0 )) {
return false;
}
pos2=pos;
tmstruct.tm_mon = substmp.toInt() - 1;
//Search day
pos = stmp.indexOf("-",pos2+1);
if (pos == -1) {
return false;
}
substmp = stmp.substring(pos2+1,pos);
if ((substmp.toInt() > 31) || (substmp.toInt() <1 )) {
return false;
}
pos2=pos;
tmstruct.tm_mday = substmp.toInt();
//Search hour
pos = stmp.indexOf("-", pos2+1);
if (pos == -1) {
return false;
}
substmp = stmp.substring(pos2+1,pos);
if ((substmp.toInt() > 23) || (substmp.toInt() <0 )) {
return false;
}
pos2=pos;
tmstruct.tm_hour = substmp.toInt();
//Search min
pos = stmp.indexOf("-", pos2+1);
if (pos == -1) {
return false;
}
substmp = stmp.substring(pos2+1,pos);
if ((substmp.toInt() > 59) || (substmp.toInt() < 0 )) {
return false;
}
tmstruct.tm_min = substmp.toInt();
//Search sec
substmp = stmp.substring(pos+1);
if ((substmp.toInt() > 59) || (substmp.toInt() < 0 )) {
return false;
} }
tmstruct.tm_isdst = 0; //ignore dst tmstruct.tm_isdst = 0; //ignore dst
tmstruct.tm_sec = substmp.toInt();
time_val.tv_sec = mktime (&tmstruct); time_val.tv_sec = mktime (&tmstruct);
//try to setTime
if(settimeofday(&time_val,0) == -1) { if(settimeofday(&time_val,0) == -1) {
return false; return false;
} }