接入设备故障告警和场站实时任务进度
This commit is contained in:
90
lib/features/device_alarms/domain/entities/device_alarm.dart
Normal file
90
lib/features/device_alarms/domain/entities/device_alarm.dart
Normal file
@@ -0,0 +1,90 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
/// 设备故障与预警记录;关闭是业务处理状态,不代表设备故障已经恢复。
|
||||
class DeviceAlarm extends Equatable {
|
||||
const DeviceAlarm({
|
||||
required this.id,
|
||||
this.alarmNo = '',
|
||||
this.title = '',
|
||||
this.content = '',
|
||||
this.deviceId = '',
|
||||
this.deviceName = '',
|
||||
this.sourceSn = '',
|
||||
this.level = '',
|
||||
this.type = '',
|
||||
this.timeValues = '',
|
||||
this.alarmTime,
|
||||
this.recoverTime,
|
||||
this.handleStatus = 1,
|
||||
this.rootCause = '',
|
||||
this.handleSuggestion = '',
|
||||
});
|
||||
|
||||
final int id;
|
||||
final String alarmNo;
|
||||
final String title;
|
||||
final String content;
|
||||
final String deviceId;
|
||||
final String deviceName;
|
||||
|
||||
/// 由订阅 Topic 标记来源;后端 deviceId 可能是数据库 ID。
|
||||
final String sourceSn;
|
||||
final String level;
|
||||
final String type;
|
||||
final String timeValues;
|
||||
final DateTime? alarmTime;
|
||||
final DateTime? recoverTime;
|
||||
final int handleStatus;
|
||||
final String rootCause;
|
||||
final String handleSuggestion;
|
||||
|
||||
bool get isClosed => handleStatus == 2;
|
||||
|
||||
/// 无服务端 ID 的推送仍可展示,按来源/故障码/发生时间标识,避免多条 id:null 互相覆盖。
|
||||
String get recordKey => id > 0
|
||||
? 'id:$id'
|
||||
: 'push:${sourceSn.isEmpty ? deviceId : sourceSn}|$alarmNo|${alarmTime?.toIso8601String() ?? ''}';
|
||||
|
||||
DeviceAlarm closed() => copyWith(handleStatus: 2);
|
||||
|
||||
DeviceAlarm copyWith({
|
||||
String? sourceSn,
|
||||
String? deviceName,
|
||||
int? handleStatus,
|
||||
}) => DeviceAlarm(
|
||||
id: id,
|
||||
alarmNo: alarmNo,
|
||||
title: title,
|
||||
content: content,
|
||||
deviceId: deviceId,
|
||||
deviceName: deviceName ?? this.deviceName,
|
||||
sourceSn: sourceSn ?? this.sourceSn,
|
||||
level: level,
|
||||
type: type,
|
||||
timeValues: timeValues,
|
||||
alarmTime: alarmTime,
|
||||
recoverTime: recoverTime,
|
||||
handleStatus: handleStatus ?? this.handleStatus,
|
||||
rootCause: rootCause,
|
||||
handleSuggestion: handleSuggestion,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
id,
|
||||
alarmNo,
|
||||
title,
|
||||
content,
|
||||
deviceId,
|
||||
deviceName,
|
||||
sourceSn,
|
||||
level,
|
||||
type,
|
||||
timeValues,
|
||||
alarmTime,
|
||||
recoverTime,
|
||||
handleStatus,
|
||||
rootCause,
|
||||
handleSuggestion,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/// Topic 使用设备 SN,与告警记录里的设备数据库 ID 分开。
|
||||
class DeviceAlarmTarget {
|
||||
const DeviceAlarmTarget({
|
||||
required this.sn,
|
||||
required this.name,
|
||||
this.deviceId = '',
|
||||
});
|
||||
final String sn;
|
||||
final String name;
|
||||
final String deviceId;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../entities/device_alarm.dart';
|
||||
|
||||
abstract class DeviceAlarmRepository {
|
||||
Stream<List<DeviceAlarm>> watch(String deviceCode);
|
||||
Future<Either<Failure, Unit>> handle(int alarmId);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../entities/device_alarm.dart';
|
||||
import '../repositories/device_alarm_repository.dart';
|
||||
|
||||
class WatchDeviceAlarmsUseCase {
|
||||
const WatchDeviceAlarmsUseCase(this.repository);
|
||||
final DeviceAlarmRepository repository;
|
||||
|
||||
Stream<List<DeviceAlarm>> call(String deviceCode) =>
|
||||
repository.watch(deviceCode.trim());
|
||||
}
|
||||
|
||||
class HandleDeviceAlarmUseCase {
|
||||
const HandleDeviceAlarmUseCase(this.repository);
|
||||
final DeviceAlarmRepository repository;
|
||||
|
||||
Future<Either<Failure, Unit>> call(int alarmId) async {
|
||||
if (alarmId <= 0) return Left(Failure(''));
|
||||
return repository.handle(alarmId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import '../../../v2/device_list/domain/usecases/get_site_devices_usecase.dart';
|
||||
import '../entities/device_alarm_target.dart';
|
||||
|
||||
/// 使用完整场站设备列表,不受页面筛选或设备在线状态影响。
|
||||
class LoadSiteAlarmTargetsUseCase {
|
||||
const LoadSiteAlarmTargetsUseCase(this.getSiteDevices);
|
||||
final GetSiteDevicesUseCase getSiteDevices;
|
||||
|
||||
Future<List<DeviceAlarmTarget>> call(int siteId) async {
|
||||
final devices = await getSiteDevices(siteId);
|
||||
final targets = <String, DeviceAlarmTarget>{};
|
||||
for (final device in devices) {
|
||||
final sn = device.name.trim();
|
||||
// 场站接口可能缺少型号/类型,不能用 UI 的 isRobot 推断过滤订阅目标。
|
||||
// 按真实场站设备 SN 建立告警订阅,保留空 SN 和跨场站保护。
|
||||
if (sn.isEmpty || (device.siteId != null && device.siteId != siteId)) {
|
||||
continue;
|
||||
}
|
||||
final alias = device.deviceAlias?.trim() ?? '';
|
||||
targets[sn] = DeviceAlarmTarget(
|
||||
sn: sn,
|
||||
name: alias.isEmpty ? sn : alias,
|
||||
deviceId: device.deviceId,
|
||||
);
|
||||
}
|
||||
return List.unmodifiable(targets.values);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user