bmsd/co2meters.c

changeset 506
8ab0e87d579e
parent 505
c09b67fd8323
child 547
4d9c96545246
equal deleted inserted replaced
505:c09b67fd8323 506:8ab0e87d579e
31 31
32 sys_co2meter_list *co2meters = NULL; 32 sys_co2meter_list *co2meters = NULL;
33 33
34 extern int debug; 34 extern int debug;
35 extern sys_config Config; 35 extern sys_config Config;
36 extern MYSQL *con;
37 extern MYSQL_RES *res_set;
38 extern MYSQL_ROW row;
36 39
37 40
38 41
39 void co2meter_set(char *edge_node, char *alias, char *payload) 42 void co2meter_set(char *edge_node, char *alias, char *payload)
40 { 43 {
209 212
210 213
211 214
212 void co2meter_log(char *topic, char *payload) 215 void co2meter_log(char *topic, char *payload)
213 { 216 {
214 char *edge_node, *alias, *line, buf[65], *logfile; 217 char *edge_node, *alias, *line, buf[128], *logfile;
215 struct json_object *jobj, *val, *metric; 218 struct json_object *jobj, *val, *metric;
216 co2pressure_log *log; 219 co2pressure_log *log;
217 struct tm *mytime; 220 struct tm *mytime;
218 time_t timestamp; 221 time_t timestamp;
219 FILE *fp; 222 FILE *fp;
225 alias = strtok(NULL, "/\0"); 228 alias = strtok(NULL, "/\0");
226 229
227 log = (co2pressure_log *)malloc(sizeof(co2pressure_log)); 230 log = (co2pressure_log *)malloc(sizeof(co2pressure_log));
228 memset(log, 0, sizeof(co2pressure_log)); 231 memset(log, 0, sizeof(co2pressure_log));
229 232
230 log->co2meter_node = xstrcpy(edge_node); 233 log->node = xstrcpy(edge_node);
231 log->co2meter_alias = xstrcpy(alias); 234 log->alias = xstrcpy(alias);
232 jobj = json_tokener_parse(payload); 235 jobj = json_tokener_parse(payload);
233 236
234 timestamp = time(NULL); 237 timestamp = time(NULL);
235 log->datetime = malloc(21); 238 log->datetime = malloc(21);
236 mytime = localtime(&timestamp); 239 mytime = localtime(&timestamp);
237 snprintf(log->datetime, 20, "%04d-%02d-%02d %02d:%02d:%02d", 240 snprintf(log->datetime, 20, "%04d-%02d-%02d %02d:%02d:%02d",
238 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); 241 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
239 242
240 if (json_object_object_get_ex(jobj, "metric", &metric)) { 243 if (json_object_object_get_ex(jobj, "metric", &metric)) {
241 244 if (json_object_object_get_ex(metric, "uuid", &val)) {
242 /* if (json_object_object_get_ex(metric2, "uuid", &val)) {
243 if (strcmp((char *)"(null)", json_object_get_string(val)))
244 log->product_uuid = xstrcpy((char *)json_object_get_string(val));
245 }
246 if (json_object_object_get_ex(metric2, "code", &val)) {
247 if (strcmp((char *)"(null)", json_object_get_string(val)))
248 log->product_code = xstrcpy((char *)json_object_get_string(val));
249 }
250 if (json_object_object_get_ex(metric2, "name", &val)) {
251 if (strcmp((char *)"(null)", json_object_get_string(val)))
252 log->product_name = xstrcpy((char *)json_object_get_string(val));
253 }
254 */
255 if (json_object_object_get_ex(metric, "co2meter_uuid", &val)) {
256 if (strcmp((char *)"(null)", json_object_get_string(val))) 245 if (strcmp((char *)"(null)", json_object_get_string(val)))
257 log->co2meter_uuid = xstrcpy((char *)json_object_get_string(val)); 246 log->uuid = xstrcpy((char *)json_object_get_string(val));
258 } 247 }
259 if (json_object_object_get_ex(metric, "temperature", &val)) { 248 if (json_object_object_get_ex(metric, "temperature", &val)) {
260 log->temperature = json_object_get_double(val); 249 log->temperature = json_object_get_double(val);
261 } 250 }
262 if (json_object_object_get_ex(metric, "pressure", &val)) { 251 if (json_object_object_get_ex(metric, "pressure", &val)) {
267 256
268 /* 257 /*
269 * Because co2meters are not so smart and don't hold product information 258 * Because co2meters are not so smart and don't hold product information
270 * search the missing pieces in the database. 259 * search the missing pieces in the database.
271 */ 260 */
272 // log->co2meter_uuid is the search, fill: 261 snprintf(buf, 127, "SELECT beercode,beername,beeruuid FROM mon_co2meters WHERE uuid='%s'", log->uuid);
273 // log->product_uuid log->product_name log->product_code 262 if (mysql_query(con, buf)) {
274 // log->co2meter_node log->co2meter_alias 263 syslog(LOG_NOTICE, "MySQL: %s error %u (%s))", buf, mysql_errno(con), mysql_error(con));
264 } else {
265 res_set = mysql_store_result(con);
266 if (res_set == NULL) {
267 syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con), mysql_error(con));
268 } else {
269 if ((row = mysql_fetch_row(res_set)) != NULL) {
270 log->product_code = xstrcpy(row[0]);
271 log->product_name = xstrcpy(row[1]);
272 log->product_uuid = xstrcpy(row[2]);
273 }
274 }
275 }
275 276
276 /* 277 /*
277 * Build csv log line 278 * Build csv log line
278 */ 279 */
279 line = xstrcpy(log->datetime); 280 line = xstrcpy(log->datetime);
282 line = xstrcat(line, buf); 283 line = xstrcat(line, buf);
283 line = xstrcat(line, (char *)","); 284 line = xstrcat(line, (char *)",");
284 snprintf(buf, 64, "%.3f", log->pressure); 285 snprintf(buf, 64, "%.3f", log->pressure);
285 line = xstrcat(line, buf); 286 line = xstrcat(line, buf);
286 line = xstrcat(line, (char *)","); 287 line = xstrcat(line, (char *)",");
287 line = xstrcat(line, log->co2meter_uuid); 288 line = xstrcat(line, log->uuid);
288 289
289 /* 290 /*
290 * Build logfile name 291 * Build logfile name
291 */ 292 */
292 logfile = xstrcpy(Config.web_root); 293 logfile = xstrcpy(Config.web_root);
318 free(log->product_uuid ); 319 free(log->product_uuid );
319 if (log->product_code ) 320 if (log->product_code )
320 free(log->product_code ); 321 free(log->product_code );
321 if (log->product_name ) 322 if (log->product_name )
322 free(log->product_name ); 323 free(log->product_name );
323 if (log->co2meter_uuid) 324 if (log->uuid)
324 free(log->co2meter_uuid); 325 free(log->uuid);
325 if (log->co2meter_node) 326 if (log->node)
326 free(log->co2meter_node); 327 free(log->node);
327 if (log->co2meter_alias) 328 if (log->alias)
328 free(log->co2meter_alias); 329 free(log->alias);
329 free(log); 330 free(log);
330 } 331 }
331 332
332 333
333 334

mercurial