bmsd/mysql.c

changeset 578
e75ce5bbda73
parent 572
7a03181d29a3
child 579
1253a237b620
equal deleted inserted replaced
577:b4bfed3684d0 578:e75ce5bbda73
1 /** 1 /**
2 * @file mysql.c 2 * @file mysql.c
3 * @brief MySQL/MariaDB access. 3 * @brief MySQL/MariaDB access.
4 * @author Michiel Broek <mbroek at mbse dot eu> 4 * @author Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * Copyright (C) 2018-2019 6 * Copyright (C) 2018-2020
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
284 } else { 284 } else {
285 while ((row = mysql_fetch_row(res_set)) != NULL) { 285 while ((row = mysql_fetch_row(res_set)) != NULL) {
286 ispindel = (sys_ispindel_list *)malloc(sizeof(sys_ispindel_list)); 286 ispindel = (sys_ispindel_list *)malloc(sizeof(sys_ispindel_list));
287 memset(ispindel, 0, sizeof(sys_ispindel_list)); 287 memset(ispindel, 0, sizeof(sys_ispindel_list));
288 ispindel->next = NULL; 288 ispindel->next = NULL;
289 ispindel->node = xstrcpy(row[1]); 289 ispindel->uuid = xstrcpy(row[1]);
290 ispindel->online = atoi(row[2]); 290 ispindel->alias = xstrcpy(row[2]);
291 ispindel->alarm = atoi(row[3]); 291 ispindel->node = xstrcpy(row[3]);
292 ispindel->beercode = xstrcpy(row[4]); 292 ispindel->online = atoi(row[4]);
293 ispindel->beername = xstrcpy(row[5]); 293 ispindel->alarm = atoi(row[5]);
294 ispindel->beeruuid = xstrcpy(row[6]); 294 ispindel->beercode = xstrcpy(row[6]);
295 ispindel->tilt = atof(row[7]); 295 ispindel->beername = xstrcpy(row[7]);
296 ispindel->temperature = atof(row[8]); 296 ispindel->beeruuid = xstrcpy(row[8]);
297 ispindel->battery = atof(row[9]); 297 ispindel->angle = atof(row[9]);
298 ispindel->gravity = atof(row[10]); 298 ispindel->temperature = atof(row[10]);
299 ispindel->interval = atoi(row[11]); 299 ispindel->battery = atof(row[11]);
300 ispindel->rssi = atoi(row[12]); 300 ispindel->gravity = atof(row[12]);
301 ispindel->interval = atoi(row[13]);
302 ispindel->mode = xstrcpy(row[14]);
301 303
302 if (ispindels == NULL) { 304 if (ispindels == NULL) {
303 ispindels = ispindel; 305 ispindels = ispindel;
304 } else { 306 } else {
305 for (tmpi = ispindels; tmpi; tmpi = tmpi->next) { 307 for (tmpi = ispindels; tmpi; tmpi = tmpi->next) {
505 507
506 snprintf(query, 1023, 508 snprintf(query, 1023,
507 "INSERT INTO mon_nodes SET uuid='%s', node='%s', online='%d', group_id='%s', " \ 509 "INSERT INTO mon_nodes SET uuid='%s', node='%s', online='%d', group_id='%s', " \
508 "hardwaremake='%s', hardwaremodel='%s', os='%s', os_version='%s', firmware='%s', firstseen='%s', lastseen='%s', " \ 510 "hardwaremake='%s', hardwaremodel='%s', os='%s', os_version='%s', firmware='%s', firstseen='%s', lastseen='%s', " \
509 "temperature='%.3f', humidity='%.3f', barometer='%.3f', gps_latitude='%.8f', gps_longitude='%.8f', gps_altitude='%.8f', " \ 511 "temperature='%.3f', humidity='%.3f', barometer='%.3f', gps_latitude='%.8f', gps_longitude='%.8f', gps_altitude='%.8f', " \
510 "net_address='%s', net_ifname='%s', net_rssi='%d'", 512 "net_address='%s', net_ifname='%s', net_rssi='%d', up_interval='%d'",
511 node->uuid, node->node, node->online ?1:0, node->group_id, 513 node->uuid, node->node, node->online ?1:0, node->group_id,
512 node->hardwaremake, node->hardwaremodel, node->os, node->os_version, node->firmware, first, last, 514 node->hardwaremake, node->hardwaremodel, node->os, node->os_version, node->firmware, first, last,
513 node->temperature, node->humidity, node->barometer, node->gps_latitude, node->gps_longitude, node->gps_altitude, 515 node->temperature, node->humidity, node->barometer, node->gps_latitude, node->gps_longitude, node->gps_altitude,
514 node->net_address, node->net_ifname, node->net_rssi); 516 node->net_address, node->net_ifname, node->net_rssi, node->interval);
515 517
516 if (bms_mysql_query(query) == 0) { 518 if (bms_mysql_query(query) == 0) {
517 syslog(LOG_NOTICE, "MySQL: insert new node %s", node->node); 519 syslog(LOG_NOTICE, "MySQL: insert new node %s", node->node);
518 } 520 }
519 521
532 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); 534 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
533 535
534 snprintf(query, 1023, 536 snprintf(query, 1023,
535 "UPDATE mon_nodes SET online='%d', hardwaremake='%s', hardwaremodel='%s', os='%s', os_version='%s', firmware='%s', lastseen='%s', " \ 537 "UPDATE mon_nodes SET online='%d', hardwaremake='%s', hardwaremodel='%s', os='%s', os_version='%s', firmware='%s', lastseen='%s', " \
536 "temperature='%.3f', humidity='%.3f', barometer='%.3f', gps_latitude='%.8f', gps_longitude='%.8f', gps_altitude='%.8f', " \ 538 "temperature='%.3f', humidity='%.3f', barometer='%.3f', gps_latitude='%.8f', gps_longitude='%.8f', gps_altitude='%.8f', " \
537 "net_address='%s', net_ifname='%s', net_rssi='%d' WHERE uuid='%s'", 539 "net_address='%s', net_ifname='%s', net_rssi='%d', up_interval='%d' WHERE uuid='%s'",
538 node->online ? 1:0, node->hardwaremake, node->hardwaremodel, node->os, node->os_version, node->firmware, last, 540 node->online ? 1:0, node->hardwaremake, node->hardwaremodel, node->os, node->os_version, node->firmware, last,
539 node->temperature, node->humidity, node->barometer, node->gps_latitude, node->gps_longitude, node->gps_altitude, 541 node->temperature, node->humidity, node->barometer, node->gps_latitude, node->gps_longitude, node->gps_altitude,
540 node->net_address, node->net_ifname, node->net_rssi, node->uuid); 542 node->net_address, node->net_ifname, node->net_rssi, node->interval, node->uuid);
541 543
542 bms_mysql_query(query); 544 bms_mysql_query(query);
543 free(query); 545 free(query);
544 } 546 }
545 547
718 void ispindel_mysql_insert(sys_ispindel_list *ispindel) 720 void ispindel_mysql_insert(sys_ispindel_list *ispindel)
719 { 721 {
720 char *query = malloc(2560); 722 char *query = malloc(2560);
721 723
722 snprintf(query, 2559, 724 snprintf(query, 2559,
723 "INSERT INTO mon_ispindels SET node='%s', online='%d', alarm='%d', " \ 725 "INSERT INTO mon_ispindels SET uuid='%s', alias='%s', node='%s', online='%d', mode='%s', alarm='%d', " \
724 "tilt='%.6f', temperature='%.4f', battery='%.4f', gravity='%.6f', interval='%d', rssi='%d'", 726 "angle='%.6f', temperature='%.4f', battery='%.4f', gravity='%.6f', up_interval='%d'",
725 ispindel->node, ispindel->online ? 1:0, ispindel->alarm, 727 ispindel->uuid, ispindel->alias, ispindel->node, ispindel->online ? 1:0, ispindel->mode, ispindel->alarm,
726 ispindel->tilt, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->rssi); 728 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval);
727 729
728 if (bms_mysql_query(query) == 0) { 730 if (bms_mysql_query(query) == 0) {
729 syslog(LOG_NOTICE, "MySQL: insert new ispindel %s", ispindel->node); 731 syslog(LOG_NOTICE, "MySQL: insert new ispindel %s", ispindel->node);
730 } 732 }
731 free(query); 733 free(query);
736 void ispindel_mysql_update(sys_ispindel_list *ispindel) 738 void ispindel_mysql_update(sys_ispindel_list *ispindel)
737 { 739 {
738 char *query = malloc(2560); 740 char *query = malloc(2560);
739 741
740 snprintf(query, 2559, 742 snprintf(query, 2559,
741 "UPDATE mon_ispindels SET online='%d', alarm='%d', " \ 743 "UPDATE mon_ispindels SET online='%d', mode='%s', alarm='%d', " \
742 "tilt='%.3f', temperature='%.3f', battery='%.3f', gravity='%.3f', interval='%d', rssi='%d' WHERE node='%s'", 744 "angle='%.3f', temperature='%.3f', battery='%.3f', gravity='%.3f', up_interval='%d' WHERE uuid='%s'",
743 ispindel->online ? 1:0, ispindel->alarm, 745 ispindel->online ? 1:0, ispindel->mode, ispindel->alarm,
744 ispindel->tilt, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->rssi, ispindel->node); 746 ispindel->angle, ispindel->temperature, ispindel->battery, ispindel->gravity, ispindel->interval, ispindel->uuid);
745 747
746 bms_mysql_query(query); 748 bms_mysql_query(query);
747 free(query); 749 free(query);
748 } 750 }
749 751
750 752
751 753
752 void ispindel_mysql_death(char *node) 754 void ispindel_mysql_death(char *uuid)
753 { 755 {
754 char *query = malloc(512); 756 char *query = malloc(512);
755 757
756 snprintf(query, 511, "UPDATE mon_ispindels SET online='0' WHERE node='%s'", node); 758 snprintf(query, 511, "UPDATE mon_ispindels SET online='0' WHERE uuid='%s'", uuid);
757 bms_mysql_query(query); 759 bms_mysql_query(query);
758 free(query); 760 free(query);
759 } 761 }

mercurial