bmsd/ispindels.c

Sun, 15 Dec 2019 15:08:09 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 15 Dec 2019 15:08:09 +0100
changeset 572
7a03181d29a3
parent 568
6f3c24e21deb
child 578
e75ce5bbda73
permissions
-rw-r--r--

Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.

567
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * @file ispindels.c
568
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
3 * @brief Handle ispindels data
567
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * @author Michiel Broek <mbroek at mbse dot eu>
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * Copyright (C) 2019
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This file is part of the bms (Brewery Management System)
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 *
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * This is free software; you can redistribute it and/or modify it
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * under the terms of the GNU General Public License as published by the
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 * Free Software Foundation; either version 2, or (at your option) any
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * later version.
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 *
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * bms is distributed in the hope that it will be useful, but
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * General Public License for more details.
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 *
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * You should have received a copy of the GNU General Public License
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 * along with ThermFerm; see the file COPYING. If not, write to the Free
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 */
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 #include "bms.h"
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 #include "xutil.h"
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 #include "ispindels.h"
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 #include "mysql.h"
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 #include "nodes.h"
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 sys_ispindel_list *ispindels = NULL;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 extern int debug;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 extern sys_config Config;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 extern MYSQL *con;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38 extern MYSQL_RES *res_set;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 extern MYSQL_ROW row;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 void ispindel_set(char *node, char *key, char *payload)
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 sys_ispindel_list *ispindel, *tmpp;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 bool new_ispindel = true, do_update = false;
568
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
47 char *t, *p, *datetime, buf[65], *line, *logfile;
567
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 float temperature = 20;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 uint8_t temp_units = 'C';
568
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
50 struct tm *mytime;
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
51 time_t timestamp;
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
52 FILE *fp;
567
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 fprintf(stdout, "ispindel_set: %s %s\n", node, payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 /*
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 * Search ispindel record in the memory array and use it if found.
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 */
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 if (ispindels) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 for (tmpp = ispindels; tmpp; tmpp = tmpp->next) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 if (strcmp(tmpp->node, node) == 0) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 new_ispindel = false;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 ispindel = tmpp;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 break;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 printf("new_ispindel %s\n", new_ispindel ? "true":"false");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71 /*
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 * Allocate new ispindel if not yet known.
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73 */
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 if (new_ispindel) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 ispindel = (sys_ispindel_list *)malloc(sizeof(sys_ispindel_list));
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 memset(ispindel, 0, sizeof(sys_ispindel_list));
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 ispindel->node = xstrcpy(node);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 if (! ispindel->online) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 ispindel->online = true;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 syslog(LOG_NOTICE, "Online ispindel %s", node);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 /*
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 * Process the simple iSpindel MQTT payload.
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 */
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 if (strcmp(key, "tilt") == 0) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89 ispindel->tilt = atof(payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 } else if (strcmp(key, "temperature") == 0) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 temperature = atof(payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 } else if (strcmp(key, "temp_units") == 0) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 temp_units = payload[0];
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 } else if (strcmp(key, "battery") == 0) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 ispindel->battery = atof(payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 } else if (strcmp(key, "gravity") == 0) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 ispindel->gravity = atof(payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
98 } else if (strcmp(key, "interval") == 0) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99 ispindel->interval = atoi(payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
100 } else if (strcmp(key, "RSSI") == 0) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
101 ispindel->rssi = atoi(payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 do_update = true;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 if (temp_units == 'C') {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104 ispindel->temperature = temperature;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 } else if (temp_units == 'F') {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 ispindel->temperature = temperature / 1.8 - 32;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 } else if (temp_units == 'K') {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 ispindel->temperature = temperature - 273.15;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 } else {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 ispindel->temperature = temperature;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
111 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 } else {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 syslog(LOG_NOTICE, "Unknown keyword `%s' from `%s'", key, node);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115
572
7a03181d29a3 Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents: 568
diff changeset
116 if (ispindel->battery < LOWBATT)
7a03181d29a3 Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents: 568
diff changeset
117 ispindel->alarm |= ALARM_FLAG_BATTERY;
7a03181d29a3 Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents: 568
diff changeset
118 else
7a03181d29a3 Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents: 568
diff changeset
119 ispindel->alarm &= ~ALARM_FLAG_BATTERY;
7a03181d29a3 Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents: 568
diff changeset
120
567
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 ispindel_dump(ispindel);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 if (new_ispindel || do_update) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 t = xstrcpy((char *)"mbv1.0/ispindels/NBIRTH/");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 t = xstrcat(t, node);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 p = xstrcpy((char *)"{\"metric\":{\"properties\":{\"hardwaremake\":\"MBSE\",\"hardwaremodel\":\"Wemos D1 mini\"},\"net\":{\"rssi\":");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 sprintf(buf, "%d", ispindel->rssi);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129 p = xstrcat(p, buf);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130 p = xstrcat(p, (char *)"}}}");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 node_birth_data(t, p);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 free(t);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133 free(p);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136 if (new_ispindel) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 if (ispindels == NULL) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138 ispindels = ispindel;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139 } else {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 for (tmpp = ispindels; tmpp; tmpp = tmpp->next) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 if (tmpp->next == NULL) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 tmpp->next = ispindel;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 break;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147 ispindel_mysql_insert(ispindel);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148 } else if (do_update) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149 ispindel_mysql_update(ispindel);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
150 }
568
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
151
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
152 /*
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
153 * The data is complete, see if we can write a log entry.
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
154 */
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
155 if (do_update && ispindel->beercode && strlen(ispindel->beercode) && ispindel->beername && strlen(ispindel->beername)) {
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
156 datetime = malloc(72);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
157 mytime = localtime(&timestamp);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
158 snprintf(datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d",
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
159 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
160
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
161 line = xstrcpy(datetime);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
162 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
163 snprintf(buf, 64, "%.3f", ispindel->tilt);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
164 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
165 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
166 snprintf(buf, 64, "%.3f", ispindel->temperature);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
167 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
168 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
169 snprintf(buf, 64, "%.3f", ispindel->battery);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
170 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
171 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
172 snprintf(buf, 64, "%.3f", ispindel->gravity);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
173 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
174 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
175 snprintf(buf, 64, "%d", ispindel->interval);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
176 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
177 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
178 snprintf(buf, 64, "%d", ispindel->rssi);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
179 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
180
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
181 /*
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
182 * Build logfile name
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
183 */
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
184 logfile = xstrcpy(Config.web_root);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
185 logfile = xstrcat(logfile, (char *)"/log/ispindel/");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
186 logfile = xstrcat(logfile, ispindel->beercode);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
187 logfile = xstrcat(logfile, (char *)" ");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
188 logfile = xstrcat(logfile, ispindel->beername);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
189 logfile = xstrcat(logfile, (char *)".log");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
190
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
191 fp = fopen(logfile, "a");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
192 if (fp) {
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
193 fprintf(fp, "%s\n", line);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
194 fclose(fp);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
195 } else {
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
196 syslog(LOG_NOTICE, "cannot append to `%s'", logfile);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
197 }
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
198
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
199 free(logfile);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
200 logfile = NULL;
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
201 free(line);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
202 line = NULL;
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
203 free(datetime);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
204 datetime = NULL;
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
205 }
567
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
206 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
207
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
208
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
209
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
210 /*
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
211 * Process iSpindel MQTT message.
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
212 */
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 void ispindel_mqtt(char *topic, char *payload)
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214 {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
215 char *namespace, *node, *keyword;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
216
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
217 namespace = strtok(topic, "/"); // must be ispindel
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
218 node = strtok(NULL, "/");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
219 keyword = strtok(NULL, "/\0");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
220
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
221 if (strcmp(namespace, "ispindels")) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
222 syslog(LOG_NOTICE, "ispindel_mqtt(%s, %s) error", topic, payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
223 return;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
224 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
225
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226 ispindel_set(node, keyword, payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
227 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
228
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
230
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
231 void ispindel_dump(sys_ispindel_list *ispindel)
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
232 {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
233 if (debug) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
234 printf("node %s\n", ispindel->node);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
235 printf("online %s\n", ispindel->online ? "yes":"no");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
236 printf("product %s / %s\n", ispindel->beercode, ispindel->beername);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
237 printf("tilt %.3f\n", ispindel->tilt);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
238 printf("temperature %.3f\n", ispindel->temperature);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
239 printf("battery %.3f\n", ispindel->battery);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
240 printf("gravity %.3f\n", ispindel->gravity);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
241 printf("interval %d\n", ispindel->interval);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242 printf("rssi %d\n", ispindel->rssi);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
243 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
244 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
245
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
246

mercurial