/// <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(); }
/// <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(); }
/// <summary> /// Ctor for simulation server. /// </summary> public PlcSimulation(PlcServer plcServer) { _plcServer = plcServer; _random = new Random(); _cyclesInPhase = SimulationCycleCount; _spikeCycleInPhase = SimulationCycleCount; _spikeAnomalyCycle = _random.Next(SimulationCycleCount); Logger.Verbose($"first spike anomaly cycle: {_spikeAnomalyCycle}"); _dipCycleInPhase = SimulationCycleCount; _dipAnomalyCycle = _random.Next(SimulationCycleCount); Logger.Verbose($"first dip anomaly cycle: {_dipAnomalyCycle}"); _posTrendAnomalyPhase = _random.Next(10); _posTrendCycleInPhase = SimulationCycleCount; Logger.Verbose($"first pos trend anomaly phase: {_posTrendAnomalyPhase}"); _negTrendAnomalyPhase = _random.Next(10); _negTrendCycleInPhase = SimulationCycleCount; Logger.Verbose($"first neg trend anomaly phase: {_negTrendAnomalyPhase}"); }