⚠️ DEPRECATED: This documentation has been consolidated into the yoto-smart-stream skill. Please refer to the skill for current information.New location:
.github/skills/yoto-smart-stream/reference/architecture.md
This document provides architectural recommendations for implementing Yoto Smart Stream, a service to stream audio to Yoto devices with MQTT event monitoring, web UI for configuration, and support for interactive "Choose Your Own Adventure" style experiences.
Rationale for Python:
- Excellent
yoto_apilibrary already exists (mature, well-maintained) - Strong async support for concurrent MQTT + HTTP operations
- Rich ecosystem for audio processing (pydub, mutagen)
- FastAPI provides modern, high-performance async web framework
Core Stack:
- Python 3.9+
- FastAPI (async web framework)
- Uvicorn (ASGI server)
- SQLAlchemy 2.0 (database ORM)
- Paho MQTT (MQTT client)
- yoto_api (Yoto API wrapper)
Recommended Options:
-
Simple/MVP: Vanilla JS + Tailwind CSS
- Quick to develop
- No build process needed
- Sufficient for CRUD operations
-
Full-Featured: React/Vue + Vite
- Better for complex interactive features
- Component reusability
- Rich ecosystem
-
Hybrid: HTMX + Alpine.js
- Minimal JavaScript
- Server-side rendering
- Progressive enhancement
Recommendation: Start with HTMX + Alpine.js for rapid development, migrate to React if complexity increases.
SQLite for Development/Single User:
- Zero configuration
- File-based, easy backup
- Sufficient for card scripts and metadata
PostgreSQL for Production/Multi-User:
- Better concurrency
- JSON field support
- Scalability
┌─────────────────┐
│ Web Browser │
│ (React/HTMX) │
└────────┬────────┘
│ HTTP/WebSocket
│
┌────────▼────────────────────────────────────┐
│ FastAPI Application │
│ ┌──────────────────────────────────────┐ │
│ │ API Routes │ │
│ │ /api/cards /api/players │ │
│ │ /api/audio /api/scripts │ │
│ └──────────────────────────────────────┘ │
│ ┌──────────────────────────────────────┐ │
│ │ Business Logic Layer │ │
│ │ - Audio Manager │ │
│ │ - Card Script Engine │ │
│ │ - Event Processor │ │
│ └──────────────────────────────────────┘ │
│ ┌──────────────────────────────────────┐ │
│ │ Yoto Integration Layer │ │
│ │ - YotoManager (yoto_api) │ │
│ │ - MQTT Event Handler │ │
│ └──────────────────────────────────────┘ │
└────────┬────────────────────┬──────────────┘
│ │
│ │ MQTT (TLS)
│ │
┌────▼─────┐ ┌───▼─────────┐
│ Database │ │ Yoto Cloud │
│ (SQLite) │ │ & MQTT │
└──────────┘ └─────┬───────┘
│
┌─────▼──────┐
│ Yoto │
│ Players │
└────────────┘
Purpose: Abstraction layer over yoto_api with additional features
# yoto_smart_stream/core/yoto_client.py
class YotoClient:
"""Enhanced Yoto API client with caching and error handling"""
def __init__(self, config: Settings):
self.manager = YotoManager(client_id=config.yoto_client_id)
self._player_cache = {}
self._library_cache = {}
self._cache_ttl = 300 # 5 minutes
async def authenticate(self, refresh_token: Optional[str] = None):
"""Authenticate with Yoto API"""
async def get_players(self, force_refresh: bool = False):
"""Get all players with caching"""
async def control_player(self, player_id: str, action: PlayerAction):
"""Control player (play, pause, skip, volume)"""Purpose: Process real-time events from Yoto devices
# yoto_smart_stream/core/mqtt_handler.py
class MQTTEventHandler:
"""Handle MQTT events from Yoto players"""
def __init__(self, yoto_client: YotoClient):
self.client = yoto_client
self.event_callbacks = []
async def connect(self):
"""Connect to MQTT broker"""
async def on_event(self, topic: str, payload: dict):
"""Process incoming MQTT events"""
# Parse event type
# Update local state
# Trigger callbacks (webhooks, scripts)
def register_callback(self, event_type: str, callback: Callable):
"""Register callback for specific event types"""Purpose: Handle audio file storage, conversion, and streaming
# yoto_smart_stream/core/audio_manager.py
class AudioManager:
"""Manage audio files and streaming"""
async def upload_audio(self, file: UploadFile) -> str:
"""Upload and process audio file"""
# Validate format
# Convert if needed (to MP3)
# Extract metadata
# Store file
# Return file ID
async def get_audio_url(self, file_id: str) -> str:
"""Get streamable URL for audio file"""
async def convert_audio(self, input_path: Path, output_format: str):
"""Convert audio to compatible format"""Purpose: Execute interactive card scripts (Choose Your Own Adventure)
# yoto_smart_stream/core/script_engine.py
class CardScript:
"""Interactive card script definition"""
card_id: str
chapters: Dict[int, Chapter]
variables: Dict[str, Any]
class ScriptEngine:
"""Execute card scripts based on events"""
async def load_script(self, card_id: str) -> CardScript:
"""Load script from database"""
async def handle_button_press(
self,
card_id: str,
player_id: str,
button: str
):
"""Process button press in script context"""
# Get current chapter
# Evaluate condition
# Determine next chapter
# Update player
async def evaluate_condition(
self,
script: CardScript,
condition: str
) -> bool:
"""Evaluate script condition"""-- Cards and audio content
CREATE TABLE cards (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
cover_image_url TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE tracks (
id TEXT PRIMARY KEY,
card_id TEXT REFERENCES cards(id),
track_number INTEGER,
title TEXT,
audio_file_id TEXT NOT NULL,
duration_seconds INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Interactive scripts
CREATE TABLE card_scripts (
id TEXT PRIMARY KEY,
card_id TEXT REFERENCES cards(id),
script_type TEXT NOT NULL, -- 'linear', 'interactive', 'cyoa'
script_data JSON NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Script execution state (for multi-session CYOA)
CREATE TABLE player_script_state (
id TEXT PRIMARY KEY,
player_id TEXT NOT NULL,
card_id TEXT REFERENCES cards(id),
current_chapter INTEGER DEFAULT 1,
variables JSON,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Audio files
CREATE TABLE audio_files (
id TEXT PRIMARY KEY,
filename TEXT NOT NULL,
file_path TEXT NOT NULL,
format TEXT NOT NULL,
size_bytes INTEGER,
duration_seconds INTEGER,
metadata JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Event log (for debugging and analytics)
CREATE TABLE events (
id TEXT PRIMARY KEY,
player_id TEXT,
event_type TEXT NOT NULL,
event_data JSON,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);GET /api/players - List all players
GET /api/players/{id} - Get player details
POST /api/players/{id}/control - Control player
Body: { "action": "play|pause|skip", "parameters": {...} }
GET /api/cards - List all cards
GET /api/cards/{id} - Get card details
POST /api/cards - Create new card
PUT /api/cards/{id} - Update card
DELETE /api/cards/{id} - Delete card
POST /api/cards/{id}/upload-audio - Upload audio track
GET /api/audio/{file_id} - Stream audio file
Headers: Range support for seeking
GET /api/scripts/{card_id} - Get card script
PUT /api/scripts/{card_id} - Update card script
POST /api/scripts/{card_id}/test - Test script execution
WS /ws/events - Real-time player events
{
"version": "1.0",
"card_id": "adventure-001",
"title": "Forest Adventure",
"chapters": {
"1": {
"audio_file_id": "intro.mp3",
"description": "You enter the forest...",
"choices": {
"left": {
"label": "Go left",
"next_chapter": 2
},
"right": {
"label": "Go right",
"next_chapter": 3
}
}
},
"2": {
"audio_file_id": "left-path.mp3",
"description": "You find a cave...",
"choices": {
"left": {
"label": "Enter cave",
"next_chapter": 4
},
"right": {
"label": "Keep walking",
"next_chapter": 5
}
}
}
}
}Yoto players have physical buttons:
- Left/Right arrows: Chapter navigation / choices
- Pause button: Pause/resume
- Shake: Special action
1. User plays interactive card
2. MQTT event: card inserted
3. Server loads script, chapter 1
4. Audio plays on device
5. User presses left button
6. MQTT event: button.press {"button": "left"}
7. Server evaluates script, moves to chapter 2
8. Server sends command to play next chapter
9. Cycle continues
Pros:
- Low latency
- No external dependencies
- Works without internet
Implementation:
@app.get("/audio/{file_id}")
async def stream_audio(file_id: str):
file_path = audio_manager.get_file_path(file_id)
return FileResponse(
file_path,
media_type="audio/mpeg",
headers={
"Accept-Ranges": "bytes",
"Content-Disposition": f"inline; filename={file_id}.mp3"
}
)Pros:
- Works from anywhere
- Scalable
- Professional
Implementation:
- Upload to S3/Cloudflare R2
- Generate signed URLs
- Return URL to Yoto API
- Set up FastAPI project structure
- Implement YotoClient wrapper
- Add authentication flow
- Test player listing and control
- Implement MQTT event handler
- Add event logging
- Test real-time event processing
- Build simple event viewer UI
- Implement audio upload
- Add format conversion
- Set up streaming endpoint
- Test with real device
- Build card CRUD API
- Add database models
- Create card editor UI
- Test card creation and playback
- Design script format
- Implement script engine
- Add button event handling
- Build script editor UI
- Test CYOA experience
- Add user authentication
- Implement multi-user support
- Add logging and monitoring
- Write documentation
- Deploy to production
- API Authentication: Use OAuth 2.0 or JWT for API access
- Yoto Credentials: Store in environment variables, never in code
- File Upload Validation: Check file types, sizes, and scan for malware
- Rate Limiting: Prevent API abuse
- HTTPS: All audio streaming over HTTPS
- Access Control: Users can only access their own cards and players
-
Caching:
- Cache player status (5 min TTL)
- Cache library data (15 min TTL)
- Cache audio file metadata
-
Async Operations:
- Use async/await throughout
- Non-blocking MQTT handling
- Concurrent API requests
-
Database Indexing:
- Index card_id, player_id
- Index timestamps for event queries
-
Audio Optimization:
- Convert to 128kbps MP3 for streaming
- Use byte-range requests for seeking
- Implement CDN for high traffic
# Test audio processing
def test_audio_conversion():
manager = AudioManager()
result = manager.convert_audio("test.wav", "mp3")
assert result.format == "mp3"
# Test script engine
def test_chapter_navigation():
engine = ScriptEngine()
script = load_test_script()
next_chapter = engine.handle_button_press(script, 1, "left")
assert next_chapter == 2# Test API endpoints
async def test_card_creation(client: TestClient):
response = await client.post("/api/cards", json={
"title": "Test Card",
"description": "Test"
})
assert response.status_code == 201
# Test Yoto API integration
async def test_player_control():
# Requires test Yoto account
pass- Test complete CYOA flow
- Test audio upload and playback
- Test MQTT event handling
- Run on local network
- Perfect for home use
- Low cost
- Accessible from anywhere
- Scalable
- Managed infrastructure
- Server in cloud
- Audio files on local NAS
- VPN for audio access
# Use structlog for structured logging
import structlog
logger = structlog.get_logger()
logger.info(
"player_control",
player_id=player_id,
action=action,
success=True
)
# Track metrics
from prometheus_client import Counter, Histogram
audio_plays = Counter('audio_plays_total', 'Total audio plays')
api_latency = Histogram('api_latency_seconds', 'API latency')Development: ~40-60 hours Self-hosted: $0-50/month (hardware) Cloud hosted: $10-50/month (depending on traffic)
- Review and approve architecture
- Set up development environment
- Begin Phase 1 implementation
- Iterate based on testing feedback