-
Notifications
You must be signed in to change notification settings - Fork 0
/
uiserver.py
executable file
·81 lines (67 loc) · 2.59 KB
/
uiserver.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
#!/usr/bin/env python3
# (C) 2020 Harrell Corp
# This server both spawns a new thread to host HTTP(S) connections to both display the sensor reading as well as provide the human operator
# a button to click to send a "whack" message to the whactuator via the message router
import time
import yaml
import json
import threading
import http.server
import socketserver
import uihandler
import queue
import wammessage
import ssl
import sys
CONF_FILE = "UIConf.yaml"
with open(CONF_FILE) as yf:
conf_data = yaml.load(yf)
req_handler = uihandler.UIRequestHandler
uihandler.refresh_interval = conf_data['refresh']
recv_queue = queue.Queue()
fro = 'UI'
msg_integrity = False
msg_integrityfunc = None
mc = wammessage.MessageClient(host = conf_data['router_host'], port = conf_data['router_port'])
ms = wammessage.MessageServer(recv_queue, host = conf_data['host'], port = conf_data['message_listen_port'])
if (conf_data['integrity'] == 'crc32'):
ms.integrity = True
ms.integrityfunc = wammessage.crc32_if
msg_integrity = True
msg_integrityfunc = wammessage.crc32_if
if (conf_data['integrity'] == 'md5'):
ms.integrity = True
ms.integrityfunc = wammessage.md5_if
msg_integrity = True
msg_integrityfunc = wammessage.md5_if
if (conf_data['integrity'] == 'sha1'):
ms.integrity = True
ms.integrityfunc = wammessage.sha1_if
msg_integrity = True
msg_integrityfunc = wammessage.sha1_if
# Start a HTTP server on a configuration-defined port
httpd = socketserver.TCPServer((conf_data['host'], conf_data['http_listen_port']), req_handler)
if (conf_data['ssl']):
print("Starting UI server located at: https://", conf_data['host'], ":", conf_data['http_listen_port'], sep = '')
httpd.socket = ssl.wrap_socket(httpd.socket, certfile = conf_data['cert'], server_side=True)
else:
print("Starting UI server located at: http://", conf_data['host'], ":", conf_data['http_listen_port'], sep = '')
uithread = threading.Thread(target=httpd.serve_forever)
# Start the HTTP server, and the message server
uithread.start()
ms.start()
# Loop to check queues and process
while True:
if not recv_queue.empty():
m = recv_queue.get()
#print("Incoming message!")
uihandler.sensor_reading = m.message
if m.message == 'SHUTDOWN':
print("Received shutdown message from router")
break
if not uihandler.click_queue.empty():
c = uihandler.click_queue.get()
m = wammessage.WAMMessage(integrity = msg_integrity, integrityfunc = msg_integrityfunc)
m.construct_message(fro, 'WHACTUATOR', 'WHACK!')
mc.send(m)
httpd.shutdown()