thermferm/websocket.c

changeset 680
8b3c86124a08
parent 678
cc49115e769e
child 682
f82be2bd472f
--- a/thermferm/websocket.c	Mon Apr 15 17:06:15 2024 +0200
+++ b/thermferm/websocket.c	Tue Apr 16 16:03:47 2024 +0200
@@ -70,7 +70,7 @@
 
 void fermenter_ws_receive(char *buf)
 {
-    struct json_object	*val, *val2, *jobj;
+    struct json_object	*val, *val2, *jobj, *profile;
     units_list		*unit;
     bool		changed;
 
@@ -212,6 +212,67 @@
                 break;
             }
 
+	    /*
+	     * We don't implement "light", "product", "profile" download here.
+	     * But "profile" commands are implemented. That means a profile
+	     * must be installed by bmsd via mqtt.
+	     * {"type":"fermenter","unit":"unit0","profile":{"command":"pause"}}
+	     * off pause start abort done
+	     */
+	    if ((json_object_object_get_ex(jobj, "profile", &profile)) && (unit->mode == UNITMODE_PROFILE)) {
+		if (json_object_object_get_ex(profile, "command", &val)) {
+		    char *cmd = xstrcpy((char *)json_object_get_string(val));
+		    syslog(LOG_NOTICE, "ws: profile command `%s'", cmd);
+		    if ((! strcmp(cmd, (char *)"off")) && (unit->prof_state == PROFILE_DONE)) {
+			unit->prof_state = PROFILE_OFF;
+			syslog(LOG_NOTICE, "ws: unit %s profile to OFF", unit->alias);
+			unit->mqtt_flag |= MQTT_FLAG_DATA;
+		    } else if (! strcmp(cmd, (char *)"pause")) {
+			if (unit->prof_state == PROFILE_RUN) {
+			    unit->prof_state = PROFILE_PAUSE;
+			    syslog(LOG_NOTICE, "ws: unit %s profile to PAUSE", unit->alias);
+			    unit->mqtt_flag |= MQTT_FLAG_DATA;
+			} else if (unit->prof_state == PROFILE_PAUSE) {
+			    unit->prof_state = PROFILE_RUN;
+			    syslog(LOG_NOTICE, "ws: unit %s profile resume RUN", unit->alias);
+			    unit->mqtt_flag |= MQTT_FLAG_DATA;
+			}
+		    } else if (! strcmp(cmd, (char *)"start")) {
+			if (unit->prof_state == PROFILE_OFF) {
+			    unit->prof_state = PROFILE_RUN;
+			    unit->prof_started = time(NULL);
+			    unit->prof_paused = unit->prof_primary_done = 0;
+			    unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
+			    syslog(LOG_NOTICE, "ws: unit %s profile start RUN", unit->alias);
+			    unit->mqtt_flag |= MQTT_FLAG_DATA;
+			    if (! unit->event_msg)
+				unit->event_msg = xstrcpy((char *)"Profile start");
+			}
+		    } else if (! strcmp(cmd, (char *)"abort")) {
+			if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) {
+			    unit->prof_state = PROFILE_OFF;
+			    unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0;
+			    unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
+			    syslog(LOG_NOTICE, "ws: unit %s profile ABORT", unit->alias);
+			    unit->mqtt_flag |= MQTT_FLAG_DATA;
+			    if (! unit->event_msg)
+				unit->event_msg = xstrcpy((char *)"Profile abort");
+			}
+		    } else if (! strcmp(cmd, (char *)"done")) {
+			if (unit->prof_state == PROFILE_DONE) {
+			    unit->prof_state = PROFILE_OFF;
+			    unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0;
+			    unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
+			    syslog(LOG_NOTICE, "ws: unit %s profile OFF", unit->alias);
+			    unit->mqtt_flag |= MQTT_FLAG_DATA;
+			}
+		    }
+		    free(cmd);
+		    cmd = NULL;
+		}
+		break;
+	    }
+
 	    return;
 	}
     }

mercurial