bmsd/co2meters.c

changeset 678
14322825cb3d
parent 677
6e82fece1f8f
child 680
0bb48333d133
--- a/bmsd/co2meters.c	Tue May 12 16:08:30 2020 +0200
+++ b/bmsd/co2meters.c	Thu May 14 14:38:20 2020 +0200
@@ -40,12 +40,147 @@
 
 
 
+void co2meter_ws_send(sys_co2meter_list *co2meter)
+{
+    char	*msg = NULL, buf[65];
+
+    msg = xstrcpy((char *)"{\"device\":\"co2meters\",\"node\":\"");
+    msg = xstrcat(msg, co2meter->node);
+    msg = xstrcat(msg, (char *)"\",\"unit\":\"");
+    msg = xstrcat(msg, co2meter->alias);
+    msg = xstrcat(msg, (char *)"\",\"online\":");
+    msg = xstrcat(msg, co2meter->online ? (char *)"1":(char *)"0");
+    msg = xstrcat(msg, (char *)",\"mode\":\"");
+    msg = xstrcat(msg, co2meter->mode);
+    msg = xstrcat(msg, (char *)"\",\"beercode\":\"");
+    msg = xstrcat(msg, co2meter->beercode);
+    msg = xstrcat(msg, (char *)"\",\"beername\":\"");
+    msg = xstrcat(msg, co2meter->beername);
+    msg = xstrcat(msg, (char *)"\",\"temperature\":");
+    snprintf(buf, 64, "%.3f", co2meter->temperature);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"pressure_channel\":");
+    snprintf(buf, 64, "%d", co2meter->pressure_channel);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"pressure_voltage\":");
+    snprintf(buf, 64, "%.3f", co2meter->pressure_voltage);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"pressure_zero\":");
+    snprintf(buf, 64, "%.3f", co2meter->pressure_zero);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"pressure_bar\":");
+    snprintf(buf, 64, "%.3f", co2meter->pressure_bar);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)",\"alarm\":");
+    snprintf(buf, 64, "%d", co2meter->alarm);
+    msg = xstrcat(msg, buf);
+    msg = xstrcat(msg, (char *)"}");
+    ws_broadcast(msg);
+    free(msg);
+    msg = NULL;
+
+}
+
+
+
+void co2meter_ws_receive(char *payload)
+{
+    struct json_object	*jobj, *val;
+    sys_co2meter_list	*tmpp;
+    char		*node = NULL, *alias = NULL, *beeruuid = NULL, *beercode = NULL, *beername = NULL;
+    char		query[512], *end;
+    MYSQL               *con2 = NULL;
+
+    syslog(LOG_NOTICE, "co2meter_ws_receive(%s)", payload);
+
+    /*
+     * Process the JSON formatted payload.
+     */
+    jobj = json_tokener_parse(payload);
+    if (json_object_object_get_ex(jobj, "node", &val))
+	node = xstrcpy((char *)json_object_get_string(val));
+    if (json_object_object_get_ex(jobj, "unit", &val))
+	alias = xstrcpy((char *)json_object_get_string(val));
+    if (json_object_object_get_ex(jobj, "beeruuid", &val))
+	beeruuid = xstrcpy((char *)json_object_get_string(val));
+    if (json_object_object_get_ex(jobj, "beercode", &val))
+	beercode = xstrcpy((char *)json_object_get_string(val));
+    if (json_object_object_get_ex(jobj, "beername", &val))
+	beername = xstrcpy((char *)json_object_get_string(val));
+    json_object_put(jobj);
+
+    /*
+     * Search co2meter record in the memory array and use it if found.
+     */
+    if (co2meters) {
+        for (tmpp = co2meters; tmpp; tmpp = tmpp->next) {
+            if ((strcmp(tmpp->alias, alias) == 0) && (strcmp(tmpp->node, node) == 0)) {
+		if (beeruuid && beercode && beername) {
+		    con2 = mysql_init(NULL);
+		    if (con2 == NULL) {
+			syslog(LOG_NOTICE, "MySQL: mysql_init() failed");
+		    } else {
+			if (mysql_real_connect(con2, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) {
+			    syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con2));
+			} else {
+			    end = stpcpy(query, "UPDATE mon_co2meters SET beeruuid='");
+			    end += mysql_real_escape_string(con2, end, beeruuid, strlen(beeruuid));
+			    end = stpcpy(end, "', beercode='");
+			    end += mysql_real_escape_string(con2, end, beercode, strlen(beercode));
+			    end = stpcpy(end, "', beername='");
+			    end += mysql_real_escape_string(con2, end, beername, strlen(beername));
+			    end = stpcpy(end, "' WHERE node='");
+			    end += mysql_real_escape_string(con2, end, node, strlen(node));
+			    end = stpcpy(end, "' AND alias='");
+			    end += mysql_real_escape_string(con2, end, alias, strlen(alias));
+			    end = stpcpy(end, "'");
+
+			    if (mysql_real_query(con2, query, (unsigned int) (end - query))) {
+				syslog(LOG_NOTICE, "MySQL: `%s' error %u (%s))", query, mysql_errno(con2), mysql_error(con2));
+			    } else {
+				/* Database updated, now update internal memory */
+				//syslog(LOG_NOTICE, "MySQL: `%s' Ok", query);
+				if (tmpp->beercode)
+                                    free(tmpp->beercode);
+                            	tmpp->beercode = xstrcpy(beercode);
+                            	if (tmpp->beername)
+                                    free(tmpp->beername);
+                            	tmpp->beername = xstrcpy(beername);
+				if (tmpp->beeruuid)
+				    free(tmpp->beeruuid);
+				tmpp->beeruuid = xstrcpy(beeruuid);
+				/* Report new state to the client */
+				co2meter_ws_send(tmpp);
+				syslog(LOG_NOTICE, "Set co2meter %s/%s new beer %s %s", node, alias, beercode, beername);
+			    }
+			    mysql_close(con2);
+			}
+		    }
+		}
+                break;
+            }
+        }
+    }
+
+    if (node)
+	free(node);
+    if (alias)
+	free(alias);
+    if (beeruuid)
+	free(beeruuid);
+    if (beercode)
+	free(beercode);
+    if (beername)
+	free(beername);
+}
+
+
+
 void co2meter_set(char *edge_node, char *alias, char *payload)
 {
     struct json_object	*jobj, *val, *sensor;
     sys_co2meter_list	*co2meter, *tmpp;
     bool		new_co2meter = true;
-    char		*msg = NULL, buf[65];
 
 //    fprintf(stdout, "co2meter_set: %s/%s %s\n", edge_node, alias, payload);
 
@@ -139,36 +274,7 @@
     }
     json_object_put(jobj);
 
-    msg = xstrcpy((char *)"{\"device\":\"co2meters\",\"node\":\"");
-    msg = xstrcat(msg, edge_node);
-    msg = xstrcat(msg, (char *)"\",\"unit\":\"");
-    msg = xstrcat(msg, alias);
-    msg = xstrcat(msg, (char *)"\",\"online\":");
-    msg = xstrcat(msg, co2meter->online ? (char *)"1":(char *)"0");
-    msg = xstrcat(msg, (char *)",\"mode\":\"");
-    msg = xstrcat(msg, co2meter->mode);
-    msg = xstrcat(msg, (char *)"\",\"temperature\":");
-    snprintf(buf, 64, "%.3f", co2meter->temperature);
-    msg = xstrcat(msg, buf);
-    msg = xstrcat(msg, (char *)",\"pressure_channel\":");
-    snprintf(buf, 64, "%d", co2meter->pressure_channel);
-    msg = xstrcat(msg, buf);
-    msg = xstrcat(msg, (char *)",\"pressure_voltage\":");
-    snprintf(buf, 64, "%.3f", co2meter->pressure_voltage);
-    msg = xstrcat(msg, buf);
-    msg = xstrcat(msg, (char *)",\"pressure_zero\":");
-    snprintf(buf, 64, "%.3f", co2meter->pressure_zero);
-    msg = xstrcat(msg, buf);
-    msg = xstrcat(msg, (char *)",\"pressure_bar\":");
-    snprintf(buf, 64, "%.3f", co2meter->pressure_bar);
-    msg = xstrcat(msg, buf);
-    msg = xstrcat(msg, (char *)",\"alarm\":");
-    snprintf(buf, 64, "%d", co2meter->alarm);
-    msg = xstrcat(msg, buf);
-    msg = xstrcat(msg, (char *)"}");
-    ws_broadcast(msg);
-    free(msg);
-    msg = NULL;
+    co2meter_ws_send(co2meter);
 
 //    co2meter_dump(co2meter);
 

mercurial