Skip to content

Commit

Permalink
Use iso-8601 to set time but time zone like suggested by @terjeio for…
Browse files Browse the repository at this point in the history
… [ESP800/140]

Simplify string parsing to set date/time using strptime
  • Loading branch information
luc-github committed Sep 11, 2022
1 parent 3eabce6 commit e73b5e8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 74 deletions.
4 changes: 2 additions & 2 deletions docs/Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ The json format is {
`[ESP131]<port> json=<no> pwd=<admin password>`

* 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
`[ESP150]<delay=time in milliseconds><verbose=ON/OFF>pwd=<admin password>`
Expand Down Expand Up @@ -284,7 +284,7 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling
`[ESP790]<Action>=<path> json=<no> pwd=<admin password>`

* 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
`[ESP900]<ENABLE/DISABLE> json=<no> pwd=<admin/user password>`
Expand Down
4 changes: 2 additions & 2 deletions esp3d/src/core/espcmd/ESP0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const char * help[]= {"[ESP] (id) - display this help",
"[ESP131](Port) - display/set Telnet port",
#endif //TELNET_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
"[ESP150](delay=time) (verbose=ON/OFF)- display/set boot delay in ms / Verbose boot",
#if defined(WS_DATA_FEATURE)
Expand Down Expand Up @@ -147,7 +147,7 @@ const char * help[]= {"[ESP] (id) - display this help",
"[ESP780](path) - List Global Filesystem",
"[ESP790](Action)=(path) - rmdir / remove / mkdir / exists / create on Global Filesystem (path)",
#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
"[ESP900](ENABLE/DISABLE) - display/set serial state",
"[ESP901]<BAUD RATE> - display/set serial baud rate",
Expand Down
4 changes: 2 additions & 2 deletions esp3d/src/core/espcmd/ESP140.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
#include "../../modules/time/time_server.h"
#define COMMANDID 140
//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 noError = true;
bool json = has_tag (cmd_params, "json");
String response;
String response="ok";
String parameter;
bool hasParam = false;
int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead
Expand Down
2 changes: 1 addition & 1 deletion esp3d/src/core/espcmd/ESP800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
//get fw version firmare target and fw version
//eventually set time with pc time
//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 noError = true;
Expand Down
76 changes: 9 additions & 67 deletions esp3d/src/modules/time/time_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,81 +151,23 @@ const char * TimeServer::current_time(time_t t)
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)
{
String stmp = stime;
String substmp;
struct tm tmstruct;
struct timeval time_val = {0, 0};
int pos2;
//make uniform separators
stmp.replace("#","-");
stmp.replace(":","-");
//Search Year
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;
memset(&tmstruct, 0, sizeof(struct tm));
if (strptime(stime,"%Y-%m-%dT%H:%M:%S", &tmstruct)==nullptr) {
//allow not to set seconds for lazy guys typing command line
if (strptime(stime,"%Y-%m-%dT%H:%M", &tmstruct)==nullptr) {
return false;
}
}
tmstruct.tm_isdst = 0; //ignore dst
tmstruct.tm_sec = substmp.toInt();
time_val.tv_sec = mktime (&tmstruct);
//try to setTime
if(settimeofday(&time_val,0) == -1) {
return false;
}
Expand Down

0 comments on commit e73b5e8

Please sign in to comment.