-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_server.py
117 lines (93 loc) · 4.47 KB
/
control_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
import time
import uasyncio
import machine
from configurator import get_configurator
from microdot_asyncio import Microdot
app = Microdot()
start_time = time.time()
form_style = """<style>
.form-group { display: flex; flex-direction: column; margin-bottom: 1rem; }
.form-group label { display: inline-block; margin-bottom: 0.5rem; font-weight: bold; }
.form-group span { display: inline-block; margin-bottom: 0.5rem; color: #6c757d; }
.form-group input,
.form-group select,
.form-group textarea {
display: block; width: 100%; padding: 0.375rem 0.75rem;
font-size: 1rem; line-height: 1.5; color: #212529; background-color: #fff;
background-clip: padding-box; border: 1px solid #ced4da; border-radius: 0.25rem;
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out; }
.form-group select { height: auto;}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus { border-color: #80bdff; box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); }
.form-group textarea { resize: vertical; min-height: 120px; }
</style>"""
table_style = """<style>
.table { border-collapse: collapse; width: 100%; margin-bottom: 1rem; color: #212529; }
.table th, .table td { padding: .75rem; text-align: left; border: 1px solid #dee2e6; }
.table th { font-weight: bold; background-color: #f5f5f5; }
.table tr:nth-child(even) { background-color: #f2f2f2; }
</style>"""
home_links = """
<tr><td><a href="/configure">Configure to setup WIFI & metrics creds</a></td><td><a href="/reset">Reset pico</a></td></tr>
"""
EXISTING_PASSWORD_STUB = '<existing>'
@app.route('/')
async def home(request):
uptime_status = f"<tr><td>Uptime:</td><td>{time.time() - start_time} seconds</td></tr>"
last_readings = f"<tr><td>Last readings:</td><td>{request.app.last_readings}</td></tr>"
wlan_info = f"<tr><td>WLAN IFCONFIG:</td><td>{request.app.wlan.ifconfig()}</td></tr>"
return ''.join([
"<!DOCTYPE html><html>",
"<body>",
table_style,
'<table class="table">',
uptime_status,
last_readings,
wlan_info,
home_links,
"</table></body><html>",
]), {'Content-Type': 'text/html'}
async def machine_reset(delay=0.0):
print(f"RESET requested, waiting for {delay}s first")
await uasyncio.sleep(delay)
print('RESETting')
machine.reset()
@app.route('/reset', methods=['GET', 'POST'])
async def reset(request):
if request.method == 'POST':
uasyncio.create_task(machine_reset(0.1))
return '<meta http-equiv="refresh" content="10">Resetting board... Will refresh in 10s', {'Content-Type': 'text/html'}
return '<form target="" method="post"><button>DO RESET</button></form>', {'Content-Type': 'text/html'}
@app.route('/configure', methods=['GET', 'POST'])
async def configure(request):
config = get_configurator()
if request.method == 'POST':
updates = {}
for param, _, current_or_default_value in config.get_all_parameters():
try:
new_value = request.form.get(param)
except KeyError:
print('No data for', param, 'skipping')
continue
# We have special value for passwords, to keep existing ones
if new_value == EXISTING_PASSWORD_STUB and param.endswith('_password'):
continue
# For the rest we coerce the type unless the current type is None
updates[param] = type(current_or_default_value)(new_value)
print("Writing updated config", updates)
config.set_params(updates, blinks=3)
uasyncio.create_task(machine_reset(0.2))
return '<meta http-equiv="refresh" content="10">Saved! Resetting board... Will refresh in 10s', {'Content-Type': 'text/html'}
form_data = '<form method="post">'
for param, description, value in config.get_all_parameters():
ivalue = EXISTING_PASSWORD_STUB if param.endswith("_password") else value
iplaceholder = f"{param} value"
form_data += f'<div class="form-group"><label for="{param}">{param}</label><span>{description}</span><input type="text" name="{param}" id="{param}" placeholder="{iplaceholder}" value="{ivalue}"></div>'
form_data += "<button>SUBMIT</button></form>"
body = table_style + form_style + ''.join([
'<table class="table"><tr><th>SCANNED WLANS</th></tr>',
''.join([f'<tr><td>{ap}</td></tr>' for ap in request.app.wlan.scan()]),
'</table>',
])
return body + form_data, {'Content-Type': 'text/html'}