feat: Add api/v1/config to get, put config
All checks were successful
Build RPi Pico firmware image / Build-Firmware (push) Successful in 4m40s
Check code formatting / Check-C-Format (push) Successful in 9s
Check code formatting / Check-Python-Flake8 (push) Successful in 12s
Check code formatting / Check-Bash-Shellcheck (push) Successful in 6s
Run unit tests on host / Run-Unit-Tests (push) Successful in 10s
Run pytests / Check-Pytest (push) Successful in 12s

Signed-off-by: Matthias Blankertz <matthias@blankertz.org>
This commit is contained in:
2025-12-03 20:12:29 +01:00
parent 2e1bc7782b
commit a7e58853bb
3 changed files with 38 additions and 4 deletions

View File

@@ -65,7 +65,7 @@ def run():
# Wifi with default config # Wifi with default config
setup_wifi() setup_wifi()
start_webserver() start_webserver(config)
# Setup MP3 player # Setup MP3 player
with SDContext(mosi=hwconfig.SD_DI, miso=hwconfig.SD_DO, sck=hwconfig.SD_SCK, ss=hwconfig.SD_CS, with SDContext(mosi=hwconfig.SD_DI, miso=hwconfig.SD_DO, sck=hwconfig.SD_SCK, ss=hwconfig.SD_CS,

View File

@@ -5,7 +5,7 @@ from errno import ENOENT
import json import json
import os import os
try: try:
from typing import TYPE_CHECKING, Mapping from typing import TYPE_CHECKING, Mapping, Any
except ImportError: except ImportError:
TYPE_CHECKING = False TYPE_CHECKING = False
@@ -71,3 +71,21 @@ class Configuration:
def get_button_map(self) -> Mapping[str, int | None]: def get_button_map(self) -> Mapping[str, int | None]:
return self._get('BUTTON_MAP') return self._get('BUTTON_MAP')
# For the web API
def get_config(self) -> Mapping[str, Any]:
return self.config
def _validate(self, default, config, path=''):
for k in config.keys():
if k not in default:
raise ValueError(f'Invalid config key {path}/{k}')
if isinstance(default[k], dict):
if not isinstance(config[k], dict):
raise ValueError(f'Invalid config: Value of {path}/{k} must be mapping')
self._validate(default[k], config[k], f'{path}/{k}')
def set_config(self, config):
self._validate(self.DEFAULT_CONFIG, config)
self.config = config
self._save()

View File

@@ -9,11 +9,13 @@ from microdot import Microdot
webapp = Microdot() webapp = Microdot()
server = None server = None
config = None
def start_webserver(): def start_webserver(config_):
global server global server, config
server = asyncio.create_task(webapp.start_server(port=80)) server = asyncio.create_task(webapp.start_server(port=80))
config = config_
@webapp.route('/') @webapp.route('/')
@@ -39,3 +41,17 @@ async def filesystem_post(request):
async def playlist_post(request): async def playlist_post(request):
print(request) print(request)
return {'success': False} return {'success': False}
@webapp.route('/api/v1/config', methods=['GET'])
async def config_get(request):
return config.get_config()
@webapp.route('/api/v1/config', methods=['PUT'])
async def config_put(request):
try:
config.set_config(request.json)
except ValueError as ex:
return str(ex), 400
return '', 204