智慧水务管理系统 - 精河县供水工程综合管理平台

water_data_model.dart 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import 'package:intl/intl.dart';
  2. class WaterDataModel {
  3. final String deviceId;
  4. final String deviceName;
  5. final String area;
  6. final double pressure;
  7. final double flowRate;
  8. final double temperature;
  9. final String status;
  10. final DateTime updateTime;
  11. final double batteryLevel;
  12. final String location;
  13. WaterDataModel({
  14. required this.deviceId,
  15. required this.deviceName,
  16. required this.area,
  17. required this.pressure,
  18. required this.flowRate,
  19. required this.temperature,
  20. required this.status,
  21. required this.updateTime,
  22. required this.batteryLevel,
  23. required this.location,
  24. });
  25. // 从JSON创建对象
  26. factory WaterDataModel.fromJson(Map<String, dynamic> json) {
  27. return WaterDataModel(
  28. deviceId: json['device_id'] ?? '',
  29. deviceName: json['device_name'] ?? '',
  30. area: json['area'] ?? '',
  31. pressure: (json['pressure'] ?? 0.0).toDouble(),
  32. flowRate: (json['flow_rate'] ?? 0.0).toDouble(),
  33. temperature: (json['temperature'] ?? 0.0).toDouble(),
  34. status: json['status'] ?? 'unknown',
  35. updateTime: json['update_time'] != null
  36. ? DateTime.parse(json['update_time'])
  37. : DateTime.now(),
  38. batteryLevel: (json['battery_level'] ?? 100.0).toDouble(),
  39. location: json['location'] ?? '',
  40. );
  41. }
  42. // 转换为JSON
  43. Map<String, dynamic> toJson() {
  44. return {
  45. 'device_id': deviceId,
  46. 'device_name': deviceName,
  47. 'area': area,
  48. 'pressure': pressure,
  49. 'flow_rate': flowRate,
  50. 'temperature': temperature,
  51. 'status': status,
  52. 'update_time': updateTime.toIso8601String(),
  53. 'battery_level': batteryLevel,
  54. 'location': location,
  55. };
  56. }
  57. // 获取状态显示颜色
  58. Color getStatusColor() {
  59. switch (status) {
  60. case 'normal':
  61. return const Color(0xFF4CAF50);
  62. case 'warning':
  63. return const Color(0xFFFF9800);
  64. case 'error':
  65. return const Color(0xFFF44336);
  66. default:
  67. return const Color(0xFF9E9E9E);
  68. }
  69. }
  70. // 获取状态显示文本
  71. String getStatusText() {
  72. switch (status) {
  73. case 'normal':
  74. return '正常';
  75. case 'warning':
  76. return '警告';
  77. case 'error':
  78. return '故障';
  79. default:
  80. return '未知';
  81. }
  82. }
  83. // 检查是否需要警告
  84. bool get needsWarning {
  85. return status == 'warning' || status == 'error';
  86. }
  87. // 获取电池状态
  88. String getBatteryStatus() {
  89. if (batteryLevel > 80) return '充足';
  90. if (batteryLevel > 50) return '正常';
  91. if (batteryLevel > 20) return '低电量';
  92. return '严重不足';
  93. }
  94. // 克隆对象并更新部分字段
  95. WaterDataModel copyWith({
  96. String? deviceId,
  97. String? deviceName,
  98. String? area,
  99. double? pressure,
  100. double? flowRate,
  101. double? temperature,
  102. String? status,
  103. DateTime? updateTime,
  104. double? batteryLevel,
  105. String? location,
  106. }) {
  107. return WaterDataModel(
  108. deviceId: deviceId ?? this.deviceId,
  109. deviceName: deviceName ?? this.deviceName,
  110. area: area ?? this.area,
  111. pressure: pressure ?? this.pressure,
  112. flowRate: flowRate ?? this.flowRate,
  113. temperature: temperature ?? this.temperature,
  114. status: status ?? this.status,
  115. updateTime: updateTime ?? this.updateTime,
  116. batteryLevel: batteryLevel ?? this.batteryLevel,
  117. location: location ?? this.location,
  118. );
  119. }
  120. @override
  121. String toString() {
  122. return 'WaterDataModel('
  123. 'deviceId: $deviceId, '
  124. 'deviceName: $deviceName, '
  125. 'area: $area, '
  126. 'pressure: $pressure, '
  127. 'flowRate: $flowRate, '
  128. 'temperature: $temperature, '
  129. 'status: $status, '
  130. 'updateTime: ${DateFormat('yyyy-MM-dd HH:mm:ss').format(updateTime)}, '
  131. 'batteryLevel: $batteryLevel, '
  132. 'location: $location)';
  133. }
  134. @override
  135. bool operator ==(Object other) {
  136. if (identical(this, other)) return true;
  137. return other is WaterDataModel &&
  138. other.deviceId == deviceId &&
  139. other.deviceName == deviceName &&
  140. other.area == area &&
  141. other.pressure == pressure &&
  142. other.flowRate == flowRate &&
  143. other.temperature == temperature &&
  144. other.status == status &&
  145. other.updateTime == updateTime &&
  146. other.batteryLevel == batteryLevel &&
  147. other.location == location;
  148. }
  149. @override
  150. int get hashCode {
  151. return Object.hash(
  152. deviceId,
  153. deviceName,
  154. area,
  155. pressure,
  156. flowRate,
  157. temperature,
  158. status,
  159. updateTime,
  160. batteryLevel,
  161. location,
  162. );
  163. }
  164. }