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 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 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, ); } }