feat(api): parallel group sends and batch identify
- asyncio.gather for group brightness and driver-config Wi-Fi pushes - Batch identify envelope for group members Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -167,6 +167,107 @@ async def _identify_send_off_after_delay(sender, transport, wifi_ip, dev_id, nam
|
||||
pass
|
||||
|
||||
|
||||
async def send_identify_to_device(dev_id: str) -> tuple[int, str]:
|
||||
"""
|
||||
Send the same identify blink as ``POST /devices/<id>/identify``.
|
||||
|
||||
Returns ``(http_status, "")`` on success, or ``(status, error_message)`` on failure
|
||||
(status matches the single-device route).
|
||||
"""
|
||||
dev = devices.read(dev_id)
|
||||
if not dev:
|
||||
return 404, "Device not found"
|
||||
sender = get_current_sender()
|
||||
if not sender:
|
||||
return 503, "Transport not configured"
|
||||
name = str(dev.get("name") or "").strip()
|
||||
if not name:
|
||||
return 400, "Device must have a name to identify"
|
||||
|
||||
transport = dev.get("transport") or "espnow"
|
||||
wifi_ip = None
|
||||
if transport == "wifi":
|
||||
wifi_ip = dev.get("address")
|
||||
if not wifi_ip:
|
||||
return 400, "Device has no IP address"
|
||||
|
||||
try:
|
||||
msg = _compact_v1_json(
|
||||
presets={_IDENTIFY_PRESET_KEY: dict(_IDENTIFY_DRIVER_PRESET)},
|
||||
select={name: [_IDENTIFY_PRESET_KEY]},
|
||||
)
|
||||
if transport == "wifi":
|
||||
ok = await send_json_line_to_ip(wifi_ip, msg)
|
||||
if not ok:
|
||||
return 503, "Wi-Fi driver not connected"
|
||||
else:
|
||||
await sender.send(msg, addr=dev_id)
|
||||
|
||||
asyncio.create_task(
|
||||
_identify_send_off_after_delay(sender, transport, wifi_ip, dev_id, name)
|
||||
)
|
||||
except Exception as e:
|
||||
return 503, str(e)
|
||||
return 200, ""
|
||||
|
||||
|
||||
async def send_identify_to_group_devices(macs: list[str]) -> tuple[int, list[dict]]:
|
||||
"""
|
||||
Identify every listed registry MAC in one delivery round: merged ``select`` and a single
|
||||
ESP-NOW split envelope when multiple peers share the serial bridge (avoids per-device
|
||||
``SerialSender`` lock serialisation). Wi-Fi peers are sent in parallel as in
|
||||
``deliver_json_messages``.
|
||||
"""
|
||||
from util.driver_delivery import deliver_json_messages
|
||||
|
||||
errors: list[dict] = []
|
||||
sender = get_current_sender()
|
||||
if not sender:
|
||||
return 0, [{"mac": "*", "error": "Transport not configured"}]
|
||||
|
||||
merged_select: dict[str, list[str]] = {}
|
||||
valid_macs: list[str] = []
|
||||
for dev_id in macs:
|
||||
dev = devices.read(dev_id)
|
||||
if not dev:
|
||||
errors.append({"mac": dev_id, "error": "Device not found"})
|
||||
continue
|
||||
name = str(dev.get("name") or "").strip()
|
||||
if not name:
|
||||
errors.append({"mac": dev_id, "error": "Device must have a name to identify"})
|
||||
continue
|
||||
transport = (dev.get("transport") or "espnow").strip().lower()
|
||||
if transport == "wifi":
|
||||
if not dev.get("address"):
|
||||
errors.append({"mac": dev_id, "error": "Device has no IP address"})
|
||||
continue
|
||||
merged_select[name] = [_IDENTIFY_PRESET_KEY]
|
||||
valid_macs.append(dev_id)
|
||||
|
||||
if not merged_select:
|
||||
return 0, errors
|
||||
|
||||
try:
|
||||
msg = _compact_v1_json(
|
||||
presets={_IDENTIFY_PRESET_KEY: dict(_IDENTIFY_DRIVER_PRESET)},
|
||||
select=merged_select,
|
||||
)
|
||||
await deliver_json_messages(sender, [msg], valid_macs, devices, delay_s=0)
|
||||
except Exception as e:
|
||||
return 0, errors + [{"mac": "*", "error": str(e)}]
|
||||
|
||||
for dev_id in valid_macs:
|
||||
dev = devices.read(dev_id) or {}
|
||||
name = str(dev.get("name") or "").strip()
|
||||
transport = (dev.get("transport") or "espnow").strip().lower()
|
||||
wifi_ip = dev.get("address") if transport == "wifi" else None
|
||||
asyncio.create_task(
|
||||
_identify_send_off_after_delay(sender, transport, wifi_ip, dev_id, name)
|
||||
)
|
||||
|
||||
return len(valid_macs), errors
|
||||
|
||||
|
||||
@controller.get("")
|
||||
async def list_devices(request):
|
||||
"""List all devices (includes ``connected`` for live Wi-Fi WebSocket presence)."""
|
||||
@@ -341,53 +442,12 @@ async def identify_device(request, id):
|
||||
this device name — same combined shape as profile sends the driver already accepts over TCP
|
||||
/ ESP-NOW. No ``save``. After ``IDENTIFY_OFF_DELAY_S``, a background task selects ``off``.
|
||||
"""
|
||||
dev = devices.read(id)
|
||||
if not dev:
|
||||
return json.dumps({"error": "Device not found"}), 404, {
|
||||
status, err = await send_identify_to_device(id)
|
||||
if status == 200:
|
||||
return json.dumps({"message": "Identify sent"}), 200, {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
sender = get_current_sender()
|
||||
if not sender:
|
||||
return json.dumps({"error": "Transport not configured"}), 503, {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
name = str(dev.get("name") or "").strip()
|
||||
if not name:
|
||||
return json.dumps({"error": "Device must have a name to identify"}), 400, {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
transport = dev.get("transport") or "espnow"
|
||||
wifi_ip = None
|
||||
if transport == "wifi":
|
||||
wifi_ip = dev.get("address")
|
||||
if not wifi_ip:
|
||||
return json.dumps({"error": "Device has no IP address"}), 400, {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
try:
|
||||
msg = _compact_v1_json(
|
||||
presets={_IDENTIFY_PRESET_KEY: dict(_IDENTIFY_DRIVER_PRESET)},
|
||||
select={name: [_IDENTIFY_PRESET_KEY]},
|
||||
)
|
||||
if transport == "wifi":
|
||||
ok = await send_json_line_to_ip(wifi_ip, msg)
|
||||
if not ok:
|
||||
return json.dumps({"error": "Wi-Fi driver not connected"}), 503, {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
else:
|
||||
await sender.send(msg, addr=id)
|
||||
|
||||
asyncio.create_task(
|
||||
_identify_send_off_after_delay(sender, transport, wifi_ip, id, name)
|
||||
)
|
||||
except Exception as e:
|
||||
return json.dumps({"error": str(e)}), 503, {"Content-Type": "application/json"}
|
||||
return json.dumps({"message": "Identify sent"}), 200, {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
return json.dumps({"error": err}), status, {"Content-Type": "application/json"}
|
||||
|
||||
|
||||
@controller.post("/<id>/brightness")
|
||||
|
||||
Reference in New Issue
Block a user