using Microsoft.Owin.Hosting; using Step.Config; using Step.Core; using Step.Database; using Step.Listeners; using Step.Model; using Step.NC; using Step.UI; using Step.Utils; using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Windows.Forms; using TeamDev.SDK.MVVM; using static Step.Config.ServerConfig; using static Step.Model.Constants; using static Step.Utils.StepLogger; namespace Step { public class Application { public static readonly ManualResetEvent StopRequest = new ManualResetEvent(false); public static int MessageService { get; private set; } public static void Main() { LogInfo("Application started"); //Check if is already running an instance of this application string AppName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location); if (System.Diagnostics.Process.GetProcessesByName(AppName).Length > 1) { MessageBox.Show("Only one istance of "+ AppName +" can be executed!", AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Create unhandled exception handler AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler); // Read config ServerConfigController.ReadStartupConfig(); // Start WinForm ServerControlWindow.Start(); DatabaseContext.SetUpDbConnectionAndDbConfig(); // Register listener to "close application" messages MessageServices.Current.Subscribe(SEND_STOP_SERVER, (a, b) => { StopRequest.Set(); if (NcConfig.NcVendor.ToUpper() == NC_VENDOR.SIEMENS) ThreadSiemensHmi.StopThread(); }); // Stop threads and disconnect from NC MessageServices.Current.Subscribe(SEND_STOP_THREADS, (a, b) => { using (NcHandler ncHandler = new NcHandler()) ncHandler.Disconnect(); // Stop Threads ThreadsHandler.Close(); // Stop messageservice listeners ListenersHandler.Stop(); }); // Start server services if (!ValidateAddress(ServerStartupConfig.ServerAddress)) ExceptionManager.Manage(ERROR_LEVEL.FATAL, "IP Address not valid (must be one configured in Windows-OS)!"); StartOptions opt = new StartOptions(); opt.Urls.Add("http://localhost:" + ServerStartupConfig.ServerPort.ToString()); opt.Urls.Add("http://127.0.0.1:" + ServerStartupConfig.ServerPort.ToString()); if (!string.IsNullOrWhiteSpace(ServerStartupConfig.ServerAddress.ToString())) opt.Urls.Add("http://" + ServerStartupConfig.ServerAddress.ToString() + ":" + ServerStartupConfig.ServerPort.ToString()); using (WebApp.Start(opt)) { // Start Threads ThreadsHandler.Start(); // Start listeners ListenersHandler.Start(); // Wait interrupt from client StopRequest.WaitOne(); using (NcHandler ncHandler = new NcHandler()) ncHandler.Disconnect(); LogInfo("Application closed"); } // Stop Threads ThreadsHandler.Close(); // Stop messageservice listeners ListenersHandler.Stop(); // Close WinForm ServerControlWindow.Stop(); } private static bool ValidateAddress(string Addr) { //If is an asterisk is OK if (string.IsNullOrWhiteSpace(Addr) || Addr == "*") return true; //Find an IP Address foreach (IPAddress ipAddr in Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList, a => a.AddressFamily == AddressFamily.InterNetwork)) { if (ipAddr.ToString() == Addr) return true; } return false; } private static void ExceptionHandler(object sender, UnhandledExceptionEventArgs args) { using (NcHandler ncHandler = new NcHandler()) { if (ncHandler.numericalControl.NC_IsConnected()) { ncHandler.Disconnect(); } } LogException((Exception)args.ExceptionObject, ERROR_LEVEL.FATAL); } } }