-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigHttpServer.py
More file actions
76 lines (57 loc) · 1.83 KB
/
Copy pathconfigHttpServer.py
File metadata and controls
76 lines (57 loc) · 1.83 KB
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
import time
import machine
import json
import simpleHttpServer
class ConfigHttpServer(simpleHttpServer.SimpleHttpServer):
def __init__(self, listener_socket):
super(ConfigHttpServer, self).__init__(self.route_client_request, listener_socket)
self.form_submission_controller = FormSubmissionController()
self.form_display_controller = FormDisplayController()
def route_client_request(self, req, resp):
if req.params.get('ssid'):
self.form_submission_controller.handle_form_submission(req, resp)
else:
self.form_display_controller.display_form(resp)
class FormDisplayController:
def display_form(self, resp):
resp.send(self.default_config_web_page())
@staticmethod
def default_config_web_page():
return """
<!DOCTYPE html>
<html>
<body>
<h2>Configure Network</h2>
<p>enter your network ssid and password</p>
<form action="">
ssid:<br>
<input type="text" name="ssid">
<br>
password:<br>
<input type="text" name="password">
<br><br>
<input type="submit">
</form>
</body>
</html>
"""
class FormSubmissionController:
def handle_form_submission(self, req, resp):
station_id = req.params.get('ssid')
password = req.params.get('password')
config = {"ssid": station_id, "password": password}
self.write_config(config)
resp.send(self.rebooting_web_page(station_id))
self.reboot_device()
@staticmethod
def reboot_device():
time.sleep(2)
machine.reset()
@staticmethod
def write_config(config):
f = open("config.json", 'w')
f.write(json.dumps(config))
f.close()
@staticmethod
def rebooting_web_page(station_id):
return "rebooting to connect to " + station_id