- Migrated from websockets to aiohttp for unified HTTP/WebSocket server - Added REST endpoints: /api/pattern, /api/parameters, /api/state, /api/tempo/reset - Implemented color palette API with 8-slot system and selected colors - First selected color (index 0) is used as primary RGB for patterns - All operations now available via simple HTTP requests (no WebSocket needed) - Added comprehensive documentation: FRONTEND_API.md, COLOR_PALETTE_API.md - Added test scripts: test_rest_api.sh, test_color_patterns.py - Updated test/test_control_server.py for new /ws WebSocket path - Configuration persistence via lighting_config.json - Pattern parameters (n1-n4, brightness, delay) controllable via API - WebSocket still available at /ws for legacy support
5.6 KiB
5.6 KiB
REST API Implementation - Complete ✅
Summary
The lighting controller now has a complete REST API - no WebSocket required for frontend operations!
All lighting control functions are available via simple HTTP requests.
What Was Added
New API Endpoints
GET /api/state
- Get complete system stateGET/POST /api/pattern
- Get/change active patternGET/POST /api/parameters
- Get/change brightness, delay, n1-n4POST /api/tempo/reset
- Reset tempo detectionGET/POST /api/color-palette
- Color palette (already existed)
WebSocket Status
- WebSocket endpoint still available at
/ws
for legacy support - New frontends should use REST API only
- Simpler, more standard, easier to debug
Test Results
All endpoints tested and working:
✅ GET /api/state - Returns complete system state
✅ POST /api/pattern - Changes pattern successfully
✅ GET /api/pattern - Returns current pattern
✅ POST /api/parameters - Updates brightness/delay/n1-n4
✅ GET /api/parameters - Returns all parameters
✅ POST /api/color-palette - Updates palette/selection
✅ GET /api/color-palette - Returns palette state
Sample Test Output:
// POST /api/pattern
{
"status": "ok",
"pattern": "rainbow"
}
// GET /api/parameters
{
"brightness": 75,
"delay": 100,
"n1": 10,
"n2": 10,
"n3": 1,
"n4": 1
}
Documentation
Main Documentation: FRONTEND_API.md
Contains:
- Complete API reference
- JavaScript examples
- React component examples
- Vanilla JS examples
- Error handling
- Full working code samples
Quick Reference: COLOR_API_QUICK_REF.md
Usage Examples
Load Initial State
const response = await fetch('http://10.42.0.1:8765/api/state');
const state = await response.json();
// state contains: pattern, parameters, color_palette, beat_index
Change Pattern
await fetch('http://10.42.0.1:8765/api/pattern', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({pattern: 'rainbow'})
});
Adjust Brightness
await fetch('http://10.42.0.1:8765/api/parameters', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({brightness: 75})
});
Select Color
await fetch('http://10.42.0.1:8765/api/color-palette', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({selected_indices: [2, 1]})
});
Benefits Over WebSocket
- Simpler - No connection management
- Standard - Works with any HTTP library
- Debuggable - Use curl, browser, Postman
- Stateless - No connection drops
- Cacheable - GET requests can be cached
- RESTful - Standard patterns
Files Modified
Backend
src/control_server.py
- Added all REST endpoints
Documentation
FRONTEND_API.md
- Complete API documentation (new)REST_API_COMPLETE.md
- This file (new)
Testing
test_rest_api.sh
- Automated test script (new)
Test Script
Run comprehensive tests:
./test_rest_api.sh localhost
./test_rest_api.sh 10.42.0.1 # Remote testing
API Endpoint Summary
Method | Endpoint | Purpose |
---|---|---|
GET | /api/state |
Get complete system state |
GET | /api/pattern |
Get current pattern |
POST | /api/pattern |
Change pattern |
GET | /api/parameters |
Get all parameters |
POST | /api/parameters |
Update parameters |
GET | /api/color-palette |
Get color palette |
POST | /api/color-palette |
Update palette/selection |
POST | /api/tempo/reset |
Reset tempo detection |
Available Patterns
Use these pattern names in POST requests:
"on"
/"o"
- Solid color"off"
- All LEDs off"alternating"
/"a"
- Alternating on/off"rainbow"
/"r"
- Rainbow cycle"pulse"
/"p"
- Pulsing effect"segmented_movement"
/"sm"
- Moving segments"flicker"
/"f"
- Flickering"n_chase"
/"nc"
- Chase effect"radiate"
/"rd"
- Radiate from center"specto"
/"s"
- Spectograph
Status
🟢 PRODUCTION READY
The REST API is fully implemented, tested, and documented. Frontend developers can now build UIs using only HTTP requests - no WebSocket needed.
Next Steps for Frontend
- Read
FRONTEND_API.md
for complete documentation - Use
/api/state
to load initial UI state - Use POST endpoints to control lights
- Implement UI with any framework (React, Vue, vanilla JS)
- No WebSocket connection needed!
Quick Start for Frontend
<!DOCTYPE html>
<html>
<head><title>Lighting Control</title></head>
<body>
<button onclick="setPattern('rainbow')">Rainbow</button>
<button onclick="setPattern('alternating')">Alternating</button>
<input type="range" min="0" max="100" onchange="setBrightness(this.value)">
<script>
const API = 'http://10.42.0.1:8765';
async function setPattern(pattern) {
await fetch(`${API}/api/pattern`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({pattern})
});
}
async function setBrightness(value) {
await fetch(`${API}/api/parameters`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({brightness: parseInt(value)})
});
}
</script>
</body>
</html>
That's it! No WebSocket, no complex setup.
Support
- Full documentation:
FRONTEND_API.md
- Test script:
./test_rest_api.sh
- Color palette details:
COLOR_PALETTE_API.md