Adds a new HiLo alarm trigger type with four configurable setpoints
(LoLo / Lo / Hi / HiHi). Each setpoint carries an optional priority,
deadband (for hysteresis), and operator message. The site runtime emits
AlarmStateChanged with an AlarmLevel field so consumers can differentiate
warning vs critical bands.
Plumbing:
- new AlarmLevel enum + AlarmStateChanged.Level/Message init properties
- AlarmTriggerEditor (Blazor) gets a HiLo render with severity tinting
- AlarmTriggerConfigCodec extracted from the editor for testability
- sitestream.proto carries level + message over gRPC
- SemanticValidator enforces numeric attribute, setpoint ordering,
non-negative deadband
- on-trigger scripts get an Alarm global (Name/Level/Priority/Message)
so notification routing can branch by severity
- per-instance InstanceAlarmOverride entity + EF migration + flattening
step + CLI commands; HiLo overrides merge setpoint-by-setpoint, binary
types whole-replace
- DebugView shows a Level badge + per-band message tooltip
- App.razor auto-reloads on permanent Blazor circuit failure
- docker/regen-proto.sh automates the proto regen workflow (the linux/arm64
protoc segfault means generated files are checked in for now)
278 lines
14 KiB
C#
278 lines
14 KiB
C#
using System.CommandLine;
|
|
using System.CommandLine.Parsing;
|
|
using ScadaLink.Commons.Messages.Management;
|
|
|
|
namespace ScadaLink.CLI.Commands;
|
|
|
|
public static class InstanceCommands
|
|
{
|
|
public static Command Build(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var command = new Command("instance") { Description = "Manage instances" };
|
|
|
|
command.Add(BuildList(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildGet(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildCreate(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildSetBindings(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildSetOverrides(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildAlarmOverride(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildSetArea(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildDiff(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildDeploy(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildEnable(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildDisable(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildDelete(urlOption, formatOption, usernameOption, passwordOption));
|
|
|
|
return command;
|
|
}
|
|
|
|
private static Command BuildGet(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
var cmd = new Command("get") { Description = "Get an instance by ID" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new GetInstanceCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildSetBindings(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
var bindingsOption = new Option<string>("--bindings") { Description = "JSON array of [attributeName, dataConnectionId] pairs", Required = true };
|
|
|
|
var cmd = new Command("set-bindings") { Description = "Set data connection bindings for an instance" };
|
|
cmd.Add(idOption);
|
|
cmd.Add(bindingsOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
var bindingsJson = result.GetValue(bindingsOption)!;
|
|
var pairs = System.Text.Json.JsonSerializer.Deserialize<List<List<System.Text.Json.JsonElement>>>(bindingsJson)
|
|
?? throw new InvalidOperationException("Invalid bindings JSON");
|
|
var bindings = pairs.Select(p =>
|
|
(p[0].GetString()!, p[1].GetInt32())).ToList();
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new SetConnectionBindingsCommand(id, bindings));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildList(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var siteIdOption = new Option<int?>("--site-id") { Description = "Filter by site ID" };
|
|
var templateIdOption = new Option<int?>("--template-id") { Description = "Filter by template ID" };
|
|
var searchOption = new Option<string?>("--search") { Description = "Search term" };
|
|
|
|
var cmd = new Command("list") { Description = "List instances" };
|
|
cmd.Add(siteIdOption);
|
|
cmd.Add(templateIdOption);
|
|
cmd.Add(searchOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var siteId = result.GetValue(siteIdOption);
|
|
var templateId = result.GetValue(templateIdOption);
|
|
var search = result.GetValue(searchOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new ListInstancesCommand(siteId, templateId, search));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildCreate(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var nameOption = new Option<string>("--name") { Description = "Unique instance name", Required = true };
|
|
var templateIdOption = new Option<int>("--template-id") { Description = "Template ID", Required = true };
|
|
var siteIdOption = new Option<int>("--site-id") { Description = "Site ID", Required = true };
|
|
var areaIdOption = new Option<int?>("--area-id") { Description = "Area ID" };
|
|
|
|
var cmd = new Command("create") { Description = "Create a new instance" };
|
|
cmd.Add(nameOption);
|
|
cmd.Add(templateIdOption);
|
|
cmd.Add(siteIdOption);
|
|
cmd.Add(areaIdOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var name = result.GetValue(nameOption)!;
|
|
var templateId = result.GetValue(templateIdOption);
|
|
var siteId = result.GetValue(siteIdOption);
|
|
var areaId = result.GetValue(areaIdOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new CreateInstanceCommand(name, templateId, siteId, areaId));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildDeploy(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
var cmd = new Command("deploy") { Description = "Deploy an instance" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new MgmtDeployInstanceCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildEnable(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
var cmd = new Command("enable") { Description = "Enable an instance" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new MgmtEnableInstanceCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildDisable(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
var cmd = new Command("disable") { Description = "Disable an instance" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new MgmtDisableInstanceCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildDelete(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
var cmd = new Command("delete") { Description = "Delete an instance" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new MgmtDeleteInstanceCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildSetOverrides(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
var overridesOption = new Option<string>("--overrides") { Description = "JSON object of attribute name -> value pairs, e.g. {\"Speed\": \"100\", \"Mode\": null}", Required = true };
|
|
|
|
var cmd = new Command("set-overrides") { Description = "Set attribute overrides for an instance" };
|
|
cmd.Add(idOption);
|
|
cmd.Add(overridesOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
var overridesJson = result.GetValue(overridesOption)!;
|
|
var overrides = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string?>>(overridesJson)
|
|
?? throw new InvalidOperationException("Invalid overrides JSON");
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new SetInstanceOverridesCommand(id, overrides));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildAlarmOverride(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var group = new Command("alarm-override") { Description = "Manage per-instance alarm overrides" };
|
|
|
|
// set
|
|
var setIdOption = new Option<int>("--instance-id") { Description = "Instance ID", Required = true };
|
|
var setAlarmOption = new Option<string>("--alarm") { Description = "Alarm canonical name (e.g., 'TempLevels' or 'Pump.TempSensor.Heat')", Required = true };
|
|
var setConfigOption = new Option<string?>("--trigger-config") { Description = "JSON override for TriggerConfiguration (HiLo: partial merge; others: whole-replace)" };
|
|
var setPriorityOption = new Option<int?>("--priority") { Description = "Priority override (0-1000)" };
|
|
var setCmd = new Command("set") { Description = "Set (upsert) an alarm override on an instance" };
|
|
setCmd.Add(setIdOption); setCmd.Add(setAlarmOption); setCmd.Add(setConfigOption); setCmd.Add(setPriorityOption);
|
|
setCmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new SetInstanceAlarmOverrideCommand(
|
|
result.GetValue(setIdOption),
|
|
result.GetValue(setAlarmOption)!,
|
|
result.GetValue(setConfigOption),
|
|
result.GetValue(setPriorityOption)));
|
|
});
|
|
group.Add(setCmd);
|
|
|
|
// delete
|
|
var delIdOption = new Option<int>("--instance-id") { Description = "Instance ID", Required = true };
|
|
var delAlarmOption = new Option<string>("--alarm") { Description = "Alarm canonical name", Required = true };
|
|
var delCmd = new Command("delete") { Description = "Remove an alarm override on an instance" };
|
|
delCmd.Add(delIdOption); delCmd.Add(delAlarmOption);
|
|
delCmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new DeleteInstanceAlarmOverrideCommand(
|
|
result.GetValue(delIdOption),
|
|
result.GetValue(delAlarmOption)!));
|
|
});
|
|
group.Add(delCmd);
|
|
|
|
// list
|
|
var listIdOption = new Option<int>("--instance-id") { Description = "Instance ID", Required = true };
|
|
var listCmd = new Command("list") { Description = "List all alarm overrides for an instance" };
|
|
listCmd.Add(listIdOption);
|
|
listCmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new ListInstanceAlarmOverridesCommand(result.GetValue(listIdOption)));
|
|
});
|
|
group.Add(listCmd);
|
|
|
|
return group;
|
|
}
|
|
|
|
private static Command BuildSetArea(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
var areaIdOption = new Option<int?>("--area-id") { Description = "Area ID (omit to clear area assignment)" };
|
|
|
|
var cmd = new Command("set-area") { Description = "Reassign an instance to a different area" };
|
|
cmd.Add(idOption);
|
|
cmd.Add(areaIdOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
var areaId = result.GetValue(areaIdOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new SetInstanceAreaCommand(id, areaId));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildDiff(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
|
|
|
var cmd = new Command("diff") { Description = "Show deployment diff (deployed vs current template)" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new GetDeploymentDiffCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
}
|