-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_server.py
executable file
·415 lines (369 loc) · 11.7 KB
/
socket_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python
import asyncio
import json
import dataclasses
from dataclasses import dataclass
from datetime import datetime
from asyncio_paho import AsyncioPahoClient
from paho.mqtt.client import MQTTMessage
from websockets.asyncio.server import (
broadcast,
serve,
ServerConnection as WebsocketConnection,
)
from hackmap.local_settings import (
DEBUG,
MQTT_HOST,
ROOM_ACTIVE_TIME,
ROOM_EXPIRY_POLL_SECS,
WEBSOCKET_BIND,
)
ROOM_WHITELIST = ["g1", "g2", "g8", "g11", "g14"]
# Open websockets
CONNECTIONS: set[WebsocketConnection] = set()
# Map from room name to presence expiry time (or True if waiting for presence sensor to fade)
ROOM_STATES: dict[str, datetime | bool] = {}
# Map from (type, target) to state
CACHED_STATES: dict[tuple[str, str], str] = {}
@dataclass
class WebsocketMessage:
# Shown on the log
display: str | None
# Targets an SVG element
type: str | None
target: str | None
# Sets a class on that element
state: str | None
async def new_websocket(websocket: WebsocketConnection):
"""Record a new websocket connection"""
CONNECTIONS.add(websocket)
try:
# Send cached state so it's up to date
for (type, target), state in CACHED_STATES.items():
msg = WebsocketMessage(None, type, target, state)
print(msg) if DEBUG else ""
await websocket.send(json.dumps(dataclasses.asdict(msg)))
await websocket.wait_closed()
finally:
CONNECTIONS.remove(websocket)
async def subscribe_topics(client, _userdata, _flags_dict, _result):
"""Subscribe to MQTT topics for a new connection"""
await client.asyncio_subscribe("doorman/+/user")
# await client.asyncio_subscribe("sensor/+/presence")
await client.asyncio_subscribe("tool/+/user")
await client.asyncio_subscribe("environment/+/heating")
await client.asyncio_subscribe("environment/+/elsys/temperature")
await client.asyncio_subscribe("sensor/g1/temperature")
async def doorman_message(msg):
"""Handle a doorman MQTT message, from doorman/+/user"""
name: str = msg.payload.decode("utf-8")
if len(name) == 0:
# Door closing again, ignore
return
# topic is of the form doorman/<room>/user
room = msg.topic.split("/")[1]
if name == "anonymous" or room not in ROOM_WHITELIST:
return
# mark the room as active for some period of time, to get around the presence sensors sucking ass
ROOM_STATES[room] = datetime.now() + ROOM_ACTIVE_TIME
await send_message(
WebsocketMessage(
f"<span class=username>{name}</span> entered <span class=room>{room}</span>",
"room",
room,
"active",
)
)
async def presence_message(msg):
"""Handle a presence MQTT message, from sensor/+/presence"""
# topic is of the form sensor/<room>/presence
room = msg.topic.split("/")[1]
state: str = msg.payload.decode("utf-8")
if room == "global":
# special thing we don't care about
return
if state == "active":
# only display a message if room isn't already active
do_display = room not in ROOM_STATES
# don't expire the room state as we'll get an empty message
ROOM_STATES[room] = True
await send_message(
WebsocketMessage(
f"someone is in <span class=room>{room}</span>" if do_display else None,
"room",
room,
"active",
)
)
elif state == "empty":
do_display = room in ROOM_STATES
if do_display:
del ROOM_STATES[room]
await send_message(
WebsocketMessage(
f"<span class=room>{room}</span> is now empty" if do_display else None,
"room",
room,
"inactive",
)
)
async def tool_message(msg):
"""Handle a tool status MQTT message, from tool/+/user"""
# topic is of the form tool/<room>/user
tool = msg.topic.split("/")[1]
name: str = msg.payload.decode("utf-8")
if len(name) == 0:
await send_message(
WebsocketMessage(
f"<span class=tool>{tool}</span> no longer in use",
"tool",
tool,
"inactive",
)
)
elif name != "anonymous":
await send_message(
WebsocketMessage(
f"<span class=username>{name}</span> is now using <span class=tool>{tool}</span>",
"tool",
tool,
"active",
)
)
async def heating_message(msg):
"""Handle a heating status MQTT message, from environment/+/heating"""
# topic is of the form environment/<room>/heating
room = msg.topic.split("/")[1]
value: str = msg.payload.decode("utf-8")
await send_message(
WebsocketMessage(
f"<span class=room>{room}</span> heating set to <span class=temp>{value}</span>",
"heating",
room,
value,
)
)
async def temperature_message(msg):
"""Handle a temperature status MQTT message, from environment/+/elsys/temperature"""
# topic is of the form environment/<room>/...
room = msg.topic.split("/")[1]
if room == "g1" and "elsys" in msg.topic:
return
value: str = msg.payload.decode("utf-8")
await send_message(WebsocketMessage(None, "temperature", room, value))
async def mqtt_message(_client, _userdata, msg: MQTTMessage):
"""Dispatch MQTT message to the correct handler"""
try:
if msg.topic.startswith("doorman/") and msg.topic.endswith("/user"):
await doorman_message(msg)
elif msg.topic.startswith("sensor/") and msg.topic.endswith("/presence"):
await presence_message(msg)
elif msg.topic.startswith("tool/") and msg.topic.endswith("/user"):
await tool_message(msg)
elif msg.topic.startswith("environment/") and msg.topic.endswith("/heating"):
await heating_message(msg)
elif msg.topic.endswith("/temperature"):
await temperature_message(msg)
except Exception as e:
print(f"Error processing message: {e}")
async def event_loop():
client = AsyncioPahoClient()
client.asyncio_listeners.add_on_connect(subscribe_topics)
client.asyncio_listeners.add_on_message(mqtt_message)
await client.asyncio_connect(MQTT_HOST)
while True:
await asyncio.sleep(ROOM_EXPIRY_POLL_SECS)
try:
for room in list(
ROOM_STATES.keys()
): # needed because dictionary size will change
expiry = ROOM_STATES[room]
if type(expiry) is datetime and expiry <= datetime.now():
del ROOM_STATES[room]
await send_message(
WebsocketMessage(
None,
"room",
room,
"inactive",
)
)
except Exception as e:
print(f"Error in event loop: {e}")
async def send_message(msg: WebsocketMessage):
print(msg) if DEBUG else ""
if msg.state is not None and msg.type is not None and msg.target is not None:
CACHED_STATES[(msg.type, msg.target)] = msg.state
broadcast(CONNECTIONS, json.dumps(dataclasses.asdict(msg)))
# Code for testing without MQTT
# MESSAGES = [
# WebsocketMessage(
# "<span class=username>aria</span> started using lasercutter",
# "tool",
# "lasercutter",
# "active",
# ),
# WebsocketMessage(
# "<span class=tool>lasercutter</span> is no longer in use",
# "tool",
# "lasercutter",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> started using lasercutter2",
# "tool",
# "lasercutter2",
# "active",
# ),
# WebsocketMessage(
# "<span class=tool>lasercutter2</span> is no longer in use",
# "tool",
# "lasercutter2",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> started using robotarm",
# "tool",
# "robotarm",
# "active",
# ),
# WebsocketMessage(
# "<span class=tool>robotarm</span> is no longer in use",
# "tool",
# "robotarm",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> started using lathe",
# "tool",
# "lathe",
# "active",
# ),
# WebsocketMessage(
# "<span class=tool>lathe</span> is no longer in use",
# "tool",
# "lathe",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> started using bandsaw",
# "tool",
# "bandsaw",
# "active",
# ),
# WebsocketMessage(
# "<span class=tool>bandsaw</span> is no longer in use",
# "tool",
# "bandsaw",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> started using sander",
# "tool",
# "sander",
# "active",
# ),
# WebsocketMessage(
# "<span class=tool>sander</span> is no longer in use",
# "tool",
# "sander",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> started using cncrouter",
# "tool",
# "cncrouter",
# "active",
# ),
# WebsocketMessage(
# "<span class=tool>cncrouter</span> is no longer in use",
# "tool",
# "cncrouter",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> started using sewingmachine",
# "tool",
# "sewingmachine",
# "active",
# ),
# WebsocketMessage(
# "<span class=tool>sewingmachine</span> is no longer in use",
# "tool",
# "sewingmachine",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> entered <span class=room>g1</span>",
# "room",
# "g1",
# "active",
# ),
# WebsocketMessage(
# None,
# "room",
# "g1",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> entered <span class=room>g8</span>",
# "room",
# "g8",
# "active",
# ),
# WebsocketMessage(
# None,
# "room",
# "g8",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> entered <span class=room>g11</span>",
# "room",
# "g11",
# "active",
# ),
# WebsocketMessage(
# None,
# "room",
# "g11",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> entered <span class=room>g14</span>",
# "room",
# "g14",
# "active",
# ),
# WebsocketMessage(
# None,
# "room",
# "g14",
# "inactive",
# ),
# WebsocketMessage(
# "<span class=username>aria</span> entered <span class=room>g2</span>",
# "room",
# "g2",
# "active",
# ),
# WebsocketMessage(
# None,
# "room",
# "g2",
# "inactive",
# ),
# ]
# async def mock_events():
# i = 0
# while True:
# await send_message(MESSAGES[i])
# print(MESSAGES[i])
# i = (i + 1) % len(MESSAGES)
# await asyncio.sleep(0.2)
async def main():
async with serve(new_websocket, WEBSOCKET_BIND[0], WEBSOCKET_BIND[1]):
await event_loop() # runs forever
# await mock_events() # runs forever
if __name__ == "__main__":
asyncio.run(main())