コード例 #1
0
ファイル: Program.cs プロジェクト: thomjak/iot-edge-opc-plc
        /// <summary>
        /// Run the server.
        /// </summary>
#pragma warning disable IDE0060 // Remove unused parameter
        private static async Task ConsoleServerAsync(string[] args)
#pragma warning restore IDE0060 // Remove unused parameter
        {
            var quitEvent           = new ManualResetEvent(false);
            var shutdownTokenSource = new CancellationTokenSource();

            ShutdownToken = shutdownTokenSource.Token;

            // init OPC configuration and tracing
            var plcOpcApplicationConfiguration = new OpcApplicationConfiguration();
            ApplicationConfiguration plcApplicationConfiguration = await plcOpcApplicationConfiguration.ConfigureAsync().ConfigureAwait(false);

            // allow canceling the connection process
            try
            {
                Console.CancelKeyPress += (sender, eArgs) =>
                {
                    quitEvent.Set();
                    eArgs.Cancel = true;
                };
            }
            catch
            {
            }

            // start the server.
            Logger.Information($"Starting server on endpoint {plcApplicationConfiguration.ServerConfiguration.BaseAddresses[0]} ...");
            Logger.Information("Simulation settings are:");
            Logger.Information($"One simulation phase consists of {SimulationCycleCount} cycles");
            Logger.Information($"One cycle takes {SimulationCycleLength} milliseconds");
            Logger.Information($"Spike generation is {(GenerateSpikes ? "enabled" : "disabled")}");
            Logger.Information($"Data generation is {(GenerateData ? "enabled" : "disabled")}");

            Logger.Information($"Anonymous authentication: {(DisableAnonymousAuth ? "disabled" : "enabled")}");
            Logger.Information($"Username/Password authentication: {(DisableUsernamePasswordAuth ? "disabled" : "enabled")}");
            Logger.Information($"Certificate authentication: {(DisableCertAuth ? "disabled" : "enabled")}");

            PlcServer = new PlcServer();
            PlcServer.Start(plcApplicationConfiguration);
            Logger.Information("OPC UA Server started.");

            PlcSimulation = new PlcSimulation(PlcServer);
            PlcSimulation.Start();

            if (ShowPublisherConfigJsonIp)
            {
                await DumpPublisherConfigJsonAsync($"{GetIpAddress()}:{ServerPort}{ServerPath}").ConfigureAwait(false);
            }
            else if (ShowPublisherConfigJsonPh)
            {
                await DumpPublisherConfigJsonAsync($"{Hostname}:{ServerPort}{ServerPath}").ConfigureAwait(false);
            }

            Logger.Information("PLC Simulation started. Press CTRL-C to exit.");

            // wait for Ctrl-C
            quitEvent.WaitOne(Timeout.Infinite);
            PlcSimulation.Stop();
            shutdownTokenSource.Cancel();
        }
コード例 #2
0
        /// <summary>
        /// Run the server.
        /// </summary>
        /// <returns></returns>
        private static async Task ConsoleServerAsync(string[] args)
        {
            var quitEvent = new ManualResetEvent(false);
            CancellationTokenSource shutdownTokenSource = new CancellationTokenSource();

            ShutdownToken = shutdownTokenSource.Token;

            // init OPC configuration and tracing
            OpcApplicationConfiguration plcOpcApplicationConfiguration = new OpcApplicationConfiguration();
            ApplicationConfiguration    plcApplicationConfiguration    = await plcOpcApplicationConfiguration.ConfigureAsync();

            // allow canceling the connection process
            try
            {
                Console.CancelKeyPress += (sender, eArgs) =>
                {
                    quitEvent.Set();
                    eArgs.Cancel = true;
                };
            }
            catch
            {
            }

            // start the server.
            Logger.Information($"Starting server on endpoint {plcApplicationConfiguration.ServerConfiguration.BaseAddresses[0].ToString()} ...");
            Logger.Information($"Simulation settings are:");
            Logger.Information($"One simulation phase consists of {SimulationCycleCount} cycles");
            Logger.Information($"One cycle takes {SimulationCycleLength} milliseconds");
            Logger.Information($"Spike generation is {(GenerateSpikes ? "enabled" : "disabled")}");
            Logger.Information($"Data generation is {(GenerateData ? "enabled" : "disabled")}");

            PlcServer = new PlcServer();
            PlcServer.Start(plcApplicationConfiguration);
            Logger.Information("OPC UA Server started.");

            PlcSimulation = new PlcSimulation(PlcServer);
            PlcSimulation.Start();
            Logger.Information("PLC Simulation started. Press CTRL-C to exit.");

            // wait for Ctrl-C
            quitEvent.WaitOne(Timeout.Infinite);
            PlcSimulation.Stop();
            shutdownTokenSource.Cancel();
        }