vpn_message.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. TimerUpdateMessage({
  31. required this.type,
  32. required this.currentTime,
  33. required this.mode,
  34. });
  35. factory TimerUpdateMessage.fromJson(Map<String, dynamic> json) {
  36. return TimerUpdateMessage(
  37. type: json['type'] ?? '',
  38. currentTime: json['currentTime'] ?? 0,
  39. mode: json['mode'] ?? 0,
  40. );
  41. }
  42. Map<String, dynamic> toJson() {
  43. return {'type': type, 'currentTime': currentTime, 'mode': mode};
  44. }
  45. }
  46. class BoostResultMessage {
  47. final String type;
  48. final String param;
  49. final bool success;
  50. final String locationCode;
  51. final String nodeId;
  52. BoostResultMessage({
  53. required this.type,
  54. required this.param,
  55. required this.success,
  56. required this.locationCode,
  57. required this.nodeId,
  58. });
  59. factory BoostResultMessage.fromJson(Map<String, dynamic> json) {
  60. return BoostResultMessage(
  61. type: json['type'] ?? '',
  62. param: json['param'] ?? '',
  63. success: json['success'] ?? false,
  64. locationCode: json['locationCode'] ?? '',
  65. nodeId: json['nodeId'] ?? '',
  66. );
  67. }
  68. Map<String, dynamic> toJson() {
  69. return {
  70. 'type': type,
  71. 'param': param,
  72. 'success': success,
  73. 'locationCode': locationCode,
  74. 'nodeId': nodeId,
  75. };
  76. }
  77. }
  78. class TimerNotificationMessage {
  79. final String type;
  80. final String message;
  81. TimerNotificationMessage({required this.type, required this.message});
  82. factory TimerNotificationMessage.fromJson(Map<String, dynamic> json) {
  83. return TimerNotificationMessage(
  84. type: json['type'] ?? '',
  85. message: json['message'] ?? '',
  86. );
  87. }
  88. Map<String, dynamic> toJson() {
  89. return {'type': type, 'message': message};
  90. }
  91. }
  92. // 通用消息处理器
  93. class VpnMessageHandler {
  94. static void handleMessage(String jsonString) {
  95. try {
  96. final Map<String, dynamic> json = jsonDecode(jsonString);
  97. final String type = json['type'] ?? '';
  98. switch (type) {
  99. case 'vpn_status':
  100. _handleVpnStatus(VpnStatusMessage.fromJson(json));
  101. break;
  102. case 'timer_update':
  103. _handleTimerUpdate(TimerUpdateMessage.fromJson(json));
  104. break;
  105. case 'timer_notification':
  106. _handleTimerNotification(TimerNotificationMessage.fromJson(json));
  107. break;
  108. default:
  109. print('未知消息类型: $type');
  110. }
  111. } catch (e) {
  112. print('解析消息失败: $e');
  113. }
  114. }
  115. static void _handleVpnStatus(VpnStatusMessage message) {
  116. print('VPN状态变化: status=${message.status}, message=${message.message}');
  117. // 处理VPN状态变化
  118. // 例如:更新UI状态、显示通知等
  119. }
  120. static void _handleTimerUpdate(TimerUpdateMessage message) {
  121. print('计时更新: time=${message.currentTime}, mode=${message.mode}');
  122. // 处理计时更新
  123. // 例如:更新计时器显示、更新UI状态等
  124. }
  125. static void _handleTimerNotification(TimerNotificationMessage message) {
  126. print('计时通知: ${message.message}');
  127. // 处理计时通知
  128. // 例如:显示Toast、更新通知栏等
  129. }
  130. }