| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import 'package:intl/intl.dart';
-
- class WaterDataModel {
- final String deviceId;
- final String deviceName;
- final String area;
- final double pressure;
- final double flowRate;
- final double temperature;
- final String status;
- final DateTime updateTime;
- final double batteryLevel;
- final String location;
-
- WaterDataModel({
- required this.deviceId,
- required this.deviceName,
- required this.area,
- required this.pressure,
- required this.flowRate,
- required this.temperature,
- required this.status,
- required this.updateTime,
- required this.batteryLevel,
- required this.location,
- });
-
- // 从JSON创建对象
- factory WaterDataModel.fromJson(Map<String, dynamic> json) {
- return WaterDataModel(
- deviceId: json['device_id'] ?? '',
- deviceName: json['device_name'] ?? '',
- area: json['area'] ?? '',
- pressure: (json['pressure'] ?? 0.0).toDouble(),
- flowRate: (json['flow_rate'] ?? 0.0).toDouble(),
- temperature: (json['temperature'] ?? 0.0).toDouble(),
- status: json['status'] ?? 'unknown',
- updateTime: json['update_time'] != null
- ? DateTime.parse(json['update_time'])
- : DateTime.now(),
- batteryLevel: (json['battery_level'] ?? 100.0).toDouble(),
- location: json['location'] ?? '',
- );
- }
-
- // 转换为JSON
- Map<String, dynamic> toJson() {
- return {
- 'device_id': deviceId,
- 'device_name': deviceName,
- 'area': area,
- 'pressure': pressure,
- 'flow_rate': flowRate,
- 'temperature': temperature,
- 'status': status,
- 'update_time': updateTime.toIso8601String(),
- 'battery_level': batteryLevel,
- 'location': location,
- };
- }
-
- // 获取状态显示颜色
- Color getStatusColor() {
- switch (status) {
- case 'normal':
- return const Color(0xFF4CAF50);
- case 'warning':
- return const Color(0xFFFF9800);
- case 'error':
- return const Color(0xFFF44336);
- default:
- return const Color(0xFF9E9E9E);
- }
- }
-
- // 获取状态显示文本
- String getStatusText() {
- switch (status) {
- case 'normal':
- return '正常';
- case 'warning':
- return '警告';
- case 'error':
- return '故障';
- default:
- return '未知';
- }
- }
-
- // 检查是否需要警告
- bool get needsWarning {
- return status == 'warning' || status == 'error';
- }
-
- // 获取电池状态
- String getBatteryStatus() {
- if (batteryLevel > 80) return '充足';
- if (batteryLevel > 50) return '正常';
- if (batteryLevel > 20) return '低电量';
- return '严重不足';
- }
-
- // 克隆对象并更新部分字段
- WaterDataModel copyWith({
- String? deviceId,
- String? deviceName,
- String? area,
- double? pressure,
- double? flowRate,
- double? temperature,
- String? status,
- DateTime? updateTime,
- double? batteryLevel,
- String? location,
- }) {
- return WaterDataModel(
- deviceId: deviceId ?? this.deviceId,
- deviceName: deviceName ?? this.deviceName,
- area: area ?? this.area,
- pressure: pressure ?? this.pressure,
- flowRate: flowRate ?? this.flowRate,
- temperature: temperature ?? this.temperature,
- status: status ?? this.status,
- updateTime: updateTime ?? this.updateTime,
- batteryLevel: batteryLevel ?? this.batteryLevel,
- location: location ?? this.location,
- );
- }
-
- @override
- String toString() {
- return 'WaterDataModel('
- 'deviceId: $deviceId, '
- 'deviceName: $deviceName, '
- 'area: $area, '
- 'pressure: $pressure, '
- 'flowRate: $flowRate, '
- 'temperature: $temperature, '
- 'status: $status, '
- 'updateTime: ${DateFormat('yyyy-MM-dd HH:mm:ss').format(updateTime)}, '
- 'batteryLevel: $batteryLevel, '
- 'location: $location)';
- }
-
- @override
- bool operator ==(Object other) {
- if (identical(this, other)) return true;
- return other is WaterDataModel &&
- other.deviceId == deviceId &&
- other.deviceName == deviceName &&
- other.area == area &&
- other.pressure == pressure &&
- other.flowRate == flowRate &&
- other.temperature == temperature &&
- other.status == status &&
- other.updateTime == updateTime &&
- other.batteryLevel == batteryLevel &&
- other.location == location;
- }
-
- @override
- int get hashCode {
- return Object.hash(
- deviceId,
- deviceName,
- area,
- pressure,
- flowRate,
- temperature,
- status,
- updateTime,
- batteryLevel,
- location,
- );
- }
- }
|