// VPN消息数据模型 import 'dart:convert'; class VpnStatusMessage { final String type; final int status; final int code; final String message; VpnStatusMessage({ required this.type, required this.status, required this.code, required this.message, }); factory VpnStatusMessage.fromJson(Map json) { return VpnStatusMessage( type: json['type'] ?? '', status: json['status'] ?? 0, code: json['code'] ?? 0, message: json['message'] ?? '', ); } Map toJson() { return {'type': type, 'status': status, 'code': code, 'message': message}; } } class TimerUpdateMessage { final String type; final int currentTime; final int mode; final bool isRunning; final bool isPaused; TimerUpdateMessage({ required this.type, required this.currentTime, required this.mode, required this.isRunning, required this.isPaused, }); factory TimerUpdateMessage.fromJson(Map json) { return TimerUpdateMessage( type: json['type'] ?? '', currentTime: json['currentTime'] ?? 0, mode: json['mode'] ?? 0, isRunning: json['isRunning'] ?? false, isPaused: json['isPaused'] ?? false, ); } Map toJson() { return { 'type': type, 'currentTime': currentTime, 'mode': mode, 'isRunning': isRunning, 'isPaused': isPaused, }; } } class BoostResultMessage { final String type; final String param; final bool success; final String locationCode; final String nodeId; BoostResultMessage({ required this.type, required this.param, required this.success, required this.locationCode, required this.nodeId, }); factory BoostResultMessage.fromJson(Map json) { return BoostResultMessage( type: json['type'] ?? '', param: json['param'] ?? '', success: json['success'] ?? false, locationCode: json['locationCode'] ?? '', nodeId: json['nodeId'] ?? '', ); } Map toJson() { return { 'type': type, 'param': param, 'success': success, 'locationCode': locationCode, 'nodeId': nodeId, }; } } class TimerNotificationMessage { final String type; final String message; TimerNotificationMessage({required this.type, required this.message}); factory TimerNotificationMessage.fromJson(Map json) { return TimerNotificationMessage( type: json['type'] ?? '', message: json['message'] ?? '', ); } Map toJson() { return {'type': type, 'message': message}; } } // 通用消息处理器 class VpnMessageHandler { static void handleMessage(String jsonString) { try { final Map json = jsonDecode(jsonString); final String type = json['type'] ?? ''; switch (type) { case 'vpn_status': _handleVpnStatus(VpnStatusMessage.fromJson(json)); break; case 'timer_update': _handleTimerUpdate(TimerUpdateMessage.fromJson(json)); break; case 'timer_notification': _handleTimerNotification(TimerNotificationMessage.fromJson(json)); break; default: print('未知消息类型: $type'); } } catch (e) { print('解析消息失败: $e'); } } static void _handleVpnStatus(VpnStatusMessage message) { print('VPN状态变化: status=${message.status}, message=${message.message}'); // 处理VPN状态变化 // 例如:更新UI状态、显示通知等 } static void _handleTimerUpdate(TimerUpdateMessage message) { print( '计时更新: time=${message.currentTime}, mode=${message.mode}, running=${message.isRunning}, paused=${message.isPaused}', ); // 处理计时更新 // 例如:更新计时器显示、更新UI状态等 } static void _handleTimerNotification(TimerNotificationMessage message) { print('计时通知: ${message.message}'); // 处理计时通知 // 例如:显示Toast、更新通知栏等 } }