diff --git a/src/ScadaLink.SiteRuntime/Actors/DeploymentManagerActor.cs b/src/ScadaLink.SiteRuntime/Actors/DeploymentManagerActor.cs index 9837dca..b320a19 100644 --- a/src/ScadaLink.SiteRuntime/Actors/DeploymentManagerActor.cs +++ b/src/ScadaLink.SiteRuntime/Actors/DeploymentManagerActor.cs @@ -405,8 +405,7 @@ public class DeploymentManagerActor : ReceiveActor, IWithTimers private readonly HashSet _createdConnections = new(); /// - /// Parses the flattened config to find bound data connections and ensures - /// the DCL Manager has corresponding connection actors created. + /// Sets up DCL connections from the flattened config (idempotent: tracks created connections). /// private void EnsureDclConnections(string configJson) { @@ -422,35 +421,10 @@ public class DeploymentManagerActor : ReceiveActor, IWithTimers if (_createdConnections.Contains(name)) continue; - var primaryDetails = new Dictionary(); - if (!string.IsNullOrEmpty(connConfig.ConfigurationJson)) - { - try - { - // Parse as JsonElement to handle mixed value types (string, int, bool) - using var doc = System.Text.Json.JsonDocument.Parse(connConfig.ConfigurationJson); - foreach (var prop in doc.RootElement.EnumerateObject()) - { - primaryDetails[prop.Name] = prop.Value.ToString(); - } - } - catch { /* Ignore parse errors */ } - } - - Dictionary? backupDetails = null; - if (!string.IsNullOrEmpty(connConfig.BackupConfigurationJson)) - { - try - { - backupDetails = new Dictionary(); - using var doc = System.Text.Json.JsonDocument.Parse(connConfig.BackupConfigurationJson); - foreach (var prop in doc.RootElement.EnumerateObject()) - { - backupDetails[prop.Name] = prop.Value.ToString(); - } - } - catch { backupDetails = null; /* Ignore parse errors */ } - } + var primaryDetails = FlattenConnectionConfig(connConfig.Protocol, connConfig.ConfigurationJson); + var backupDetails = string.IsNullOrEmpty(connConfig.BackupConfigurationJson) + ? null + : FlattenConnectionConfig(connConfig.Protocol, connConfig.BackupConfigurationJson); _dclManager.Tell(new Commons.Messages.DataConnection.CreateConnectionCommand( name, connConfig.Protocol, primaryDetails, backupDetails, connConfig.FailoverRetryCount)); @@ -467,6 +441,32 @@ public class DeploymentManagerActor : ReceiveActor, IWithTimers } } + private static IDictionary FlattenConnectionConfig(string protocol, string? json) + { + if (string.IsNullOrEmpty(json)) + return new Dictionary(); + + if (string.Equals(protocol, "OpcUa", StringComparison.OrdinalIgnoreCase)) + { + var (config, _) = Commons.Serialization.OpcUaEndpointConfigSerializer.Deserialize(json); + return Commons.Serialization.OpcUaEndpointConfigSerializer.ToFlatDict(config); + } + + // Fallback: assume legacy flat-dict shape for any future / unknown protocol. + try + { + var dict = new Dictionary(); + using var doc = System.Text.Json.JsonDocument.Parse(json); + foreach (var prop in doc.RootElement.EnumerateObject()) + dict[prop.Name] = prop.Value.ToString(); + return dict; + } + catch + { + return new Dictionary(); + } + } + // ── Shared Script Loading ── private void LoadSharedScriptsFromStorage()