bmsd/mysql.c

changeset 680
0bb48333d133
parent 628
a42166cbb19a
child 684
ccb9f24d0fe9
equal deleted inserted replaced
679:48f8f3fce7c0 680:0bb48333d133
298 ispindel->temperature = atof(row[10]); 298 ispindel->temperature = atof(row[10]);
299 ispindel->battery = atof(row[11]); 299 ispindel->battery = atof(row[11]);
300 ispindel->gravity = atof(row[12]); 300 ispindel->gravity = atof(row[12]);
301 ispindel->interval = atoi(row[13]); 301 ispindel->interval = atoi(row[13]);
302 ispindel->mode = xstrcpy(row[14]); 302 ispindel->mode = xstrcpy(row[14]);
303 ispindel->og_gravity = atof(row[15]);
303 304
304 if (ispindels == NULL) { 305 if (ispindels == NULL) {
305 ispindels = ispindel; 306 ispindels = ispindel;
306 } else { 307 } else {
307 for (tmpi = ispindels; tmpi; tmpi = tmpi->next) { 308 for (tmpi = ispindels; tmpi; tmpi = tmpi->next) {
315 } 316 }
316 mysql_free_result(res_set); 317 mysql_free_result(res_set);
317 } 318 }
318 } 319 }
319 320
320 syslog(LOG_NOTICE, "MySQL: loaded %d nodes, %d fermenters, %d co2meters %d ispindels", ncnt, fcnt, ccnt, icnt); 321 syslog(LOG_NOTICE, "MySQL: loaded %d nodes, %d fermenters, %d co2meters, %d ispindels", ncnt, fcnt, ccnt, icnt);
321 return 0; 322 return 0;
322 } 323 }
323 324
324 325
325 326
727 { 728 {
728 char *query = malloc(2560); 729 char *query = malloc(2560);
729 730
730 snprintf(query, 2559, 731 snprintf(query, 2559,
731 "INSERT INTO mon_ispindels SET uuid='%s', alias='%s', node='%s', online='%d', mode='%s', alarm='%d', " \ 732 "INSERT INTO mon_ispindels SET uuid='%s', alias='%s', node='%s', online='%d', mode='%s', alarm='%d', " \
732 "angle='%.5f', temperature='%.4f', battery='%.6f', gravity='%.5f', up_interval='%d', og_gravity='0.0'", 733 "angle='%.6f', temperature='%.4f', battery='%.6f', gravity='%.6f', up_interval='%d', og_gravity='0.0'",
733 ispindel->uuid, ispindel->alias, ispindel->node, ispindel->online ? 1:0, ispindel->mode, ispindel->alarm, 734 ispindel->uuid, ispindel->alias, ispindel->node, ispindel->online ? 1:0, ispindel->mode, ispindel->alarm,
734 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval); 735 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval);
735 736
736 if (bms_mysql_query(query) == 0) { 737 if (bms_mysql_query(query) == 0) {
737 syslog(LOG_NOTICE, "MySQL: insert new ispindel %s", ispindel->node); 738 syslog(LOG_NOTICE, "MySQL: insert new ispindel %s", ispindel->node);
744 void ispindel_mysql_update(sys_ispindel_list *ispindel) 745 void ispindel_mysql_update(sys_ispindel_list *ispindel)
745 { 746 {
746 char *query = malloc(2560); 747 char *query = malloc(2560);
747 748
748 snprintf(query, 2559, 749 snprintf(query, 2559,
749 "UPDATE mon_ispindels SET online='%d', mode='%s', alarm='%d', " \ 750 "UPDATE mon_ispindels SET online='%d', mode='%s', alias='%s', alarm='%d', " \
750 "angle='%.5f', temperature='%.4f', battery='%.6f', gravity='%.5f', up_interval='%d', og_gravity=GREATEST(og_gravity, '%.5f') WHERE uuid='%s'", 751 "angle='%.6f', temperature='%.4f', battery='%.6f', gravity='%.6f', up_interval='%d', og_gravity=GREATEST(og_gravity, '%.6f') WHERE uuid='%s'",
751 ispindel->online ? 1:0, ispindel->mode, ispindel->alarm, 752 ispindel->online ? 1:0, ispindel->mode, ispindel->alias, ispindel->alarm,
752 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->gravity, ispindel->uuid); 753 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->gravity, ispindel->uuid);
753
754 bms_mysql_query(query); 754 bms_mysql_query(query);
755 free(query); 755 free(query);
756 } 756 }
757 757
758 758
764 snprintf(query, 511, "UPDATE mon_ispindels SET online='0' WHERE node='%s'", node); 764 snprintf(query, 511, "UPDATE mon_ispindels SET online='0' WHERE node='%s'", node);
765 bms_mysql_query(query); 765 bms_mysql_query(query);
766 free(query); 766 free(query);
767 } 767 }
768 768
769
770
771 /*
772 * Check using a new MySQL connection because we are running from another thread.
773 */
774 void ispindel_mysql_check(void)
775 {
776 sys_ispindel_list *tmpp;
777 MYSQL *con2 = NULL;
778 MYSQL_RES *res_set2;
779 MYSQL_ROW row2;
780
781 if (ispindels == NULL)
782 return;
783
784 con2 = mysql_init(NULL);
785 if (con2 == NULL) {
786 syslog(LOG_NOTICE, "MySQL: mysql_init() failed");
787 return;
788 }
789
790 if (mysql_real_connect(con2, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) {
791 syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con2));
792 return;
793 }
794
795 if (mysql_query(con2, "SELECT uuid,alias,beercode,beername,beeruuid,mode FROM mon_ispindels;")) {
796 syslog(LOG_NOTICE, "MySQL: SELECT uuid,alias,beercode,beername,beeruuid,mode FROM mon_ispindels error %u (%s))", mysql_errno(con2), mysql_error(con2));
797 } else {
798 res_set2 = mysql_store_result(con2);
799 if (res_set2 == NULL) {
800 syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con2), mysql_error(con2));
801 } else {
802 while ((row2 = mysql_fetch_row(res_set2)) != NULL) {
803 for (tmpp = ispindels; tmpp; tmpp = tmpp->next) {
804 if (strcmp(tmpp->uuid, row2[0]) == 0) {
805 if (strcmp(tmpp->beercode, row2[2]) || strcmp(tmpp->beername, row2[3]) || strcmp(tmpp->beeruuid, row2[4])) {
806 syslog(LOG_NOTICE, "ispindel `%s` change beer to `%s %s`", row2[1], row2[2], row2[3]);
807 if (tmpp->beercode)
808 free(tmpp->beercode);
809 tmpp->beercode = xstrcpy(row2[2]);
810 if (tmpp->beername)
811 free(tmpp->beername);
812 tmpp->beername = xstrcpy(row2[3]);
813 if (tmpp->beeruuid)
814 free(tmpp->beeruuid);
815 tmpp->beeruuid = xstrcpy(row2[4]);
816 }
817 if (strcmp(tmpp->mode, row2[5])) {
818 syslog(LOG_NOTICE, "ispindel `%s` change mode `%s`", row2[1], row2[5]);
819 if (tmpp->mode)
820 free(tmpp->mode);
821 tmpp->mode = xstrcpy(row2[5]);
822 }
823 }
824 }
825 }
826 mysql_free_result(res_set2);
827 }
828 }
829
830 mysql_close(con2);
831 }
832

mercurial