vpn_message.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // VPN消息数据模型
  2. import 'dart:convert';
  3. class VpnStatusMessage {
  4. final String type;
  5. final int status;
  6. final String message;
  7. VpnStatusMessage({
  8. required this.type,
  9. required this.status,
  10. required this.message,
  11. });
  12. factory VpnStatusMessage.fromJson(Map<String, dynamic> json) {
  13. return VpnStatusMessage(
  14. type: json['type'] ?? '',
  15. status: json['status'] ?? 0,
  16. message: json['message'] ?? '',
  17. );
  18. }
  19. Map<String, dynamic> toJson() {
  20. return {'type': type, 'status': status, 'message': message};
  21. }
  22. }
  23. class TimerUpdateMessage {
  24. final String type;
  25. final int currentTime;
  26. final int mode;
  27. final bool isRunning;
  28. final bool isPaused;
  29. TimerUpdateMessage({
  30. required this.type,
  31. required this.currentTime,
  32. required this.mode,
  33. required this.isRunning,
  34. required this.isPaused,
  35. });
  36. factory TimerUpdateMessage.fromJson(Map<String, dynamic> json) {
  37. return TimerUpdateMessage(
  38. type: json['type'] ?? '',
  39. currentTime: json['currentTime'] ?? 0,
  40. mode: json['mode'] ?? 0,
  41. isRunning: json['isRunning'] ?? false,
  42. isPaused: json['isPaused'] ?? false,
  43. );
  44. }
  45. Map<String, dynamic> toJson() {
  46. return {
  47. 'type': type,
  48. 'currentTime': currentTime,
  49. 'mode': mode,
  50. 'isRunning': isRunning,
  51. 'isPaused': isPaused,
  52. };
  53. }
  54. }
  55. class TimerNotificationMessage {
  56. final String type;
  57. final String message;
  58. TimerNotificationMessage({required this.type, required this.message});
  59. factory TimerNotificationMessage.fromJson(Map<String, dynamic> json) {
  60. return TimerNotificationMessage(
  61. type: json['type'] ?? '',
  62. message: json['message'] ?? '',
  63. );
  64. }
  65. Map<String, dynamic> toJson() {
  66. return {'type': type, 'message': message};
  67. }
  68. }
  69. // 通用消息处理器
  70. class VpnMessageHandler {
  71. static void handleMessage(String jsonString) {
  72. try {
  73. final Map<String, dynamic> json = jsonDecode(jsonString);
  74. final String type = json['type'] ?? '';
  75. switch (type) {
  76. case 'vpn_status':
  77. _handleVpnStatus(VpnStatusMessage.fromJson(json));
  78. break;
  79. case 'timer_update':
  80. _handleTimerUpdate(TimerUpdateMessage.fromJson(json));
  81. break;
  82. case 'timer_notification':
  83. _handleTimerNotification(TimerNotificationMessage.fromJson(json));
  84. break;
  85. default:
  86. print('未知消息类型: $type');
  87. }
  88. } catch (e) {
  89. print('解析消息失败: $e');
  90. }
  91. }
  92. static void _handleVpnStatus(VpnStatusMessage message) {
  93. print('VPN状态变化: status=${message.status}, message=${message.message}');
  94. // 处理VPN状态变化
  95. // 例如:更新UI状态、显示通知等
  96. }
  97. static void _handleTimerUpdate(TimerUpdateMessage message) {
  98. print(
  99. '计时更新: time=${message.currentTime}, mode=${message.mode}, running=${message.isRunning}, paused=${message.isPaused}',
  100. );
  101. // 处理计时更新
  102. // 例如:更新计时器显示、更新UI状态等
  103. }
  104. static void _handleTimerNotification(TimerNotificationMessage message) {
  105. print('计时通知: ${message.message}');
  106. // 处理计时通知
  107. // 例如:显示Toast、更新通知栏等
  108. }
  109. }