bmsd/mysql.c

changeset 567
6bf0afc33e70
parent 558
a8e065a9f851
child 572
7a03181d29a3
--- a/bmsd/mysql.c	Tue Dec 10 20:13:00 2019 +0100
+++ b/bmsd/mysql.c	Fri Dec 13 16:49:50 2019 +0100
@@ -36,6 +36,7 @@
 extern sys_node_list		*nodes;
 extern sys_fermenter_list	*fermenters;
 extern sys_co2meter_list	*co2meters;
+extern sys_ispindel_list	*ispindels;
 extern int              	debug;
 
 
@@ -62,7 +63,8 @@
     sys_node_list	*node, *tmpp;
     sys_fermenter_list	*fermenter, *tmpf;
     sys_co2meter_list	*co2meter, *tmpc;
-    int			ccnt = 0, ncnt = 0, fcnt = 0;
+    sys_ispindel_list   *ispindel, *tmpi;
+    int			icnt = 0, ccnt = 0, ncnt = 0, fcnt = 0;
 
     con = mysql_init(NULL);
     if (con == NULL) {
@@ -115,6 +117,7 @@
 		node->net_address   = xstrcpy(row[18]);
 		node->net_ifname    = xstrcpy(row[19]);
 		node->net_rssi      = atoi(row[20]);
+		node->interval      = atoi(row[21]);
 
 		if (nodes == NULL) {
 		    nodes = node;
@@ -272,7 +275,47 @@
 	}
     }
 
-    syslog(LOG_NOTICE, "MySQL: loaded %d nodes, %d fermenters, %d co2meters", ncnt, fcnt, ccnt);
+    if (mysql_query(con, "SELECT * FROM mon_ispindels")) {
+        syslog(LOG_NOTICE, "MySQL: SELECT * FROM mon_ispindels error %u (%s))", mysql_errno(con), mysql_error(con));
+    } else {
+        res_set = mysql_store_result(con);
+        if (res_set == NULL) {
+            syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con), mysql_error(con));
+        } else {
+            while ((row = mysql_fetch_row(res_set)) != NULL) {
+                ispindel = (sys_ispindel_list *)malloc(sizeof(sys_ispindel_list));
+                memset(ispindel, 0, sizeof(sys_ispindel_list));
+                ispindel->next                = NULL;
+		ispindel->node                = xstrcpy(row[1]);
+		ispindel->online              = 0;  // Will be set later
+		ispindel->alarm               = atoi(row[3]);
+		ispindel->beercode            = xstrcpy(row[4]);
+                ispindel->beername            = xstrcpy(row[5]);
+                ispindel->beeruuid            = xstrcpy(row[6]);
+		ispindel->tilt                = atof(row[7]);
+		ispindel->temperature         = atof(row[8]);
+                ispindel->battery             = atof(row[9]);
+		ispindel->gravity             = atof(row[10]);
+		ispindel->interval            = atoi(row[11]);
+		ispindel->rssi                = atoi(row[12]);
+
+                if (ispindels == NULL) {
+                    ispindels = ispindel;
+                } else {
+                    for (tmpi = ispindels; tmpi; tmpi = tmpi->next) {
+                        if (tmpi->next == NULL) {
+                            tmpi->next = ispindel;
+                            break;
+                        }
+                    }
+                }
+                icnt++;
+            }
+            mysql_free_result(res_set);
+        }
+    }
+
+    syslog(LOG_NOTICE, "MySQL: loaded %d nodes, %d fermenters, %d co2meters %d ispindels", ncnt, fcnt, ccnt, icnt);
     return 0;
 }
 
@@ -657,3 +700,46 @@
 }
 
 
+
+void ispindel_mysql_insert(sys_ispindel_list *ispindel)
+{
+    char        *query = malloc(2560);
+
+    snprintf(query, 2559,
+        "INSERT INTO mon_ispindels SET node='%s', online='%d', alarm='%d', " \
+        "tilt='%.3f', temperature='%.3f', battery='%.3f', gravity='%.3f', interval='%d', rssi='%d'",
+        ispindel->node, ispindel->online ? 1:0, ispindel->alarm,
+        ispindel->tilt, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->rssi);
+
+    if (bms_mysql_query(query) == 0) {
+        syslog(LOG_NOTICE,  "MySQL: insert new ispindel %s", ispindel->node);
+    }
+    free(query);
+}
+
+
+
+void ispindel_mysql_update(sys_ispindel_list *ispindel)
+{
+    char        *query = malloc(2560);
+
+    snprintf(query, 2559,
+        "UPDATE mon_ispindels SET online='%d', alarm='%d', " \
+	"tilt='%.3f', temperature='%.3f', battery='%.3f', gravity='%.3f', interval='%d', rssi='%d' WHERE node='%s'",
+        ispindel->online ? 1:0, ispindel->alarm,
+	ispindel->tilt, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->rssi, ispindel->node);
+
+    bms_mysql_query(query);
+    free(query);
+}
+
+
+
+void ispindel_mysql_death(char *node)
+{
+    char        *query = malloc(512);
+
+    snprintf(query, 511, "UPDATE mon_ispindels SET online='0' WHERE node='%s'", node);
+    bms_mysql_query(query);
+    free(query);
+}

mercurial