bmsd/mysql.c

changeset 567
6bf0afc33e70
parent 558
a8e065a9f851
child 572
7a03181d29a3
equal deleted inserted replaced
566:e526dc911bc1 567:6bf0afc33e70
34 34
35 extern sys_config Config; 35 extern sys_config Config;
36 extern sys_node_list *nodes; 36 extern sys_node_list *nodes;
37 extern sys_fermenter_list *fermenters; 37 extern sys_fermenter_list *fermenters;
38 extern sys_co2meter_list *co2meters; 38 extern sys_co2meter_list *co2meters;
39 extern sys_ispindel_list *ispindels;
39 extern int debug; 40 extern int debug;
40 41
41 42
42 time_t datetime_to_time_t(char *dt_string) 43 time_t datetime_to_time_t(char *dt_string)
43 { 44 {
60 int bms_mysql_init(void) 61 int bms_mysql_init(void)
61 { 62 {
62 sys_node_list *node, *tmpp; 63 sys_node_list *node, *tmpp;
63 sys_fermenter_list *fermenter, *tmpf; 64 sys_fermenter_list *fermenter, *tmpf;
64 sys_co2meter_list *co2meter, *tmpc; 65 sys_co2meter_list *co2meter, *tmpc;
65 int ccnt = 0, ncnt = 0, fcnt = 0; 66 sys_ispindel_list *ispindel, *tmpi;
67 int icnt = 0, ccnt = 0, ncnt = 0, fcnt = 0;
66 68
67 con = mysql_init(NULL); 69 con = mysql_init(NULL);
68 if (con == NULL) { 70 if (con == NULL) {
69 syslog(LOG_NOTICE, "MySQL: mysql_init() failed"); 71 syslog(LOG_NOTICE, "MySQL: mysql_init() failed");
70 return 1; 72 return 1;
113 node->gps_longitude = atof(row[16]); 115 node->gps_longitude = atof(row[16]);
114 node->gps_altitude = atof(row[17]); 116 node->gps_altitude = atof(row[17]);
115 node->net_address = xstrcpy(row[18]); 117 node->net_address = xstrcpy(row[18]);
116 node->net_ifname = xstrcpy(row[19]); 118 node->net_ifname = xstrcpy(row[19]);
117 node->net_rssi = atoi(row[20]); 119 node->net_rssi = atoi(row[20]);
120 node->interval = atoi(row[21]);
118 121
119 if (nodes == NULL) { 122 if (nodes == NULL) {
120 nodes = node; 123 nodes = node;
121 } else { 124 } else {
122 for (tmpp = nodes; tmpp; tmpp = tmpp->next) { 125 for (tmpp = nodes; tmpp; tmpp = tmpp->next) {
270 } 273 }
271 mysql_free_result(res_set); 274 mysql_free_result(res_set);
272 } 275 }
273 } 276 }
274 277
275 syslog(LOG_NOTICE, "MySQL: loaded %d nodes, %d fermenters, %d co2meters", ncnt, fcnt, ccnt); 278 if (mysql_query(con, "SELECT * FROM mon_ispindels")) {
279 syslog(LOG_NOTICE, "MySQL: SELECT * FROM mon_ispindels error %u (%s))", mysql_errno(con), mysql_error(con));
280 } else {
281 res_set = mysql_store_result(con);
282 if (res_set == NULL) {
283 syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con), mysql_error(con));
284 } else {
285 while ((row = mysql_fetch_row(res_set)) != NULL) {
286 ispindel = (sys_ispindel_list *)malloc(sizeof(sys_ispindel_list));
287 memset(ispindel, 0, sizeof(sys_ispindel_list));
288 ispindel->next = NULL;
289 ispindel->node = xstrcpy(row[1]);
290 ispindel->online = 0; // Will be set later
291 ispindel->alarm = atoi(row[3]);
292 ispindel->beercode = xstrcpy(row[4]);
293 ispindel->beername = xstrcpy(row[5]);
294 ispindel->beeruuid = xstrcpy(row[6]);
295 ispindel->tilt = atof(row[7]);
296 ispindel->temperature = atof(row[8]);
297 ispindel->battery = atof(row[9]);
298 ispindel->gravity = atof(row[10]);
299 ispindel->interval = atoi(row[11]);
300 ispindel->rssi = atoi(row[12]);
301
302 if (ispindels == NULL) {
303 ispindels = ispindel;
304 } else {
305 for (tmpi = ispindels; tmpi; tmpi = tmpi->next) {
306 if (tmpi->next == NULL) {
307 tmpi->next = ispindel;
308 break;
309 }
310 }
311 }
312 icnt++;
313 }
314 mysql_free_result(res_set);
315 }
316 }
317
318 syslog(LOG_NOTICE, "MySQL: loaded %d nodes, %d fermenters, %d co2meters %d ispindels", ncnt, fcnt, ccnt, icnt);
276 return 0; 319 return 0;
277 } 320 }
278 321
279 322
280 323
655 bms_mysql_query(query); 698 bms_mysql_query(query);
656 free(query); 699 free(query);
657 } 700 }
658 701
659 702
703
704 void ispindel_mysql_insert(sys_ispindel_list *ispindel)
705 {
706 char *query = malloc(2560);
707
708 snprintf(query, 2559,
709 "INSERT INTO mon_ispindels SET node='%s', online='%d', alarm='%d', " \
710 "tilt='%.3f', temperature='%.3f', battery='%.3f', gravity='%.3f', interval='%d', rssi='%d'",
711 ispindel->node, ispindel->online ? 1:0, ispindel->alarm,
712 ispindel->tilt, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->rssi);
713
714 if (bms_mysql_query(query) == 0) {
715 syslog(LOG_NOTICE, "MySQL: insert new ispindel %s", ispindel->node);
716 }
717 free(query);
718 }
719
720
721
722 void ispindel_mysql_update(sys_ispindel_list *ispindel)
723 {
724 char *query = malloc(2560);
725
726 snprintf(query, 2559,
727 "UPDATE mon_ispindels SET online='%d', alarm='%d', " \
728 "tilt='%.3f', temperature='%.3f', battery='%.3f', gravity='%.3f', interval='%d', rssi='%d' WHERE node='%s'",
729 ispindel->online ? 1:0, ispindel->alarm,
730 ispindel->tilt, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->rssi, ispindel->node);
731
732 bms_mysql_query(query);
733 free(query);
734 }
735
736
737
738 void ispindel_mysql_death(char *node)
739 {
740 char *query = malloc(512);
741
742 snprintf(query, 511, "UPDATE mon_ispindels SET online='0' WHERE node='%s'", node);
743 bms_mysql_query(query);
744 free(query);
745 }

mercurial