bmsd/ispindels.c

changeset 843
29e7223e37ae
parent 799
cf145b35d65f
child 849
16079aef4c4c
equal deleted inserted replaced
842:897bf2a43253 843:29e7223e37ae
1 /** 1 /**
2 * @file ispindels.c 2 * @file ispindels.c
3 * @brief Handle ispindels data 3 * @brief Handle ispindels data
4 * @author Michiel Broek <mbroek at mbse dot eu> 4 * @author Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * Copyright (C) 2019-2022 6 * Copyright (C) 2019-2023
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
56 msg = xstrcat(msg, ispindel->beeruuid); 56 msg = xstrcat(msg, ispindel->beeruuid);
57 msg = xstrcat(msg, (char *)"\",\"beercode\":\""); 57 msg = xstrcat(msg, (char *)"\",\"beercode\":\"");
58 msg = xstrcat(msg, ispindel->beercode); 58 msg = xstrcat(msg, ispindel->beercode);
59 msg = xstrcat(msg, (char *)"\",\"beername\":\""); 59 msg = xstrcat(msg, (char *)"\",\"beername\":\"");
60 msg = xstrcat(msg, ispindel->beername); 60 msg = xstrcat(msg, ispindel->beername);
61 msg = xstrcat(msg, (char *)"\",\"temperature\":"); 61 msg = xstrcat(msg, (char *)"\",\"yeast_lo\":");
62 snprintf(buf, 64, "%.3f", ispindel->yeast_lo);
63 msg = xstrcat(msg, buf);
64 msg = xstrcat(msg, (char *)",\"yeast_hi\":");
65 snprintf(buf, 64, "%.3f", ispindel->yeast_hi);
66 msg = xstrcat(msg, buf);
67 msg = xstrcat(msg, (char *)",\"temperature\":");
62 snprintf(buf, 64, "%.4f", ispindel->temperature); 68 snprintf(buf, 64, "%.4f", ispindel->temperature);
63 msg = xstrcat(msg, buf); 69 msg = xstrcat(msg, buf);
64 msg = xstrcat(msg, (char *)",\"angle\":"); 70 msg = xstrcat(msg, (char *)",\"angle\":");
65 snprintf(buf, 64, "%.6f", ispindel->angle); 71 snprintf(buf, 64, "%.6f", ispindel->angle);
66 msg = xstrcat(msg, buf); 72 msg = xstrcat(msg, buf);
87 void ispindel_ws_receive(char *payload) 93 void ispindel_ws_receive(char *payload)
88 { 94 {
89 struct json_object *jobj, *val; 95 struct json_object *jobj, *val;
90 sys_ispindel_list *tmpp; 96 sys_ispindel_list *tmpp;
91 char *node = NULL, *alias = NULL, *mode = NULL, *beeruuid = NULL, *beercode = NULL, *beername = NULL; 97 char *node = NULL, *alias = NULL, *mode = NULL, *beeruuid = NULL, *beercode = NULL, *beername = NULL;
92 char query[512], *end; 98 float yeast_lo = 20, yeast_hi = 25;
99 char query[512], buf[65], *end;
93 MYSQL *con2 = NULL; 100 MYSQL *con2 = NULL;
94 101
95 /* 102 /*
96 * Process the JSON formatted payload. 103 * Process the JSON formatted payload.
97 */ 104 */
104 beeruuid = xstrcpy((char *)json_object_get_string(val)); 111 beeruuid = xstrcpy((char *)json_object_get_string(val));
105 if (json_object_object_get_ex(jobj, "beercode", &val)) 112 if (json_object_object_get_ex(jobj, "beercode", &val))
106 beercode = xstrcpy((char *)json_object_get_string(val)); 113 beercode = xstrcpy((char *)json_object_get_string(val));
107 if (json_object_object_get_ex(jobj, "beername", &val)) 114 if (json_object_object_get_ex(jobj, "beername", &val))
108 beername = xstrcpy((char *)json_object_get_string(val)); 115 beername = xstrcpy((char *)json_object_get_string(val));
116 if (json_object_object_get_ex(jobj, "yeast_lo", &val))
117 yeast_lo = json_object_get_double(val);
118 if (json_object_object_get_ex(jobj, "yeast_hi", &val))
119 yeast_hi = json_object_get_double(val);
109 if (json_object_object_get_ex(jobj, "mode", &val)) 120 if (json_object_object_get_ex(jobj, "mode", &val))
110 mode = xstrcpy((char *)json_object_get_string(val)); 121 mode = xstrcpy((char *)json_object_get_string(val));
111 json_object_put(jobj); 122 json_object_put(jobj);
112 123
113 /* 124 /*
128 end += mysql_real_escape_string(con2, end, beeruuid, strlen(beeruuid)); 139 end += mysql_real_escape_string(con2, end, beeruuid, strlen(beeruuid));
129 end = stpcpy(end, "', beercode='"); 140 end = stpcpy(end, "', beercode='");
130 end += mysql_real_escape_string(con2, end, beercode, strlen(beercode)); 141 end += mysql_real_escape_string(con2, end, beercode, strlen(beercode));
131 end = stpcpy(end, "', beername='"); 142 end = stpcpy(end, "', beername='");
132 end += mysql_real_escape_string(con2, end, beername, strlen(beername)); 143 end += mysql_real_escape_string(con2, end, beername, strlen(beername));
133 end = stpcpy(end, "', og_gravity='0.0' WHERE node='"); 144 end = stpcpy(end, "', og_gravity='0.0', yeast_lo='");
145 snprintf(buf, 64, "%.3f", yeast_lo);
146 end = stpcpy(end, buf);
147 end = stpcpy(end, "', yeast_hi='");
148 snprintf(buf, 64, "%.3f", yeast_hi);
149 end = stpcpy(end, buf);
150 end = stpcpy(end, "' WHERE node='");
134 end += mysql_real_escape_string(con2, end, node, strlen(node)); 151 end += mysql_real_escape_string(con2, end, node, strlen(node));
135 end = stpcpy(end, "'"); 152 end = stpcpy(end, "'");
136 153
137 if (mysql_real_query(con2, query, (unsigned int) (end - query))) { 154 if (mysql_real_query(con2, query, (unsigned int) (end - query))) {
138 syslog(LOG_NOTICE, "MySQL: `%s' error %u (%s))", query, mysql_errno(con2), mysql_error(con2)); 155 syslog(LOG_NOTICE, "MySQL: `%s' error %u (%s))", query, mysql_errno(con2), mysql_error(con2));
146 tmpp->beername = xstrcpy(beername); 163 tmpp->beername = xstrcpy(beername);
147 if (tmpp->beeruuid) 164 if (tmpp->beeruuid)
148 free(tmpp->beeruuid); 165 free(tmpp->beeruuid);
149 tmpp->beeruuid = xstrcpy(beeruuid); 166 tmpp->beeruuid = xstrcpy(beeruuid);
150 tmpp->og_gravity = 0.0; 167 tmpp->og_gravity = 0.0;
168 tmpp->yeast_lo = yeast_lo;
169 tmpp->yeast_hi = yeast_hi;
151 /* Report new state to the client */ 170 /* Report new state to the client */
152 ispindel_ws_send(tmpp); 171 ispindel_ws_send(tmpp);
153 syslog(LOG_NOTICE, "Set ispindel %s/%s new beer %s %s", node, alias, beercode, beername); 172 syslog(LOG_NOTICE, "Set ispindel %s/%s new beer %s %s", node, alias, beercode, beername);
154 } 173 }
155 } 174 }

mercurial