thermferm/websocket.c

changeset 680
8b3c86124a08
parent 678
cc49115e769e
child 682
f82be2bd472f
equal deleted inserted replaced
679:ecfcb1104b54 680:8b3c86124a08
68 68
69 69
70 70
71 void fermenter_ws_receive(char *buf) 71 void fermenter_ws_receive(char *buf)
72 { 72 {
73 struct json_object *val, *val2, *jobj; 73 struct json_object *val, *val2, *jobj, *profile;
74 units_list *unit; 74 units_list *unit;
75 bool changed; 75 bool changed;
76 76
77 jobj = json_tokener_parse(buf); 77 jobj = json_tokener_parse(buf);
78 json_object_object_get_ex(jobj, "unit", &val); 78 json_object_object_get_ex(jobj, "unit", &val);
210 unit->mqtt_flag |= MQTT_FLAG_DATA; 210 unit->mqtt_flag |= MQTT_FLAG_DATA;
211 syslog(LOG_NOTICE, "ws: unit %s fan_state to %d", unit->alias, unit->fan_state); 211 syslog(LOG_NOTICE, "ws: unit %s fan_state to %d", unit->alias, unit->fan_state);
212 break; 212 break;
213 } 213 }
214 214
215 /*
216 * We don't implement "light", "product", "profile" download here.
217 * But "profile" commands are implemented. That means a profile
218 * must be installed by bmsd via mqtt.
219 * {"type":"fermenter","unit":"unit0","profile":{"command":"pause"}}
220 * off pause start abort done
221 */
222 if ((json_object_object_get_ex(jobj, "profile", &profile)) && (unit->mode == UNITMODE_PROFILE)) {
223 if (json_object_object_get_ex(profile, "command", &val)) {
224 char *cmd = xstrcpy((char *)json_object_get_string(val));
225 syslog(LOG_NOTICE, "ws: profile command `%s'", cmd);
226 if ((! strcmp(cmd, (char *)"off")) && (unit->prof_state == PROFILE_DONE)) {
227 unit->prof_state = PROFILE_OFF;
228 syslog(LOG_NOTICE, "ws: unit %s profile to OFF", unit->alias);
229 unit->mqtt_flag |= MQTT_FLAG_DATA;
230 } else if (! strcmp(cmd, (char *)"pause")) {
231 if (unit->prof_state == PROFILE_RUN) {
232 unit->prof_state = PROFILE_PAUSE;
233 syslog(LOG_NOTICE, "ws: unit %s profile to PAUSE", unit->alias);
234 unit->mqtt_flag |= MQTT_FLAG_DATA;
235 } else if (unit->prof_state == PROFILE_PAUSE) {
236 unit->prof_state = PROFILE_RUN;
237 syslog(LOG_NOTICE, "ws: unit %s profile resume RUN", unit->alias);
238 unit->mqtt_flag |= MQTT_FLAG_DATA;
239 }
240 } else if (! strcmp(cmd, (char *)"start")) {
241 if (unit->prof_state == PROFILE_OFF) {
242 unit->prof_state = PROFILE_RUN;
243 unit->prof_started = time(NULL);
244 unit->prof_paused = unit->prof_primary_done = 0;
245 unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
246 syslog(LOG_NOTICE, "ws: unit %s profile start RUN", unit->alias);
247 unit->mqtt_flag |= MQTT_FLAG_DATA;
248 if (! unit->event_msg)
249 unit->event_msg = xstrcpy((char *)"Profile start");
250 }
251 } else if (! strcmp(cmd, (char *)"abort")) {
252 if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) {
253 unit->prof_state = PROFILE_OFF;
254 unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0;
255 unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
256 syslog(LOG_NOTICE, "ws: unit %s profile ABORT", unit->alias);
257 unit->mqtt_flag |= MQTT_FLAG_DATA;
258 if (! unit->event_msg)
259 unit->event_msg = xstrcpy((char *)"Profile abort");
260 }
261 } else if (! strcmp(cmd, (char *)"done")) {
262 if (unit->prof_state == PROFILE_DONE) {
263 unit->prof_state = PROFILE_OFF;
264 unit->prof_started = unit->prof_paused = unit->prof_primary_done = 0;
265 unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
266 syslog(LOG_NOTICE, "ws: unit %s profile OFF", unit->alias);
267 unit->mqtt_flag |= MQTT_FLAG_DATA;
268 }
269 }
270 free(cmd);
271 cmd = NULL;
272 }
273 break;
274 }
275
215 return; 276 return;
216 } 277 }
217 } 278 }
218 syslog(LOG_NOTICE, "fermenter_ws_receive(%s)", buf); 279 syslog(LOG_NOTICE, "fermenter_ws_receive(%s)", buf);
219 } 280 }

mercurial