Beispiel #1
0
        /// <summary>
        /// Read and parse the startup configuration file
        /// </summary>
        /// <returns></returns>
        public static async Task <bool> ReadOpcConfigurationAsync()
        {
            // get information on actions and validate the json by deserializing it
            try
            {
                await OpcActionListSemaphore.WaitAsync();

                Logger.Information($"The name of the action configuration file is: {OpcActionConfigurationFilename}");

                // if the file exists, read it, if not just continue
                if (File.Exists(OpcActionConfigurationFilename))
                {
                    Logger.Information($"Attemtping to load action configuration from: {OpcActionConfigurationFilename}");
                    _actionConfiguration = JsonConvert.DeserializeObject <List <OpcActionConfigurationModel> >(File.ReadAllText(OpcActionConfigurationFilename));
                }
                else
                {
                    Logger.Information($"The action configuration file '{OpcActionConfigurationFilename}' does not exist. Continue and wait for remote configuration requests.");
                }
            }
            catch (Exception e)
            {
                Logger.Fatal(e, "Loading of the action configuration file failed. Does the file exist and has correct syntax? Exiting...");
                return(false);
            }
            finally
            {
                OpcActionListSemaphore.Release();
            }
            Logger.Information($"There are {_actionConfiguration.Count.ToString()} actions.");
            return(true);
        }
Beispiel #2
0
 /// <summary>
 /// Frees resources for the node configuration
 /// </summary>
 public static void Deinit()
 {
     OpcSessions = null;
     OpcSessionsListSemaphore.Dispose();
     OpcSessionsListSemaphore = null;
     OpcActionListSemaphore.Dispose();
     OpcActionListSemaphore = null;
 }
Beispiel #3
0
        /// <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)));
                        config?.HistoryRead.ForEach(a => opcSession.OpcActions.Add(new OpcHistoryReadAction(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, "Error: creation of the internal OPC management structures failed. Exiting...");
                Environment.ExitCode = 1;
                return(false);
            }
            finally
            {
                OpcSessionsListSemaphore.Release();
                OpcActionListSemaphore.Release();
            }
            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Read and parse the startup configuration file.
        /// </summary>
        public static async Task <bool> ReadOpcConfigurationAsync()
        {
            try
            {
                await OpcActionListSemaphore.WaitAsync();

                // if the file exists, read it, if not just continue
                if (File.Exists(OpcActionConfigurationFilename))
                {
                    Logger.Information($"Attemtping to load action configuration from: {OpcActionConfigurationFilename}");
                    _actionConfiguration = JsonConvert.DeserializeObject <List <OpcActionConfigurationModel> >(File.ReadAllText(OpcActionConfigurationFilename));
                }
                else
                {
                    Logger.Information($"The action configuration file '{OpcActionConfigurationFilename}' does not exist. Continue...");
                }

                // add connectivity test action if requested
                if (TestConnectivity)
                {
                    Logger.Information($"Creating test action to test connectivity");
                    _actionConfiguration.Add(new OpcActionConfigurationModel(new TestActionModel()));
                }

                // add unsecure connectivity test action if requested
                if (TestUnsecureConnectivity)
                {
                    Logger.Information($"Creating test action to test unsecured connectivity");
                    _actionConfiguration.Add(new OpcActionConfigurationModel(new TestActionModel(), DefaultEndpointUrl, false));
                }
            }
            catch (Exception e)
            {
                Logger.Fatal(e, "Error: loading of the action configuration file failed. Does the file exist and has correct syntax? Exiting...");
                Environment.ExitCode = 1;
                return(false);
            }
            finally
            {
                OpcActionListSemaphore.Release();
            }
            Logger.Information($"There is/are {_actionConfiguration.Sum(c => c.Read.Count + c.Test.Count + c.HistoryRead.Count)} action(s) configured.");
            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// Create the data structures to manage OPC sessions and actions.
        /// </summary>
        /// <returns></returns>
        public static async Task <bool> CreateOpcActionDataAsync()
        {
            try
            {
                await OpcActionListSemaphore.WaitAsync();

                await OpcSessionsListSemaphore.WaitAsync();

                // create actions out of the configuration
                var uniqueEndpointUrls = _actionConfiguration.Select(n => n.EndpointUrl).Distinct();
                foreach (var endpointUrl in uniqueEndpointUrls)
                {
                    // create new session info.
                    OpcSession opcSession = new OpcSession(endpointUrl, _actionConfiguration.Where(n => n.EndpointUrl == endpointUrl).First().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(r => opcSession.OpcActions.Add(new OpcReadAction(r)));
                        config?.Write.ForEach(w => opcSession.OpcActions.Add(new OpcWriteAction(w)));
                    }

                    // 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);
        }