using Microsoft.Owin.Hosting; using Step.Config; using Step.Core; using Step.Database; using Step.Listeners; using Step.NC; using Step.UI; using Step.Utils; using System; using System.Net; using System.Net.Sockets; using System.Threading; 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); //Set the Mutef of GUID private static Mutex CmsStepMutex = new Mutex(true, "{689e76ce-b845-49f0-919c-c26491079fca}"); public static int MessageService { get; private set; } public static void Main() { LogInfo("Application started"); //Check if is already running an instance of this application if (!CmsStepMutex.WaitOne(TimeSpan.Zero, true)) ExceptionManager.Manage(ERROR_LEVEL.FATAL, "Only one istance of Step-Server can be executed!"); // Start WinForm ServerControlWindow.Start(); // Create unhandled exception handler AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler); // Read config ServerConfigController.ReadStartupConfig(); 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(); }); if(NcConfig.NcVendor.ToUpper() != NC_VENDOR.SIEMENS) { // Setup Osai/fanuc toolManager using (NcHandler handler = new NcHandler()) { handler.Connect(); handler.SetupNcToolManager(); } } // 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(); if (ServerStartupConfig.AutoOpenCmsClient) ServerControlWindow.StartCMSClient(null); // 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); } } }