42 |
42 |
43 void ispindel_set(char *node, char *key, char *payload) |
43 void ispindel_set(char *node, char *key, char *payload) |
44 { |
44 { |
45 sys_ispindel_list *ispindel, *tmpp; |
45 sys_ispindel_list *ispindel, *tmpp; |
46 bool new_ispindel = true, do_update = false; |
46 bool new_ispindel = true, do_update = false; |
47 char *t, *p; |
47 char *t, *p, *datetime, buf[65], *line, *logfile; |
48 float temperature = 20; |
48 float temperature = 20; |
49 uint8_t temp_units = 'C'; |
49 uint8_t temp_units = 'C'; |
|
50 struct tm *mytime; |
|
51 time_t timestamp; |
|
52 FILE *fp; |
50 |
53 |
51 fprintf(stdout, "ispindel_set: %s %s\n", node, payload); |
54 fprintf(stdout, "ispindel_set: %s %s\n", node, payload); |
52 |
55 |
53 /* |
56 /* |
54 * Search ispindel record in the memory array and use it if found. |
57 * Search ispindel record in the memory array and use it if found. |
140 } |
141 } |
141 ispindel_mysql_insert(ispindel); |
142 ispindel_mysql_insert(ispindel); |
142 } else if (do_update) { |
143 } else if (do_update) { |
143 ispindel_mysql_update(ispindel); |
144 ispindel_mysql_update(ispindel); |
144 } |
145 } |
|
146 |
|
147 /* |
|
148 * The data is complete, see if we can write a log entry. |
|
149 */ |
|
150 if (do_update && ispindel->beercode && strlen(ispindel->beercode) && ispindel->beername && strlen(ispindel->beername)) { |
|
151 datetime = malloc(72); |
|
152 mytime = localtime(×tamp); |
|
153 snprintf(datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d", |
|
154 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
|
155 |
|
156 line = xstrcpy(datetime); |
|
157 line = xstrcat(line, (char *)","); |
|
158 snprintf(buf, 64, "%.3f", ispindel->tilt); |
|
159 line = xstrcat(line, buf); |
|
160 line = xstrcat(line, (char *)","); |
|
161 snprintf(buf, 64, "%.3f", ispindel->temperature); |
|
162 line = xstrcat(line, buf); |
|
163 line = xstrcat(line, (char *)","); |
|
164 snprintf(buf, 64, "%.3f", ispindel->battery); |
|
165 line = xstrcat(line, buf); |
|
166 line = xstrcat(line, (char *)","); |
|
167 snprintf(buf, 64, "%.3f", ispindel->gravity); |
|
168 line = xstrcat(line, buf); |
|
169 line = xstrcat(line, (char *)","); |
|
170 snprintf(buf, 64, "%d", ispindel->interval); |
|
171 line = xstrcat(line, buf); |
|
172 line = xstrcat(line, (char *)","); |
|
173 snprintf(buf, 64, "%d", ispindel->rssi); |
|
174 line = xstrcat(line, buf); |
|
175 |
|
176 /* |
|
177 * Build logfile name |
|
178 */ |
|
179 logfile = xstrcpy(Config.web_root); |
|
180 logfile = xstrcat(logfile, (char *)"/log/ispindel/"); |
|
181 logfile = xstrcat(logfile, ispindel->beercode); |
|
182 logfile = xstrcat(logfile, (char *)" "); |
|
183 logfile = xstrcat(logfile, ispindel->beername); |
|
184 logfile = xstrcat(logfile, (char *)".log"); |
|
185 |
|
186 fp = fopen(logfile, "a"); |
|
187 if (fp) { |
|
188 fprintf(fp, "%s\n", line); |
|
189 fclose(fp); |
|
190 } else { |
|
191 syslog(LOG_NOTICE, "cannot append to `%s'", logfile); |
|
192 } |
|
193 |
|
194 free(logfile); |
|
195 logfile = NULL; |
|
196 free(line); |
|
197 line = NULL; |
|
198 free(datetime); |
|
199 datetime = NULL; |
|
200 } |
145 } |
201 } |
146 |
202 |
147 |
203 |
148 |
204 |
149 /* |
205 /* |
161 syslog(LOG_NOTICE, "ispindel_mqtt(%s, %s) error", topic, payload); |
217 syslog(LOG_NOTICE, "ispindel_mqtt(%s, %s) error", topic, payload); |
162 return; |
218 return; |
163 } |
219 } |
164 |
220 |
165 ispindel_set(node, keyword, payload); |
221 ispindel_set(node, keyword, payload); |
166 } |
|
167 |
|
168 |
|
169 |
|
170 void ispindel_log(char *topic, char *payload) |
|
171 { |
|
172 char *edge_node, *alias, *line, buf[128], *logfile; |
|
173 struct json_object *jobj, *val, *metric; |
|
174 co2pressure_log *log; |
|
175 struct tm *mytime; |
|
176 time_t timestamp; |
|
177 FILE *fp; |
|
178 |
|
179 strtok(topic, "/"); // ignore namespace |
|
180 strtok(NULL, "/"); // group_id |
|
181 strtok(NULL, "/"); // message_type |
|
182 edge_node = strtok(NULL, "/\0"); |
|
183 alias = strtok(NULL, "/\0"); |
|
184 |
|
185 log = (co2pressure_log *)malloc(sizeof(co2pressure_log)); |
|
186 memset(log, 0, sizeof(co2pressure_log)); |
|
187 |
|
188 log->node = xstrcpy(edge_node); |
|
189 log->alias = xstrcpy(alias); |
|
190 jobj = json_tokener_parse(payload); |
|
191 |
|
192 timestamp = time(NULL); |
|
193 log->datetime = malloc(73); |
|
194 mytime = localtime(×tamp); |
|
195 snprintf(log->datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d", |
|
196 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
|
197 |
|
198 if (json_object_object_get_ex(jobj, "metric", &metric)) { |
|
199 if (json_object_object_get_ex(metric, "uuid", &val)) { |
|
200 if (strcmp((char *)"(null)", json_object_get_string(val))) |
|
201 log->uuid = xstrcpy((char *)json_object_get_string(val)); |
|
202 } |
|
203 if (json_object_object_get_ex(metric, "temperature", &val)) { |
|
204 log->temperature = json_object_get_double(val); |
|
205 } |
|
206 if (json_object_object_get_ex(metric, "pressure", &val)) { |
|
207 log->pressure = json_object_get_double(val); |
|
208 } |
|
209 } |
|
210 json_object_put(jobj); |
|
211 |
|
212 /* |
|
213 * Because ispindels are not so smart and don't hold product information |
|
214 * search the missing pieces in the database. |
|
215 */ |
|
216 snprintf(buf, 127, "SELECT beercode,beername,beeruuid FROM mon_ispindels WHERE uuid='%s'", log->uuid); |
|
217 if (mysql_query(con, buf)) { |
|
218 syslog(LOG_NOTICE, "MySQL: %s error %u (%s))", buf, mysql_errno(con), mysql_error(con)); |
|
219 } else { |
|
220 res_set = mysql_store_result(con); |
|
221 if (res_set == NULL) { |
|
222 syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con), mysql_error(con)); |
|
223 } else { |
|
224 if ((row = mysql_fetch_row(res_set)) != NULL) { |
|
225 /* |
|
226 * Ignore when the beer_name or beer_code is not set. |
|
227 */ |
|
228 if ((int)strlen(row[0]) == 0 || (int)strlen(row[1]) == 0) { |
|
229 if (log->datetime) |
|
230 free(log->datetime); |
|
231 if (log->uuid) |
|
232 free(log->uuid); |
|
233 if (log->node) |
|
234 free(log->node); |
|
235 if (log->alias) |
|
236 free(log->alias); |
|
237 free(log); |
|
238 return; |
|
239 } |
|
240 log->product_code = xstrcpy(row[0]); |
|
241 log->product_name = xstrcpy(row[1]); |
|
242 log->product_uuid = xstrcpy(row[2]); |
|
243 } |
|
244 } |
|
245 } |
|
246 |
|
247 /* |
|
248 * Build csv log line |
|
249 */ |
|
250 line = xstrcpy(log->datetime); |
|
251 line = xstrcat(line, (char *)","); |
|
252 snprintf(buf, 64, "%.3f", log->temperature); |
|
253 line = xstrcat(line, buf); |
|
254 line = xstrcat(line, (char *)","); |
|
255 snprintf(buf, 64, "%.3f", log->pressure); |
|
256 line = xstrcat(line, buf); |
|
257 line = xstrcat(line, (char *)","); |
|
258 line = xstrcat(line, log->uuid); |
|
259 |
|
260 /* |
|
261 * Build logfile name |
|
262 */ |
|
263 logfile = xstrcpy(Config.web_root); |
|
264 logfile = xstrcat(logfile, (char *)"/log/co2pressure/"); |
|
265 logfile = xstrcat(logfile, log->product_code); |
|
266 logfile = xstrcat(logfile, (char *)" "); |
|
267 logfile = xstrcat(logfile, log->product_name); |
|
268 logfile = xstrcat(logfile, (char *)".log"); |
|
269 |
|
270 if (debug) |
|
271 fprintf(stdout, "%s %s\n", logfile, line); |
|
272 |
|
273 fp = fopen(logfile, "a"); |
|
274 if (fp) { |
|
275 fprintf(fp, "%s\n", line); |
|
276 fclose(fp); |
|
277 } else { |
|
278 syslog(LOG_NOTICE, "cannot append to `%s'", logfile); |
|
279 } |
|
280 |
|
281 free(logfile); |
|
282 logfile = NULL; |
|
283 free(line); |
|
284 line = NULL; |
|
285 |
|
286 if (log->datetime) |
|
287 free(log->datetime); |
|
288 if (log->product_uuid ) |
|
289 free(log->product_uuid ); |
|
290 if (log->product_code ) |
|
291 free(log->product_code ); |
|
292 if (log->product_name ) |
|
293 free(log->product_name ); |
|
294 if (log->uuid) |
|
295 free(log->uuid); |
|
296 if (log->node) |
|
297 free(log->node); |
|
298 if (log->alias) |
|
299 free(log->alias); |
|
300 free(log); |
|
301 } |
222 } |
302 |
223 |
303 |
224 |
304 |
225 |
305 void ispindel_dump(sys_ispindel_list *ispindel) |
226 void ispindel_dump(sys_ispindel_list *ispindel) |