forked from adafruit/Adafruit_CircuitPython_HTTPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver_url_parameters.py
76 lines (53 loc) · 1.95 KB
/
httpserver_url_parameters.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
# SPDX-FileCopyrightText: 2023 Michał Pokusa
#
# SPDX-License-Identifier: Unlicense
import socketpool
import wifi
from adafruit_httpserver import Server, Request, Response
pool = socketpool.SocketPool(wifi.radio)
server = Server(pool, debug=True)
class Device:
def turn_on(self): # pylint: disable=no-self-use
print("Turning on device.")
def turn_off(self): # pylint: disable=no-self-use
print("Turning off device.")
def get_device(device_id: str) -> Device: # pylint: disable=unused-argument
"""
This is a **made up** function that returns a `Device` object.
"""
return Device()
@server.route("/device/<device_id>/action/<action>")
@server.route("/device/emergency-power-off/<device_id>")
def perform_action(
request: Request, device_id: str, action: str = "emergency_power_off"
):
"""
Performs an "action" on a specified device.
"""
device = get_device(device_id)
if action in ["turn_on"]:
device.turn_on()
elif action in ["turn_off", "emergency_power_off"]:
device.turn_off()
else:
return Response(request, f"Unknown action ({action})")
return Response(
request, f"Action ({action}) performed on device with ID: {device_id}"
)
@server.route("/device/<device_id>/status/<date>")
def device_status_on_date(request: Request, **params: dict):
"""
Return the status of a specified device between two dates.
"""
device_id = params.get("device_id")
date = params.get("date")
return Response(request, f"Status of {device_id} on {date}: ...")
@server.route("/device/.../status", append_slash=True)
@server.route("/device/....", append_slash=True)
def device_status(request: Request):
"""
Returns the status of all devices no matter what their ID is.
Unknown commands also return the status of all devices.
"""
return Response(request, "Status of all devices: ...")
server.serve_forever(str(wifi.radio.ipv4_address))