Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
2a9c719baThis file is pinned to the selected release. Download the original to open it in your design tool.
"""HivePal app API (/api/v1/app/*): claiming/sharing devices, measurements,
firmware upload/approval and calibration triggers."""
from datetime import datetime
from typing import Any, Optional
from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, UploadFile
from auth import require_device_role, require_hivepal_service_key, require_user_id
from recordings import (
MAX_PCM_SLICE,
delete_recording,
get_recording,
list_recordings,
recording_pcm,
recording_wav,
request_recording,
)
from commands import check_relay_slot, create_command, queue_relay_firmware_update
from db import get_conn, hash_claim_code
from devices import (
apply_device_channels,
fetch_device_channels,
fetch_device_config,
get_device_owner_id,
run_temp_compensation_fit,
update_device_config,
)
from firmware import (
get_approved_firmware_version,
get_device_board,
latest_release_for_owner,
other_board_releases,
parse_version,
set_approved_firmware_version,
store_firmware_upload,
)
from measurements import (
MEASUREMENT_SELECT_COLUMNS,
execute_measurement_query,
serialize_measurements,
)
from schemas import (
AppCalibrationModeStartIn,
AppDeviceConfigUpdate,
ClaimDeviceIn,
DeviceChannelsUpdateIn,
DeviceCommandIn,
ShareDeviceIn,
TempCoefficientFitIn,
)
router = APIRouter()
@router.post("/api/v1/app/devices/claim", dependencies=[Depends(require_hivepal_service_key)])
def claim_device(payload: ClaimDeviceIn, user_id: str = Depends(require_user_id)):
claim_hash = hash_claim_code(payload.claim_code)
with get_conn() as conn:
with conn.cursor() as cur:
# Prefer an unclaimed device (NULLS FIRST) but still select a claimed
# one so an already-claimed code can be reported as such instead of
# collapsing into the generic "no device found" 404 below. Telling the
# two apart is what lets the app say "this device is already paired"
# rather than sending the beekeeper hunting for a typo in the code.
cur.execute(
"""
SELECT device_id, claimed_at FROM devices
WHERE claim_code_hash = %s
ORDER BY claimed_at NULLS FIRST
LIMIT 1;
""",
(claim_hash,),
)
r = cur.fetchone()
if not r:
raise HTTPException(status_code=404, detail="No unclaimed device found with that claim code")
device_id, already_claimed_at = r
if already_claimed_at is not None:
cur.execute(
"SELECT 1 FROM device_members WHERE device_id = %s AND user_id = %s;",
(device_id, user_id),
)
if cur.fetchone():
raise HTTPException(
status_code=409,
detail="You have already claimed this device; it should be in your device list.",
)
raise HTTPException(
status_code=409,
detail=(
"This claim code belongs to a device that is already claimed. "
"Its owner must release it (remove it in the app) before it can be claimed again."
),
)
cur.execute(
"UPDATE devices SET claimed_at = now(), display_name = %s WHERE device_id = %s;",
(payload.display_name, device_id),
)
cur.execute(
"""
INSERT INTO device_members (device_id, user_id, role)
VALUES (%s, %s, 'owner')
ON CONFLICT (device_id, user_id) DO UPDATE SET role = 'owner';
""",
(device_id, user_id),
)
for ch_num, ch_name in [
(1, payload.scale_1_display_name),
(2, payload.scale_2_display_name),
]:
if ch_name:
cur.execute(
"""
INSERT INTO device_channels (device_id, channel_number, name)
VALUES (%s, %s, %s)
ON CONFLICT (device_id, channel_number) DO UPDATE SET name = EXCLUDED.name;
""",
(device_id, ch_num, ch_name),
)
conn.commit()
return {"status": "claimed", "device_id": device_id}
@router.get("/api/v1/app/devices", dependencies=[Depends(require_hivepal_service_key)])
def list_devices(user_id: str = Depends(require_user_id)):
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"""
SELECT d.device_id, d.display_name, d.claimed_at, d.last_seen_at,
d.last_firmware_version, dm.role
FROM devices d
JOIN device_members dm ON dm.device_id = d.device_id
WHERE dm.user_id = %s
ORDER BY d.last_seen_at DESC NULLS LAST;
""",
(user_id,),
)
rows = cur.fetchall()
device_ids = [r[0] for r in rows]
channels: dict[str, dict] = {}
if device_ids:
cur.execute(
"SELECT device_id, channel_number, name FROM device_channels WHERE device_id = ANY(%s);",
(device_ids,),
)
for ch in cur.fetchall():
channels.setdefault(ch[0], {})[ch[1]] = ch[2]
return [
{
"device_id": r[0],
"display_name": r[1],
"claimed_at": r[2],
"last_seen_at": r[3],
"last_firmware_version": r[4],
"role": r[5],
"channels": {
"scale_1": channels.get(r[0], {}).get(1),
"scale_2": channels.get(r[0], {}).get(2),
# All custom hive names this device has (index "1".."18" -> name),
# so the dashboard can label hives beyond the first two.
"names": {
str(num): name
for num, name in channels.get(r[0], {}).items()
if name is not None
},
},
}
for r in rows
]
@router.delete("/api/v1/app/devices/{device_id}", dependencies=[Depends(require_hivepal_service_key)])
def remove_device_membership(device_id: str, user_id: str = Depends(require_user_id)):
"""Remove the caller's membership, releasing the device when nobody is left.
Dropping the last member also clears ``claimed_at``. Without that, a device
removed in the app kept its "claimed" flag with zero members: nobody could
see its readings and nobody could ever claim it again, because
``claim_device`` only matches rows with ``claimed_at IS NULL``. Releasing it
puts the device back in the pool so the same claim code re-pairs it.
The device row, its config, channel names and stored measurements all
survive, so re-claiming restores the history rather than starting over. Use
the local dashboard's device delete to erase the data itself.
"""
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT role FROM device_members WHERE device_id = %s AND user_id = %s;",
(device_id, user_id),
)
r = cur.fetchone()
if not r:
raise HTTPException(status_code=404, detail="Device membership not found")
cur.execute(
"DELETE FROM device_members WHERE device_id = %s AND user_id = %s;",
(device_id, user_id),
)
cur.execute(
"SELECT count(*) FROM device_members WHERE device_id = %s;",
(device_id,),
)
released = cur.fetchone()[0] == 0
if released:
cur.execute(
"UPDATE devices SET claimed_at = NULL WHERE device_id = %s;",
(device_id,),
)
conn.commit()
return {"status": "removed", "device_id": device_id, "released": released}
@router.delete("/api/v1/app/devices/{device_id}/claim", dependencies=[Depends(require_hivepal_service_key)])
def release_device_claim(device_id: str, user_id: str = Depends(require_user_id)):
"""Owner-only "forget this device": drop every member and unclaim it.
``remove_device_membership`` only removes the caller, so a shared device
stays claimed until every member happens to remove themselves. An owner who
wants the device back in the unclaimed pool — to re-pair it, hand it on, or
recover from a half-broken pairing — needs to be able to do that in one
step without chasing the people they shared it with.
Measurements, config and channel names are kept; only the pairing is undone.
"""
require_device_role(user_id, device_id, ["owner"])
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute("DELETE FROM device_members WHERE device_id = %s;", (device_id,))
members_removed = cur.rowcount
cur.execute(
"UPDATE devices SET claimed_at = NULL WHERE device_id = %s;",
(device_id,),
)
if cur.rowcount == 0:
raise HTTPException(status_code=404, detail="Device not found")
conn.commit()
return {
"status": "released",
"device_id": device_id,
"members_removed": members_removed,
}
@router.get("/api/v1/app/devices/{device_id}/channels", dependencies=[Depends(require_hivepal_service_key)])
def get_device_channels(device_id: str, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner", "admin", "viewer"])
return fetch_device_channels(device_id)
@router.patch("/api/v1/app/devices/{device_id}/channels", dependencies=[Depends(require_hivepal_service_key)])
def update_device_channels(device_id: str, payload: DeviceChannelsUpdateIn, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner", "admin"])
return apply_device_channels(device_id, payload)
@router.get("/api/v1/app/devices/{device_id}/members", dependencies=[Depends(require_hivepal_service_key)])
def list_device_members(device_id: str, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner", "admin", "viewer"])
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT user_id, role, created_at FROM device_members WHERE device_id = %s ORDER BY created_at;",
(device_id,),
)
rows = cur.fetchall()
return [{"user_id": r[0], "role": r[1], "joined_at": r[2]} for r in rows]
@router.post("/api/v1/app/devices/{device_id}/members", dependencies=[Depends(require_hivepal_service_key)])
def add_device_member(device_id: str, payload: ShareDeviceIn, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner"])
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"""
INSERT INTO device_members (device_id, user_id, role)
VALUES (%s, %s, %s)
ON CONFLICT (device_id, user_id) DO UPDATE SET role = EXCLUDED.role;
""",
(device_id, payload.user_id, payload.role),
)
conn.commit()
return {"status": "ok", "device_id": device_id, "user_id": payload.user_id, "role": payload.role}
@router.delete("/api/v1/app/devices/{device_id}/members/{member_user_id}", dependencies=[Depends(require_hivepal_service_key)])
def remove_device_member(device_id: str, member_user_id: str, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner"])
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT role FROM device_members WHERE device_id = %s AND user_id = %s;",
(device_id, member_user_id),
)
r = cur.fetchone()
if not r:
raise HTTPException(status_code=404, detail="Member not found")
if r[0] == "owner":
raise HTTPException(status_code=400, detail="Owner access cannot be revoked here")
cur.execute(
"DELETE FROM device_members WHERE device_id = %s AND user_id = %s;",
(device_id, member_user_id),
)
conn.commit()
return {"status": "revoked", "device_id": device_id, "user_id": member_user_id}
@router.get("/api/v1/app/devices/{device_id}/config", dependencies=[Depends(require_hivepal_service_key)])
def get_device_config_from_app(device_id: str, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner", "admin", "viewer"])
return fetch_device_config(device_id)
@router.get("/api/v1/app/devices/{device_id}/measurements", dependencies=[Depends(require_hivepal_service_key)])
def list_device_measurements(
device_id: str,
limit: int = 200,
start_at: Optional[datetime] = None,
end_at: Optional[datetime] = None,
max_points: Optional[int] = None,
user_id: str = Depends(require_user_id),
):
require_device_role(user_id, device_id, ["owner", "admin", "viewer"])
limit = min(max(limit, 1), 10000)
# Optional, opt-in down-sampling for chart consumers (e.g. HivePal wide
# ranges). Default None keeps the existing full-resolution behaviour, so this
# is non-breaking; the full column set is preserved either way.
if max_points is not None:
max_points = min(max(max_points, 0), 10000)
where_parts = ["device_id = %s"]
params: list[Any] = [device_id]
if start_at is not None:
where_parts.append("measured_at >= %s")
params.append(start_at)
if end_at is not None:
where_parts.append("measured_at <= %s")
params.append(end_at)
with get_conn() as conn:
with conn.cursor() as cur:
execute_measurement_query(
cur, MEASUREMENT_SELECT_COLUMNS, where_parts, params, limit, max_points
)
rows = cur.fetchall()
return serialize_measurements(rows)
@router.get("/api/v1/app/devices/{device_id}/measurements/latest", dependencies=[Depends(require_hivepal_service_key)])
def latest_device_measurements(device_id: str, limit: int = 50, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner", "admin", "viewer"])
limit = min(max(limit, 1), 500)
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
f"""
SELECT {MEASUREMENT_SELECT_COLUMNS}
FROM measurements
WHERE device_id = %s
ORDER BY measured_at DESC
LIMIT %s;
""",
(device_id, limit),
)
rows = cur.fetchall()
return serialize_measurements(rows)
@router.patch("/api/v1/app/devices/{device_id}/config", dependencies=[Depends(require_hivepal_service_key)])
def update_device_config_from_app(device_id: str, patch: AppDeviceConfigUpdate, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner", "admin"])
return update_device_config(device_id, patch)
@router.post(
"/api/v1/app/devices/{device_id}/temp-compensation/fit",
dependencies=[Depends(require_hivepal_service_key)],
)
def fit_temp_compensation_from_app(
device_id: str,
body: TempCoefficientFitIn,
user_id: str = Depends(require_user_id),
):
"""Derive a load-cell temperature coefficient from this device's history.
Regresses the chosen scale's *raw* weight against an EMA-smoothed temperature
channel over the requested window (see server/tempcomp.fit_temp_coefficient
and ema_temperatures) — the same smoothing read-time compensation applies, so
the coefficient is fitted in the regime it is used in — and returns the fit.
With ``apply=true`` the coefficient and temperature source are written to the
device config and compensation is enabled — applying ``apply`` requires
owner/admin, a plain fit needs only viewer access. The reference temperature
is left alone unless ``set_ref_temp=true``: it means "the temperature at which
this scale reads true", i.e. the one it was tared/spanned at, which a
regression cannot recover. The returned ``ref_temp_c`` is the window's mean
temperature, offered for information.
"""
role = ["owner", "admin"] if body.apply else ["owner", "admin", "viewer"]
require_device_role(user_id, device_id, role)
return run_temp_compensation_fit(device_id, body)
@router.post(
"/api/v1/app/devices/{device_id}/firmware",
dependencies=[Depends(require_hivepal_service_key)],
)
async def upload_firmware_from_app(
device_id: str,
file: UploadFile = File(...),
version: str = Form(...),
target: str = Form("hivescale"),
active: bool = Form(True),
board: str = Form(""),
user_id: str = Depends(require_user_id),
):
"""Upload a firmware binary from HivePal and register it as a release.
Unlike POST /api/v1/firmware/releases (which only registers a file that is
already present in FIRMWARE_DIR and is authenticated with the device
X-API-Key), this endpoint accepts the binary itself as multipart/form-data,
writes it into FIRMWARE_DIR, computes its CRC-32 and upserts the
firmware_releases row.
Authorization is per-device: the caller must be owner or admin on the given
device. The release is scoped to that device's OWNER (owner_user_id), so it is
only offered to scales owned by the same user — not the whole fleet. Pushing a
global / official build is still possible via the master-key
POST /api/v1/firmware/releases (which leaves owner_user_id NULL).
"""
require_device_role(user_id, device_id, ["owner", "admin"])
# Scope the release to the device's owner so only that owner's scales can pick
# it up. Fall back to the uploader (an admin acting on an owner-less device)
# so a release always has an owner and never silently becomes global.
owner_user_id = get_device_owner_id(device_id) or user_id
return await store_firmware_upload(
device_id, file, version, target, active, board, owner_user_id
)
# ── Hive audio (issue #71) ─────────────────────────────────────────────────
#
# Same three-surface shape as everything else here: the master key drives
# /api/v1/devices/*, the dashboard session drives /api/v1/local/*, and HivePal
# users reach the same functions through their per-device role.
#
# Requesting audio needs owner or admin — it switches a microphone on inside a
# hive. Listening to and deleting what is already there follows the usual roles.
@router.post(
"/api/v1/app/devices/{device_id}/recordings",
dependencies=[Depends(require_hivepal_service_key)],
)
def app_request_recording(device_id: str, hive: int = Query(1),
duration: float = Query(0.0), gain_db: int = Query(0),
user_id: str = Depends(require_user_id)):
"""Ask a hive for audio. ``duration=0`` is live; the node stops at 60 s."""
require_device_role(user_id, device_id, ["owner", "admin"])
return request_recording(device_id, hive, duration, gain_db,
requested_by=user_id)
@router.get(
"/api/v1/app/devices/{device_id}/recordings",
dependencies=[Depends(require_hivepal_service_key)],
)
def app_list_recordings(device_id: str, hive: Optional[int] = Query(None),
limit: int = Query(50),
user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner", "admin", "viewer"])
return {"recordings": list_recordings(device_id, hive, limit)}
@router.get(
"/api/v1/app/recordings/{recording_id}",
dependencies=[Depends(require_hivepal_service_key)],
)
def app_read_recording(recording_id: int, user_id: str = Depends(require_user_id)):
rec = get_recording(recording_id)
require_device_role(user_id, rec["device_id"], ["owner", "admin", "viewer"])
return rec
@router.get(
"/api/v1/app/recordings/{recording_id}/pcm",
dependencies=[Depends(require_hivepal_service_key)],
)
def app_recording_pcm(recording_id: int, offset: int = Query(0),
limit: int = Query(MAX_PCM_SLICE),
user_id: str = Depends(require_user_id)):
"""Raw PCM from ``offset`` — how a live session is followed while in flight."""
rec = get_recording(recording_id)
require_device_role(user_id, rec["device_id"], ["owner", "admin", "viewer"])
return recording_pcm(recording_id, offset, limit)
@router.get(
"/api/v1/app/recordings/{recording_id}/audio.wav",
dependencies=[Depends(require_hivepal_service_key)],
)
def app_recording_wav(recording_id: int, user_id: str = Depends(require_user_id)):
rec = get_recording(recording_id)
require_device_role(user_id, rec["device_id"], ["owner", "admin", "viewer"])
return recording_wav(recording_id)
@router.delete(
"/api/v1/app/recordings/{recording_id}",
dependencies=[Depends(require_hivepal_service_key)],
)
def app_delete_recording(recording_id: int, user_id: str = Depends(require_user_id)):
"""Delete a recording and its audio — the only thing that ever does."""
rec = get_recording(recording_id)
require_device_role(user_id, rec["device_id"], ["owner", "admin"])
return delete_recording(recording_id)
@router.get(
"/api/v1/app/devices/{device_id}/firmware/status",
dependencies=[Depends(require_hivepal_service_key)],
)
def firmware_status_from_app(device_id: str, user_id: str = Depends(require_user_id)):
"""Report the device's firmware-update status for the HivePal setup panel.
HivePal renders an "update available — apply" notice from this.
``current_version`` is the version the device last reported; ``latest_version``
is the newest active release resolved owner-first (the owner's own build, else
a global/official one). ``update_available`` means latest > current;
``pending_approval`` means an update is available but the owner has not approved
it yet, so the device will NOT auto-flash until they do via the approve
endpoint below. Any role may read.
"""
require_device_role(user_id, device_id, ["owner", "admin", "viewer"])
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT last_firmware_version FROM devices WHERE device_id = %s;",
(device_id,),
)
row = cur.fetchone()
if row is None:
raise HTTPException(status_code=404, detail="Device not found")
current_version = row[0]
owner_id = get_device_owner_id(device_id)
# Resolve the latest release for this device's reported board so a C6 device is
# never shown an esp32-only build as "available" (and vice versa). Falls back to
# board-agnostic when the device has not yet checked in with the board param.
device_board = get_device_board(device_id)
release = latest_release_for_owner("hivescale", owner_id, device_board)
latest_version = release[0] if release else None
latest_is_official = bool(release) and release[3] is None
approved_version = get_approved_firmware_version(device_id)
update_available = bool(
latest_version is not None
and current_version is not None
and parse_version(latest_version) > parse_version(current_version)
)
return {
"device_id": device_id,
"target": "hivescale",
"current_version": current_version,
"latest_version": latest_version,
"latest_is_official": latest_is_official,
"approved_version": approved_version,
"update_available": update_available,
"pending_approval": update_available and approved_version != latest_version,
# The board this device reports, plus any newer releases uploaded for a
# DIFFERENT board — so a wrong-board upload is explained rather than
# silently filtered out of "latest".
"device_board": device_board,
"other_board_releases": other_board_releases(
"hivescale", owner_id, device_board, current_version
),
}
@router.post(
"/api/v1/app/devices/{device_id}/firmware/approve",
dependencies=[Depends(require_hivepal_service_key)],
)
def approve_firmware_from_app(device_id: str, user_id: str = Depends(require_user_id)):
"""Approve the latest available firmware so this device may apply it.
The accept-to-apply step: it records the approved version for this device (so
check_firmware starts returning update=true for it) and queues an
``ota_update`` command to nudge the scale to update on its next check-in rather
than waiting for its scheduled OTA poll. Requires owner or admin.
"""
require_device_role(user_id, device_id, ["owner", "admin"])
owner_id = get_device_owner_id(device_id)
# Approve the latest release built for this device's board, so we never record
# an approval for a version that only exists for the other architecture.
release = latest_release_for_owner("hivescale", owner_id, get_device_board(device_id))
if not release:
raise HTTPException(
status_code=404, detail="No firmware release available for this device"
)
latest_version = release[0]
set_approved_firmware_version(device_id, latest_version)
# Nudge the device to check now rather than waiting for its scheduled poll.
command = create_command(
device_id, DeviceCommandIn(command_type="ota_update", payload={})
)
return {
"status": "approved",
"device_id": device_id,
"version": latest_version,
"command_id": command["id"],
}
@router.post("/api/v1/app/devices/{device_id}/calibration/start", dependencies=[Depends(require_hivepal_service_key)])
def start_calibration_mode_from_app(
device_id: str,
payload: Optional[AppCalibrationModeStartIn] = None,
user_id: str = Depends(require_user_id),
):
require_device_role(user_id, device_id, ["owner", "admin"])
payload = payload or AppCalibrationModeStartIn()
command_payload = {
"interval_seconds": payload.interval_seconds,
"timeout_seconds": payload.timeout_seconds,
}
result = create_command(
device_id,
DeviceCommandIn(
command_type="start_calibration_mode",
payload=command_payload,
),
)
return {
"status": result["status"],
"id": result["id"],
"command_type": "start_calibration_mode",
"payload": command_payload,
}
@router.post("/api/v1/app/devices/{device_id}/calibration/stop", dependencies=[Depends(require_hivepal_service_key)])
def stop_calibration_mode_from_app(device_id: str, user_id: str = Depends(require_user_id)):
require_device_role(user_id, device_id, ["owner", "admin"])
result = create_command(
device_id,
DeviceCommandIn(
command_type="stop_calibration_mode",
payload={},
),
)
return {
"status": result["status"],
"id": result["id"],
"command_type": "stop_calibration_mode",
"payload": {},
}
@router.post(
"/api/v1/app/devices/{device_id}/commands/update-hiveinside",
dependencies=[Depends(require_hivepal_service_key)],
)
def queue_hiveinside_update_from_app(
device_id: str,
slot: int = Query(1),
force: bool = Query(False),
user_id: str = Depends(require_user_id),
):
"""App-facing trigger for a HiveInside OTA relay.
Uploading a HiveInside binary (POST .../firmware) only *registers* the
release; it does not start the relay. HivePal calls this endpoint to actually
queue the ``update_hiveinside`` command for the HiveHub to pick up. The
caller must be owner or admin on the device.
``slot`` is the hive index — any hive the device reports, not just the first
two. The relay is refused with 409 when the release is not newer than the
version that node advertises; ``force=true`` relays it regardless.
"""
check_relay_slot(slot)
require_device_role(user_id, device_id, ["owner", "admin"])
result = queue_relay_firmware_update(
device_id, "hiveinside", "update_hiveinside", slot, force
)
return {
"status": result["status"],
"id": result["id"],
"command_type": "update_hiveinside",
"payload": {"slot": slot},
"version": result.get("version"),
"current_version": result.get("current_version"),
}
@router.post(
"/api/v1/app/devices/{device_id}/commands/update-beecounter",
dependencies=[Depends(require_hivepal_service_key)],
)
def queue_beecounter_update_from_app(
device_id: str,
slot: int = Query(1),
force: bool = Query(False),
user_id: str = Depends(require_user_id),
):
"""App-facing trigger for a HiveTraffic counter OTA relay.
Same contract as update-hiveinside above: uploading a binary only registers
the release, and this is what actually queues the relay. The counter stops
counting bees for the duration of the transfer, so the 409 version gate
matters more here than it does for HiveInside — a redundant relay costs real
traffic data. ``force=true`` relays anyway.
"""
check_relay_slot(slot)
require_device_role(user_id, device_id, ["owner", "admin"])
result = queue_relay_firmware_update(
device_id, "beecounter", "update_beecounter", slot, force
)
return {
"status": result["status"],
"id": result["id"],
"command_type": "update_beecounter",
"payload": {"slot": slot},
"version": result.get("version"),
"current_version": result.get("current_version"),
}