-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.py
executable file
·209 lines (152 loc) · 5.14 KB
/
webserver.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
import json
import logging
import queue
import time
import uuid
from flask import Flask, send_from_directory
from flask import Response, request
# from waitress import serve
cRed = "\033[31m"
cGreen = "\033[32m"
cYellow = "\033[33m"
cBlue = "\033[34m"
cMag = "\033[35m"
cCyan = "\033[36m"
cNorm = "\033[0m"
cyclerqueue = queue.Queue()
webqueue = {}
app = Flask(__name__)
def get_request():
rid = str(uuid.uuid4())
webqueue[rid] = queue.Queue()
return rid
def is_comms_busy():
"""
:return:
"""
if not cyclerqueue.empty() and len(cyclerqueue.queue) > 20:
logging.debug("Cycler queue too busy: Empty? '{}'".format(cyclerqueue.empty()))
logging.debug("Queue content: {}".format(list(cyclerqueue.queue)))
return True
def wait_and_get_response(rid):
"""
:return:
"""
while webqueue[rid].empty():
time.sleep(0.5)
response = webqueue[rid].get()
webqueue[rid].task_done()
del webqueue[rid]
logging.debug("SendingResponse: {}".format(response))
return Response(response['message'], status=response['code'], mimetype=response['mimetype'])
@app.route('/js/<path:path>')
def send_js(path):
"""
:param path:
:return:
"""
return send_from_directory('web/js', path)
@app.route('/img/<path:path>')
def send_img(path):
"""
:param path:
:return:
"""
return send_from_directory('web/img', path)
@app.route('/css/<path:path>')
def send_css(path):
"""
:param path:
:return:
"""
return send_from_directory('web/css', path)
@app.route('/', methods=['GET'])
@app.route('/<path:path>', methods=['GET'])
def root(path='index.html'):
"""
:return:
"""
return send_from_directory('web', path)
@app.route('/api/status/<int:slot_id>', methods=['POST'])
def api_status(slot_id):
"""
:return:
"""
payload = json.loads(request.stream.read().decode('utf-8'))
logging.info("{}Requested: {} {}{}".format(cMag, request.path, payload, cNorm))
if is_comms_busy():
return Response("Comms service too busy", status=429)
rid = get_request()
cyclerqueue.put({'slot_id': slot_id, 'action': 'status', 'payload': payload, 'request_id': rid})
return wait_and_get_response(rid)
@app.route('/api/stop/<int:slot_id>', methods=['POST'])
def api_stop(slot_id):
"""
:return:
"""
payload = json.loads(request.stream.read().decode('utf-8'))
logging.info("{}Requested: {} {}{}".format(cMag, request.path, payload, cNorm))
if is_comms_busy():
return Response("Comms service too busy", status=429)
rid = get_request()
cyclerqueue.put({'slot_id': slot_id, 'action': 'stop', 'payload': payload, 'request_id': rid})
return wait_and_get_response(rid)
@app.route('/api/charge/<int:slot_id>', methods=['POST'])
def api_charge(slot_id):
"""
:return:
"""
payload = json.loads(request.stream.read().decode('utf-8'))
logging.info("{}Requested: {}{}".format(cMag, request.path, cNorm))
if is_comms_busy():
return Response("Comms service too busy", status=429)
rid = get_request()
cyclerqueue.put(
{'slot_id': slot_id, 'action': 'charge', 'payload': format_request(payload), 'request_id': rid})
return wait_and_get_response(rid)
@app.route('/api/cycle/<int:slot_id>', methods=['POST'])
def api_cycle(slot_id):
"""
:return:
"""
payload = json.loads(request.stream.read().decode('utf-8'))
logging.info("{}Requested: {}{}".format(cMag, request.path, cNorm))
if is_comms_busy():
return Response("Comms service too busy", status=429)
rid = get_request()
cyclerqueue.put(
{'slot_id': slot_id, 'action': 'cycle', 'payload': format_request(payload), 'request_id': rid})
return wait_and_get_response(rid)
@app.route('/api/discharge/<int:slot_id>', methods=['POST'])
def api_discharge(slot_id):
"""
:return:
"""
payload = json.loads(request.stream.read().decode('utf-8'))
logging.info("{}Requested: {}{}".format(cMag, request.path, cNorm))
if is_comms_busy():
return Response("Comms service too busy", status=429)
rid = get_request()
cyclerqueue.put(
{'slot_id': slot_id, 'action': 'discharge', 'payload': format_request(payload), 'request_id': rid})
return wait_and_get_response(rid)
@app.route('/api/history/<int:slot_id>', methods=['GET'])
def history(slot_id):
"""
:param slot_id:
:return:
"""
logging.info("{}Requested: {}{}".format(cMag, request.path, cNorm))
if is_comms_busy():
return Response("Comms service too busy", status=429)
rid = get_request()
cyclerqueue.put({'slot_id': slot_id, 'action': 'history', 'payload': '{}', 'request_id': rid})
return wait_and_get_response(rid)
def format_request(payload: dict):
return {item['name']: item['value'] for item in payload}
def run_server():
"""
:return:
"""
logging.info("Webserver starting")
app.run(host="0.0.0.0", port=8080)