bmsd/ispindels.c

changeset 852
71f0fa38b634
parent 850
26cc7ea0f790
child 853
f54decd31acb
equal deleted inserted replaced
851:b15fa90a9af5 852:71f0fa38b634
229 void ispindel_set(char *node, char *payload) 229 void ispindel_set(char *node, char *payload)
230 { 230 {
231 sys_ispindel_list *ispindel, *tmpp; 231 sys_ispindel_list *ispindel, *tmpp;
232 struct json_object *jobj, *metric, *val; 232 struct json_object *jobj, *metric, *val;
233 bool new_ispindel = true; 233 bool new_ispindel = true;
234 char *datetime, buf[65], *line, *logfile, *query = malloc(512); 234 char *datetime, *query = malloc(512);
235 struct tm *mytime; 235 struct tm *mytime;
236 time_t timestamp; 236 time_t timestamp;
237 FILE *fp;
238 237
239 //syslog(LOG_NOTICE, "ispindel_set: %s %s", node, payload); 238 //syslog(LOG_NOTICE, "ispindel_set: %s %s", node, payload);
240 239
241 /* 240 /*
242 * Search ispindel record in the memory array and use it if found. 241 * Search ispindel record in the memory array and use it if found.
339 timestamp = time(NULL); 338 timestamp = time(NULL);
340 mytime = localtime(&timestamp); 339 mytime = localtime(&timestamp);
341 snprintf(datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d", 340 snprintf(datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d",
342 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); 341 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
343 342
344 line = xstrcpy(datetime);
345 line = xstrcat(line, (char *)",");
346 snprintf(buf, 64, "%.4f", ispindel->temperature);
347 line = xstrcat(line, buf);
348 line = xstrcat(line, (char *)",");
349 snprintf(buf, 64, "%.5f", ispindel->gravity);
350 line = xstrcat(line, buf);
351 line = xstrcat(line, (char *)",");
352 snprintf(buf, 64, "%.5f", plato_to_sg(ispindel->gravity));
353 line = xstrcat(line, buf);
354 line = xstrcat(line, (char *)",");
355 snprintf(buf, 64, "%.6f", ispindel->battery);
356 line = xstrcat(line, buf);
357 line = xstrcat(line, (char *)",");
358 snprintf(buf, 64, "%.5f", ispindel->angle);
359 line = xstrcat(line, buf);
360 line = xstrcat(line, (char *)",");
361 snprintf(buf, 64, "%d", ispindel->interval);
362 line = xstrcat(line, buf);
363 line = xstrcat(line, (char *)",");
364 line = xstrcat(line, ispindel->uuid);
365
366 snprintf(query, 511, "INSERT IGNORE INTO log_ispindel SET code='%s', datetime='%s', " \ 343 snprintf(query, 511, "INSERT IGNORE INTO log_ispindel SET code='%s', datetime='%s', " \
367 "temperature='%.4f', plato='%.5f', sg='%.5f', battery='%.6f', " \ 344 "temperature='%.4f', plato='%.5f', sg='%.5f', battery='%.6f', " \
368 "angle='%.5f', refresh='%d', uuid='%s'", 345 "angle='%.5f', refresh='%d', uuid='%s'",
369 ispindel->beercode, datetime, ispindel->temperature, ispindel->gravity, plato_to_sg(ispindel->gravity), 346 ispindel->beercode, datetime, ispindel->temperature, ispindel->gravity, plato_to_sg(ispindel->gravity),
370 ispindel->battery, ispindel->angle, ispindel->interval, ispindel->uuid); 347 ispindel->battery, ispindel->angle, ispindel->interval, ispindel->uuid);
371 //syslog(LOG_NOTICE, "%s", query); 348 syslog(LOG_NOTICE, "%s", query);
372 bms_mysql_query(query); 349 bms_mysql_query(query);
373 350
374 /*
375 * Build logfile name
376 */
377 logfile = xstrcpy(Config.web_root);
378 logfile = xstrcat(logfile, (char *)"/log/ispindel/");
379 logfile = xstrcat(logfile, ispindel->beercode);
380 logfile = xstrcat(logfile, (char *)" ");
381 logfile = xstrcat(logfile, ispindel->beername);
382 logfile = xstrcat(logfile, (char *)".log");
383
384 fp = fopen(logfile, "a");
385 if (fp) {
386 fprintf(fp, "%s\n", line);
387 fclose(fp);
388 } else {
389 syslog(LOG_NOTICE, "cannot append to `%s'", logfile);
390 }
391
392 free(logfile);
393 logfile = NULL;
394 free(line);
395 line = NULL;
396 free(datetime); 351 free(datetime);
397 datetime = NULL; 352 datetime = NULL;
398 } 353 }
399 } 354 }
400 355

mercurial