Wed, 23 Apr 2014 17:19:00 +0200
Server communication seems ok, can handle server shutdowns too.
1 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2014 | |
3 | * | |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with EC-65K; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
23 | #include "../lib/mbselib.h" |
1 | 24 | #include "main.h" |
25 | ||
26 | ||
6 | 27 | #define STATUS_CONNECTING 0 |
28 | #define STATUS_CONNACK_RECVD 1 | |
29 | #define STATUS_WAITING 2 | |
30 | ||
31 | /* Global variables for use in callbacks. See sub_client.c for an example of | |
32 | * using a struct to hold variables for use in callbacks. */ | |
33 | static char *topic = NULL; | |
34 | //static char *message = NULL; | |
35 | //static long msglen = 0; | |
36 | static int qos = 0; | |
37 | //static int retain = 0; | |
38 | static int status = STATUS_CONNECTING; | |
39 | static int mid_sent = 0; | |
40 | static int last_mid = -1; | |
41 | static int last_mid_sent = -1; | |
42 | static bool connected = true; | |
43 | static bool disconnect_sent = false; | |
44 | static bool shutdown = false; | |
45 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
46 | extern bool debug; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
47 | extern sys_config Config; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
48 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
49 | int server(void); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
50 | void help(void); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
51 | void die(int); |
6 | 52 | |
53 | ||
1 | 54 | void help(void) |
55 | { | |
56 | fprintf(stdout, "Usage: thermomeneters [-d] [-h]\n"); | |
57 | fprintf(stdout, " -d --debug Debug on\n"); | |
58 | fprintf(stdout, " -h --help Display this help\n"); | |
59 | } | |
60 | ||
61 | ||
62 | ||
63 | void die(int onsig) | |
64 | { | |
65 | switch (onsig) { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
66 | case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); |
1 | 67 | break; |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
68 | case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); |
1 | 69 | break; |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
70 | case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); |
1 | 71 | break; |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
72 | default: syslog(LOG_NOTICE, "die() on signal %d", onsig); |
1 | 73 | } |
74 | ||
6 | 75 | shutdown = true; |
76 | } | |
77 | ||
78 | ||
79 | ||
80 | void my_connect_callback(struct mosquitto *mosq, void *obj, int result) | |
81 | { | |
82 | if (!result) { | |
83 | status = STATUS_CONNACK_RECVD; | |
84 | } else { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
85 | syslog(LOG_NOTICE, "my_connect_callback: %s\n", mosquitto_connack_string(result)); |
6 | 86 | } |
87 | } | |
88 | ||
89 | ||
90 | ||
91 | void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc) | |
92 | { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
93 | if (shutdown) { |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
94 | syslog(LOG_NOTICE, "Acknowledged DISCONNECT from %s", Config.mosq_host); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
95 | connected = false; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
96 | } else { |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
97 | /* |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
98 | * The remove server was brought down. We must keep running |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
99 | */ |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
100 | syslog(LOG_NOTICE, "Received DISCONNECT from %s but we want to run", Config.mosq_host); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
101 | /* |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
102 | * We need a temp state |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
103 | */ |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
104 | } |
6 | 105 | } |
106 | ||
107 | ||
108 | ||
109 | void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) | |
110 | { | |
111 | fprintf(stdout, (char *)"my_publish_callback mid=%d\n", mid); | |
112 | ||
113 | last_mid_sent = mid; | |
114 | } | |
115 | ||
116 | ||
117 | ||
118 | void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str) | |
119 | { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
120 | syslog(LOG_NOTICE, "MQTT: %s", str); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
121 | printf("MQTT: %s\n", str); |
1 | 122 | } |
123 | ||
124 | ||
125 | ||
126 | int main(int argc, char *argv[]) | |
127 | { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
128 | int rc, c, i; |
1 | 129 | |
130 | while (1) { | |
131 | int option_index = 0; | |
132 | static struct option long_options[] = { | |
133 | {"debug", 0, 0, 'c'}, | |
134 | {"help", 0, 0, 'h'}, | |
135 | {0, 0, 0, 0} | |
136 | }; | |
137 | ||
138 | c = getopt_long(argc, argv, "dh", long_options, &option_index); | |
139 | if (c == -1) | |
140 | break; | |
141 | ||
142 | switch (c) { | |
6 | 143 | case 'd': debug = true; |
1 | 144 | break; |
145 | case 'h': help(); | |
146 | return 1; | |
147 | } | |
148 | } | |
149 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
150 | openlog("thermometers", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
151 | syslog(LOG_NOTICE, "mbsePi-apps thermometers v%s starting", VERSION); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
152 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
153 | if (rdconfig()) { |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
154 | fprintf(stderr, "Error reading configuration\n"); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
155 | syslog(LOG_NOTICE, "halted"); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
156 | return 1; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
157 | } |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
158 | |
1 | 159 | /* |
160 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored | |
161 | * but that's live. This daemon should only be stopped by SIGTERM. | |
162 | * Don't catch SIGCHLD. | |
163 | */ | |
164 | for (i = 0; i < NSIG; i++) { | |
165 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) | |
166 | signal(i, (void (*))die); | |
167 | } | |
168 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
169 | rc = server(); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
170 | syslog(LOG_NOTICE, "Finished, rc=%d", rc); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
171 | return rc; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
172 | } |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
173 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
174 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
175 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
176 | int server(void) |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
177 | { |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
178 | char *id = NULL, *state = NULL; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
179 | struct mosquitto *mosq = NULL; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
180 | char hostname[256], buf[1024]; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
181 | int rc, keepalive = 60; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
182 | unsigned int max_inflight = 20; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
183 | char err[1024]; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
184 | |
6 | 185 | /* |
186 | * Initialize mosquitto communication | |
187 | */ | |
188 | mosquitto_lib_init(); | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
189 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
190 | /* |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
191 | * Build MQTT id |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
192 | */ |
6 | 193 | hostname[0] = '\0'; |
194 | gethostname(hostname, 256); | |
195 | hostname[255] = '\0'; | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
196 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
197 | id = xstrcpy((char *)"thermometers/"); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
198 | id = xstrcat(id, hostname); |
6 | 199 | if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { |
200 | /* | |
201 | * Enforce maximum client id length of 23 characters | |
202 | */ | |
203 | id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; | |
204 | } | |
1 | 205 | |
6 | 206 | mosq = mosquitto_new(id, true, NULL); |
207 | if(!mosq) { | |
208 | switch(errno) { | |
209 | case ENOMEM: | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
210 | syslog(LOG_NOTICE, "mosquitto_new: Out of memory"); |
6 | 211 | break; |
212 | case EINVAL: | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
213 | syslog(LOG_NOTICE, "mosquitto_new: Invalid id"); |
6 | 214 | break; |
215 | } | |
216 | mosquitto_lib_cleanup(); | |
217 | return 1; | |
218 | } | |
219 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
220 | if (debug) { |
6 | 221 | mosquitto_log_callback_set(mosq, my_log_callback); |
222 | } | |
223 | ||
224 | /* | |
225 | * Set our will | |
226 | */ | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
227 | state = xstrcpy((char *)"clients/"); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
228 | state = xstrcat(state, hostname); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
229 | state = xstrcat(state, (char *)"/thermometers/state"); |
6 | 230 | sprintf(buf, "0"); |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
231 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
232 | rc = mosquitto_will_set(mosq, state, strlen(buf), buf, qos, true); |
6 | 233 | if (rc) { |
234 | if (rc == MOSQ_ERR_INVAL) { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
235 | syslog(LOG_NOTICE, "mosquitto_will_set: input parameters invalid"); |
6 | 236 | } else if (rc == MOSQ_ERR_NOMEM) { |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
237 | syslog(LOG_NOTICE, "mosquitto_will_set: Out of Memory"); |
6 | 238 | } else if (rc == MOSQ_ERR_PAYLOAD_SIZE) { |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
239 | syslog(LOG_NOTICE, "mosquitto_will_set: invalid payload size"); |
6 | 240 | } |
241 | mosquitto_lib_cleanup(); | |
242 | return rc; | |
243 | } | |
244 | ||
245 | mosquitto_max_inflight_messages_set(mosq, max_inflight); | |
246 | mosquitto_connect_callback_set(mosq, my_connect_callback); | |
247 | mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); | |
248 | mosquitto_publish_callback_set(mosq, my_publish_callback); | |
249 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
250 | rc = mosquitto_connect(mosq, Config.mosq_host, Config.mosq_port, keepalive); |
6 | 251 | if (rc) { |
252 | if (rc == MOSQ_ERR_ERRNO) { | |
253 | strerror_r(errno, err, 1024); | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
254 | syslog(LOG_NOTICE, "mosquitto_connect: error: %s", err); |
6 | 255 | } else { |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
256 | syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc); |
6 | 257 | } |
258 | mosquitto_lib_cleanup(); | |
259 | return rc; | |
260 | } | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
261 | syslog(LOG_NOTICE, "Connected with %s:%d", Config.mosq_host, Config.mosq_port); |
6 | 262 | |
263 | /* | |
264 | * Initialise is complete, report our presence state | |
265 | */ | |
266 | mosquitto_loop_start(mosq); | |
267 | ||
268 | sprintf(buf, "1"); | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
269 | rc = mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, 1); |
6 | 270 | |
271 | fprintf(stdout, (char *)"Enter loop, connected %d\n", connected); | |
272 | do { | |
273 | if (status == STATUS_CONNACK_RECVD) { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
274 | /* |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
275 | * Sleep just log enough to keep the system load low. |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
276 | */ |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
277 | usleep(1); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
278 | /* |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
279 | * Here send our sensors values |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
280 | */ |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
281 | |
6 | 282 | if (shutdown) { |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
283 | /* |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
284 | * Final publish 0 to clients/<hostname>/thermometers/state |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
285 | */ |
6 | 286 | sprintf(buf, "0"); |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
287 | mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, true); |
6 | 288 | free(topic); |
289 | last_mid = mid_sent; | |
290 | status = STATUS_WAITING; | |
291 | } | |
292 | } else if (status == STATUS_WAITING) { | |
293 | fprintf(stdout, (char *)"Waiting\n"); | |
294 | if (last_mid_sent == last_mid && disconnect_sent == false) { | |
295 | mosquitto_disconnect(mosq); | |
296 | disconnect_sent = true; | |
297 | } | |
298 | usleep(100000); | |
299 | } | |
300 | rc = MOSQ_ERR_SUCCESS; | |
301 | ||
302 | } while(rc == MOSQ_ERR_SUCCESS && connected); | |
303 | fprintf(stdout, (char *)"Out of loop\n"); | |
304 | ||
305 | mosquitto_loop_stop(mosq, false); | |
306 | ||
307 | mosquitto_destroy(mosq); | |
308 | mosquitto_lib_cleanup(); | |
309 | ||
1 | 310 | return 0; |
311 | } | |
312 |