#!/usr/bin/env python3 """ REST API routes — wszystkie endpointy HTTP. Importowane przez app.py. FIXED: meters format dla Pico (data zamiast bands, uv vs spectrum) """ import json import os import uuid import psutil import subprocess import time from flask import Blueprint, jsonify, request, current_app, send_file def _bt(): return getattr(current_app, 'bt_manager', None) def _net(): return getattr(current_app, 'net_manager', None) def _eq(): return getattr(current_app, 'eq_manager', None) bp = Blueprint('api', __name__, url_prefix='/api') STATIONS_FILE = os.path.join(os.path.dirname(__file__), '..', 'radio', 'stations.json') # Ograniczenie częstotliwości wysyłania LED (np. co 100ms) _last_led_send = 0 # ── Helper ───────────────────────────────────────────────────────────────── def _mgr(): return current_app.source def _uart(): return current_app.display def _load_stations() -> dict: try: with open(STATIONS_FILE) as f: return json.load(f) except Exception: return {'version': '3.0', 'stations': []} def _save_stations(data: dict): with open(STATIONS_FILE, 'w') as f: json.dump(data, f, indent=2, ensure_ascii=False) # ── Status ───────────────────────────────────────────────────────────────── @bp.route('/version', methods=['GET']) def api_version(): """Zwróć wersję z pliku VERSION.""" import os base = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) try: ver = open(os.path.join(base, 'VERSION')).read().strip() except Exception: ver = '0.0.0' return jsonify({'version': ver}) @bp.route('/status') def api_status(): """Pełny status systemu.""" sm = _mgr() net = _net() uart = _uart() net_status = net.get_status() cpu_temp = None try: with open('/sys/class/thermal/thermal_zone0/temp') as f: cpu_temp = round(int(f.read().strip()) / 1000, 1) except Exception: try: temps = psutil.sensors_temperatures() for key in ('cpu_thermal', 'cpu-thermal', 'coretemp'): if key in temps and temps[key]: cpu_temp = round(temps[key][0].current, 1) break except Exception: pass try: cpu_load = psutil.cpu_percent(interval=0.0) except Exception: cpu_load = None try: with open('/proc/uptime') as f: secs = int(float(f.read().split()[0])) days = secs // 86400 hours = (secs % 86400) // 3600 mins = (secs % 3600) // 60 uptime = f"{days}d {hours}h {mins}m" if days else f"{hours}h {mins}m" except Exception: uptime = '' return jsonify({ **sm.get_all_status(), 'volume': sm.volume, 'loudness': sm.get_config('loudness', True), 'autogain': sm.get_config('autogain', True), 'meter_mode': sm.get_config('meter_mode', 'vu'), 'direct': sm.get_config('direct', False), 'source_gains': sm._cfg.get('source_gains', {}), 'mono': sm.get_config('mono', False), 'network': net_status, 'uart': current_app.display.connected if current_app.display else False, 'cd_ready': False, 'cd_disc': None, 'cpu_temp': cpu_temp, 'cpu_load': cpu_load, 'cpu_hot': bool(cpu_temp is not None and cpu_temp >= 70.0), 'uptime': uptime, }) # ── Source ───────────────────────────────────────────────────────────────── @bp.route('/source', methods=['GET']) def api_source_get(): return jsonify(_mgr().get_all_status()) @bp.route('/source', methods=['POST']) def api_source_set(): data = request.get_json(force=True) or {} source_id = data.get('source') if not source_id: return jsonify({'error': 'source required'}), 400 ok = _mgr().switch(source_id) if not ok: return jsonify({'error': f'Cannot activate source: {source_id}'}), 400 _mgr().set_config('last_source', source_id) status = _mgr().active.get_status() if _mgr().active else {} if _uart(): _uart().send_state() return jsonify(_mgr().get_all_status()) # ── Volume ───────────────────────────────────────────────────────────────── @bp.route('/volume', methods=['GET']) def api_volume_get(): return jsonify({'volume': _mgr().volume}) @bp.route('/volume', methods=['POST']) def api_volume_set(): data = request.get_json(force=True) or {} vol = int(data.get('volume', 75)) _mgr().set_volume(vol) if _uart(): if current_app.display: current_app.display.send_fast_update() return jsonify({'volume': vol}) # ── Level (VU meter) ─────────────────────────────────────────────────────── @bp.route('/level', methods=['GET']) def api_level(): if source and hasattr(source, 'get_level'): return jsonify(source.get_level()) return jsonify({'rms_l': -60, 'rms_r': -60, 'peak_l': -60, 'peak_r': -60}) @bp.route('/spectrum', methods=['GET']) def api_spectrum(): source = _mgr().active if source and hasattr(source, 'get_spectrum'): return jsonify({'bands': source.get_spectrum()}) return jsonify({'bands': [-60.0] * 32}) @bp.route('/meters', methods=['GET']) def api_meters(): """Level + Spectrum w jednym requeście.""" global _last_led_send source = _mgr().active level = {'rms_l': -60.0, 'rms_r': -60.0, 'peak_l': -60.0, 'peak_r': -60.0} bands = [-60.0] * 32 mgr = _mgr() if mgr: level = mgr.get_level() bands = mgr.get_spectrum() now = time.time() if _uart() and _uart().connected and (now - _last_led_send > 0.1): _last_led_send = now _uart().send_meters(bands) return jsonify({**level, 'bands': bands}) # ── Stream Info ──────────────────────────────────────────────────────────── @bp.route('/stream', methods=['GET']) def api_stream_info(): """Zwraca parametry strumienia (bitrate, sample rate itp.) dla aktywnego źródła.""" source = _mgr().active if source and hasattr(source, 'get_status'): status = source.get_status() return jsonify(status.get('stream', {})) return jsonify({}) # ── Cover art ─────────────────────────────────────────────────────────────── @bp.route('/cover/', methods=['GET']) def api_cover_file(filename): """Serwuje plik okładki z cache.""" from pathlib import Path covers_dir = Path(__file__).parent / 'covers' path = covers_dir / filename if not path.exists() or path.suffix.lower() not in ('.jpg', '.jpeg', '.png'): return '', 404 return send_file(str(path), mimetype='image/jpeg') @bp.route('/cover', methods=['GET']) def api_cover_fetch(): """Pobierz okładkę dla podanego artysty/tytułu/stacji.""" artist = request.args.get('artist', '') title = request.args.get('title', '') station = request.args.get('station', '') try: from display import get_cover_url url = get_cover_url(artist, title, station) return jsonify({'url': url}) except Exception as e: return jsonify({'url': None, 'error': str(e)}) # ── Settings ─────────────────────────────────────────────────────────────── @bp.route('/setting', methods=['POST']) def api_setting(): data = request.get_json(force=True) or {} key = data.get('key') value = data.get('value') if key in ('loudness', 'mono', 'autogain', 'meter_mode'): _mgr().set_config(key, value if key == 'meter_mode' else bool(value)) return jsonify({'key': key, 'value': value}) return jsonify({'error': 'Unknown setting'}), 400 @bp.route('/setting/direct', methods=['POST']) def api_direct(): data = request.json or {} enabled = bool(data.get('enabled', False)) sm = _mgr() sm.set_config('direct', enabled) src = sm.get('radio') if src and hasattr(src, 'set_direct'): src.set_direct(enabled) return jsonify({'direct': enabled}) # ── EQ ───────────────────────────────────────────────────────────────────── @bp.route('/eq/', methods=['GET']) def api_eq_get(source_id): return jsonify({ 'source': source_id, 'gains': _eq().get(source_id), 'bands': _eq().get_band_names(), }) @bp.route('/eq/', methods=['POST']) def api_eq_set(source_id): data = request.get_json(force=True) or {} gains = data.get('gains') if not gains or len(gains) != 10: return jsonify({'error': 'gains must be array of 10'}), 400 _mgr().set_eq(source_id, gains) if _eq(): _eq().set(source_id, gains) return jsonify({'source': source_id, 'gains': gains}) @bp.route('/eq//preset/', methods=['POST']) def api_eq_preset(source_id, preset): try: gains = _eq().apply_preset(source_id, preset) _mgr().set_eq(source_id, gains) return jsonify({'source': source_id, 'preset': preset, 'gains': gains}) except ValueError as e: return jsonify({'error': str(e)}), 400 @bp.route('/eq/presets', methods=['GET']) def api_eq_presets(): eq = _eq() return jsonify({'presets': eq.get_presets(), 'names': eq.get_preset_names()}) @bp.route('/eq/user//save', methods=['POST']) def api_eq_user_save(preset_id): data = request.json or {} source_id = data.get('source', _mgr().active.SOURCE_ID if _mgr().active else 'radio') eq = _eq() gains = eq.get(source_id) try: saved = eq.save_user_preset(preset_id, gains) return jsonify({'preset': preset_id, 'gains': saved}) except ValueError as e: return jsonify({'error': str(e)}), 400 @bp.route('/eq/user//name', methods=['POST']) def api_eq_user_rename(preset_id): data = request.json or {} name = data.get('name', '').strip() if not name: return jsonify({'error': 'name required'}), 400 try: new_name = _eq().set_preset_name(preset_id, name) return jsonify({'preset': preset_id, 'name': new_name}) except ValueError as e: return jsonify({'error': str(e)}), 400 # ── Radio / Stations ──────────────────────────────────────────────────────── @bp.route('/radio/stations', methods=['GET']) def api_stations(): return jsonify(_load_stations()) @bp.route('/radio/stations', methods=['POST']) def api_add_station(): data = request.get_json(force=True) or {} name = (data.get('name') or '').strip() url = (data.get('url') or '').strip() genre = (data.get('genre') or '').strip() if not name or not url: return jsonify({'error': 'name and url required'}), 400 sd = _load_stations() station = { 'id': str(uuid.uuid4())[:8], 'name': name, 'url': url, 'genre': genre, 'bitrate': int(data.get('bitrate', 128)), 'codec': data.get('codec', 'MP3'), 'favorite': False, 'enabled': True, } sd['stations'].append(station) _save_stations(sd) return jsonify({'status': 'created', 'station': station}), 201 @bp.route('/radio/stations/', methods=['DELETE']) def api_delete_station(station_id): sd = _load_stations() sd['stations'] = [s for s in sd['stations'] if s['id'] != station_id] _save_stations(sd) return jsonify({'status': 'deleted'}) @bp.route('/radio/stations//favorite', methods=['POST']) def api_favorite(station_id): add = bool(request.get_json(force=True).get('add', True)) sd = _load_stations() for s in sd['stations']: if s['id'] == station_id: s['favorite'] = add _save_stations(sd) return jsonify({'status': 'ok', 'favorite': add}) @bp.route('/radio/play', methods=['POST']) def api_radio_play(): data = request.get_json(force=True) or {} station_id = data.get('id') url = data.get('url') station_name = '' if station_id: stations = _load_stations().get('stations', []) station = next((s for s in stations if s['id'] == station_id), None) if not station: return jsonify({'error': 'Station not found'}), 404 url = station['url'] station_name = station['name'] if not url: return jsonify({'error': 'No URL or station ID'}), 400 if not _mgr().switch('radio'): return jsonify({'error': 'Cannot activate radio'}), 500 radio = _mgr().get('radio') radio.play(url, station_name) if station_id: _mgr().set_config('last_station_id', station_id) if _uart(): _uart().send_state() return jsonify({'status': 'playing', 'url': url, 'station': station_name}) @bp.route('/radio/stop', methods=['POST']) def api_radio_stop(): radio = _mgr().get('radio') if radio: radio.stop() if _uart(): _uart().send_state() return jsonify({'status': 'stopped'}) # ── Network ───────────────────────────────────────────────────────────────── @bp.route('/network/status', methods=['GET']) def api_net_status(): return jsonify(_net().get_status()) @bp.route('/network/scan', methods=['GET']) def api_net_scan(): return jsonify({'networks': _net().scan()}) @bp.route('/network/connect', methods=['POST']) def api_net_connect(): data = request.get_json(force=True) or {} ssid = data.get('ssid', '') password = data.get('password', '') if not ssid: return jsonify({'error': 'ssid required'}), 400 result = _net().connect(ssid, password) return jsonify(result) @bp.route('/network/disconnect', methods=['POST']) def api_net_disconnect(): return jsonify(_net().disconnect()) @bp.route('/network/wifi', methods=['GET']) def api_wifi_state(): return jsonify({'wifi': _net().get_wifi_enabled()}) @bp.route('/network/wifi', methods=['POST']) def api_wifi_toggle(): data = request.json or {} enabled = bool(data.get('enabled', True)) ok = _net().set_wifi_enabled(enabled) return jsonify({'wifi': enabled, 'ok': ok}) # ── Source Gain ───────────────────────────────────────────────────────────── @bp.route('/source//gain', methods=['GET']) def api_get_gain(source_id): return jsonify({'source': source_id, 'gain': _mgr().get_source_gain(source_id)}) @bp.route('/source//gain', methods=['POST']) def api_set_gain(source_id): data = request.json or {} gain = float(data.get('gain', 0)) new_gain = _mgr().set_source_gain(source_id, gain) return jsonify({'source': source_id, 'gain': new_gain}) @bp.route('/source/gains', methods=['GET']) def api_get_all_gains(): sm = _mgr() sources = ['radio', 'bluetooth', 'phono', 'line1', 'line2', 'spdif'] return jsonify({s: sm.get_source_gain(s) for s in sources}) # ── Bluetooth ─────────────────────────────────────────────────────────────── @bp.route('/bluetooth/devices', methods=['GET']) def api_bt_devices(): return jsonify({ 'paired': _bt().get_paired(), 'scanning': _bt().scanning, 'mode': _bt().mode, 'connected': _bt().connected_device, }) @bp.route('/bluetooth/scan', methods=['POST']) def api_bt_scan(): _bt().scan_async(duration=10, callback=None) return jsonify({'status': 'scanning', 'message': 'Scan started (10s)'}) @bp.route('/bluetooth/pair', methods=['POST']) def api_bt_pair(): mac = (request.get_json(force=True) or {}).get('mac', '') if not mac: return jsonify({'error': 'mac required'}), 400 ok = _bt().pair(mac) return jsonify({'status': 'paired' if ok else 'error', 'mac': mac}) @bp.route('/bluetooth/connect', methods=['POST']) def api_bt_connect(): mac = (request.get_json(force=True) or {}).get('mac', '') if not mac: return jsonify({'error': 'mac required'}), 400 ok = _bt().connect(mac) return jsonify({'status': 'connected' if ok else 'error', 'mac': mac}) @bp.route('/bluetooth/disconnect', methods=['POST']) def api_bt_disconnect(): mac = (request.get_json(force=True) or {}).get('mac', '') _bt().disconnect(mac) return jsonify({'status': 'disconnected'}) @bp.route('/bluetooth/unpair', methods=['POST']) def api_bt_unpair(): data = request.get_json(force=True) or {} mac = data.get('mac', '') if not mac: return jsonify({'error': 'mac required'}), 400 _bt().disconnect_device(mac) _bt().remove_device(mac) return jsonify({'status': 'unpaired', 'mac': mac}) @bp.route('/bluetooth/mode', methods=['POST']) def api_bt_mode(): mode = (request.get_json(force=True) or {}).get('mode', 'sink') _bt().set_mode(mode) return jsonify({'mode': mode}) # ── System ────────────────────────────────────────────────────────────────── @bp.route('/system/reboot', methods=['POST']) def api_reboot(): import threading, time def _do(): time.sleep(1) subprocess.run(['sudo', 'reboot']) threading.Thread(target=_do, daemon=True).start() return jsonify({'status': 'rebooting'}) @bp.route('/system/shutdown', methods=['POST']) def api_shutdown(): import threading, time def _do(): time.sleep(1) subprocess.run(['sudo', 'poweroff']) threading.Thread(target=_do, daemon=True).start() return jsonify({'ok': True}) @bp.route('/system/info', methods=['GET']) def api_sysinfo(): try: cpu = psutil.cpu_percent(interval=0.5) mem = psutil.virtual_memory().percent disk = psutil.disk_usage('/').percent temps = psutil.sensors_temperatures() temp = round(temps['cpu_thermal'][0].current, 1) if 'cpu_thermal' in temps else None except Exception: cpu = mem = disk = temp = None try: with open('/proc/uptime') as f: secs = int(float(f.read().split()[0])) uptime = f"{secs//86400}d {(secs%86400)//3600}h {(secs%3600)%60}m" except Exception: uptime = '' return jsonify({'cpu_pct': cpu, 'mem_pct': mem, 'disk_pct': disk, 'temp': temp, 'uptime': uptime})