Adafruit_CircuitPython_HTTP.../examples/httpserver_url_parameters.py

51 lines
1.4 KiB
Python

# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
import socketpool
import wifi
from adafruit_httpserver import Server, Request, Response
pool = socketpool.SocketPool(wifi.radio)
server = Server(pool)
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:
with Response(request, content_type="text/plain") as response:
response.send(f"Unknown action ({action})")
return
with Response(request, content_type="text/plain") as response:
response.send(f"Action ({action}) performed on device with ID: {device_id}")