Skip to content

Commit e73b5e8

Browse files
committed
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
1 parent 3eabce6 commit e73b5e8

File tree

5 files changed

+16
-74
lines changed

5 files changed

+16
-74
lines changed

docs/Commands.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The json format is {
7171
`[ESP131]<port> json=<no> pwd=<admin password>`
7272

7373
* Sync / Set / Get current time
74-
`[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>`
74+
`[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>`
7575

7676
* Get/Set display/set boot delay in ms / Verbose boot
7777
`[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
284284
`[ESP790]<Action>=<path> json=<no> pwd=<admin password>`
285285

286286
* FW Informations
287-
`[ESP800]json=<no> pwd=<admin password>`
287+
`[ESP800]json=<no> pwd=<admin password> <time=YYYY-MM-DDTHH:mm:ss> <version=3.0.0-a11> <setup=0/1>`
288288

289289
* Get state / Set Enable / Disable Serial Communication
290290
`[ESP900]<ENABLE/DISABLE> json=<no> pwd=<admin/user password>`

esp3d/src/core/espcmd/ESP0.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const char * help[]= {"[ESP] (id) - display this help",
5959
"[ESP131](Port) - display/set Telnet port",
6060
#endif //TELNET_FEATURE
6161
#if defined(TIMESTAMP_FEATURE)
62-
"[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",
62+
"[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",
6363
#endif //TIMESTAMP_FEATURE
6464
"[ESP150](delay=time) (verbose=ON/OFF)- display/set boot delay in ms / Verbose boot",
6565
#if defined(WS_DATA_FEATURE)
@@ -147,7 +147,7 @@ const char * help[]= {"[ESP] (id) - display this help",
147147
"[ESP780](path) - List Global Filesystem",
148148
"[ESP790](Action)=(path) - rmdir / remove / mkdir / exists / create on Global Filesystem (path)",
149149
#endif //GLOBAL_FILESYSTEM_FEATURE
150-
"[ESP800](time=YYYY-MM-DD-HH-MM-SS) - display FW Informations in plain/JSON",
150+
"[ESP800](time=YYYY-MM-DDTHH:mm:ss)(version=3.0.0-a11)(setup=0/1) - display FW Informations /set time",
151151
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
152152
"[ESP900](ENABLE/DISABLE) - display/set serial state",
153153
"[ESP901]<BAUD RATE> - display/set serial baud rate",

esp3d/src/core/espcmd/ESP140.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
#include "../../modules/time/time_server.h"
2727
#define COMMANDID 140
2828
//Sync / Set / Get current time
29-
//[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>
29+
//[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>
3030
bool Commands::ESP140(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
3131
{
3232
bool noError = true;
3333
bool json = has_tag (cmd_params, "json");
34-
String response;
34+
String response="ok";
3535
String parameter;
3636
bool hasParam = false;
3737
int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead

esp3d/src/core/espcmd/ESP800.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
//get fw version firmare target and fw version
4747
//eventually set time with pc time
4848
//output is JSON or plain text according parameter
49-
//[ESP800]json=<no><time=YYYY-MM-DD-HH-MM-SS>
49+
//[ESP800]json=<no><time=YYYY-MM-DDTHH:mm:ss> <version=3.0.0-a11> <setup=0/1>
5050
bool Commands::ESP800(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
5151
{
5252
bool noError = true;

esp3d/src/modules/time/time_server.cpp

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -151,81 +151,23 @@ const char * TimeServer::current_time(time_t t)
151151
return stmp.c_str();
152152
}
153153

154+
//the string date time need to be iso-8601
155+
//the time zone part will be ignored
154156
bool TimeServer::setTime(const char* stime)
155157
{
156158
String stmp = stime;
157-
String substmp;
158159
struct tm tmstruct;
159160
struct timeval time_val = {0, 0};
160-
int pos2;
161-
//make uniform separators
162-
stmp.replace("#","-");
163-
stmp.replace(":","-");
164-
//Search Year
165-
int pos = stmp.indexOf("-");
166-
if (pos == -1) {
167-
return false;
168-
}
169-
substmp = stmp.substring(0,pos);
170-
if (substmp.length()!=4) {
171-
return false;
172-
}
173-
pos2=pos;
174-
tmstruct.tm_year = substmp.toInt() - 1900;
175-
//Search Month
176-
pos = stmp.indexOf("-",pos2+1);
177-
if (pos == -1) {
178-
return false;
179-
}
180-
substmp = stmp.substring(pos2+1,pos);
181-
if ((substmp.toInt() > 11) || (substmp.toInt() <0 )) {
182-
return false;
183-
}
184-
pos2=pos;
185-
tmstruct.tm_mon = substmp.toInt() - 1;
186-
//Search day
187-
pos = stmp.indexOf("-",pos2+1);
188-
if (pos == -1) {
189-
return false;
190-
}
191-
substmp = stmp.substring(pos2+1,pos);
192-
if ((substmp.toInt() > 31) || (substmp.toInt() <1 )) {
193-
return false;
194-
}
195-
pos2=pos;
196-
tmstruct.tm_mday = substmp.toInt();
197-
198-
//Search hour
199-
pos = stmp.indexOf("-", pos2+1);
200-
if (pos == -1) {
201-
return false;
202-
}
203-
substmp = stmp.substring(pos2+1,pos);
204-
if ((substmp.toInt() > 23) || (substmp.toInt() <0 )) {
205-
206-
return false;
207-
}
208-
pos2=pos;
209-
tmstruct.tm_hour = substmp.toInt();
210-
211-
//Search min
212-
pos = stmp.indexOf("-", pos2+1);
213-
if (pos == -1) {
214-
return false;
215-
}
216-
substmp = stmp.substring(pos2+1,pos);
217-
if ((substmp.toInt() > 59) || (substmp.toInt() < 0 )) {
218-
return false;
219-
}
220-
tmstruct.tm_min = substmp.toInt();
221-
//Search sec
222-
substmp = stmp.substring(pos+1);
223-
if ((substmp.toInt() > 59) || (substmp.toInt() < 0 )) {
224-
return false;
161+
memset(&tmstruct, 0, sizeof(struct tm));
162+
if (strptime(stime,"%Y-%m-%dT%H:%M:%S", &tmstruct)==nullptr) {
163+
//allow not to set seconds for lazy guys typing command line
164+
if (strptime(stime,"%Y-%m-%dT%H:%M", &tmstruct)==nullptr) {
165+
return false;
166+
}
225167
}
226168
tmstruct.tm_isdst = 0; //ignore dst
227-
tmstruct.tm_sec = substmp.toInt();
228169
time_val.tv_sec = mktime (&tmstruct);
170+
//try to setTime
229171
if(settimeofday(&time_val,0) == -1) {
230172
return false;
231173
}

0 commit comments

Comments
 (0)