bmsd/ispindels.c

Fri, 13 Dec 2019 20:33:38 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 13 Dec 2019 20:33:38 +0100
changeset 568
6f3c24e21deb
parent 567
6bf0afc33e70
child 572
7a03181d29a3
permissions
-rw-r--r--

Added ispindel logging. Updated the documentation.

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
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 ispindel_dump(ispindel);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118 if (new_ispindel || do_update) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 t = xstrcpy((char *)"mbv1.0/ispindels/NBIRTH/");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 t = xstrcat(t, node);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 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
123 sprintf(buf, "%d", ispindel->rssi);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 p = xstrcat(p, buf);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 p = xstrcat(p, (char *)"}}}");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126 node_birth_data(t, p);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 free(t);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 free(p);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 if (new_ispindel) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 if (ispindels == NULL) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133 ispindels = ispindel;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 } else {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 for (tmpp = ispindels; tmpp; tmpp = tmpp->next) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136 if (tmpp->next == NULL) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 tmpp->next = ispindel;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138 break;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 ispindel_mysql_insert(ispindel);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 } else if (do_update) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 ispindel_mysql_update(ispindel);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 }
568
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
146
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
147 /*
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
148 * 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
149 */
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
150 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
151 datetime = malloc(72);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
152 mytime = localtime(&timestamp);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
153 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
154 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
155
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
156 line = xstrcpy(datetime);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
157 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
158 snprintf(buf, 64, "%.3f", ispindel->tilt);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
159 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
160 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
161 snprintf(buf, 64, "%.3f", ispindel->temperature);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
162 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
163 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
164 snprintf(buf, 64, "%.3f", ispindel->battery);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
165 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
166 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
167 snprintf(buf, 64, "%.3f", ispindel->gravity);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
168 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
169 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
170 snprintf(buf, 64, "%d", ispindel->interval);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
171 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
172 line = xstrcat(line, (char *)",");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
173 snprintf(buf, 64, "%d", ispindel->rssi);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
174 line = xstrcat(line, buf);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
175
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
176 /*
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
177 * Build logfile name
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
178 */
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
179 logfile = xstrcpy(Config.web_root);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
180 logfile = xstrcat(logfile, (char *)"/log/ispindel/");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
181 logfile = xstrcat(logfile, ispindel->beercode);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
182 logfile = xstrcat(logfile, (char *)" ");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
183 logfile = xstrcat(logfile, ispindel->beername);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
184 logfile = xstrcat(logfile, (char *)".log");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
185
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
186 fp = fopen(logfile, "a");
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
187 if (fp) {
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
188 fprintf(fp, "%s\n", line);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
189 fclose(fp);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
190 } else {
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
191 syslog(LOG_NOTICE, "cannot append to `%s'", logfile);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
192 }
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
193
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
194 free(logfile);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
195 logfile = NULL;
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
196 free(line);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
197 line = NULL;
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
198 free(datetime);
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
199 datetime = NULL;
6f3c24e21deb Added ispindel logging. Updated the documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 567
diff changeset
200 }
567
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
201 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
202
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
203
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
204
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
205 /*
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
206 * Process iSpindel MQTT message.
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 void ispindel_mqtt(char *topic, char *payload)
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 char *namespace, *node, *keyword;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
211
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
212 namespace = strtok(topic, "/"); // must be ispindel
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 node = strtok(NULL, "/");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214 keyword = strtok(NULL, "/\0");
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
215
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
216 if (strcmp(namespace, "ispindels")) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
217 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
218 return;
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
219 }
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 ispindel_set(node, keyword, payload);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
222 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
223
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 void ispindel_dump(sys_ispindel_list *ispindel)
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 if (debug) {
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229 printf("node %s\n", ispindel->node);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
230 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
231 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
232 printf("tilt %.3f\n", ispindel->tilt);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
233 printf("temperature %.3f\n", ispindel->temperature);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
234 printf("battery %.3f\n", ispindel->battery);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
235 printf("gravity %.3f\n", ispindel->gravity);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
236 printf("interval %d\n", ispindel->interval);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
237 printf("rssi %d\n", ispindel->rssi);
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
238 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
239 }
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
240
6bf0afc33e70 Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
241

mercurial