ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
ESP140.cpp
Go to the documentation of this file.
1 /*
2  ESP140.cpp - ESP3D command 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 #include "../../include/esp3d_config.h"
21 #if defined (TIMESTAMP_FEATURE)
22 #include "../commands.h"
23 #include "../esp3doutput.h"
24 #include "../settings_esp3d.h"
25 #include "../../modules/authentication/authentication_service.h"
26 #include "../../modules/time/time_server.h"
27 //Sync / Set / Get current time
28 //[ESP140]<SYNC> <srv1=XXXXX> <srv2=XXXXX> <srv3=XXXXX> <zone=xxx> <dst=YES/NO> <time=YYYY-MM-DD#H24:MM:SS> pwd=<admin password>
29 bool Commands::ESP140(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
30 {
31  bool response = true;
32  String parameter;
33 #ifdef AUTHENTICATION_FEATURE
34  if (auth_type == LEVEL_GUEST) {
35  output->printERROR("Wrong authentication!", 401);
36  return false;
37  }
38 #else
39  (void)auth_type;
40 #endif //AUTHENTICATION_FEATURE
41  parameter = get_param (cmd_params, "");
42  //get
43  if (parameter.length() != 0) {
44 #ifdef AUTHENTICATION_FEATURE
45  if (auth_type != LEVEL_ADMIN) {
46  output->printERROR("Wrong authentication!", 401);
47  return false;
48  }
49 #endif //AUTHENTICATION_FEATURE
50  String s = get_param (cmd_params, "srv1=");
51  if (s.length() > 0 && s.length() < Settings_ESP3D::get_max_string_size(ESP_TIME_SERVER1)) {
53  output->printERROR("Set server 1 failed!");
54  }
55  }
56  s = get_param (cmd_params, "srv2=");
57  if (s.length() > 0 && s.length() < Settings_ESP3D::get_max_string_size(ESP_TIME_SERVER2)) {
59  output->printERROR("Set server 2 failed!");
60  }
61  }
62  s = get_param (cmd_params, "srv3=");
63  if (s.length() > 0 && s.length() < Settings_ESP3D::get_max_string_size(ESP_TIME_SERVER3)) {
65  output->printERROR("Set server 2 failed!");
66  }
67  }
68  s = get_param (cmd_params, "zone=");
69  if (s.length() > 0 && (s.toInt() <= (int8_t)Settings_ESP3D::get_max_byte(ESP_TIMEZONE)) && (s.toInt() >= (int8_t)Settings_ESP3D::get_min_byte(ESP_TIMEZONE))) {
70  if (!Settings_ESP3D::write_byte (ESP_TIMEZONE, s.toInt())) {
71  output->printERROR("Set time zone failed!");
72  }
73  }
74  s = get_param (cmd_params, "dst=");
75  s.toUpperCase();
76  if (s.length() > 0 ) {
77  if (!Settings_ESP3D::write_byte (ESP_TIME_IS_DST, (s == "NO")?0:1)) {
78  output->printERROR("Set dayligh failed!");
79  }
80  }
81  s = get_param (cmd_params, "time=");
82  s.toUpperCase();
83  if (s.length() > 0 ) {
84  output->printMSG("Setting time");
85  if(!timeserver.setTime(s.c_str())) {
86  output->printERROR("Set time failed!");
87  response = false;
88  }
89  }
90 
91  if (hastag(parameter.c_str(), "SYNC")) {
93  output->printMSG("Contacting time servers");
94  if(!timeserver.begin()) {
95  output->printERROR("Init time failed!");
96  response = false;
97  }
98  } else {
99  output->printERROR("Time is manual!");
100  response = false;
101  }
102  }
103  }
104 
105  output->printMSG(timeserver.current_time());
106  return response;
107 }
108 
109 #endif //TIMESTAMP_FEATURE
Settings_ESP3D::write_byte
static bool write_byte(int pos, const uint8_t value)
Definition: settings_esp3d.cpp:749
Commands::get_param
const char * get_param(const char *cmd_params, const char *label)
Definition: commands.cpp:162
ESP_TIME_SERVER3
#define ESP_TIME_SERVER3
Definition: settings_esp3d.h:77
LEVEL_GUEST
@ LEVEL_GUEST
Definition: authentication_service.h:26
timeserver
TimeServer timeserver
TimeServer::setTime
bool setTime(const char *stime)
ESP_TIMEZONE
#define ESP_TIMEZONE
Definition: settings_esp3d.h:73
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
ESP_TIME_SERVER2
#define ESP_TIME_SERVER2
Definition: settings_esp3d.h:76
ESP3DOutput::printMSG
size_t printMSG(const char *s, bool withNL=true)
Definition: esp3doutput.cpp:190
Commands::hastag
bool hastag(const char *cmd_params, const char *tag)
Definition: commands.cpp:221
TimeServer::begin
bool begin()
TimeServer::is_internet_time
bool is_internet_time(bool readfromsettings=false)
ESP3DOutput::printERROR
size_t printERROR(const char *s, int code_error=200)
Definition: esp3doutput.cpp:247
Settings_ESP3D::write_string
static bool write_string(int pos, const char *byte_buffer)
Definition: settings_esp3d.cpp:870
Settings_ESP3D::get_max_byte
static uint8_t get_max_byte(int pos)
Definition: settings_esp3d.cpp:507
Settings_ESP3D::get_max_string_size
static uint8_t get_max_string_size(int pos)
Definition: settings_esp3d.cpp:617
ESP_TIME_IS_DST
#define ESP_TIME_IS_DST
Definition: settings_esp3d.h:74
ESP_TIME_SERVER1
#define ESP_TIME_SERVER1
Definition: settings_esp3d.h:75
Settings_ESP3D::get_min_byte
static uint8_t get_min_byte(int pos)
Definition: settings_esp3d.cpp:527
LEVEL_ADMIN
@ LEVEL_ADMIN
Definition: authentication_service.h:28
ESP3DOutput
Definition: esp3doutput.h:48
TimeServer::current_time
const char * current_time(time_t t=0)