38 extern MYSQL *con; |
38 extern MYSQL *con; |
39 extern MYSQL_RES *res_set; |
39 extern MYSQL_RES *res_set; |
40 extern MYSQL_ROW row; |
40 extern MYSQL_ROW row; |
41 |
41 |
42 |
42 |
|
43 void ispindel_ws_send(sys_ispindel_list *ispindel) |
|
44 { |
|
45 char *msg = NULL, buf[65]; |
|
46 |
|
47 msg = xstrcpy((char *)"{\"device\":\"ispindels\",\"node\":\""); |
|
48 msg = xstrcat(msg, ispindel->node); |
|
49 msg = xstrcat(msg, (char *)"\",\"unit\":\""); |
|
50 msg = xstrcat(msg, ispindel->alias); |
|
51 msg = xstrcat(msg, (char *)"\",\"online\":"); |
|
52 msg = xstrcat(msg, ispindel->online ? (char *)"1":(char *)"0"); |
|
53 msg = xstrcat(msg, (char *)",\"mode\":\""); |
|
54 msg = xstrcat(msg, ispindel->mode); |
|
55 msg = xstrcat(msg, (char *)"\",\"beeruuid\":\""); |
|
56 msg = xstrcat(msg, ispindel->beeruuid); |
|
57 msg = xstrcat(msg, (char *)"\",\"beercode\":\""); |
|
58 msg = xstrcat(msg, ispindel->beercode); |
|
59 msg = xstrcat(msg, (char *)"\",\"beername\":\""); |
|
60 msg = xstrcat(msg, ispindel->beername); |
|
61 msg = xstrcat(msg, (char *)"\",\"temperature\":"); |
|
62 snprintf(buf, 64, "%.4f", ispindel->temperature); |
|
63 msg = xstrcat(msg, buf); |
|
64 msg = xstrcat(msg, (char *)",\"angle\":"); |
|
65 snprintf(buf, 64, "%.6f", ispindel->angle); |
|
66 msg = xstrcat(msg, buf); |
|
67 msg = xstrcat(msg, (char *)",\"battery\":"); |
|
68 snprintf(buf, 64, "%.6f", ispindel->battery); |
|
69 msg = xstrcat(msg, buf); |
|
70 msg = xstrcat(msg, (char *)",\"gravity\":"); |
|
71 snprintf(buf, 64, "%.6f", ispindel->gravity); |
|
72 msg = xstrcat(msg, buf); |
|
73 msg = xstrcat(msg, (char *)",\"og_gravity\":"); |
|
74 snprintf(buf, 64, "%.6f", ispindel->og_gravity); |
|
75 msg = xstrcat(msg, buf); |
|
76 msg = xstrcat(msg, (char *)",\"alarm\":"); |
|
77 snprintf(buf, 64, "%d", ispindel->alarm); |
|
78 msg = xstrcat(msg, buf); |
|
79 msg = xstrcat(msg, (char *)"}"); |
|
80 ws_broadcast(msg); |
|
81 free(msg); |
|
82 msg = NULL; |
|
83 } |
|
84 |
|
85 |
|
86 |
|
87 void ispindel_ws_receive(char *payload) |
|
88 { |
|
89 struct json_object *jobj, *val; |
|
90 sys_ispindel_list *tmpp; |
|
91 char *node = NULL, *alias = NULL, *mode = NULL, *beeruuid = NULL, *beercode = NULL, *beername = NULL; |
|
92 char query[512], *end; |
|
93 MYSQL *con2 = NULL; |
|
94 |
|
95 // syslog(LOG_NOTICE, "ispindel_ws_receive(%s)", payload); |
|
96 |
|
97 /* |
|
98 * Process the JSON formatted payload. |
|
99 */ |
|
100 jobj = json_tokener_parse(payload); |
|
101 if (json_object_object_get_ex(jobj, "node", &val)) |
|
102 node = xstrcpy((char *)json_object_get_string(val)); |
|
103 if (json_object_object_get_ex(jobj, "unit", &val)) |
|
104 alias = xstrcpy((char *)json_object_get_string(val)); |
|
105 if (json_object_object_get_ex(jobj, "beeruuid", &val)) |
|
106 beeruuid = xstrcpy((char *)json_object_get_string(val)); |
|
107 if (json_object_object_get_ex(jobj, "beercode", &val)) |
|
108 beercode = xstrcpy((char *)json_object_get_string(val)); |
|
109 if (json_object_object_get_ex(jobj, "beername", &val)) |
|
110 beername = xstrcpy((char *)json_object_get_string(val)); |
|
111 if (json_object_object_get_ex(jobj, "mode", &val)) |
|
112 mode = xstrcpy((char *)json_object_get_string(val)); |
|
113 json_object_put(jobj); |
|
114 |
|
115 /* |
|
116 * Search ispindel record in the memory array and use it if found. |
|
117 */ |
|
118 if (ispindels) { |
|
119 for (tmpp = ispindels; tmpp; tmpp = tmpp->next) { |
|
120 if (strcmp(tmpp->node, node) == 0) { |
|
121 con2 = mysql_init(NULL); |
|
122 if (con2 == NULL) { |
|
123 syslog(LOG_NOTICE, "MySQL: mysql_init() failed"); |
|
124 } else { |
|
125 if (mysql_real_connect(con2, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) { |
|
126 syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con2)); |
|
127 } else { |
|
128 if (beeruuid && beercode && beername) { |
|
129 end = stpcpy(query, "UPDATE mon_ispindels SET beeruuid='"); |
|
130 end += mysql_real_escape_string(con2, end, beeruuid, strlen(beeruuid)); |
|
131 end = stpcpy(end, "', beercode='"); |
|
132 end += mysql_real_escape_string(con2, end, beercode, strlen(beercode)); |
|
133 end = stpcpy(end, "', beername='"); |
|
134 end += mysql_real_escape_string(con2, end, beername, strlen(beername)); |
|
135 end = stpcpy(end, "', og_gravity='0.0' WHERE node='"); |
|
136 end += mysql_real_escape_string(con2, end, node, strlen(node)); |
|
137 end = stpcpy(end, "'"); |
|
138 |
|
139 if (mysql_real_query(con2, query, (unsigned int) (end - query))) { |
|
140 syslog(LOG_NOTICE, "MySQL: `%s' error %u (%s))", query, mysql_errno(con2), mysql_error(con2)); |
|
141 } else { |
|
142 /* Database updated, now update internal memory */ |
|
143 syslog(LOG_NOTICE, "MySQL: `%s' Ok", query); |
|
144 if (tmpp->beercode) |
|
145 free(tmpp->beercode); |
|
146 tmpp->beercode = xstrcpy(beercode); |
|
147 if (tmpp->beername) |
|
148 free(tmpp->beername); |
|
149 tmpp->beername = xstrcpy(beername); |
|
150 if (tmpp->beeruuid) |
|
151 free(tmpp->beeruuid); |
|
152 tmpp->beeruuid = xstrcpy(beeruuid); |
|
153 tmpp->og_gravity = 0.0; |
|
154 /* Report new state to the client */ |
|
155 ispindel_ws_send(tmpp); |
|
156 syslog(LOG_NOTICE, "Set ispindel %s/%s new beer %s %s", node, alias, beercode, beername); |
|
157 } |
|
158 } |
|
159 if (mode) { |
|
160 end = stpcpy(query, "UPDATE mon_ispindels SET mode='"); |
|
161 end += mysql_real_escape_string(con2, end, mode, strlen(mode)); |
|
162 end = stpcpy(end, "' WHERE node='"); |
|
163 end += mysql_real_escape_string(con2, end, node, strlen(node)); |
|
164 end = stpcpy(end, "'"); |
|
165 |
|
166 if (mysql_real_query(con2, query, (unsigned int) (end - query))) { |
|
167 syslog(LOG_NOTICE, "MySQL: `%s' error %u (%s))", query, mysql_errno(con2), mysql_error(con2)); |
|
168 } else { |
|
169 /* Database updated, now update internal memory */ |
|
170 syslog(LOG_NOTICE, "MySQL: `%s' Ok", query); |
|
171 if (tmpp->mode) |
|
172 free(tmpp->mode); |
|
173 tmpp->mode = xstrcpy(mode); |
|
174 /* Report new state to the client */ |
|
175 ispindel_ws_send(tmpp); |
|
176 syslog(LOG_NOTICE, "Set ispindel %s/%s new mode %s", node, alias, mode); |
|
177 } |
|
178 } |
|
179 mysql_close(con2); |
|
180 } |
|
181 } |
|
182 break; |
|
183 } |
|
184 } |
|
185 } |
|
186 |
|
187 if (node) |
|
188 free(node); |
|
189 if (alias) |
|
190 free(alias); |
|
191 if (mode) |
|
192 free(mode); |
|
193 if (beeruuid) |
|
194 free(beeruuid); |
|
195 if (beercode) |
|
196 free(beercode); |
|
197 if (beername) |
|
198 free(beername); |
|
199 } |
|
200 |
|
201 |
43 |
202 |
44 void ispindel_set(char *node, char *payload) |
203 void ispindel_set(char *node, char *payload) |
45 { |
204 { |
46 sys_ispindel_list *ispindel, *tmpp; |
205 sys_ispindel_list *ispindel, *tmpp; |
47 struct json_object *jobj, *metric, *val; |
206 struct json_object *jobj, *metric, *val; |
48 bool new_ispindel = true; |
207 bool new_ispindel = true; |
49 char *datetime, buf[65], *line, *logfile, *msg = NULL; |
208 char *datetime, buf[65], *line, *logfile; |
50 struct tm *mytime; |
209 struct tm *mytime; |
51 time_t timestamp; |
210 time_t timestamp; |
52 FILE *fp; |
211 FILE *fp; |
53 |
212 |
54 // if (debug) |
213 // syslog(LOG_NOTICE, "ispindel_set: %s %s", node, payload); |
55 // printf("ispindel_set: %s %s\n", node, payload); |
|
56 |
214 |
57 /* |
215 /* |
58 * Search ispindel record in the memory array and use it if found. |
216 * Search ispindel record in the memory array and use it if found. |
59 */ |
217 */ |
60 if (ispindels) { |
218 if (ispindels) { |
100 if (json_object_object_get_ex(metric, "alias", &val)) { |
258 if (json_object_object_get_ex(metric, "alias", &val)) { |
101 if (ispindel->alias) |
259 if (ispindel->alias) |
102 free(ispindel->alias); |
260 free(ispindel->alias); |
103 ispindel->alias = xstrcpy((char *)json_object_get_string(val)); |
261 ispindel->alias = xstrcpy((char *)json_object_get_string(val)); |
104 } |
262 } |
105 if (json_object_object_get_ex(metric, "alarm", &val)) { |
263 if (json_object_object_get_ex(metric, "alarm", &val)) |
106 ispindel->alarm = json_object_get_int(val); |
264 ispindel->alarm = json_object_get_int(val); |
107 } |
265 if (json_object_object_get_ex(metric, "interval", &val)) |
108 if (json_object_object_get_ex(metric, "interval", &val)) { |
|
109 ispindel->interval = json_object_get_int(val); |
266 ispindel->interval = json_object_get_int(val); |
110 } |
267 if (json_object_object_get_ex(metric, "angle", &val)) |
111 if (json_object_object_get_ex(metric, "angle", &val)) { |
|
112 ispindel->angle = json_object_get_double(val); |
268 ispindel->angle = json_object_get_double(val); |
113 } |
269 if (json_object_object_get_ex(metric, "temperature", &val)) |
114 if (json_object_object_get_ex(metric, "temperature", &val)) { |
|
115 ispindel->temperature = json_object_get_double(val); |
270 ispindel->temperature = json_object_get_double(val); |
116 } |
271 if (json_object_object_get_ex(metric, "battery", &val)) |
117 if (json_object_object_get_ex(metric, "battery", &val)) { |
|
118 ispindel->battery = json_object_get_double(val); |
272 ispindel->battery = json_object_get_double(val); |
119 } |
|
120 if (json_object_object_get_ex(metric, "gravity", &val)) { |
273 if (json_object_object_get_ex(metric, "gravity", &val)) { |
121 ispindel->gravity = json_object_get_double(val); |
274 ispindel->gravity = json_object_get_double(val); |
|
275 if (ispindel->gravity > ispindel->og_gravity) |
|
276 ispindel->og_gravity = ispindel->gravity; |
122 } |
277 } |
123 } |
278 } |
124 |
279 |
125 msg = xstrcpy((char *)"{\"device\":\"ispindels\",\"node\":\""); |
|
126 msg = xstrcat(msg, node); |
|
127 msg = xstrcat(msg, (char *)"\",\"unit\":\""); |
|
128 msg = xstrcat(msg, ispindel->alias); |
|
129 msg = xstrcat(msg, (char *)"\",\"online\":"); |
|
130 msg = xstrcat(msg, ispindel->online ? (char *)"1":(char *)"0"); |
|
131 msg = xstrcat(msg, (char *)",\"mode\":\""); |
|
132 msg = xstrcat(msg, ispindel->mode); |
|
133 msg = xstrcat(msg, (char *)"\",\"temperature\":"); |
|
134 snprintf(buf, 64, "%.4f", ispindel->temperature); |
|
135 msg = xstrcat(msg, buf); |
|
136 msg = xstrcat(msg, (char *)",\"angle\":"); |
|
137 snprintf(buf, 64, "%.5f", ispindel->angle); |
|
138 msg = xstrcat(msg, buf); |
|
139 msg = xstrcat(msg, (char *)",\"battery\":"); |
|
140 snprintf(buf, 64, "%.6f", ispindel->battery); |
|
141 msg = xstrcat(msg, buf); |
|
142 msg = xstrcat(msg, (char *)",\"gravity\":"); |
|
143 snprintf(buf, 64, "%.5f", ispindel->gravity); |
|
144 msg = xstrcat(msg, buf); |
|
145 msg = xstrcat(msg, (char *)",\"alarm\":"); |
|
146 snprintf(buf, 64, "%d", ispindel->alarm); |
|
147 msg = xstrcat(msg, buf); |
|
148 msg = xstrcat(msg, (char *)"}"); |
|
149 ws_broadcast(msg); |
|
150 free(msg); |
|
151 msg = NULL; |
|
152 |
|
153 // ispindel_dump(ispindel); |
280 // ispindel_dump(ispindel); |
|
281 |
154 |
282 |
155 if (new_ispindel) { |
283 if (new_ispindel) { |
156 if (ispindels == NULL) { |
284 if (ispindels == NULL) { |
157 ispindels = ispindel; |
285 ispindels = ispindel; |
158 } else { |
286 } else { |
185 line = xstrcat(line, buf); |
314 line = xstrcat(line, buf); |
186 line = xstrcat(line, (char *)","); |
315 line = xstrcat(line, (char *)","); |
187 snprintf(buf, 64, "%.5f", ispindel->gravity); |
316 snprintf(buf, 64, "%.5f", ispindel->gravity); |
188 line = xstrcat(line, buf); |
317 line = xstrcat(line, buf); |
189 line = xstrcat(line, (char *)","); |
318 line = xstrcat(line, (char *)","); |
190 // snprintf(buf, 64, "%.5f", 1 + (ispindel->gravity / (258.6 - ((ispindel->gravity / 258.2) * 227.1)))); |
|
191 snprintf(buf, 64, "%.5f", 1.00001 + (0.0038661 * ispindel->gravity) + (1.3488e-5 * ispindel->gravity * ispindel->gravity) + |
319 snprintf(buf, 64, "%.5f", 1.00001 + (0.0038661 * ispindel->gravity) + (1.3488e-5 * ispindel->gravity * ispindel->gravity) + |
192 (4.3074e-8 * ispindel->gravity * ispindel->gravity * ispindel->gravity)); |
320 (4.3074e-8 * ispindel->gravity * ispindel->gravity * ispindel->gravity)); |
193 line = xstrcat(line, buf); |
321 line = xstrcat(line, buf); |
194 line = xstrcat(line, (char *)","); |
322 line = xstrcat(line, (char *)","); |
195 snprintf(buf, 64, "%.6f", ispindel->battery); |
323 snprintf(buf, 64, "%.6f", ispindel->battery); |