vpn_message.dart 4.0 KB

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