using ZB.MOM.WW.OtOpcUa.AdminUI.Uns.TagEditors;
using ZB.MOM.WW.OtOpcUa.Driver.AbCip;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.DeviceForms;
///
/// Typed working model for an AB CIP Device's connection endpoint (the v3 endpoint→DeviceConfig
/// split). Authors the PascalCase HostAddress/PlcFamily keys that
/// AbCipDeviceOptions binds — the exact casing the seed and runtime probe
/// (PropertyNameCaseInsensitive) expect. Preserves unrecognised keys across a load→save.
///
public sealed class AbCipDeviceModel
{
/// libplctag gateway/path host address (e.g. ab://gateway/1,0).
public string HostAddress { get; set; } = "";
/// Allen-Bradley PLC family.
public AbCipPlcFamily PlcFamily { get; set; } = AbCipPlcFamily.ControlLogix;
private System.Text.Json.Nodes.JsonObject _bag = new();
/// Loads a model from a DeviceConfig JSON string, defaulting absent keys and retaining
/// every original key so fields this editor doesn't expose survive a load→save.
/// The raw DeviceConfig JSON string, or null for a new/empty device.
/// The populated .
public static AbCipDeviceModel FromJson(string? json)
{
var o = TagConfigJson.ParseOrNew(json);
return new AbCipDeviceModel
{
HostAddress = TagConfigJson.GetString(o, "HostAddress") ?? "",
PlcFamily = TagConfigJson.GetEnum(o, "PlcFamily", AbCipPlcFamily.ControlLogix),
_bag = o,
};
}
/// Serializes this model back to a DeviceConfig JSON string, writing the PascalCase endpoint
/// keys over the preserved key bag.
/// The serialised DeviceConfig JSON string.
public string ToJson()
{
TagConfigJson.Set(_bag, "HostAddress", HostAddress);
TagConfigJson.Set(_bag, "PlcFamily", PlcFamily);
return TagConfigJson.Serialize(_bag);
}
/// Validation hook; returns an error message or null when the model is valid.
/// An error message describing the validation failure, or null when the model is valid.
public string? Validate()
=> string.IsNullOrWhiteSpace(HostAddress) ? "Host address is required." : null;
}