diff -r 48f8f3fce7c0 -r 0bb48333d133 bmsd/ispindels.c --- a/bmsd/ispindels.c Mon May 18 11:00:59 2020 +0200 +++ b/bmsd/ispindels.c Tue May 19 13:50:14 2020 +0200 @@ -40,19 +40,177 @@ extern MYSQL_ROW row; +void ispindel_ws_send(sys_ispindel_list *ispindel) +{ + char *msg = NULL, buf[65]; + + msg = xstrcpy((char *)"{\"device\":\"ispindels\",\"node\":\""); + msg = xstrcat(msg, ispindel->node); + msg = xstrcat(msg, (char *)"\",\"unit\":\""); + msg = xstrcat(msg, ispindel->alias); + msg = xstrcat(msg, (char *)"\",\"online\":"); + msg = xstrcat(msg, ispindel->online ? (char *)"1":(char *)"0"); + msg = xstrcat(msg, (char *)",\"mode\":\""); + msg = xstrcat(msg, ispindel->mode); + msg = xstrcat(msg, (char *)"\",\"beeruuid\":\""); + msg = xstrcat(msg, ispindel->beeruuid); + msg = xstrcat(msg, (char *)"\",\"beercode\":\""); + msg = xstrcat(msg, ispindel->beercode); + msg = xstrcat(msg, (char *)"\",\"beername\":\""); + msg = xstrcat(msg, ispindel->beername); + msg = xstrcat(msg, (char *)"\",\"temperature\":"); + snprintf(buf, 64, "%.4f", ispindel->temperature); + msg = xstrcat(msg, buf); + msg = xstrcat(msg, (char *)",\"angle\":"); + snprintf(buf, 64, "%.6f", ispindel->angle); + msg = xstrcat(msg, buf); + msg = xstrcat(msg, (char *)",\"battery\":"); + snprintf(buf, 64, "%.6f", ispindel->battery); + msg = xstrcat(msg, buf); + msg = xstrcat(msg, (char *)",\"gravity\":"); + snprintf(buf, 64, "%.6f", ispindel->gravity); + msg = xstrcat(msg, buf); + msg = xstrcat(msg, (char *)",\"og_gravity\":"); + snprintf(buf, 64, "%.6f", ispindel->og_gravity); + msg = xstrcat(msg, buf); + msg = xstrcat(msg, (char *)",\"alarm\":"); + snprintf(buf, 64, "%d", ispindel->alarm); + msg = xstrcat(msg, buf); + msg = xstrcat(msg, (char *)"}"); + ws_broadcast(msg); + free(msg); + msg = NULL; +} + + + +void ispindel_ws_receive(char *payload) +{ + struct json_object *jobj, *val; + sys_ispindel_list *tmpp; + char *node = NULL, *alias = NULL, *mode = NULL, *beeruuid = NULL, *beercode = NULL, *beername = NULL; + char query[512], *end; + MYSQL *con2 = NULL; + +// syslog(LOG_NOTICE, "ispindel_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)); + if (json_object_object_get_ex(jobj, "mode", &val)) + mode = xstrcpy((char *)json_object_get_string(val)); + json_object_put(jobj); + + /* + * Search ispindel record in the memory array and use it if found. + */ + if (ispindels) { + for (tmpp = ispindels; tmpp; tmpp = tmpp->next) { + if (strcmp(tmpp->node, node) == 0) { + 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 { + if (beeruuid && beercode && beername) { + end = stpcpy(query, "UPDATE mon_ispindels 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, "', og_gravity='0.0' WHERE node='"); + end += mysql_real_escape_string(con2, end, node, strlen(node)); + 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); + tmpp->og_gravity = 0.0; + /* Report new state to the client */ + ispindel_ws_send(tmpp); + syslog(LOG_NOTICE, "Set ispindel %s/%s new beer %s %s", node, alias, beercode, beername); + } + } + if (mode) { + end = stpcpy(query, "UPDATE mon_ispindels SET mode='"); + end += mysql_real_escape_string(con2, end, mode, strlen(mode)); + end = stpcpy(end, "' WHERE node='"); + end += mysql_real_escape_string(con2, end, node, strlen(node)); + 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->mode) + free(tmpp->mode); + tmpp->mode = xstrcpy(mode); + /* Report new state to the client */ + ispindel_ws_send(tmpp); + syslog(LOG_NOTICE, "Set ispindel %s/%s new mode %s", node, alias, mode); + } + } + mysql_close(con2); + } + } + break; + } + } + } + + if (node) + free(node); + if (alias) + free(alias); + if (mode) + free(mode); + if (beeruuid) + free(beeruuid); + if (beercode) + free(beercode); + if (beername) + free(beername); +} + + void ispindel_set(char *node, char *payload) { sys_ispindel_list *ispindel, *tmpp; struct json_object *jobj, *metric, *val; bool new_ispindel = true; - char *datetime, buf[65], *line, *logfile, *msg = NULL; + char *datetime, buf[65], *line, *logfile; struct tm *mytime; time_t timestamp; FILE *fp; -// if (debug) -// printf("ispindel_set: %s %s\n", node, payload); + // syslog(LOG_NOTICE, "ispindel_set: %s %s", node, payload); /* * Search ispindel record in the memory array and use it if found. @@ -102,55 +260,25 @@ free(ispindel->alias); ispindel->alias = xstrcpy((char *)json_object_get_string(val)); } - if (json_object_object_get_ex(metric, "alarm", &val)) { + if (json_object_object_get_ex(metric, "alarm", &val)) ispindel->alarm = json_object_get_int(val); - } - if (json_object_object_get_ex(metric, "interval", &val)) { + if (json_object_object_get_ex(metric, "interval", &val)) ispindel->interval = json_object_get_int(val); - } - if (json_object_object_get_ex(metric, "angle", &val)) { + if (json_object_object_get_ex(metric, "angle", &val)) ispindel->angle = json_object_get_double(val); - } - if (json_object_object_get_ex(metric, "temperature", &val)) { + if (json_object_object_get_ex(metric, "temperature", &val)) ispindel->temperature = json_object_get_double(val); - } - if (json_object_object_get_ex(metric, "battery", &val)) { + if (json_object_object_get_ex(metric, "battery", &val)) ispindel->battery = json_object_get_double(val); - } if (json_object_object_get_ex(metric, "gravity", &val)) { ispindel->gravity = json_object_get_double(val); + if (ispindel->gravity > ispindel->og_gravity) + ispindel->og_gravity = ispindel->gravity; } } - msg = xstrcpy((char *)"{\"device\":\"ispindels\",\"node\":\""); - msg = xstrcat(msg, node); - msg = xstrcat(msg, (char *)"\",\"unit\":\""); - msg = xstrcat(msg, ispindel->alias); - msg = xstrcat(msg, (char *)"\",\"online\":"); - msg = xstrcat(msg, ispindel->online ? (char *)"1":(char *)"0"); - msg = xstrcat(msg, (char *)",\"mode\":\""); - msg = xstrcat(msg, ispindel->mode); - msg = xstrcat(msg, (char *)"\",\"temperature\":"); - snprintf(buf, 64, "%.4f", ispindel->temperature); - msg = xstrcat(msg, buf); - msg = xstrcat(msg, (char *)",\"angle\":"); - snprintf(buf, 64, "%.5f", ispindel->angle); - msg = xstrcat(msg, buf); - msg = xstrcat(msg, (char *)",\"battery\":"); - snprintf(buf, 64, "%.6f", ispindel->battery); - msg = xstrcat(msg, buf); - msg = xstrcat(msg, (char *)",\"gravity\":"); - snprintf(buf, 64, "%.5f", ispindel->gravity); - msg = xstrcat(msg, buf); - msg = xstrcat(msg, (char *)",\"alarm\":"); - snprintf(buf, 64, "%d", ispindel->alarm); - msg = xstrcat(msg, buf); - msg = xstrcat(msg, (char *)"}"); - ws_broadcast(msg); - free(msg); - msg = NULL; +// ispindel_dump(ispindel); -// ispindel_dump(ispindel); if (new_ispindel) { if (ispindels == NULL) { @@ -167,6 +295,7 @@ } else { ispindel_mysql_update(ispindel); } + ispindel_ws_send(ispindel); /* * The data is complete, see if we can write a log entry. @@ -187,7 +316,6 @@ snprintf(buf, 64, "%.5f", ispindel->gravity); line = xstrcat(line, buf); line = xstrcat(line, (char *)","); -// snprintf(buf, 64, "%.5f", 1 + (ispindel->gravity / (258.6 - ((ispindel->gravity / 258.2) * 227.1)))); snprintf(buf, 64, "%.5f", 1.00001 + (0.0038661 * ispindel->gravity) + (1.3488e-5 * ispindel->gravity * ispindel->gravity) + (4.3074e-8 * ispindel->gravity * ispindel->gravity * ispindel->gravity)); line = xstrcat(line, buf); @@ -264,6 +392,7 @@ printf("battery %.6f\n", ispindel->battery); printf("gravity %.5f\n", ispindel->gravity); printf("interval %d\n", ispindel->interval); + printf("og_gravity %.5f\n", ispindel->og_gravity); } }