Added songs endpoint

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 13:30:09 +09:00
co-authored by Claude Opus 5.5
parent 11657dbb0b
commit 610fd41c58
+55
View File
@@ -10,6 +10,7 @@ While the assistant (lumi.py) is running it owns the robot: motion and speech en
import json import json
import math import math
import os import os
import random
import re import re
import signal import signal
import subprocess import subprocess
@@ -123,6 +124,8 @@ def running_now():
return "skill" return "skill"
if _route.thread and _route.thread.is_alive(): if _route.thread and _route.thread.is_alive():
return "route" return "route"
if _sing["thread"] and _sing["thread"].is_alive():
return "song"
if _waving.is_set(): if _waving.is_set():
return "wave" return "wave"
if wave_greet.busy(): if wave_greet.busy():
@@ -284,6 +287,57 @@ def agv_plan():
return {"points": pts[::step] + (pts[-1:] if pts and (len(pts) - 1) % step else [])} return {"points": pts[::step] + (pts[-1:] if pts and (len(pts) - 1) % step else [])}
# ---------- songs (the Lumi app's Commands, 2026-11-18) ----------
class SingBody(BaseModel):
id: str | None = Field(None, description="a song id from GET /songs; empty = any of them")
_sing = {"thread": None, "stop": threading.Event(), "id": None}
@app.get("/songs", tags=["voice"], summary="The songs Lumi sings: id and title (English, Korean)")
def songs_list():
from audio import sing
return {"songs": [{"id": k, "title": s.get("title") or k, "title_ko": s.get("title_ko") or s.get("title") or k}
for k, s in sing.songs().items()],
"singing": _sing["id"] if _sing["thread"] and _sing["thread"].is_alive() else None}
@app.post("/sing", tags=["voice"], summary="Sing a song with head moves; returns at once (STOP or /sing/stop ends it)")
def sing_start(body: SingBody):
from audio import sing
all_songs = sing.songs()
if body.id is not None and body.id not in all_songs:
raise HTTPException(404, f"no song {body.id}; songs: {list(all_songs)}")
if assistant_pids(): # it owns the speaker and the head: it sings, as if it heard it
title = all_songs[body.id]["title"] if body.id else None
send_to_assistant("heard", text=f"sing {title}" if title else "sing a song")
return {"ok": True, "by": "assistant"}
robot_free(manual=True) # not while a task, a wave or a drive runs
if _sing["thread"] and _sing["thread"].is_alive():
raise HTTPException(409, "Already singing.")
song = body.id or random.choice(list(all_songs))
stop = _sing["stop"] = threading.Event()
def run():
try:
with _voice_lock:
sing.sing(song, abort=stop.is_set)
finally:
_sing["id"] = None
_sing["id"] = song
_sing["thread"] = threading.Thread(target=run, daemon=True)
_sing["thread"].start()
return {"ok": True, "by": "api", "id": song}
@app.post("/sing/stop", tags=["voice"], summary="Stop the song (music, voice and head)")
def sing_stop():
_sing["stop"].set()
return {"ok": True}
# ---------- status ---------- # ---------- status ----------
@app.get("/status", tags=["status"], summary="Battery, position, base / head / arm state, assistant") @app.get("/status", tags=["status"], summary="Battery, position, base / head / arm state, assistant")
def status(): def status():
@@ -713,6 +767,7 @@ def stop_all():
_waving_off() # first: the supervisor must not bring lumi.py back to wave _waving_off() # first: the supervisor must not bring lumi.py back to wave
jogger.stop() jogger.stop()
_route.stop() _route.stop()
_sing["stop"].set() # a song from /sing
task = part(_fetch_stop) # first, so the runner starts nothing new task = part(_fetch_stop) # first, so the runner starts nothing new
arm_parts = [_stop_pool.submit(part, skills.cancel)] arm_parts = [_stop_pool.submit(part, skills.cancel)]
stopped = assistant_stop(arm_too=False) stopped = assistant_stop(arm_too=False)