| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- // 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<String, dynamic> json) {
- return VpnStatusMessage(
- type: json['type'] ?? '',
- status: json['status'] ?? 0,
- code: json['code'] ?? 0,
- message: json['message'] ?? '',
- );
- }
- Map<String, dynamic> 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<String, dynamic> json) {
- return TimerUpdateMessage(
- type: json['type'] ?? '',
- currentTime: json['currentTime'] ?? 0,
- mode: json['mode'] ?? 0,
- isRunning: json['isRunning'] ?? false,
- isPaused: json['isPaused'] ?? false,
- );
- }
- Map<String, dynamic> 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<String, dynamic> json) {
- return BoostResultMessage(
- type: json['type'] ?? '',
- param: json['param'] ?? '',
- success: json['success'] ?? false,
- locationCode: json['locationCode'] ?? '',
- nodeId: json['nodeId'] ?? '',
- );
- }
- Map<String, dynamic> 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<String, dynamic> json) {
- return TimerNotificationMessage(
- type: json['type'] ?? '',
- message: json['message'] ?? '',
- );
- }
- Map<String, dynamic> toJson() {
- return {'type': type, 'message': message};
- }
- }
- // 通用消息处理器
- class VpnMessageHandler {
- static void handleMessage(String jsonString) {
- try {
- final Map<String, dynamic> 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、更新通知栏等
- }
- }
|