-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathnspanel.hpp
More file actions
279 lines (232 loc) · 7.68 KB
/
nspanel.hpp
File metadata and controls
279 lines (232 loc) · 7.68 KB
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
#ifndef MQTT_MANAGER_NSPANEL
#define MQTT_MANAGER_NSPANEL
#include "entity/entity.hpp"
#include "protobuf_mqttmanager.pb.h"
#include "protobuf_nspanel.pb.h"
#include "websocket_server/websocket_server.hpp"
#include <atomic>
#include <command_manager/command_manager.hpp>
#include <cstdint>
#include <database_manager/database_manager.hpp>
#include <memory>
#include <mqtt_manager/mqtt_manager.hpp>
#include <mutex>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>
class Room; // Forward declare "Room" as to avoid dependancy loops in CMake
enum MQTT_MANAGER_NSPANEL_STATE {
UNKNOWN,
WAITING,
ONLINE,
OFFLINE,
UPDATING_FIRMWARE,
UPDATING_DATA,
UPDATING_TFT,
AWAITING_ACCEPT,
DENIED
};
struct NSPanelWarningWebsocketRepresentation {
std::string level;
std::string text;
};
class NSPanel {
public:
NSPanel(uint32_t panel_id);
enum ButtonMode {
DIRECT,
DETACHED,
MQTT_PAYLOAD,
FOLLOW,
THERMOSTAT_HEATING,
THERMOSTAT_COOLING,
};
/*
* Build a new NSPanel object from a discovery request and add it to the database as pending.
* Returns a shared_ptr to the new NSPanel object if successful, otherwill nullptr.
*/
static std::shared_ptr<NSPanel> create_from_discovery_request(nlohmann::json request_data);
/*
* Reload configuration from database
*/
void reload_config();
~NSPanel();
/*
Rebuild all MQTT topics from new settings.
*/
void reset_mqtt_topics();
/**
* Reset MQTT topics used to register entities to HA.
*/
void reset_ha_mqtt_topics();
/**
* Send an updated config to the config topic on MQTT
*/
void send_config();
/**
* Get the ID of this NSPanel.
*/
uint get_id();
std::string get_mac();
std::string get_name();
MQTT_MANAGER_NSPANEL_STATE get_state();
void mqtt_callback(std::string topic, std::string payload);
/**
* Handle log messages sent over MQTT from panel
*/
void mqtt_log_callback(std::string topic, std::string payload);
/**
* Dump JSON as string and send to NSPanel command topic.
*/
void send_command(nlohmann::json &command);
/**
* Get the JSON message that will be sent over the websocket when a client requests the state of the NSPanel.
*/
nlohmann::json get_websocket_json_representation();
/**
* Sends a reboot command to the panel over MQTT.
*/
void reboot();
/**
* Sends a command to start firmware update.
*/
void firmware_update(bool force);
/**
* Sends a command to start TFT screen update.
*/
void tft_update();
/**
* This panel has been deleted from the manager and we wish to completly remove all details about it every existing.
*/
void erase();
/**
* Returns true if the NSPanel is register in manager, otherwise false.
*/
bool has_registered_to_manager();
/**
* Send an event over the websocket that indicates that the NSPanel status changed.
*/
void send_websocket_status_update();
/**
* Compare stored firmware checksum with current firmware checksum and return true if they are different.
*/
bool has_firmware_update();
/**
* Compare stored LittleFS checksum with current LittleFS checksum and return true if they are different.
*/
bool has_littlefs_update();
/**
* Compare stored TFT checksum with current TFT checksum for the selected panel type and background (ie. TFT-file) and return true if they are different.
*/
bool has_tft_update();
/**
* Register NSPanel to manager.
*/
bool register_to_manager(const nlohmann::json ®ister_request_payload);
/**
* Register NSPanel to manager.
*/
void register_to_home_assistant();
/**
* Turn a relay on or off
*/
void set_relay_state(uint8_t relay, bool state);
/**
* Handle command from NSPanel
*/
void command_callback(NSPanelMQTTManagerCommand &command);
/*
* handle button presses
*/
void handle_button_pressed_command_callback(ButtonMode button_mode, std::optional<uint64_t> entity_id, std::string topic,std::string payload );
/**
* When an IPC request for NSPanel status comes in handle it and send the response back
*/
void handle_stomp_command_callback(StompFrame frame);
/**
* Check if NSPanel is locked to default room through settings, and if so, return true, otherwise false.
*/
bool is_locked_to_default_room();
/**
* Get the default room ID from NSPanel settings.
*/
int32_t get_default_room_id();
private:
std::string _get_nspanel_setting_with_default(std::string key, std::string default_value);
// Vars:
uint32_t _id;
database_manager::NSPanel _settings; // Settings loaded from database
std::mutex _settings_mutex; // Mutex to only allow access to _settings for one thread at the time
std::string _mac;
std::string _name;
std::string _version;
bool _is_us_panel;
enum US_PANEL_ORIENTATION {
LANDSCAPE_LEFT,
LANDSCAPE_RIGHT,
PORTRAIT
};
US_PANEL_ORIENTATION _us_panel_orientation;
bool _has_registered_to_manager;
std::string _ip_address;
int16_t _rssi;
float _temperature;
uint8_t _heap_used_pct;
uint8_t _update_progress;
MQTT_MANAGER_NSPANEL_STATE _state;
std::vector<NSPanelWarningWebsocketRepresentation> _nspanel_warnings;
std::string _nspanel_warnings_from_manager;
std::string _mqtt_register_mac;
std::string _current_firmware_md5_checksum;
std::string _current_littlefs_md5_checksum;
std::string _current_tft_md5_checksum;
// MQTT Stuff:
// Wether or not relay1 should be registered to Home Assistant as a switch or light.
bool _register_relay1_as_light;
// The topic to send commands to the relay1
std::string _mqtt_relay1_command_topic;
// The topic where relay1 state is published
std::string _mqtt_relay1_state_topic;
// Wether or not relay1 is on
bool _relay1_state;
// Wether or not relay2 should be registered to Home Assistant as a switch or light.
bool _register_relay2_as_light;
// The topic to send commands to the relay2
std::string _mqtt_relay2_command_topic;
// The topic where relay2 state is published
std::string _mqtt_relay2_state_topic;
// Wether or not relay2 is on
bool _relay2_state;
// The topic to capture logs from MQTT
std::string _mqtt_log_topic;
// The topic to capture status (online/offline) from MQTT
std::string _mqtt_status_topic;
// The topic to capture status reports from MQTT
std::string _mqtt_status_report_topic;
// The topic to send out temperature in raw format instead of encoded in protobuf status report
std::string _mqtt_temperature_topic;
// The topic to send commands to panel to via MQTT
std::string _mqtt_command_topic;
// Home Assistant MQTT registration topics:
std::string _mqtt_config_topic;
std::string _mqtt_sensor_temperature_topic;
std::string _mqtt_switch_relay1_topic;
std::string _mqtt_light_relay1_topic;
std::string _mqtt_switch_relay2_topic;
std::string _mqtt_light_relay2_topic;
std::string _mqtt_switch_screen_topic;
std::string _mqtt_number_screen_brightness_topic;
std::string _mqtt_number_screensaver_brightness_topic;
std::string _mqtt_select_screensaver_topic;
// Topic where to send the NSPanelHomePageStatus protobuf state updates
std::string _mqtt_topic_home_page_status;
// Topic where to send the NSPanelHomePageStatus protobuf state updates for "All rooms" mode
std::string _mqtt_topic_home_page_all_rooms_status;
// Topic where to send the NSPanelRoomEntitiesPage protobuf state updates
std::string _mqtt_topic_room_entities_page_status;
// JSON representation of log messages backlog. Used to buffer historical log messages before sending them to web interface
nlohmann::json _log_messages_backlog;
// Mutex to allow only one thread to send config at a time
std::mutex _send_config_mutex;
};
#endif // !MQTT_MANAGER_NSPANEL