-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
system.h
290 lines (234 loc) · 8.4 KB
/
system.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* EMS-ESP - https://github.com/emsesp/EMS-ESP
* Copyright 2020 Paul Derbyshire
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EMSESP_SYSTEM_H_
#define EMSESP_SYSTEM_H_
#include <Arduino.h>
#include <ArduinoJson.h>
#include "helpers.h"
#include "console.h"
#include "mqtt.h"
#include "telegram.h"
#ifndef EMSESP_STANDALONE
#include <esp_wifi.h>
#include <esp_bt.h>
#include <ETH.h>
#include <uuid/syslog.h>
#endif
#include <uuid/log.h>
#include <PButton.h>
using uuid::console::Shell;
namespace emsesp {
enum PHY_type : uint8_t { PHY_TYPE_NONE = 0, PHY_TYPE_LAN8720, PHY_TYPE_TLK110 };
class System {
public:
void start();
void loop();
// commands
static bool command_pin(const char * value, const int8_t id);
static bool command_send(const char * value, const int8_t id);
static bool command_publish(const char * value, const int8_t id);
static bool command_fetch(const char * value, const int8_t id);
static bool command_restart(const char * value, const int8_t id);
#if defined(EMSESP_DEBUG)
static bool command_test(const char * value, const int8_t id);
#endif
static bool command_syslog_level(const char * value, const int8_t id);
static bool command_watch(const char * value, const int8_t id);
static bool command_info(const char * value, const int8_t id, JsonObject & output);
static bool command_commands(const char * value, const int8_t id, JsonObject & output);
std::string reset_reason(uint8_t cpu) const;
void system_restart();
void format(uuid::console::Shell & shell);
void upload_status(bool in_progress);
bool upload_status();
void show_mem(const char * note);
void reload_settings();
void wifi_tweak();
void syslog_init();
bool check_upgrade();
bool heartbeat_json(JsonObject & output);
void send_heartbeat();
bool syslog_enabled() {
return syslog_enabled_;
}
#ifndef EMSESP_STANDALONE
unsigned long syslog_count() {
return syslog_.message_count();
}
unsigned long syslog_fails() {
return syslog_.message_fails();
}
#endif
void led_init(bool refresh);
void network_init(bool refresh);
void button_init(bool refresh);
void commands_init();
static void extractSettings(const char * filename, const char * section, JsonObject & output);
static bool saveSettings(const char * filename, const char * section, JsonObject & input);
static bool is_valid_gpio(uint8_t pin);
static bool load_board_profile(std::vector<int8_t> & data, const std::string & board_profile);
static void restart_requested(bool restart_requested) {
restart_requested_ = restart_requested;
}
static bool restart_requested() {
return restart_requested_;
}
bool telnet_enabled() {
return telnet_enabled_;
}
bool analog_enabled() {
return analog_enabled_;
}
bool readonly_mode() {
return readonly_mode_;
}
void readonly_mode(bool readonly_mode) {
readonly_mode_ = readonly_mode;
}
uint8_t bool_format() {
return bool_format_;
}
uint8_t bool_dashboard() {
return bool_dashboard_;
}
// see default_settings.h
// BOOL_FORMAT_ONOFF_STR = 1,
// BOOL_FORMAT_ONOFF_STR_CAP = 2
// BOOL_FORMAT_TRUEFALSE_STR = 3
// BOOL_FORMAT_TRUEFALSE = 4
// BOOL_FORMAT_10_STR = 5
// BOOL_FORMAT_10 = 6
void bool_format(uint8_t format) {
bool_format_ = format;
}
void bool_dashboard(uint8_t format) {
bool_dashboard_ = format;
}
uint8_t enum_format() {
return enum_format_;
}
void enum_format(uint8_t format) {
enum_format_ = format;
}
std::string board_profile() {
return std::string(board_profile_.c_str());
}
std::string hostname() {
return hostname_;
}
void hostname(std::string hostname) {
hostname_ = hostname;
}
bool ethernet_connected() {
return ethernet_connected_;
}
void ethernet_connected(bool b) {
ethernet_connected_ = b;
}
void ntp_connected(bool b);
bool ntp_connected() {
// timeout 2 hours, ntp sync is normally every hour.
if ((uuid::get_uptime_sec() - ntp_last_check_ > 7201) && ntp_connected_) {
ntp_connected(false);
}
return ntp_connected_;
}
bool network_connected() {
#ifndef EMSESP_STANDALONE
return (ethernet_connected() || WiFi.isConnected());
#else
return true;
#endif
}
void fahrenheit(bool b) {
fahrenheit_ = b;
}
bool fahrenheit() {
return fahrenheit_;
}
void healthcheck(uint8_t healthcheck) {
healthcheck_ = healthcheck;
}
void show_system(uuid::console::Shell & shell);
void wifi_reconnect();
void show_users(uuid::console::Shell & shell);
private:
static uuid::log::Logger logger_;
static bool restart_requested_;
// button
static PButton myPButton_; // PButton instance
static void button_OnClick(PButton & b);
static void button_OnDblClick(PButton & b);
static void button_OnLongPress(PButton & b);
static void button_OnVLongPress(PButton & b);
static constexpr uint32_t BUTTON_Debounce = 40; // Debounce period to prevent flickering when pressing or releasing the button (in ms)
static constexpr uint32_t BUTTON_DblClickDelay = 250; // Max period between clicks for a double click event (in ms)
static constexpr uint32_t BUTTON_LongPressDelay = 750; // Hold period for a long press event (in ms)
static constexpr uint32_t BUTTON_VLongPressDelay = 9000; // Hold period for a very long press event (in ms)
// healthcheck
static constexpr uint32_t SYSTEM_CHECK_FREQUENCY = 5000; // do a system check every 5 seconds
static constexpr uint32_t HEALTHCHECK_LED_LONG_DUARATION = 1500;
static constexpr uint32_t HEALTHCHECK_LED_FLASH_DUARATION = 150;
static constexpr uint8_t HEALTHCHECK_NO_BUS = (1 << 0); // 1
static constexpr uint8_t HEALTHCHECK_NO_NETWORK = (1 << 1); // 2
static constexpr uint32_t SYSTEM_HEARTBEAT_INTERVAL = 60000; // in milliseconds, how often the MQTT heartbeat is sent (1 min)
static constexpr uint8_t LED_ON = HIGH; // LED on
#ifndef EMSESP_STANDALONE
static uuid::syslog::SyslogService syslog_;
#endif
void led_monitor();
void system_check();
int8_t wifi_quality(int8_t dBm);
uint8_t healthcheck_ = HEALTHCHECK_NO_NETWORK | HEALTHCHECK_NO_BUS; // start with all flags set, no wifi and no ems bus connection
uint32_t last_heartbeat_ = 0;
uint32_t last_system_check_ = 0;
bool upload_status_ = false; // true if we're in the middle of a OTA firmware upload
bool ethernet_connected_ = false;
bool ntp_connected_ = false;
uint32_t ntp_last_check_ = 0;
// EMS-ESP settings
// copies from WebSettings class in WebSettingsService.h and loaded with reload_settings()
std::string hostname_ = FACTORY_WIFI_HOSTNAME;
bool hide_led_;
uint8_t led_gpio_;
bool analog_enabled_;
bool low_clock_;
String board_profile_;
uint8_t pbutton_gpio_;
uint8_t rx_gpio_;
uint8_t tx_gpio_;
uint8_t dallas_gpio_;
bool telnet_enabled_;
bool syslog_enabled_;
int8_t syslog_level_;
uint32_t syslog_mark_interval_;
String syslog_host_;
uint16_t syslog_port_;
bool fahrenheit_;
uint8_t bool_dashboard_;
uint8_t bool_format_;
uint8_t enum_format_;
bool readonly_mode_;
// ethernet
uint8_t phy_type_;
int8_t eth_power_;
uint8_t eth_phy_addr_;
uint8_t eth_clock_mode_;
};
} // namespace emsesp
#endif