Files
CloudHome-Ops/lib/models/alert.dart

96 lines
2.0 KiB
Dart
Raw Permalink Normal View History

/// 告警级别枚举。
enum AlertLevel {
/// 严重。
critical,
/// 警告。
warning,
/// 提示。
info;
/// 级别中文名称。
String get label {
switch (this) {
case AlertLevel.critical:
return '严重';
case AlertLevel.warning:
return '警告';
case AlertLevel.info:
return '提示';
}
}
/// 从字符串解析级别。
static AlertLevel fromString(String value) {
return AlertLevel.values.firstWhere(
(level) => level.name == value,
orElse: () => AlertLevel.info,
);
}
}
/// 告警模型。
class Alert {
const Alert({
required this.id,
required this.deviceId,
required this.deviceName,
required this.title,
required this.message,
required this.level,
required this.occurredAt,
required this.acknowledged,
});
/// 告警唯一标识。
final String id;
/// 关联设备 ID。
final String deviceId;
/// 关联设备名称。
final String deviceName;
/// 告警标题。
final String title;
/// 告警详情。
final String message;
/// 告警级别。
final AlertLevel level;
/// 发生时间。
final DateTime occurredAt;
/// 是否已确认。
final bool acknowledged;
factory Alert.fromJson(Map<String, dynamic> json) {
return Alert(
id: json['id'] as String,
deviceId: json['deviceId'] as String,
deviceName: json['deviceName'] as String,
title: json['title'] as String,
message: json['message'] as String,
level: AlertLevel.fromString(json['level'] as String),
occurredAt: DateTime.parse(json['occurredAt'] as String),
acknowledged: json['acknowledged'] as bool,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'deviceId': deviceId,
'deviceName': deviceName,
'title': title,
'message': message,
'level': level.name,
'occurredAt': occurredAt.toIso8601String(),
'acknowledged': acknowledged,
};
}
}