diff -r 542bdc7f6522 -r 16079aef4c4c bmsd/mysql.c --- a/bmsd/mysql.c Wed Aug 23 10:30:09 2023 +0200 +++ b/bmsd/mysql.c Thu Oct 12 14:19:46 2023 +0200 @@ -308,6 +308,8 @@ ispindel->og_gravity = atof(row[15]); ispindel->yeast_lo = atof(row[16]); ispindel->yeast_hi = atof(row[17]); + ispindel->calibrate = xstrcpy(row[18]); + ispindel->lastseen = datetime_to_time_t(row[19]); if (ispindels == NULL) { ispindels = ispindel; @@ -360,6 +362,8 @@ free(tmpi->beeruuid); if (tmpi->mode) free(tmpi->mode); + if (tmpi->calibrate) + free(tmpi->calibrate); free(tmpi); } @@ -710,17 +714,63 @@ +/* + * Update the gravity calculated from the calibrate data. + */ +double ispindel_mysql_plato(char *uuid, double angle) +{ + char *query = malloc(1024), *cal; + struct json_object *jobj, *metric, *val; + double poly[4], plato = 0; + + snprintf(query, 1023, "SELECT uuid,calibrate FROM mon_ispindels WHERE uuid='%s'", uuid); + if (mysql_query(con, query)) { + syslog(LOG_NOTICE, "MySQL: %s error %u (%s))", query, mysql_errno(con), mysql_error(con)); + free(query); + return 0.0; + } + + res_set = mysql_store_result(con); + if (res_set == NULL) { + syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con), mysql_error(con)); + free(query); + return 0.0; + } + free(query); + + if ((row = mysql_fetch_row(res_set)) != NULL) { + cal = xstrcpy(row[1]); + jobj = json_tokener_parse(cal); + if (json_object_object_get_ex(jobj, "polyData", &metric)) { + int arraylen = json_object_array_length(metric); + for (int i = 0; i < arraylen; i++) { + val = json_object_array_get_idx(metric, i); + poly[i] = json_object_get_double(val); + } + plato = (poly[0] * pow(angle, 3)) + (poly[1] * pow(angle, 2)) + (poly[2] * angle) + poly[3]; + } + free(cal); + } + return plato; +} + + void ispindel_mysql_insert(sys_ispindel_list *ispindel) { - char *query = malloc(2560); + char *query = malloc(2560), last[73]; + struct tm *mytime; + + mytime = localtime(&ispindel->lastseen); + snprintf(last, 64, "%04d-%02d-%02d %02d:%02d:%02d", + mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); snprintf(query, 2559, "INSERT INTO mon_ispindels SET uuid='%s', alias='%s', node='%s', online='%d', mode='%s', alarm='%d', " \ "angle='%.6f', temperature='%.4f', battery='%.6f', gravity='%.6f', up_interval='%d', og_gravity='0.0', " \ - "yeast_lo='%.1f', yeast_hi='%.1f'", + "yeast_lo='%.1f', yeast_hi='%.1f', lastseen='%s'", ispindel->uuid, ispindel->alias, ispindel->node, ispindel->online ? 1:0, ispindel->mode, ispindel->alarm, ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, - ispindel->yeast_lo, ispindel->yeast_hi); + ispindel->yeast_lo, ispindel->yeast_hi, last); if (bms_mysql_query(query) == 0) { syslog(LOG_NOTICE, "MySQL: insert new ispindel %s", ispindel->node); @@ -732,15 +782,23 @@ void ispindel_mysql_update(sys_ispindel_list *ispindel) { - char *query = malloc(2560); + char *query = malloc(2560), last[73]; + struct tm *mytime; + mytime = localtime(&ispindel->lastseen); + snprintf(last, 64, "%04d-%02d-%02d %02d:%02d:%02d", + mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); + + /* + * Do not update the calibrate field with regular updates. + */ snprintf(query, 2559, "UPDATE mon_ispindels SET online='%d', mode='%s', alias='%s', alarm='%d', " \ "angle='%.6f', temperature='%.4f', battery='%.6f', gravity='%.6f', up_interval='%d', og_gravity=GREATEST(og_gravity, '%.6f'), " \ - "yeast_lo='%.1f', yeast_hi='%.1f' WHERE uuid='%s'", + "yeast_lo='%.1f', yeast_hi='%.1f', lastseen='%s' WHERE uuid='%s'", ispindel->online ? 1:0, ispindel->mode, ispindel->alias, ispindel->alarm, ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->gravity, - ispindel->yeast_lo, ispindel->yeast_hi, ispindel->uuid); + ispindel->yeast_lo, ispindel->yeast_hi, last, ispindel->uuid); bms_mysql_query(query); free(query); }