import '../../domain/entities/device_alarm.dart'; class DeviceAlarmModel { static DeviceAlarm fromJson( Map json, { bool allowMissingId = false, }) { final id = int.tryParse('${json['id']}'); final missingPushId = allowMissingId && json['id'] == null && (json['alarmNo']?.toString().trim().isNotEmpty ?? false) && ((json['alarmTitle']?.toString().trim().isNotEmpty ?? false) || (json['alarmContent']?.toString().trim().isNotEmpty ?? false)); if ((id == null || id <= 0) && !missingPushId) { throw const FormatException('Invalid alarm ID'); } String text(String key) => json[key]?.toString().trim() ?? ''; DateTime? time(String key) { final value = json[key]; if (value is! List) return DateTime.tryParse(text(key)); // Java LocalDateTime 的数组格式:[年,月,日,时,分,秒,纳秒]。 if (value.length != 6 && value.length != 7) return null; final parts = []; for (final item in value) { final parsed = int.tryParse('$item'); if (parsed == null) return null; parts.add(parsed); } final nanos = parts.length == 7 ? parts[6] : 0; if (parts[0] < 1 || parts[0] > 9999) return null; if (nanos < 0 || nanos >= 1000000000) return null; final result = DateTime( parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], nanos ~/ 1000000, (nanos ~/ 1000) % 1000, ); if (result.year != parts[0] || result.month != parts[1] || result.day != parts[2] || result.hour != parts[3] || result.minute != parts[4] || result.second != parts[5]) { return null; } return result; } return DeviceAlarm( // 0 仅代表未提供服务端 ID;不会作为处理接口的参数。 id: id ?? 0, alarmNo: text('alarmNo'), title: text('alarmTitle'), content: text('alarmContent'), deviceId: text('deviceId'), deviceName: text('deviceName'), level: text('alarmLevel'), type: text('alarmType'), timeValues: text('timeValues'), alarmTime: time('alarmTime'), recoverTime: time('recoverTime'), handleStatus: int.tryParse(text('handleStatus')) ?? 1, rootCause: text('rootCause'), handleSuggestion: text('handleSuggestion'), ); } }