bmsd/mysql.c

changeset 849
16079aef4c4c
parent 844
a6ee7b5b036b
child 852
71f0fa38b634
equal deleted inserted replaced
848:542bdc7f6522 849:16079aef4c4c
306 ispindel->interval = atoi(row[13]); 306 ispindel->interval = atoi(row[13]);
307 ispindel->mode = xstrcpy(row[14]); 307 ispindel->mode = xstrcpy(row[14]);
308 ispindel->og_gravity = atof(row[15]); 308 ispindel->og_gravity = atof(row[15]);
309 ispindel->yeast_lo = atof(row[16]); 309 ispindel->yeast_lo = atof(row[16]);
310 ispindel->yeast_hi = atof(row[17]); 310 ispindel->yeast_hi = atof(row[17]);
311 ispindel->calibrate = xstrcpy(row[18]);
312 ispindel->lastseen = datetime_to_time_t(row[19]);
311 313
312 if (ispindels == NULL) { 314 if (ispindels == NULL) {
313 ispindels = ispindel; 315 ispindels = ispindel;
314 } else { 316 } else {
315 for (tmpi = ispindels; tmpi; tmpi = tmpi->next) { 317 for (tmpi = ispindels; tmpi; tmpi = tmpi->next) {
358 free(tmpi->beername); 360 free(tmpi->beername);
359 if (tmpi->beeruuid) 361 if (tmpi->beeruuid)
360 free(tmpi->beeruuid); 362 free(tmpi->beeruuid);
361 if (tmpi->mode) 363 if (tmpi->mode)
362 free(tmpi->mode); 364 free(tmpi->mode);
365 if (tmpi->calibrate)
366 free(tmpi->calibrate);
363 free(tmpi); 367 free(tmpi);
364 } 368 }
365 369
366 for (tmpp = co2meters; tmpp; tmpp = oldtmpp) { 370 for (tmpp = co2meters; tmpp; tmpp = oldtmpp) {
367 oldtmpp = tmpp->next; 371 oldtmpp = tmpp->next;
708 free(query); 712 free(query);
709 } 713 }
710 714
711 715
712 716
717 /*
718 * Update the gravity calculated from the calibrate data.
719 */
720 double ispindel_mysql_plato(char *uuid, double angle)
721 {
722 char *query = malloc(1024), *cal;
723 struct json_object *jobj, *metric, *val;
724 double poly[4], plato = 0;
725
726 snprintf(query, 1023, "SELECT uuid,calibrate FROM mon_ispindels WHERE uuid='%s'", uuid);
727 if (mysql_query(con, query)) {
728 syslog(LOG_NOTICE, "MySQL: %s error %u (%s))", query, mysql_errno(con), mysql_error(con));
729 free(query);
730 return 0.0;
731 }
732
733 res_set = mysql_store_result(con);
734 if (res_set == NULL) {
735 syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con), mysql_error(con));
736 free(query);
737 return 0.0;
738 }
739 free(query);
740
741 if ((row = mysql_fetch_row(res_set)) != NULL) {
742 cal = xstrcpy(row[1]);
743 jobj = json_tokener_parse(cal);
744 if (json_object_object_get_ex(jobj, "polyData", &metric)) {
745 int arraylen = json_object_array_length(metric);
746 for (int i = 0; i < arraylen; i++) {
747 val = json_object_array_get_idx(metric, i);
748 poly[i] = json_object_get_double(val);
749 }
750 plato = (poly[0] * pow(angle, 3)) + (poly[1] * pow(angle, 2)) + (poly[2] * angle) + poly[3];
751 }
752 free(cal);
753 }
754 return plato;
755 }
756
757
713 void ispindel_mysql_insert(sys_ispindel_list *ispindel) 758 void ispindel_mysql_insert(sys_ispindel_list *ispindel)
714 { 759 {
715 char *query = malloc(2560); 760 char *query = malloc(2560), last[73];
761 struct tm *mytime;
762
763 mytime = localtime(&ispindel->lastseen);
764 snprintf(last, 64, "%04d-%02d-%02d %02d:%02d:%02d",
765 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
716 766
717 snprintf(query, 2559, 767 snprintf(query, 2559,
718 "INSERT INTO mon_ispindels SET uuid='%s', alias='%s', node='%s', online='%d', mode='%s', alarm='%d', " \ 768 "INSERT INTO mon_ispindels SET uuid='%s', alias='%s', node='%s', online='%d', mode='%s', alarm='%d', " \
719 "angle='%.6f', temperature='%.4f', battery='%.6f', gravity='%.6f', up_interval='%d', og_gravity='0.0', " \ 769 "angle='%.6f', temperature='%.4f', battery='%.6f', gravity='%.6f', up_interval='%d', og_gravity='0.0', " \
720 "yeast_lo='%.1f', yeast_hi='%.1f'", 770 "yeast_lo='%.1f', yeast_hi='%.1f', lastseen='%s'",
721 ispindel->uuid, ispindel->alias, ispindel->node, ispindel->online ? 1:0, ispindel->mode, ispindel->alarm, 771 ispindel->uuid, ispindel->alias, ispindel->node, ispindel->online ? 1:0, ispindel->mode, ispindel->alarm,
722 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, 772 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval,
723 ispindel->yeast_lo, ispindel->yeast_hi); 773 ispindel->yeast_lo, ispindel->yeast_hi, last);
724 774
725 if (bms_mysql_query(query) == 0) { 775 if (bms_mysql_query(query) == 0) {
726 syslog(LOG_NOTICE, "MySQL: insert new ispindel %s", ispindel->node); 776 syslog(LOG_NOTICE, "MySQL: insert new ispindel %s", ispindel->node);
727 } 777 }
728 free(query); 778 free(query);
730 780
731 781
732 782
733 void ispindel_mysql_update(sys_ispindel_list *ispindel) 783 void ispindel_mysql_update(sys_ispindel_list *ispindel)
734 { 784 {
735 char *query = malloc(2560); 785 char *query = malloc(2560), last[73];
736 786 struct tm *mytime;
787
788 mytime = localtime(&ispindel->lastseen);
789 snprintf(last, 64, "%04d-%02d-%02d %02d:%02d:%02d",
790 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
791
792 /*
793 * Do not update the calibrate field with regular updates.
794 */
737 snprintf(query, 2559, 795 snprintf(query, 2559,
738 "UPDATE mon_ispindels SET online='%d', mode='%s', alias='%s', alarm='%d', " \ 796 "UPDATE mon_ispindels SET online='%d', mode='%s', alias='%s', alarm='%d', " \
739 "angle='%.6f', temperature='%.4f', battery='%.6f', gravity='%.6f', up_interval='%d', og_gravity=GREATEST(og_gravity, '%.6f'), " \ 797 "angle='%.6f', temperature='%.4f', battery='%.6f', gravity='%.6f', up_interval='%d', og_gravity=GREATEST(og_gravity, '%.6f'), " \
740 "yeast_lo='%.1f', yeast_hi='%.1f' WHERE uuid='%s'", 798 "yeast_lo='%.1f', yeast_hi='%.1f', lastseen='%s' WHERE uuid='%s'",
741 ispindel->online ? 1:0, ispindel->mode, ispindel->alias, ispindel->alarm, 799 ispindel->online ? 1:0, ispindel->mode, ispindel->alias, ispindel->alarm,
742 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->gravity, 800 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->gravity,
743 ispindel->yeast_lo, ispindel->yeast_hi, ispindel->uuid); 801 ispindel->yeast_lo, ispindel->yeast_hi, last, ispindel->uuid);
744 bms_mysql_query(query); 802 bms_mysql_query(query);
745 free(query); 803 free(query);
746 } 804 }
747 805
748 806

mercurial