-
Notifications
You must be signed in to change notification settings - Fork 0
/
housewiz.c
258 lines (222 loc) · 8.21 KB
/
housewiz.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
/* HouseWiz - A simple home web server for control of Philips Wiz devices.
*
* Copyright 2020, 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.
*
*
* housewiz.c - Main loop of the housewiz 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 "housewiz_device.h"
static int use_houseportal = 0;
static const char *housewiz_status (const char *method, const char *uri,
const char *data, int length) {
static char buffer[65537];
ParserToken token[1024];
char pool[65537];
int count = housewiz_device_count();
int i;
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", houselog_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 = housewiz_device_deadline(i);
const char *name = housewiz_device_name(i);
const char *status = housewiz_device_failure(i);
if (!status) status = housewiz_device_get(i)?"on":"off";
const char *commanded = housewiz_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 *housewiz_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 = housewiz_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, housewiz_device_name(i)) == 0)) {
found = 1;
housewiz_device_set (i, state, pulse);
}
}
if (! found) {
echttp_error (404, "invalid point name");
return "";
}
return housewiz_status (method, uri, data, length);
}
static const char *housewiz_config (const char *method, const char *uri,
const char *data, int length) {
if (strcmp ("GET", method) == 0) {
static char buffer[65537];
housewiz_device_live_config (buffer, sizeof(buffer));
echttp_content_type_json ();
return buffer;
} else if (strcmp ("POST", method) == 0) {
const char *error = houseconfig_update(data);
if (error) {
echttp_error (400, error);
} else {
housewiz_device_refresh("AFTER USER CHANGE");
houselog_event ("SYSTEM", "CONFIG", "SAVE", "TO DEPOT %s", houseconfig_name());
housedepositor_put ("config", houseconfig_name(), data, length);
}
} else {
echttp_error (400, "invalid method");
}
return "";
}
static void housewiz_background (int fd, int mode) {
time_t now = time(0);
if (use_houseportal) {
static time_t LastRenewal = 0;
if (now >= LastRenewal + 60) {
if (LastRenewal > 0) {
houseportal_renew();
} else {
static const char *path[] = {"control:/wiz"};
houseportal_register (echttp_port(4), path, 1);
}
LastRenewal = now;
}
}
housewiz_device_periodic(now);
if (housewiz_device_changed()) {
static char buffer[65537];
housewiz_device_live_config (buffer, sizeof(buffer));
houseconfig_update(buffer);
houselog_event ("SYSTEM", "CONFIG", "SAVE", "TO DEPOT %s (AUTODETECT)", houseconfig_name());
housedepositor_put ("config", houseconfig_name(), buffer, strlen(buffer));
if (echttp_isdebug()) fprintf (stderr, "Configuration saved\n");
}
housediscover (now);
houselog_background (now);
housedepositor_periodic (now);
}
static void housewiz_config_listener (const char *name, time_t timestamp,
const char *data, int length) {
houselog_event ("SYSTEM", "CONFIG", "LOAD", "FROM DEPOT %s", name);
if (!houseconfig_update (data))
housewiz_device_refresh("AFTER DEPOT UPDATE");
}
static void housewiz_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 ("wiz", argc, argv);
housedepositor_initialize (argc, argv);
houseconfig_default ("--config=wiz");
error = houseconfig_load (argc, argv);
if (error) {
houselog_trace
(HOUSE_FAILURE, "CONFIG", "Cannot load configuration: %s\n", error);
}
error = housewiz_device_initialize (argc, argv);
if (error) {
houselog_trace
(HOUSE_FAILURE, "PLUG", "Cannot initialize: %s\n", error);
exit(1);
}
housedepositor_subscribe ("config", houseconfig_name(), housewiz_config_listener);
echttp_cors_allow_method("GET");
echttp_protect (0, housewiz_protect);
echttp_route_uri ("/wiz/status", housewiz_status);
echttp_route_uri ("/wiz/set", housewiz_set);
echttp_route_uri ("/wiz/config", housewiz_config);
echttp_static_route ("/", "/usr/local/share/house/public");
echttp_background (&housewiz_background);
houselog_event ("SERVICE", "wiz", "STARTED", "ON %s", houselog_host());
echttp_loop();
}