/// <inheritdoc /> public override void Execute(OpcSession session) { Logger.Debug($"Start action {Description} on '{session.EndpointUrl}'"); if (OpcUaNodeId == null) { // get the NodeId OpcUaNodeId = session.GetNodeIdFromId(OpcNodeId); } // read the node info Node node = session.OpcUaClientSession.ReadNode(OpcUaNodeId); // report the node info Logger.Debug($"Action ({Description}) Node DisplayName is '{node.DisplayName}'"); Logger.Debug($"Action ({Description}) Node Description is '{node.Description}'"); // read the value DataValue dataValue = session.OpcUaClientSession.ReadValue(OpcUaNodeId); try { Value = dataValue.Value; } catch (Exception e) { Logger.Warning(e, $"Cannot convert type of read value."); Value = "Cannot convert type of read value."; } // report the node value Logger.Debug($"Action ({Description}) Node data value is '{dataValue.Value}'"); }
/// <summary> /// Create the data structures to manage actions. /// </summary> public static async Task <bool> CreateOpcActionDataAsync() { try { await OpcActionListSemaphore.WaitAsync(); await OpcSessionsListSemaphore.WaitAsync(); // create actions out of the configuration var uniqueSessionInfo = _actionConfiguration.Select(n => new Tuple <Uri, bool>(n.EndpointUrl, n.UseSecurity)).Distinct(); foreach (var sessionInfo in uniqueSessionInfo) { // create new session info. OpcSession opcSession = new OpcSession(sessionInfo.Item1, sessionInfo.Item2, OpcSessionCreationTimeout); // add all actions to the session List <OpcAction> actionsOnEndpoint = new List <OpcAction>(); var endpointConfigs = _actionConfiguration.Where(c => c.EndpointUrl == sessionInfo.Item1 && c.UseSecurity == sessionInfo.Item2); foreach (var config in endpointConfigs) { config?.Read.ForEach(a => opcSession.OpcActions.Add(new OpcReadAction(config.EndpointUrl, a))); config?.Test.ForEach(a => opcSession.OpcActions.Add(new OpcTestAction(config.EndpointUrl, a))); } // report actions Logger.Information($"Actions on '{opcSession.EndpointUrl.AbsoluteUri}' {(opcSession.UseSecurity ? "with" : "without")} security."); foreach (var action in opcSession.OpcActions) { Logger.Information($"{action.Description}, recurring each: {action.Interval} sec"); } // add session OpcSessions.Add(opcSession); } } catch (Exception e) { Logger.Fatal(e, "Creation of the internal OPC management structures failed. Exiting..."); return(false); } finally { OpcSessionsListSemaphore.Release(); OpcActionListSemaphore.Release(); } return(true); }
/// <summary> /// Shutdown all sessions. /// </summary> public async static Task SessionShutdownAsync() { try { while (OpcSessions.Count > 0) { OpcSession opcSession = null; try { await OpcSessionsListSemaphore.WaitAsync(); opcSession = OpcSessions.ElementAt(0); OpcSessions.RemoveAt(0); } finally { OpcSessionsListSemaphore.Release(); } await opcSession?.ShutdownAsync(); } } catch (Exception e) { Logger.Fatal(e, "Failed to shutdown all sessions."); } // wait and continue after a while uint maxTries = ShutdownRetryCount; while (true) { int sessionCount = OpcSessions.Count; if (sessionCount == 0) { return; } if (maxTries-- == 0) { Logger.Information($"There are still {sessionCount} sessions alive. Ignore and continue shutdown."); return; } Logger.Information($"{ProgramName} is shutting down. Wait {SessionConnectWait} seconds, since there are stil {sessionCount} sessions alive..."); await Task.Delay(SessionConnectWait * 1000); } }
/// <inheritdoc /> public override void Execute(OpcSession session) { Logger.Information($"Start action {Description} on '{session.EndpointUrl}'"); // read the node info Node node = session.OpcUaClientSession.ReadNode(OpcUaNodeId); // report the node info Logger.Information($"Node Displayname is '{node.DisplayName}'"); Logger.Information($"Node Description is '{node.Description}'"); // read the value DataValue dataValue = session.OpcUaClientSession.ReadValue(OpcUaNodeId); // report the node value Logger.Information($"Node Value is '{dataValue.Value}'"); Logger.Information($"Node Value is '{dataValue.ToString()}'"); }
/// <summary> /// Create an OPC session management data structures. /// </summary> private static void CreateOpcSession(Uri endpointUrl, bool useSecurity) { try { // create new session info OpcSession opcSession = new OpcSession(endpointUrl, useSecurity, OpcSessionCreationTimeout); // add all actions to the session List <OpcAction> actionsOnEndpoint = new List <OpcAction>(); var endpointConfigs = _actionConfiguration.Where(c => c.EndpointUrl == endpointUrl); foreach (var config in endpointConfigs) { config?.Read.ForEach(a => opcSession.OpcActions.Add(new OpcReadAction(config.EndpointUrl, a))); config?.Test.ForEach(a => opcSession.OpcActions.Add(new OpcTestAction(config.EndpointUrl, a))); } } catch (Exception e) { Logger.Fatal(e, "Creation of the OPC session failed."); throw e; } }
/// <summary> /// Execute function needs to be overloaded. /// </summary> public virtual void Execute(OpcSession session) { Logger.Error($"No Execute method for action ({Description}) defined."); throw new Exception($"No Execute method for action ({ Description}) defined."); }