bmsd/fermenters.c

changeset 671
4b54d6f79d25
parent 558
a8e065a9f851
child 674
c865e3cdee04
equal deleted inserted replaced
670:638e7dd1d560 671:4b54d6f79d25
1 /** 1 /**
2 * @file fermenters.c 2 * @file fermenters.c
3 * @brief Handle fermenters status 3 * @brief Handle fermenters status
4 * @author Michiel Broek <mbroek at mbse dot eu> 4 * @author Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * Copyright (C) 2018-2019 6 * Copyright (C) 2018-2020
7 * 7 *
8 * This file is part of the bms (Brewery Management System) 8 * This file is part of the bms (Brewery Management System)
9 * 9 *
10 * This is free software; you can redistribute it and/or modify it 10 * This is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the 11 * under the terms of the GNU General Public License as published by the
25 25
26 #include "bms.h" 26 #include "bms.h"
27 #include "xutil.h" 27 #include "xutil.h"
28 #include "fermenters.h" 28 #include "fermenters.h"
29 #include "mysql.h" 29 #include "mysql.h"
30 #include "websocket.h"
30 31
31 32
32 sys_fermenter_list *fermenters = NULL; 33 sys_fermenter_list *fermenters = NULL;
33 34
34 extern int debug; 35 extern int debug;
39 void fermenter_set(char *edge_node, char *alias, bool birth, char *payload) 40 void fermenter_set(char *edge_node, char *alias, bool birth, char *payload)
40 { 41 {
41 struct json_object *jobj, *val, *sensor, *temp; 42 struct json_object *jobj, *val, *sensor, *temp;
42 sys_fermenter_list *fermenter, *tmpp; 43 sys_fermenter_list *fermenter, *tmpp;
43 bool new_fermenter = true; 44 bool new_fermenter = true;
45 char *msg = NULL, buf[65];
44 46
45 // fprintf(stdout, "fermenter_set: %s/%s %s %s\n", edge_node, alias, birth ? "BIRTH":"DATA", payload); 47 // fprintf(stdout, "fermenter_set: %s/%s %s %s\n", edge_node, alias, birth ? "BIRTH":"DATA", payload);
46 48
47 /* 49 /*
48 * Search fermenter record in the memory array and use it if found. 50 * Search fermenter record in the memory array and use it if found.
315 fermenter->yeast_lo = 12; 317 fermenter->yeast_lo = 12;
316 fermenter->yeast_hi = 24; 318 fermenter->yeast_hi = 24;
317 } 319 }
318 } 320 }
319 json_object_put(jobj); 321 json_object_put(jobj);
322
323 msg = xstrcpy((char *)"{\"device\":\"fermenter\",\"node\":\"");
324 msg = xstrcat(msg, edge_node);
325 msg = xstrcat(msg, (char *)"\",\"unit\":\"");
326 msg = xstrcat(msg, alias);
327 msg = xstrcat(msg, (char *)"\",\"online\":");
328 msg = xstrcat(msg, fermenter->online ? (char *)"1":(char *)"0");
329 msg = xstrcat(msg, (char *)",\"mode\":\"");
330 msg = xstrcat(msg, fermenter->mode);
331 msg = xstrcat(msg, (char *)"\",\"yeast_lo\":");
332 snprintf(buf, 64, "%.3f", fermenter->yeast_lo);
333 msg = xstrcat(msg, buf);
334 msg = xstrcat(msg, (char *)",\"yeast_hi\":");
335 snprintf(buf, 64, "%.3f", fermenter->yeast_hi);
336 msg = xstrcat(msg, buf);
337 if (fermenter->air_address) {
338 msg = xstrcat(msg, (char *)",\"air\":");
339 snprintf(buf, 64, "%.3f", fermenter->air_temperature);
340 msg = xstrcat(msg, buf);
341 }
342 if (fermenter->beer_address) {
343 msg = xstrcat(msg, (char *)",\"beer\":");
344 snprintf(buf, 64, "%.3f", fermenter->beer_temperature);
345 msg = xstrcat(msg, buf);
346 }
347 if (fermenter->chiller_address) {
348 msg = xstrcat(msg, (char *)",\"chiller\":");
349 snprintf(buf, 64, "%.3f", fermenter->chiller_temperature);
350 msg = xstrcat(msg, buf);
351 }
352 if (fermenter->heater_address) {
353 msg = xstrcat(msg, (char *)",\"heater\":");
354 snprintf(buf, 64, "%d", fermenter->heater_state);
355 msg = xstrcat(msg, buf);
356 }
357 if (fermenter->cooler_address) {
358 msg = xstrcat(msg, (char *)",\"cooler\":");
359 snprintf(buf, 64, "%d", fermenter->cooler_state);
360 msg = xstrcat(msg, buf);
361 }
362 if (fermenter->fan_address) {
363 msg = xstrcat(msg, (char *)",\"fan\":");
364 snprintf(buf, 64, "%d", fermenter->fan_state);
365 msg = xstrcat(msg, buf);
366 }
367 if (fermenter->light_address) {
368 msg = xstrcat(msg, (char *)",\"light\":");
369 snprintf(buf, 64, "%d", fermenter->light_state);
370 msg = xstrcat(msg, buf);
371 }
372 if (fermenter->door_address) {
373 msg = xstrcat(msg, (char *)",\"door\":");
374 snprintf(buf, 64, "%d", fermenter->door_state);
375 msg = xstrcat(msg, buf);
376 }
377 msg = xstrcat(msg, (char *)",\"sp_lo\":");
378 snprintf(buf, 64, "%.3f", fermenter->setpoint_low);
379 msg = xstrcat(msg, buf);
380 msg = xstrcat(msg, (char *)",\"sp_hi\":");
381 snprintf(buf, 64, "%.3f", fermenter->setpoint_high);
382 msg = xstrcat(msg, buf);
383 msg = xstrcat(msg, (char *)",\"alarm\":");
384 snprintf(buf, 64, "%d", fermenter->alarm);
385 msg = xstrcat(msg, buf);
386 msg = xstrcat(msg, (char *)",\"stage\":\"");
387 msg = xstrcat(msg, fermenter->stage);
388 msg = xstrcat(msg, (char *)"\"}");
389 ws_broadcast(msg);
390 free(msg);
391 msg = NULL;
320 392
321 // fermenter_dump(fermenter); 393 // fermenter_dump(fermenter);
322 394
323 if (new_fermenter) { 395 if (new_fermenter) {
324 if (fermenters == NULL) { 396 if (fermenters == NULL) {
669 741
670 742
671 743
672 void fermenter_death(char *topic) 744 void fermenter_death(char *topic)
673 { 745 {
674 char *edge_node, *alias; 746 char *edge_node, *alias, *msg = NULL;
675 sys_fermenter_list *tmpp; 747 sys_fermenter_list *tmpp;
676 748
677 printf("fermenter_death: %s\n", topic); 749 printf("fermenter_death: %s\n", topic);
678 strtok(topic, "/"); // ignore namespace 750 strtok(topic, "/"); // ignore namespace
679 strtok(NULL, "/"); // ignore group_id 751 strtok(NULL, "/"); // ignore group_id
687 for (tmpp = fermenters; tmpp; tmpp = tmpp->next) { 759 for (tmpp = fermenters; tmpp; tmpp = tmpp->next) {
688 if ((strcmp(tmpp->node, edge_node) == 0) && (strcmp(tmpp->alias, alias) == 0)) { 760 if ((strcmp(tmpp->node, edge_node) == 0) && (strcmp(tmpp->alias, alias) == 0)) {
689 if (tmpp->online) 761 if (tmpp->online)
690 syslog(LOG_NOTICE, "Offline fermenter %s/%s", tmpp->node, tmpp->alias); 762 syslog(LOG_NOTICE, "Offline fermenter %s/%s", tmpp->node, tmpp->alias);
691 tmpp->online = false; 763 tmpp->online = false;
764 msg = xstrcpy((char *)"{\"device\":\"fermenter\",\"node\":\"");
765 msg = xstrcat(msg, edge_node);
766 msg = xstrcat(msg, (char *)"\",\"unit\":\"");
767 msg = xstrcat(msg, alias);
768 msg = xstrcat(msg, (char *)"\",\"online\":0}");
769 ws_broadcast(msg);
770 free(msg);
771 msg = NULL;
692 break; 772 break;
693 } 773 }
694 } 774 }
695 } else { 775 } else {
696 for (tmpp = fermenters; tmpp; tmpp = tmpp->next) { 776 for (tmpp = fermenters; tmpp; tmpp = tmpp->next) {
697 if (strcmp(tmpp->node, edge_node) == 0) { 777 if (strcmp(tmpp->node, edge_node) == 0) {
698 if (tmpp->online) 778 if (tmpp->online)
699 syslog(LOG_NOTICE, "Offline fermenter %s/%s", tmpp->node, tmpp->alias); 779 syslog(LOG_NOTICE, "Offline fermenter %s/%s", tmpp->node, tmpp->alias);
700 tmpp->online = false; 780 tmpp->online = false;
781 msg = xstrcpy((char *)"{\"device\":\"fermenter\",\"node\":\"");
782 msg = xstrcat(msg, edge_node);
783 msg = xstrcat(msg, (char *)"\",\"unit\":\"");
784 msg = xstrcat(msg, tmpp->alias);
785 msg = xstrcat(msg, (char *)"\",\"online\":0}");
786 ws_broadcast(msg);
787 free(msg);
788 msg = NULL;
701 } 789 }
702 } 790 }
703 } 791 }
704 } 792 }
705 793

mercurial