-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwebgui.py
executable file
·99 lines (73 loc) · 2.91 KB
/
webgui.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
#!/usr/bin/env python3
import asyncio
import json
import logging
import websockets
from aiohttp import web
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("webgui")
websocket_clients = set()
loop = asyncio.get_event_loop()
system_config = None
system_health_report = None
file_paths = {}
async def handle_websocket_connection(websocket, path):
global system_config, system_health_report, new_filepath
log = logging.getLogger("websocket")
log.info("new websocket-client, repeating system_config, system_health_report and file_paths")
await websocket.send(system_config)
await websocket.send(system_health_report)
for file_path in file_paths.values():
await websocket.send(file_path)
websocket_clients.add(websocket)
log.info("now %u websocket-clients" % len(websocket_clients))
try:
while True:
message = await websocket.recv()
log.debug("received from websocket: %s" % message)
except:
websocket_clients.remove(websocket)
log.info("now %u websocket-clients" % len(websocket_clients))
async def read_from_tcp(host, port):
global system_config, system_health_report, new_filepath
log = logging.getLogger("tcp-server")
reader, writer = await asyncio.open_connection(host, port)
log.info("connected")
while not reader.at_eof():
bytes = await reader.readline()
if len(bytes) == 0:
break
line = bytes.decode('utf-8').rstrip()
message = json.loads(line)
if message['type'] == 'system_config':
log.info("received system_config: %s" % line)
system_config = line
if message['type'] == 'system_health_report':
log.info("received system_health_report: %s" % line)
system_health_report = line
if message['type'] == 'new_filepath':
log.debug("received new_filepath: %s" % line)
channel_index = message['channel_index']
file_paths[channel_index] = line
log.debug('Received: %s' % line)
if websocket_clients:
await asyncio.wait([client.send(line) for client in websocket_clients])
log.info("disconnected, stopping mainloop")
writer.close()
loop.stop()
def handle_index(request):
with open('ui/index.html', 'r') as f:
return web.Response(text=f.read(), content_type="text/html", charset="utf-8")
app = web.Application()
app.router.add_get("/", handle_index)
app.router.add_static("/ui", "ui", show_index=True)
webserver = loop.create_server(app.make_handler(), '0.0.0.0', 8080)
log.info("Starting Webserver on port 8080")
loop.run_until_complete(webserver)
log.info("Starting Websocket-Server on port 9998")
loop.run_until_complete(websockets.serve(
handle_websocket_connection, '0.0.0.0', 9998))
log.info("Starting TCP-Client on port 9999")
loop.run_until_complete(read_from_tcp('127.0.0.1', 9999))
loop.run_forever()
log.info("Bye")