-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path_definitions.h
More file actions
275 lines (242 loc) · 8.88 KB
/
Copy path_definitions.h
File metadata and controls
275 lines (242 loc) · 8.88 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
#pragma once
#include <map>
#include <memory>
#include <string>
#include <cstring>
#include "esphome/core/log.h"
#include <functional>
namespace esphome {
namespace sec_touch {
#define STX 0x02 // Start of Text
#define ETX 0x0A // End of Text
#define ACK 0x06 // Acknowledge
#define NAK 0x15 // Not Acknowledge
#define TAB 0x09 // Tab
#define NOISE 0xFF // 255
constexpr const int COMMANDID_SET = 32;
constexpr const int COMMANDID_GET = 32800;
constexpr const std::array<int, 6> FAN_LEVEL_IDS = {173, 174, 175, 176, 177, 178};
constexpr const std::array<int, 6> FAN_LABEL_IDS = {78, 79, 80, 81, 82, 83};
constexpr const int SUMMER_VENTILATION_PROPERTY_ID = 48;
constexpr const std::array<int, 1> GLOBAL_SETTING_IDS = {SUMMER_VENTILATION_PROPERTY_ID};
constexpr const int NAME_MAPPING_COUNT = 69;
constexpr const char *NAME_MAPPING[NAME_MAPPING_COUNT] = {
"-", // 0 do NOT use an empty string since the text sensor will crash :)
"Bereich 1", // 1
"Bereich 2", // 2
"Bereich 3", // 3
"Bereich 4", // 4
"Bereich 5", // 5
"Bereich 6", // 6
"Wohnzimmer", // 7
"Wohnzimmer 1", // 8
"Wohnzimmer 2", // 9
"Esszimmer", // 10
"Esszimmer 1", // 11
"Esszimmer 2", // 12
"Schlafzimmer", // 13
"Schlafzimmer 1", // 14
"Schlafzimmer 2", // 15
"Kinderzimmer", // 16
"Kinderzimmer 1", // 17
"Kinderzimmer 2", // 18
"Kinderzimmer 3", // 19
"Kinderzimmer 4", // 20
"Küche", // 21
"Küche 1", // 22
"Küche 2", // 23
"Bad", // 24
"Master Bad", // 25
"Gäste Bad", // 26
"WC", // 27
"Gäste WC", // 28
"Arbeitszimmer", // 29
"Arbeitszimmer 1", // 30
"Arbeitszimmer 2", // 31
"Hobbyraum", // 32
"Mehrzweckraum", // 33
"Abstellraum", // 34
"Kellerraum", // 35
"Kellerraum 1", // 36
"Kellerraum 2", // 37
"Kellerraum 3", // 38
"Dachboden", // 39
"Dachboden 1", // 40
"Dachboden 2", // 41
"Dachboden 3", // 42
"Büro", // 43
"Büro 1", // 44
"Büro 2", // 45
"Büro 3", // 46
"Büro 4", // 47
"Büro 6", // 48
"Chef Büro", // 49
"Abtl.Ltr. Büro", // 50
"Büro EK", // 51
"Büro AB", // 52
"Büro Entw.", // 53
"Büro Konstr.", // 54
"Büro Buchh.", // 55
"Speiseraum", // 56
"Besp. Raum", // 57
"Besp. Raum 1", // 58
"Besp. Raum 2", // 59
"Besp. Raum 3", // 60
"Louge", // 61
"Bibliothek", // 62
"Fitnessraum", // 63
"Wintergarten", // 64
"Bastelraum", // 65
"Ankleidez.", // 66
"HWR", // 67
"leer" // 68
};
/**
* Callback for when a property is updated
* @param property_id The ID of the property that was updated for example 173 for level of the fan 1
* @param new_value The new value of the property obtained from the touch controller
*/
using UpdateCallbackListener = std::function<void(int property_id, int new_value)>;
// Includes command_id so the sniffer can record whether the device message was
// a data response (command_id=32) or, if the hardware ever sends unsolicited messages
// with a different command_id, distinguish those as well.
using RawMessageCallbackListener = std::function<void(int command_id, int property_id, int new_value)>;
// Called when the task queue is empty and no task is awaiting a response
using QueueEmptyListener = std::function<void()>;
// Helper function to check if an ID is in the array
template<typename T, size_t N> static bool contains(const std::array<T, N> &arr, int value) {
return std::find(arr.begin(), arr.end(), value) != arr.end();
}
/**
* Used to couple `SetDataTask->id` with the level_id or label_id for example
*/
enum class TaskTargetType { LEVEL, LABEL, GLOBAL_SETTING };
enum class TaskType {
SET_DATA,
GET_DATA,
/**
* Used to indicate that no task is currently being processed
*/
NONE
};
class EnumToString {
private:
EnumToString() = default;
public:
static const char *TaskTargetType(TaskTargetType v) {
switch (v) {
case TaskTargetType::LEVEL:
return "LEVEL";
case TaskTargetType::LABEL:
return "LABEL";
case TaskTargetType::GLOBAL_SETTING:
return "GLOBAL_SETTING";
default:
return "UNKNOWN";
}
}
static const char *TaskType(TaskType v) {
switch (v) {
case TaskType::SET_DATA:
return "SET_DATA";
case TaskType::GET_DATA:
return "GET_DATA";
case TaskType::NONE:
return "NONE";
default:
return "UNKNOWN";
}
}
};
struct IncomingMessage {
private:
static constexpr const char *EMPTY = "---1";
std::string returned_id = IncomingMessage::EMPTY;
std::string returned_value = IncomingMessage::EMPTY;
public:
char buffer[64];
int buffer_index = -1;
void reset() {
this->buffer_index = -1;
this->returned_id = IncomingMessage::EMPTY;
this->returned_value = IncomingMessage::EMPTY;
memset(this->buffer, 0, sizeof(this->buffer));
}
bool returned_id_is_empty() { return this->returned_id == IncomingMessage::EMPTY; }
bool returned_value_is_empty() { return this->returned_value == IncomingMessage::EMPTY; }
void add_to_returned_id(char data) {
if (this->returned_id_is_empty()) {
this->returned_id = "";
}
this->returned_id += data;
}
void add_to_returned_value(char data) {
if (this->returned_value_is_empty()) {
this->returned_value = "";
}
this->returned_value += data;
}
std::string get_returned_id() { return this->returned_id; }
std::string get_returned_value() { return this->returned_value; }
int get_returned_id_as_int() { return std::stoi(this->returned_id); }
int get_returned_value_as_int() { return std::stoi(this->returned_value); }
/**
* @returns the index of the last byte stored in the buffer
*/
int store_data(uint8_t data) {
if (this->buffer_index + 1 < (int) sizeof(this->buffer) - 1) {
this->buffer_index++;
this->buffer[this->buffer_index] = data;
} else {
ESP_LOGW("sec_touch.incoming", " Buffer overflow! Discarding extra data.");
}
return this->buffer_index;
}
};
struct BaseTask {
virtual ~BaseTask() = default;
virtual TaskType get_task_type() const = 0;
TaskTargetType targetType;
int property_id;
};
struct SetDataTask : public BaseTask {
char value[8];
static std::unique_ptr<SetDataTask> create(TaskTargetType targetType, int property_id, const char *value) {
if ((targetType == TaskTargetType::LEVEL && contains(FAN_LEVEL_IDS, property_id)) ||
(targetType == TaskTargetType::LABEL && contains(FAN_LABEL_IDS, property_id)) ||
(targetType == TaskTargetType::GLOBAL_SETTING && contains(GLOBAL_SETTING_IDS, property_id))) {
return std::unique_ptr<SetDataTask>(new SetDataTask(targetType, property_id, value)); // Valid task
}
return nullptr; // Null unique_ptr if validation fails
}
TaskType get_task_type() const override { return TaskType::SET_DATA; }
private:
SetDataTask(TaskTargetType targetType, int property_id, const char *value) {
this->targetType = targetType;
this->property_id = property_id;
std::strncpy(this->value, value, sizeof(this->value) - 1);
this->value[sizeof(this->value) - 1] = '\0'; // Ensure null termination
}
};
struct GetDataTask : public BaseTask {
static std::unique_ptr<GetDataTask> create(TaskTargetType targetType, int property_id) {
if ((targetType == TaskTargetType::LEVEL && contains(FAN_LEVEL_IDS, property_id)) ||
(targetType == TaskTargetType::LABEL && contains(FAN_LABEL_IDS, property_id)) ||
(targetType == TaskTargetType::GLOBAL_SETTING && contains(GLOBAL_SETTING_IDS, property_id))) {
return std::unique_ptr<GetDataTask>(new GetDataTask(targetType, property_id)); // Valid task
}
return nullptr; // Null unique_ptr if validation fails
}
// Bypasses ID validation — for sniffer/discovery use only
static std::unique_ptr<GetDataTask> create_unchecked(int property_id) {
return std::unique_ptr<GetDataTask>(new GetDataTask(TaskTargetType::LEVEL, property_id));
}
TaskType get_task_type() const override { return TaskType::GET_DATA; }
private:
GetDataTask(TaskTargetType targetType, int property_id) {
this->targetType = targetType;
this->property_id = property_id;
}
};
} // namespace sec_touch
} // namespace esphome