chore: add pattern samples, http driver helpers, OTA/UDP test tools
- patterns/: sample dynamic pattern modules for OTA - esp32/msg.json: example bridge message shape - models/http_driver.py, wifi_peer.py: Wi-Fi driver HTTP poll helpers - tests: pattern OTA send script and UDP discovery echo server - Submodule led-driver: http_poll and test utilities Made-with: Cursor
This commit is contained in:
99
tests/test_pattern_ota_send.py
Normal file
99
tests/test_pattern_ota_send.py
Normal file
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Manual test helper for pattern OTA send flow.
|
||||
|
||||
Examples:
|
||||
python tests/test_pattern_ota_send.py --base-url http://led.local --pattern blink
|
||||
python tests/test_pattern_ota_send.py --base-url http://127.0.0.1:8080 --pattern blink --device-id 102030405060
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from urllib import request, error
|
||||
|
||||
|
||||
def _http_json(method, url, payload=None):
|
||||
data = None
|
||||
headers = {"Accept": "application/json"}
|
||||
if payload is not None:
|
||||
data = json.dumps(payload).encode("utf-8")
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = request.Request(url, data=data, method=method, headers=headers)
|
||||
try:
|
||||
with request.urlopen(req, timeout=15) as resp:
|
||||
body = resp.read().decode("utf-8")
|
||||
return resp.status, json.loads(body) if body else {}
|
||||
except error.HTTPError as e:
|
||||
body = e.read().decode("utf-8")
|
||||
try:
|
||||
parsed = json.loads(body) if body else {}
|
||||
except Exception:
|
||||
parsed = {"raw": body}
|
||||
return e.code, parsed
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Test /patterns/<name>/send OTA flow.")
|
||||
parser.add_argument(
|
||||
"--base-url",
|
||||
default="http://127.0.0.1",
|
||||
help="Controller base URL (default: http://127.0.0.1)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--pattern",
|
||||
required=True,
|
||||
help="Pattern name (without .py), e.g. blink",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--device-id",
|
||||
default="",
|
||||
help="Optional device id (MAC). If omitted, sends to all Wi-Fi devices.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
base = args.base_url.rstrip("/")
|
||||
pattern = args.pattern.strip()
|
||||
if not pattern:
|
||||
print("Pattern name is required.")
|
||||
return 2
|
||||
|
||||
# Quick visibility before send.
|
||||
status, patterns = _http_json("GET", f"{base}/patterns")
|
||||
print(f"GET /patterns -> {status}")
|
||||
if status != 200:
|
||||
print(patterns)
|
||||
return 1
|
||||
if pattern not in patterns:
|
||||
print(f"Pattern {pattern!r} not found in /patterns list.")
|
||||
return 1
|
||||
|
||||
status, devices = _http_json("GET", f"{base}/devices")
|
||||
print(f"GET /devices -> {status}")
|
||||
if status != 200:
|
||||
print(devices)
|
||||
return 1
|
||||
wifi_ids = [
|
||||
did
|
||||
for did, d in (devices or {}).items()
|
||||
if isinstance(d, dict) and str(d.get("transport", "")).lower() == "wifi"
|
||||
]
|
||||
print(f"Wi-Fi devices in registry: {len(wifi_ids)}")
|
||||
if wifi_ids:
|
||||
print(" - " + "\n - ".join(wifi_ids))
|
||||
|
||||
payload = {"device_id": args.device_id} if args.device_id else {}
|
||||
status, result = _http_json(
|
||||
"POST", f"{base}/patterns/{pattern}/send", payload=payload
|
||||
)
|
||||
print(f"POST /patterns/{pattern}/send -> {status}")
|
||||
print(json.dumps(result, indent=2))
|
||||
|
||||
if status != 200:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
|
||||
89
tests/udp_server.py
Normal file
89
tests/udp_server.py
Normal file
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
"""UDP echo server for testing the led-driver UDP client (MicroPython ESP32).
|
||||
|
||||
Listens on UDP, prints each datagram (peer + payload), sends the same bytes back.
|
||||
|
||||
Run on the Pi (or any host on the LAN):
|
||||
|
||||
python3 tests/udp_server.py
|
||||
python3 tests/udp_server.py -p 8766 --bind 0.0.0.0
|
||||
|
||||
Pair with **`led-driver/tests/udp_client.py`**: the device broadcasts a hello; this server
|
||||
echoes so the client learns the controller's **unicast IP** from the reply (firmware uses that
|
||||
for HTTP to the web server only; it is not stored in settings). Some Wi‑Fi APs block broadcast between clients —
|
||||
prefer a wired listener.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import socket
|
||||
import sys
|
||||
|
||||
|
||||
DEFAULT_PORT = 8766
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="UDP echo server for led-driver tests")
|
||||
parser.add_argument(
|
||||
"--bind",
|
||||
default="0.0.0.0",
|
||||
metavar="ADDR",
|
||||
help="Address to bind (default: all interfaces)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--port",
|
||||
type=int,
|
||||
default=DEFAULT_PORT,
|
||||
help=f"UDP port (default: {DEFAULT_PORT})",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
except (AttributeError, OSError):
|
||||
pass
|
||||
try:
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
except (AttributeError, OSError):
|
||||
pass
|
||||
try:
|
||||
sock.bind((args.bind, args.port))
|
||||
except OSError as e:
|
||||
print(f"bind {args.bind!r}:{args.port} failed: {e}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
print(f"UDP echo listening on {args.bind}:{args.port} (Ctrl+C to stop)")
|
||||
while True:
|
||||
try:
|
||||
data, addr = sock.recvfrom(2048)
|
||||
except KeyboardInterrupt:
|
||||
print("\nStopping.")
|
||||
return 0
|
||||
client_ip, client_port = addr[0], addr[1]
|
||||
text = data.decode("utf-8", errors="replace")
|
||||
print(f"client_ip={client_ip} client_udp_port={client_port} ({len(data)} bytes)")
|
||||
print(f" payload: {text!r}")
|
||||
line = data.split(b"\n", 1)[0].strip()
|
||||
if line:
|
||||
try:
|
||||
obj = json.loads(line.decode("utf-8"))
|
||||
if isinstance(obj, dict) and obj.get("type") == "led":
|
||||
print(
|
||||
" hello: device_name=%r mac=%r v=%r"
|
||||
% (obj.get("device_name"), obj.get("mac"), obj.get("v"))
|
||||
)
|
||||
except (UnicodeError, ValueError, TypeError):
|
||||
pass
|
||||
try:
|
||||
sock.sendto(data, addr)
|
||||
except OSError as e:
|
||||
print(f" sendto failed: {e}", file=sys.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user