- 新增 Flutter 跨平台客户端项目 (clients/flutter/)
- 29 个 Dart 文件: 服务层/状态管理/5个页面/BLE配网
- BLE 蓝牙配网: 扫描设备、写入WiFi凭据、配网状态监听
- HTTP API 客户端: 覆盖全部端点 (播放/场景/WiFi/视频/配置/文件/插件)
- WebSocket 实时通信: 事件流 + 自动重连
- 暗色主题 Material 3 UI, 中文界面
- Android 配置: minSdkVersion 21, BLE/网络权限
- PRD 产品需求文档 + 开发任务看板
- Web UI 添加 APK 下载入口 (routes.rs)
- 下载弹窗 + 二维码 + /download/{filename} 静态文件路由
- BLE 插件增加自动重连循环 (ble/mod.rs)
- BLE 默认设备名修正为 'Showen' (config.rs)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
185 lines
5.2 KiB
Dart
185 lines
5.2 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import '../models/player_status.dart';
|
|
import '../services/http_api_service.dart';
|
|
import '../services/web_socket_service.dart';
|
|
|
|
class PlayerProvider extends ChangeNotifier {
|
|
PlayerProvider({
|
|
required HttpApiService httpApiService,
|
|
required WebSocketService webSocketService,
|
|
}) : _httpApiService = httpApiService,
|
|
_webSocketService = webSocketService {
|
|
_statusSubscription = _webSocketService.onStatusUpdate.listen((payload) {
|
|
_status = PlayerStatus.fromJson(payload);
|
|
notifyListeners();
|
|
});
|
|
_stateSubscription = _webSocketService.onStateUpdate.listen((payload) {
|
|
_currentState = payload['new_state']?.toString() ?? _currentState;
|
|
notifyListeners();
|
|
});
|
|
_configSubscription = _webSocketService.onConfigUpdate.listen((payload) {
|
|
_updateSceneOptions(payload);
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
final HttpApiService _httpApiService;
|
|
final WebSocketService _webSocketService;
|
|
late final StreamSubscription<Map<String, dynamic>> _statusSubscription;
|
|
late final StreamSubscription<Map<String, dynamic>> _stateSubscription;
|
|
late final StreamSubscription<Map<String, dynamic>> _configSubscription;
|
|
|
|
PlayerStatus _status = PlayerStatus.initial();
|
|
List<String> _playlist = const <String>[];
|
|
List<String> _sceneOptions = const <String>['idle', 'intro', 'loop'];
|
|
bool _isLoading = false;
|
|
String? _errorMessage;
|
|
String? _currentState;
|
|
|
|
PlayerStatus get status => _status;
|
|
List<String> get playlist => _playlist;
|
|
List<String> get sceneOptions => _sceneOptions;
|
|
bool get isLoading => _isLoading;
|
|
String? get errorMessage => _errorMessage;
|
|
String get currentState => _currentState ?? _status.currentVideo ?? 'idle';
|
|
|
|
Future<void> bootstrap() async {
|
|
_setLoading(true);
|
|
try {
|
|
final results = await Future.wait<dynamic>([
|
|
_httpApiService.getPlaybackStatus(),
|
|
_httpApiService.getPlaylist(),
|
|
_httpApiService.getConfig(),
|
|
]);
|
|
_status = results[0] as PlayerStatus;
|
|
_playlist = results[1] as List<String>;
|
|
_updateSceneOptions(results[2] as Map<String, dynamic>);
|
|
_errorMessage = null;
|
|
} catch (error) {
|
|
_errorMessage = error.toString();
|
|
} finally {
|
|
_setLoading(false);
|
|
}
|
|
}
|
|
|
|
Future<void> fetchStatus() async {
|
|
try {
|
|
_status = await _httpApiService.getPlaybackStatus();
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
} catch (error) {
|
|
_errorMessage = error.toString();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> fetchPlaylist() async {
|
|
try {
|
|
_playlist = await _httpApiService.getPlaylist();
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
} catch (error) {
|
|
_errorMessage = error.toString();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> play() => _runCommand(_httpApiService.play);
|
|
|
|
Future<void> pause() => _runCommand(_httpApiService.pause);
|
|
|
|
Future<void> next() => _runCommand(_httpApiService.next);
|
|
|
|
Future<void> previous() => _runCommand(_httpApiService.previous);
|
|
|
|
Future<void> gotoIndex(int index) async {
|
|
await _runCommand(() => _httpApiService.goto(index));
|
|
}
|
|
|
|
Future<void> switchScene(String name) async {
|
|
await _runCommand(() => _httpApiService.changeScene(name));
|
|
_currentState = name;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> triggerEvent(String name, {String? value}) {
|
|
return _runCommand(() => _httpApiService.trigger(name, value));
|
|
}
|
|
|
|
Future<void> togglePlayPause() async {
|
|
if (_status.running && !_status.paused) {
|
|
await pause();
|
|
return;
|
|
}
|
|
await play();
|
|
}
|
|
|
|
Future<void> _runCommand(Future<dynamic> Function() action) async {
|
|
_setLoading(true);
|
|
try {
|
|
await action();
|
|
await Future.wait<void>([
|
|
fetchStatus(),
|
|
fetchPlaylist(),
|
|
]);
|
|
_errorMessage = null;
|
|
} catch (error) {
|
|
_errorMessage = error.toString();
|
|
notifyListeners();
|
|
} finally {
|
|
_setLoading(false);
|
|
}
|
|
}
|
|
|
|
void _updateSceneOptions(Map<String, dynamic> config) {
|
|
final candidates = <String>{};
|
|
final scenes = config['scenes'];
|
|
if (scenes is List) {
|
|
for (final scene in scenes) {
|
|
final value = scene.toString();
|
|
if (value.isNotEmpty) {
|
|
candidates.add(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
final stateMachine = config['state_machine'];
|
|
if (stateMachine is Map) {
|
|
final states = stateMachine['states'];
|
|
if (states is Map) {
|
|
for (final entry in states.keys) {
|
|
final value = entry.toString();
|
|
if (value.isNotEmpty) {
|
|
candidates.add(value);
|
|
}
|
|
}
|
|
}
|
|
final initialState = stateMachine['initial_state']?.toString();
|
|
if (initialState != null && initialState.isNotEmpty) {
|
|
candidates.add(initialState);
|
|
_currentState ??= initialState;
|
|
}
|
|
}
|
|
|
|
if (candidates.isNotEmpty) {
|
|
_sceneOptions = candidates.toList(growable: false)..sort();
|
|
}
|
|
}
|
|
|
|
void _setLoading(bool value) {
|
|
_isLoading = value;
|
|
notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
unawaited(_statusSubscription.cancel());
|
|
unawaited(_stateSubscription.cancel());
|
|
unawaited(_configSubscription.cancel());
|
|
super.dispose();
|
|
}
|
|
}
|