ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
time_server.cpp
Go to the documentation of this file.
1 /*
2  time_server.cpp - time server functions class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #include "../../include/esp3d_config.h"
22 #ifdef TIMESTAMP_FEATURE
23 #include "time_server.h"
24 #include <time.h>
25 #include "../../core/settings_esp3d.h"
26 #include "../../core/esp3doutput.h"
27 #if defined (WIFI_FEATURE)
28 #include "../wifi/wificonfig.h"
29 #endif //WIFI_FEATURE
30 #if defined (BLUETOOTH_FEATURE)
31 #include "../bluetooth/BT_service.h"
32 #endif //BLUETOOTH_FEATURE
33 #if defined (ETH_FEATURE)
34 #include "../ethernet/ethconfig.h"
35 #endif //ETH_FEATURE
36 
38 
40 {
41  _started = false;
42  _is_internet_time = false;
43 }
45 {
46  end();
47 }
48 
49 bool TimeServer::is_internet_time(bool readfromsettings)
50 {
51  if (readfromsettings) {
52  _is_internet_time = (Settings_ESP3D::read_byte (ESP_INTERNET_TIME) == 0);
53  }
54  return _is_internet_time;
55 }
56 
57 bool TimeServer::begin()
58 {
59  bool res = true;
60  end();
61  String s1, s2, s3;
62  int8_t t1;
63  byte d1;
64 #if defined (WIFI_FEATURE)
65  //no time server in AP mode
66  if (WiFi.getMode() == WIFI_AP) {
67  return false;
68  }
69 #endif //WIFI_FEATURE
70 #if defined (BLUETOOTH_FEATURE)
71  //no time server in BT
72  if (bt_service.started()) {
73  return false;
74  }
75 #endif //BLUETOOTH_FEATURE
76 
77 #if defined (ETH_FEATURE)
78  if (!EthConfig::started()) {
79 #if defined (WIFI_FEATURE)
80  //no time server if no ETH started and no WiFi started
81  if (WiFi.getMode() == WIFI_OFF) {
82  return false;
83  }
84 #else
85  //no time server if no ETH started and no WiFi
86  return false;
87 #endif //WIFI_FEATURE
88  }
89 #endif //ETH_FEATURE
90  if (!is_internet_time()) {
91  return false;
92  }
98  configTime (3600 * (t1), d1 * 3600, s1.c_str(), s2.c_str(), s3.c_str() );
99  time_t now = time(nullptr);
100  int nb = 0;
101  while ((now < (8 * 3600 * 2)) && (nb < 20)) {
102  yield();
103  delay(500);
104  nb++;
105  now = time(nullptr);
106  }
107  if (now < (8 * 3600 * 2)) {
108  res = false;
109  }
110  if (!res) {
111  end();
112  }
113  _started = res;
114  return _started;
115 }
116 
117 const char * TimeServer::current_time(time_t t)
118 {
119  static String stmp;
120  struct tm tmstruct;
121  time_t now;
122  stmp = "";
123  //get current time
124  if (t == 0) {
125  time(&now);
126  localtime_r(&now, &tmstruct);
127  } else {
128  /* struct tm * tmstructtmp = localtime(&t);
129  tmstruct.tm_year = tmstructtmp->tm_year;
130  tmstruct.tm_mon = tmstructtmp->tm_mon;
131  tmstruct.tm_mday = tmstructtmp->tm_mday;
132  tmstruct.tm_hour = tmstructtmp->tm_hour;
133  tmstruct.tm_min = tmstructtmp->tm_min;
134  tmstruct.tm_sec = tmstructtmp->tm_sec;*/
135  localtime_r(&t, &tmstruct);
136  }
137  stmp = String((tmstruct.tm_year)+1900) + "-";
138  if (((tmstruct.tm_mon)+1) < 10) {
139  stmp +="0";
140  }
141  stmp += String(( tmstruct.tm_mon)+1) + "-";
142  if (tmstruct.tm_mday < 10) {
143  stmp +="0";
144  }
145  stmp += String(tmstruct.tm_mday) + " ";
146  if (tmstruct.tm_hour < 10) {
147  stmp +="0";
148  }
149  stmp += String(tmstruct.tm_hour) + ":";
150  if (tmstruct.tm_min < 10) {
151  stmp +="0";
152  }
153  stmp += String(tmstruct.tm_min) + ":";
154  if (tmstruct.tm_sec < 10) {
155  stmp +="0";
156  }
157  stmp += String(tmstruct.tm_sec);
158  return stmp.c_str();
159 }
160 
161 bool TimeServer::setTime(const char* stime)
162 {
163  String stmp = stime;
164  String substmp;
165  struct tm tmstruct;
166  struct timeval time_val = {0, 0};
167  int pos2;
168  //make uniform separators
169  stmp.replace("#","-");
170  stmp.replace(":","-");
171  //Search Year
172  int pos = stmp.indexOf("-");
173  if (pos == -1) {
174  return false;
175  }
176  substmp = stmp.substring(0,pos);
177  if (substmp.length()!=4) {
178  return false;
179  }
180  pos2=pos;
181  tmstruct.tm_year = substmp.toInt() - 1900;
182  //Search Month
183  pos = stmp.indexOf("-",pos2+1);
184  if (pos == -1) {
185  return false;
186  }
187  substmp = stmp.substring(pos2+1,pos);
188  if ((substmp.toInt() > 11) || (substmp.toInt() <0 )) {
189  return false;
190  }
191  pos2=pos;
192  tmstruct.tm_mon = substmp.toInt() - 1;
193  //Search day
194  pos = stmp.indexOf("-",pos2+1);
195  if (pos == -1) {
196  return false;
197  }
198  substmp = stmp.substring(pos2+1,pos);
199  if ((substmp.toInt() > 31) || (substmp.toInt() <1 )) {
200  return false;
201  }
202  pos2=pos;
203  tmstruct.tm_mday = substmp.toInt();
204 
205  //Search hour
206  pos = stmp.indexOf("-", pos2+1);
207  if (pos == -1) {
208  return false;
209  }
210  substmp = stmp.substring(pos2+1,pos);
211  if ((substmp.toInt() > 23) || (substmp.toInt() <0 )) {
212 
213  return false;
214  }
215  pos2=pos;
216  tmstruct.tm_hour = substmp.toInt();
217 
218  //Search min
219  pos = stmp.indexOf("-", pos2+1);
220  if (pos == -1) {
221  return false;
222  }
223  substmp = stmp.substring(pos2+1,pos);
224  if ((substmp.toInt() > 59) || (substmp.toInt() < 0 )) {
225  return false;
226  }
227  tmstruct.tm_min = substmp.toInt();
228  //Search sec
229  substmp = stmp.substring(pos+1);
230  if ((substmp.toInt() > 59) || (substmp.toInt() < 0 )) {
231  return false;
232  }
233  tmstruct.tm_isdst = 0; //ignore dst
234  //reset servers, time zone and dst
235  configTime (0, 0,"", "", "");
236  tmstruct.tm_sec = substmp.toInt();
237  time_val.tv_sec = mktime (&tmstruct);
238  if(settimeofday(&time_val,0) == -1) {
239  return false;
240  }
241  return true;
242 }
243 
244 bool TimeServer::started()
245 {
246  return _started;
247 }
248 
249 //currently not used
250 void TimeServer::end()
251 {
252  _started = false;
253  _is_internet_time = false;
254 }
255 
256 //currently not used
257 void TimeServer::handle()
258 {
259  if (_started) {
260  }
261 }
262 
263 #endif //TimeServer_DEVICE
Settings_ESP3D::read_string
static const char * read_string(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:794
ESP_TIME_SERVER3
#define ESP_TIME_SERVER3
Definition: settings_esp3d.h:77
TimeServer::started
bool started()
TimeServer::handle
void handle()
ESP_INTERNET_TIME
#define ESP_INTERNET_TIME
Definition: settings_esp3d.h:51
timeserver
TimeServer timeserver
TimeServer::setTime
bool setTime(const char *stime)
EthConfig::started
static bool started()
TimeServer::TimeServer
TimeServer()
time_server.h
ESP_TIMEZONE
#define ESP_TIMEZONE
Definition: settings_esp3d.h:73
ESP_TIME_SERVER2
#define ESP_TIME_SERVER2
Definition: settings_esp3d.h:76
TimeServer::begin
bool begin()
TimeServer
Definition: time_server.h:28
TimeServer::is_internet_time
bool is_internet_time(bool readfromsettings=false)
ESP_TIME_IS_DST
#define ESP_TIME_IS_DST
Definition: settings_esp3d.h:74
TimeServer::end
void end()
BTService::started
bool started()
Settings_ESP3D::read_byte
static uint8_t read_byte(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:715
ESP_TIME_SERVER1
#define ESP_TIME_SERVER1
Definition: settings_esp3d.h:75
bt_service
BTService bt_service
TimeServer::~TimeServer
~TimeServer()
TimeServer::current_time
const char * current_time(time_t t=0)