feat(cli): instance import-overrides --file (T16)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.Parsing;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
||||
|
||||
@@ -23,6 +24,7 @@ public static class InstanceCommands
|
||||
command.Add(BuildCreate(urlOption, formatOption, usernameOption, passwordOption));
|
||||
command.Add(BuildSetBindings(urlOption, formatOption, usernameOption, passwordOption));
|
||||
command.Add(BuildSetOverrides(urlOption, formatOption, usernameOption, passwordOption));
|
||||
command.Add(BuildImportOverrides(urlOption, formatOption, usernameOption, passwordOption));
|
||||
command.Add(BuildAlarmOverride(urlOption, formatOption, usernameOption, passwordOption));
|
||||
command.Add(BuildNativeAlarmSourceOverride(urlOption, formatOption, usernameOption, passwordOption));
|
||||
command.Add(BuildSetArea(urlOption, formatOption, usernameOption, passwordOption));
|
||||
@@ -296,6 +298,46 @@ public static class InstanceCommands
|
||||
return cmd;
|
||||
}
|
||||
|
||||
private static Command BuildImportOverrides(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
||||
{
|
||||
var idOption = new Option<int>("--id") { Description = "Instance ID", Required = true };
|
||||
var fileOption = new Option<string>("--file") { Description = "Path to the override CSV file (columns: AttributeName,Value,ElementType)", Required = true };
|
||||
|
||||
var cmd = new Command("import-overrides") { Description = "Apply attribute overrides from a CSV file" };
|
||||
cmd.Add(idOption);
|
||||
cmd.Add(fileOption);
|
||||
cmd.SetAction(async (ParseResult result) =>
|
||||
{
|
||||
var id = result.GetValue(idOption);
|
||||
var filePath = result.GetValue(fileOption)!;
|
||||
|
||||
string csvText;
|
||||
try
|
||||
{
|
||||
csvText = File.ReadAllText(filePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OutputFormatter.WriteError($"Cannot read file '{filePath}': {ex.Message}", "FILE_READ_ERROR");
|
||||
return 1;
|
||||
}
|
||||
|
||||
var parseResult = OverrideCsvParser.Parse(csvText);
|
||||
if (parseResult.Errors.Count > 0)
|
||||
{
|
||||
foreach (var error in parseResult.Errors)
|
||||
OutputFormatter.WriteError(error, "INVALID_CSV");
|
||||
return 1;
|
||||
}
|
||||
|
||||
var overrides = parseResult.Rows.ToDictionary(r => r.AttributeName, r => r.Value);
|
||||
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" };
|
||||
|
||||
@@ -539,6 +539,30 @@ scadabridge --url <url> instance set-overrides --id <int> --overrides <json>
|
||||
| `--id` | yes | Instance ID |
|
||||
| `--overrides` | yes | JSON object of attribute name to value (e.g. `{"Speed": "100", "Mode": null}`); null clears an override |
|
||||
|
||||
#### `instance import-overrides`
|
||||
|
||||
Apply attribute value overrides from a CSV file. The CSV must have a header row (`AttributeName,Value,ElementType`; the `ElementType` column is optional). An empty `Value` cell clears the override for that attribute. If the file contains any parse errors, all errors are printed and no overrides are applied.
|
||||
|
||||
```sh
|
||||
scadabridge --url <url> instance import-overrides --id <int> --file <path.csv>
|
||||
```
|
||||
|
||||
| Option | Required | Description |
|
||||
|--------|----------|-------------|
|
||||
| `--id` | yes | Instance ID |
|
||||
| `--file` | yes | Path to the override CSV file |
|
||||
|
||||
CSV format example:
|
||||
|
||||
```csv
|
||||
AttributeName,Value,ElementType
|
||||
Speed,100,
|
||||
Mode,,
|
||||
Label,"Hello, World",
|
||||
```
|
||||
|
||||
(`Value` empty = clear the override for that attribute.)
|
||||
|
||||
#### `instance alarm-override set`
|
||||
|
||||
Set (upsert) an alarm override on an instance.
|
||||
|
||||
Reference in New Issue
Block a user