Fri, 13 Dec 2019 16:49:50 +0100
Initial code for iSpindel support in the daemon
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 |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * @brief Handle ispindels status |
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; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | char *t, *p; |
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'; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | 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
|
52 | |
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 | * 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
|
55 | */ |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | if (ispindels) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | for (tmpp = ispindels; tmpp; tmpp = tmpp->next) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | if (strcmp(tmpp->node, node) == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | new_ispindel = false; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | ispindel = tmpp; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | break; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | } |
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 | 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
|
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 | * Allocate new ispindel if not yet known. |
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 | if (new_ispindel) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | 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
|
73 | memset(ispindel, 0, sizeof(sys_ispindel_list)); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | ispindel->node = xstrcpy(node); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | if (! ispindel->online) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | ispindel->online = true; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | syslog(LOG_NOTICE, "Online ispindel %s", node); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | /* |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | * Process the simple iSpindel MQTT payload. |
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 | if (strcmp(key, "tilt") == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | ispindel->tilt = atof(payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | } else if (strcmp(key, "temperature") == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
88 | temperature = atof(payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
89 | } else if (strcmp(key, "temp_units") == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | temp_units = payload[0]; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | } else if (strcmp(key, "battery") == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
92 | ispindel->battery = atof(payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | } else if (strcmp(key, "gravity") == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
94 | ispindel->gravity = atof(payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
95 | } else if (strcmp(key, "interval") == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
96 | ispindel->interval = atoi(payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
97 | } else if (strcmp(key, "RSSI") == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
98 | ispindel->rssi = atoi(payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
99 | do_update = true; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
100 | if (temp_units == 'C') { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
101 | ispindel->temperature = temperature; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
102 | } else if (temp_units == 'F') { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | ispindel->temperature = temperature / 1.8 - 32; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
104 | } else if (temp_units == 'K') { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
105 | ispindel->temperature = temperature - 273.15; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
106 | } else { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | ispindel->temperature = temperature; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
108 | } |
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 | 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
|
111 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
112 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
113 | ispindel_dump(ispindel); |
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 | if (new_ispindel || do_update) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
116 | char buf[21]; |
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 | t = xstrcpy((char *)"mbv1.0/ispindels/NBIRTH/"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
119 | t = xstrcat(t, node); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
120 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
121 | 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
|
122 | sprintf(buf, "%d", ispindel->rssi); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
123 | p = xstrcat(p, buf); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
124 | p = xstrcat(p, (char *)"}}}"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
125 | node_birth_data(t, p); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
126 | free(t); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
127 | free(p); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
128 | } |
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 | if (new_ispindel) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
131 | if (ispindels == NULL) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
132 | ispindels = ispindel; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | } else { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
134 | for (tmpp = ispindels; tmpp; tmpp = tmpp->next) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
135 | if (tmpp->next == NULL) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
136 | tmpp->next = ispindel; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
137 | break; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
138 | } |
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 | ispindel_mysql_insert(ispindel); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
142 | } else if (do_update) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
143 | ispindel_mysql_update(ispindel); |
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 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
148 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
149 | /* |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
150 | * Process iSpindel MQTT message. |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
151 | */ |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
152 | void ispindel_mqtt(char *topic, char *payload) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
153 | { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
154 | char *namespace, *node, *keyword; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
155 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
156 | namespace = strtok(topic, "/"); // must be ispindel |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
157 | node = strtok(NULL, "/"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
158 | keyword = strtok(NULL, "/\0"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
159 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
160 | if (strcmp(namespace, "ispindels")) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
161 | 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
|
162 | return; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
163 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
164 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
165 | ispindel_set(node, keyword, payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
166 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
167 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
168 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
169 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
170 | void ispindel_log(char *topic, char *payload) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
171 | { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
172 | char *edge_node, *alias, *line, buf[128], *logfile; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
173 | struct json_object *jobj, *val, *metric; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
174 | co2pressure_log *log; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
175 | struct tm *mytime; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
176 | time_t timestamp; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
177 | FILE *fp; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
178 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
179 | strtok(topic, "/"); // ignore namespace |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
180 | strtok(NULL, "/"); // group_id |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
181 | strtok(NULL, "/"); // message_type |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
182 | edge_node = strtok(NULL, "/\0"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
183 | alias = strtok(NULL, "/\0"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
184 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
185 | log = (co2pressure_log *)malloc(sizeof(co2pressure_log)); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
186 | memset(log, 0, sizeof(co2pressure_log)); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
187 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
188 | log->node = xstrcpy(edge_node); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
189 | log->alias = xstrcpy(alias); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
190 | jobj = json_tokener_parse(payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
191 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
192 | timestamp = time(NULL); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
193 | log->datetime = malloc(73); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
194 | mytime = localtime(×tamp); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
195 | snprintf(log->datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d", |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
196 | mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
197 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
198 | if (json_object_object_get_ex(jobj, "metric", &metric)) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
199 | if (json_object_object_get_ex(metric, "uuid", &val)) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
200 | if (strcmp((char *)"(null)", json_object_get_string(val))) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
201 | log->uuid = xstrcpy((char *)json_object_get_string(val)); |
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 | if (json_object_object_get_ex(metric, "temperature", &val)) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
204 | log->temperature = json_object_get_double(val); |
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 | if (json_object_object_get_ex(metric, "pressure", &val)) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
207 | log->pressure = json_object_get_double(val); |
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 | json_object_put(jobj); |
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 | /* |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
213 | * Because ispindels are not so smart and don't hold product information |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
214 | * search the missing pieces in the database. |
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 | snprintf(buf, 127, "SELECT beercode,beername,beeruuid FROM mon_ispindels WHERE uuid='%s'", log->uuid); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
217 | if (mysql_query(con, buf)) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
218 | syslog(LOG_NOTICE, "MySQL: %s error %u (%s))", buf, mysql_errno(con), mysql_error(con)); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
219 | } else { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
220 | res_set = mysql_store_result(con); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
221 | if (res_set == NULL) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
222 | syslog(LOG_NOTICE, "MySQL: mysq_store_result error %u (%s))", mysql_errno(con), mysql_error(con)); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
223 | } else { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
224 | if ((row = mysql_fetch_row(res_set)) != NULL) { |
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 | * Ignore when the beer_name or beer_code is not set. |
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 ((int)strlen(row[0]) == 0 || (int)strlen(row[1]) == 0) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
229 | if (log->datetime) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
230 | free(log->datetime); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
231 | if (log->uuid) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
232 | free(log->uuid); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
233 | if (log->node) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
234 | free(log->node); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
235 | if (log->alias) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
236 | free(log->alias); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
237 | free(log); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
238 | return; |
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 | log->product_code = xstrcpy(row[0]); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
241 | log->product_name = xstrcpy(row[1]); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
242 | log->product_uuid = xstrcpy(row[2]); |
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 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
247 | /* |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
248 | * Build csv log line |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
249 | */ |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
250 | line = xstrcpy(log->datetime); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
251 | line = xstrcat(line, (char *)","); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
252 | snprintf(buf, 64, "%.3f", log->temperature); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
253 | line = xstrcat(line, buf); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
254 | line = xstrcat(line, (char *)","); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
255 | snprintf(buf, 64, "%.3f", log->pressure); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
256 | line = xstrcat(line, buf); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
257 | line = xstrcat(line, (char *)","); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
258 | line = xstrcat(line, log->uuid); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
259 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
260 | /* |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
261 | * Build logfile name |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
262 | */ |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
263 | logfile = xstrcpy(Config.web_root); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
264 | logfile = xstrcat(logfile, (char *)"/log/co2pressure/"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
265 | logfile = xstrcat(logfile, log->product_code); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
266 | logfile = xstrcat(logfile, (char *)" "); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
267 | logfile = xstrcat(logfile, log->product_name); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
268 | logfile = xstrcat(logfile, (char *)".log"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
269 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
270 | if (debug) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
271 | fprintf(stdout, "%s %s\n", logfile, line); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
272 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
273 | fp = fopen(logfile, "a"); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
274 | if (fp) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
275 | fprintf(fp, "%s\n", line); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
276 | fclose(fp); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
277 | } else { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
278 | syslog(LOG_NOTICE, "cannot append to `%s'", logfile); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
279 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
280 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
281 | free(logfile); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
282 | logfile = NULL; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
283 | free(line); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
284 | line = NULL; |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
285 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
286 | if (log->datetime) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
287 | free(log->datetime); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
288 | if (log->product_uuid ) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
289 | free(log->product_uuid ); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
290 | if (log->product_code ) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
291 | free(log->product_code ); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
292 | if (log->product_name ) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
293 | free(log->product_name ); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
294 | if (log->uuid) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
295 | free(log->uuid); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
296 | if (log->node) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
297 | free(log->node); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
298 | if (log->alias) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
299 | free(log->alias); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
300 | free(log); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
301 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
302 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
303 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
304 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
305 | void ispindel_dump(sys_ispindel_list *ispindel) |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
306 | { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
307 | if (debug) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
308 | printf("node %s\n", ispindel->node); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
309 | 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
|
310 | 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
|
311 | printf("tilt %.3f\n", ispindel->tilt); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
312 | printf("temperature %.3f\n", ispindel->temperature); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
313 | printf("battery %.3f\n", ispindel->battery); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
314 | printf("gravity %.3f\n", ispindel->gravity); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
315 | printf("interval %d\n", ispindel->interval); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
316 | printf("rssi %d\n", ispindel->rssi); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
317 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
318 | } |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
319 | |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
320 |