feat(uns): scripted-alarm CRUD in IUnsTreeService for the equipment Alarms tab
This commit is contained in:
@@ -1286,4 +1286,82 @@ public sealed class UnsTreeService(IDbContextFactory<OtOpcUaConfigDbContext> dbF
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<EquipmentAlarmRow>> LoadAlarmsForEquipmentAsync(string equipmentId, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
return await db.ScriptedAlarms.AsNoTracking()
|
||||
.Where(a => a.EquipmentId == equipmentId)
|
||||
.OrderBy(a => a.Name)
|
||||
.Select(a => new EquipmentAlarmRow(a.ScriptedAlarmId, a.Name, a.AlarmType, a.Severity, a.PredicateScriptId, a.Enabled))
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ScriptedAlarmEditDto?> LoadScriptedAlarmAsync(string scriptedAlarmId, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
return await db.ScriptedAlarms.AsNoTracking()
|
||||
.Where(a => a.ScriptedAlarmId == scriptedAlarmId)
|
||||
.Select(a => new ScriptedAlarmEditDto(a.ScriptedAlarmId, a.EquipmentId, a.Name, a.AlarmType, a.Severity,
|
||||
a.MessageTemplate, a.PredicateScriptId, a.HistorizeToAveva, a.Retain, a.Enabled, a.RowVersion))
|
||||
.FirstOrDefaultAsync(ct);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<UnsMutationResult> CreateScriptedAlarmAsync(string equipmentId, ScriptedAlarmInput input, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
if (await db.ScriptedAlarms.AnyAsync(a => a.ScriptedAlarmId == input.ScriptedAlarmId, ct))
|
||||
return new UnsMutationResult(false, $"ScriptedAlarm '{input.ScriptedAlarmId}' already exists.");
|
||||
|
||||
db.ScriptedAlarms.Add(new ScriptedAlarm
|
||||
{
|
||||
ScriptedAlarmId = input.ScriptedAlarmId,
|
||||
EquipmentId = equipmentId,
|
||||
Name = input.Name,
|
||||
AlarmType = input.AlarmType,
|
||||
Severity = input.Severity,
|
||||
MessageTemplate = input.MessageTemplate,
|
||||
PredicateScriptId = input.PredicateScriptId,
|
||||
HistorizeToAveva = input.HistorizeToAveva,
|
||||
Retain = input.Retain,
|
||||
Enabled = input.Enabled,
|
||||
});
|
||||
await db.SaveChangesAsync(ct);
|
||||
return new UnsMutationResult(true, null, input.ScriptedAlarmId);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<UnsMutationResult> UpdateScriptedAlarmAsync(string scriptedAlarmId, ScriptedAlarmInput input, byte[] rowVersion, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
var entity = await db.ScriptedAlarms.FirstOrDefaultAsync(a => a.ScriptedAlarmId == scriptedAlarmId, ct);
|
||||
if (entity is null) return new UnsMutationResult(false, "Row no longer exists.");
|
||||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = rowVersion;
|
||||
entity.Name = input.Name;
|
||||
entity.AlarmType = input.AlarmType;
|
||||
entity.Severity = input.Severity;
|
||||
entity.MessageTemplate = input.MessageTemplate;
|
||||
entity.PredicateScriptId = input.PredicateScriptId;
|
||||
entity.HistorizeToAveva = input.HistorizeToAveva;
|
||||
entity.Retain = input.Retain;
|
||||
entity.Enabled = input.Enabled;
|
||||
try { await db.SaveChangesAsync(ct); return new UnsMutationResult(true, null); }
|
||||
catch (DbUpdateConcurrencyException) { return new UnsMutationResult(false, "Another user changed this scripted alarm while you were editing."); }
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<UnsMutationResult> DeleteScriptedAlarmAsync(string scriptedAlarmId, byte[] rowVersion, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
var entity = await db.ScriptedAlarms.FirstOrDefaultAsync(a => a.ScriptedAlarmId == scriptedAlarmId, ct);
|
||||
if (entity is null) return new UnsMutationResult(true, null);
|
||||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = rowVersion;
|
||||
db.ScriptedAlarms.Remove(entity);
|
||||
try { await db.SaveChangesAsync(ct); return new UnsMutationResult(true, null); }
|
||||
catch (DbUpdateConcurrencyException) { return new UnsMutationResult(false, "Another user changed this alarm while you were viewing it."); }
|
||||
catch (Exception ex) { return new UnsMutationResult(false, $"Delete failed: {ex.Message}."); }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user