bmsd/websocket.c

Tue, 19 May 2020 13:50:14 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 19 May 2020 13:50:14 +0200
changeset 680
0bb48333d133
parent 679
48f8f3fce7c0
child 681
7ed5c380e21b
permissions
-rw-r--r--

Fixed missing co2meters websocket data. Added websockets and removed polling for the mon-ispindel screen. The iSpindel SG gauge adjusts the resolution to the highest detected OG.

671
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * @file websocket.c
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 * @brief WebSockets interface
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * @author Michiel Broek <mbroek at mbse dot eu>
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * Copyright (C) 2020
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * Michiel Broek <mbroek at mbse dot eu>
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 *
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * This file is part of the bms (Brewery Management System)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 *
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 * This is free software; you can redistribute it and/or modify it
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * under the terms of the GNU General Public License as published by the
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * Free Software Foundation; either version 2, or (at your option) any
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * later version.
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 *
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 * bms is distributed in the hope that it will be useful, but
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * General Public License for more details.
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 * You should have received a copy of the GNU General Public License
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 * along with ThermFerm; see the file COPYING. If not, write to the Free
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 #include "bms.h"
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 #include "xutil.h"
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 #include "websocket.h"
679
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
30 #include "fermenters.h"
678
14322825cb3d The co2meters monitor screen rebuild to use websockets instead of data polling.
Michiel Broek <mbroek@mbse.eu>
parents: 677
diff changeset
31 #include "co2meters.h"
680
0bb48333d133 Fixed missing co2meters websocket data. Added websockets and removed polling for the mon-ispindel screen. The iSpindel SG gauge adjusts the resolution to the highest detected OG.
Michiel Broek <mbroek@mbse.eu>
parents: 679
diff changeset
32 #include "ispindels.h"
671
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 #include <libwebsockets.h>
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 extern sys_config Config;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 extern int debug;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38 extern int my_shutdown;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40 struct lws_context *context;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 int ws_clients = 0;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 time_t last_msg = 0;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 pthread_mutex_t ws_mutex;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 /*
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 * Based on lws-mirror-protocol from libwebsockets v2.0.x
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 * Debian ships v2.0.3, on Slackware we have 2.4.0 and there
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 * are lots of changes in the api.
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 #define MAX_MESSAGE_QUEUE 512
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52
679
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
53 #define WS_INBUF 2048
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
54
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
55
671
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 /*
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 * one of these created for each message
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 struct a_message {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 void *payload; /* is malloc'd */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 size_t len;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 };
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 static int ringbuffer_head;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70 static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71 {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 struct per_session_data__lws_mirror *pss = (struct per_session_data__lws_mirror *)user;
678
14322825cb3d The co2meters monitor screen rebuild to use websockets instead of data polling.
Michiel Broek <mbroek@mbse.eu>
parents: 677
diff changeset
73 int n, m;
679
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
74 char buf[WS_INBUF + 1];
671
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 switch (reason) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78 case LWS_CALLBACK_ESTABLISHED: {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79 ws_clients++;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 syslog(LOG_NOTICE, "ws: new connection, total %d", ws_clients);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 pss->ringbuffer_tail = ringbuffer_head;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 pss->wsi = wsi;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 break;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 case LWS_CALLBACK_PROTOCOL_DESTROY:
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 syslog(LOG_NOTICE, "ws: protocol cleaning up");
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 for (n = 0; n < sizeof ringbuffer / sizeof ringbuffer[0]; n++)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89 if (ringbuffer[n].payload)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 free(ringbuffer[n].payload);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 break;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 case LWS_CALLBACK_SERVER_WRITEABLE:
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 while (pss->ringbuffer_tail != ringbuffer_head) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 m = ringbuffer[pss->ringbuffer_tail].len;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 n = lws_write(wsi, (unsigned char *)ringbuffer[pss->ringbuffer_tail].payload + LWS_PRE, m, LWS_WRITE_TEXT);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 if (n < 0) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
98 syslog(LOG_NOTICE, "ws: ERROR %d writing", n);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99 return -1;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
100 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
101 if (n < m)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 syslog(LOG_NOTICE, "ws: partial write %d vs %d", n, m);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104 if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 pss->ringbuffer_tail = 0;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 else
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 pss->ringbuffer_tail++;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 if (((ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 lws_rx_flow_allow_all_protocol(lws_get_context(wsi), lws_get_protocol(wsi));
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
111
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 if (lws_send_pipe_choked(wsi)) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 lws_callback_on_writable(wsi);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 break;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117 break;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 case LWS_CALLBACK_RECEIVE:
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120
678
14322825cb3d The co2meters monitor screen rebuild to use websockets instead of data polling.
Michiel Broek <mbroek@mbse.eu>
parents: 677
diff changeset
121 memcpy(buf, in, len);
14322825cb3d The co2meters monitor screen rebuild to use websockets instead of data polling.
Michiel Broek <mbroek@mbse.eu>
parents: 677
diff changeset
122 buf[len] = '\0';
14322825cb3d The co2meters monitor screen rebuild to use websockets instead of data polling.
Michiel Broek <mbroek@mbse.eu>
parents: 677
diff changeset
123 syslog(LOG_NOTICE, "ws: reveived %ld bytes %s", len, buf);
679
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
124 if (strncmp(buf, (char *)"{\"device\":\"fermenters\",", 23) == 0) {
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
125 fermenter_ws_receive(buf);
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
126 } else if (strncmp(buf, (char *)"{\"device\":\"co2meters\",", 22) == 0) {
678
14322825cb3d The co2meters monitor screen rebuild to use websockets instead of data polling.
Michiel Broek <mbroek@mbse.eu>
parents: 677
diff changeset
127 co2meter_ws_receive(buf);
680
0bb48333d133 Fixed missing co2meters websocket data. Added websockets and removed polling for the mon-ispindel screen. The iSpindel SG gauge adjusts the resolution to the highest detected OG.
Michiel Broek <mbroek@mbse.eu>
parents: 679
diff changeset
128 } else if (strncmp(buf, (char *)"{\"device\":\"ispindels\",", 22) == 0) {
0bb48333d133 Fixed missing co2meters websocket data. Added websockets and removed polling for the mon-ispindel screen. The iSpindel SG gauge adjusts the resolution to the highest detected OG.
Michiel Broek <mbroek@mbse.eu>
parents: 679
diff changeset
129 ispindel_ws_receive(buf);
678
14322825cb3d The co2meters monitor screen rebuild to use websockets instead of data polling.
Michiel Broek <mbroek@mbse.eu>
parents: 677
diff changeset
130 }
671
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 break;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 case LWS_CALLBACK_CLOSED:
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 ws_clients--;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136 syslog(LOG_NOTICE, "ws: connection closed, left %d", ws_clients);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 break;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139 default:
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 break;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 return 0;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148 static struct lws_protocols protocols[] = {
679
48f8f3fce7c0 Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.
Michiel Broek <mbroek@mbse.eu>
parents: 678
diff changeset
149 { "bmsd-protocol", callback_ws, sizeof(struct per_session_data__lws_mirror), WS_INBUF },
671
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
150 { NULL, NULL, 0, 0 } /* terminator */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
151 };
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
152
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
154 /*
673
9924b1218d39 Added co2meters to the websockets. Added node and units offline messages to the websockets.
Michiel Broek <mbroek@mbse.eu>
parents: 672
diff changeset
155 * {"node":"host","group":"group","online":1,"lastseen":"datetime","temperature":20.5,"humidity":47,"ip":"ipaddr","rssi":-1}
677
6e82fece1f8f Consistent use of group names is important.
Michiel Broek <mbroek@mbse.eu>
parents: 676
diff changeset
156 * {"device":"fermenters","node":"seaport","unit":"unit0","online":1,"mode":"FRIDGE","yeast_lo":12.0,"yeast_hi":24.0,"air":19.875,"beer":19.812,"chiller":1.500,"heater":100,"cooler":0,"fan":100,"light":0,"door":0,"sp_lo":17.0,"sp_hi":17.5,"alarm":0,"stage":"PRIMARY"}
6e82fece1f8f Consistent use of group names is important.
Michiel Broek <mbroek@mbse.eu>
parents: 676
diff changeset
157 * {"device":"co2meters","node":"seaport","unit":"unit0","online":1,"mode":"ON","temperature":20.875,"pressure_channel":6,"pressure_voltage":0.834,"pressure_zero":0.110,"pressure_bar":2.3,"alarm":0}
6e82fece1f8f Consistent use of group names is important.
Michiel Broek <mbroek@mbse.eu>
parents: 676
diff changeset
158 * {"device":"ispindels","node":"seaport","unit":"unit0","online":1,"mode":"ON","temperature":20.875,"angle":45.223,"battery":4.121,"gravity":14.832,"alarm":0}
671
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159 */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160 void ws_broadcast(char *msg)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
161 {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162 int len, err;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
163
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164 err = pthread_mutex_lock(&ws_mutex);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
165 if (err) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166 syslog(LOG_NOTICE, "ws_broadcast pthread_mutex_lock error %d", err);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
167 } else {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
168
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169 len = strlen(msg);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 if (ringbuffer[ringbuffer_head].payload)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171 free(ringbuffer[ringbuffer_head].payload);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
172
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
173 ringbuffer[ringbuffer_head].payload = malloc(LWS_PRE + len);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
174 ringbuffer[ringbuffer_head].len = len;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175 memcpy((char *)ringbuffer[ringbuffer_head].payload + LWS_PRE, msg, len);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176 if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
177 ringbuffer_head = 0;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
178 else
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179 ringbuffer_head++;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180
676
b0718965dcb1 Turned off debug logs in ws_broadcast
Michiel Broek <mbroek@mbse.eu>
parents: 675
diff changeset
181 // syslog(LOG_NOTICE, "ws: broadcast buffer=%d len=%d", ringbuffer_head, len);
671
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
182
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
183 lws_callback_on_writable_all_protocol(context, &protocols[0]);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184 err = pthread_mutex_unlock(&ws_mutex);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
185 if (err) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
186 syslog(LOG_NOTICE, "ws_broadcast pthread_mutex_unlock error %d", err);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
187 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
188 last_msg = time(NULL);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
189 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
190 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
191
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
192
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
193
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
194 /*
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
195 * Called every 5 seconds.
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
196 */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
197 void ws_check(void)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
198 {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
199 time_t now = time(NULL);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
200
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
201 if (((int)now - (int)last_msg) > 45) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
202 ws_broadcast((char *)"{\"ping\":1}");
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
203 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
204 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
205
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
206
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
207
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
208 void *ws_loop(void *threadid)
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
209 {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
210 struct lws_context_creation_info info;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
211 int n = 0;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
212
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 syslog(LOG_NOTICE, "Thread ws_loop started");
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
215 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
216 info.port = 8010;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
217 info.protocols = protocols;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
218 info.gid = -1;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
219 info.uid = -1;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
220 info.keepalive_timeout = 900;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
221 info.options = LWS_SERVER_OPTION_VALIDATE_UTF8;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
222
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
223 context = lws_create_context(&info);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
224
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
225 if (context == NULL) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226 syslog(LOG_NOTICE, "libwebsocket_create_context() failed");
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
227 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
228
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229 /*
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
230 * Loop forever until external shutdown variable is set.
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
231 */
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
232 while (n >= 0 && ! my_shutdown) {
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
233
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
234 n = lws_service(context, 50);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
235 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
236 lws_context_destroy(context);
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
237
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
238 syslog(LOG_NOTICE, "Thread ws_loop stopped");
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
239 return 0;
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
240 }
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
241
4b54d6f79d25 Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242

mercurial