-
Notifications
You must be signed in to change notification settings - Fork 0
/
housetuya.c
298 lines (256 loc) · 9.21 KB
/
housetuya.c
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
291
292
293
294
295
296
297
/* HouseTuya - A simple web service for control of Tuya devices
*
* Copyright 2024, Pascal Martin
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* housetuya.c - Main loop of the housetuya program.
*
* SYNOPSYS:
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "echttp.h"
#include "echttp_cors.h"
#include "echttp_json.h"
#include "echttp_static.h"
#include "houseportalclient.h"
#include "housediscover.h"
#include "houselog.h"
#include "houseconfig.h"
#include "housedepositor.h"
#include "housetuya.h"
#include "housetuya_model.h"
#include "housetuya_device.h"
static int use_houseportal = 0;
static int WasLoadedFromDepot = 0;
int housetuya_isdebug (void) {
return echttp_isdebug();
}
static const char *housetuya_status (const char *method, const char *uri,
const char *data, int length) {
static char buffer[65537];
ParserToken token[1024];
char pool[65537];
char host[256];
int count = housetuya_device_count();
int i;
gethostname (host, sizeof(host));
ParserContext context = echttp_json_start (token, 1024, pool, sizeof(pool));
int root = echttp_json_add_object (context, 0, 0);
echttp_json_add_string (context, root, "host", host);
echttp_json_add_string (context, root, "proxy", houseportal_server());
echttp_json_add_integer (context, root, "timestamp", (long)time(0));
int top = echttp_json_add_object (context, root, "control");
int container = echttp_json_add_object (context, top, "status");
for (i = 0; i < count; ++i) {
time_t pulsed = housetuya_device_deadline(i);
const char *name = housetuya_device_name(i);
const char *status = housetuya_device_failure(i);
if (!status) status = housetuya_device_get(i)?"on":"off";
const char *commanded = housetuya_device_commanded(i)?"on":"off";
int point = echttp_json_add_object (context, container, name);
echttp_json_add_string (context, point, "state", status);
echttp_json_add_string (context, point, "command", commanded);
if (pulsed)
echttp_json_add_integer (context, point, "pulse", (int)pulsed);
echttp_json_add_string (context, point, "gear", "light");
}
const char *error = echttp_json_export (context, buffer, sizeof(buffer));
if (error) {
echttp_error (500, error);
return "";
}
echttp_content_type_json ();
return buffer;
}
static const char *housetuya_set (const char *method, const char *uri,
const char *data, int length) {
const char *point = echttp_parameter_get("point");
const char *statep = echttp_parameter_get("state");
const char *pulsep = echttp_parameter_get("pulse");
int state;
int pulse;
int i;
int count = housetuya_device_count();
int found = 0;
if (!point) {
echttp_error (404, "missing point name");
return "";
}
if (!statep) {
echttp_error (400, "missing state value");
return "";
}
if ((strcmp(statep, "on") == 0) || (strcmp(statep, "1") == 0)) {
state = 1;
} else if ((strcmp(statep, "off") == 0) || (strcmp(statep, "0") == 0)) {
state = 0;
} else {
echttp_error (400, "invalid state value");
return "";
}
pulse = pulsep ? atoi(pulsep) : 0;
if (pulse < 0) {
echttp_error (400, "invalid pulse value");
return "";
}
for (i = 0; i < count; ++i) {
if ((strcmp (point, "all") == 0) ||
(strcmp (point, housetuya_device_name(i)) == 0)) {
found = 1;
housetuya_device_set (i, state, pulse);
}
}
if (! found) {
echttp_error (404, "invalid point name");
return "";
}
return housetuya_status (method, uri, data, length);
}
static const char *housetuya_export (void) {
static char buffer[65537];
static char pool[65537];
static ParserToken token[1024];
ParserContext context = echttp_json_start (token, 1024, pool, sizeof(pool));
int root = echttp_json_add_object (context, 0, 0);
int top = echttp_json_add_object (context, root, "tuya");
housetuya_device_live_config (context, top);
housetuya_model_live_config (context, top);
const char *error = echttp_json_export (context, buffer, sizeof(buffer));
if (error) {
houselog_trace (HOUSE_FAILURE, "CONFIG",
"Cannot export configuration: %s\n", error);
return 0;
}
return buffer;
}
static void housetuya_refresh (void) {
housetuya_device_refresh();
housetuya_model_refresh();
}
static void housetuya_save_to_depot (const char *data, int length) {
if (!WasLoadedFromDepot) return;
houselog_event ("CONFIG", houseconfig_name(), "SAVE", "TO DEPOT");
housedepositor_put ("config", houseconfig_name(), data, length);
}
static const char *housetuya_config (const char *method, const char *uri,
const char *data, int length) {
if (strcmp ("GET", method) == 0) {
const char *response = housetuya_export();
if (response) {
echttp_content_type_json ();
return response;
}
echttp_error (400, "No configuration");
} else if (strcmp ("POST", method) == 0) {
const char *error = houseconfig_update(data);
if (error) {
echttp_error (400, error);
} else {
housetuya_refresh();
housetuya_save_to_depot (data, length);
}
} else {
echttp_error (400, "invalid method");
}
return "";
}
static void housetuya_background (int fd, int mode) {
static time_t LastRenewal = 0;
time_t now = time(0);
if (use_houseportal) {
static const char *path[] = {"control:/tuya"};
if (now >= LastRenewal + 60) {
if (LastRenewal > 0)
houseportal_renew();
else
houseportal_register (echttp_port(4), path, 1);
LastRenewal = now;
}
}
housetuya_device_periodic(now);
if (housetuya_device_changed() || housetuya_model_changed()) {
const char *buffer = housetuya_export();
houseconfig_update(buffer);
housetuya_save_to_depot (buffer, strlen(buffer));
if (echttp_isdebug()) fprintf (stderr, "Configuration saved\n");
}
housediscover (now);
houselog_background (now);
housedepositor_periodic (now);
}
static void housetuya_config_listener (const char *name, time_t timestamp,
const char *data, int length) {
houselog_event ("CONFIG", houseconfig_name(), "LOAD", "FROM DEPOT %s", name);
if (!houseconfig_update (data)) {
housetuya_refresh();
WasLoadedFromDepot = 1;
}
}
static void housetuya_protect (const char *method, const char *uri) {
echttp_cors_protect(method, uri);
}
int main (int argc, const char **argv) {
const char *error;
// These strange statements are to make sure that fds 0 to 2 are
// reserved, since this application might output some errors.
// 3 descriptors are wasted if 0, 1 and 2 are already open. No big deal.
//
open ("/dev/null", O_RDONLY);
dup(open ("/dev/null", O_WRONLY));
signal(SIGPIPE, SIG_IGN);
echttp_default ("-http-service=dynamic");
argc = echttp_open (argc, argv);
if (echttp_dynamic_port()) {
houseportal_initialize (argc, argv);
use_houseportal = 1;
}
housediscover_initialize (argc, argv);
houselog_initialize ("tuya", argc, argv);
housedepositor_initialize (argc, argv);
houseconfig_default ("--config=tuya");
error = houseconfig_load (argc, argv);
if (error) {
houselog_trace
(HOUSE_FAILURE, "CONFIG", "Cannot load configuration: %s\n", error);
}
error = housetuya_device_initialize (argc, argv);
if (error) {
houselog_trace
(HOUSE_FAILURE, "PLUG", "Cannot initialize: %s\n", error);
exit(1);
}
housedepositor_subscribe ("config", houseconfig_name(), housetuya_config_listener);
echttp_cors_allow_method("GET");
echttp_protect (0, housetuya_protect);
echttp_route_uri ("/tuya/status", housetuya_status);
echttp_route_uri ("/tuya/set", housetuya_set);
echttp_route_uri ("/tuya/config", housetuya_config);
echttp_static_route ("/", "/usr/local/share/house/public");
echttp_background (&housetuya_background);
houselog_event ("SERVICE", "tuya", "STARTED", "ON %s", houselog_host());
echttp_loop();
}