using Iob.Net.UI; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using static SteamWare.Logger.Logging; using static SteamWare.Logger.Constants; using System.Threading; namespace Iob.Net { internal static class Program { #region Public Fields public static readonly ManualResetEvent StopRequest = new ManualResetEvent(false); #endregion Public Fields #region Public Methods 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); // Create directories if they don't exist CreateDirectories(); #if false // Read config ServerConfigController.ReadStartupConfig(); #endif // Start WinForm IobControlWindow.Start(); #if false bool databaseStatus = 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 (NcAdapter ncAdapter = new NcAdapter()) ncAdapter.Disconnect(); // Stop Threads ThreadsHandler.Close(); // Stop messageservice listeners ListenersHandler.Stop(); }); #endif #if false // Start server services if (!ValidateAddress(ServerStartupConfig.ServerAddress)) ExceptionManager.ManageError(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()); // read and save last CURRENT RECIPE data... NcFileAdapter.ReadLastRecipe(); RecipeController.WriteCurrentRecipeToPlc(); #endif #if false //starts threads using (WebApp.Start(opt)) { if (databaseStatus) { // Start Threads ThreadsHandler.Start(); // Start listeners ListenersHandler.Start(); } // Wait interrupt from client StopRequest.WaitOne(); using (NcAdapter ncAdapter = new NcAdapter()) ncAdapter.Disconnect(); LogInfo("Application closed"); } #endif // avvio i thread: lettura PLC, pubblicazione http su MP/IO, gestione task MP/IO, invio update UI // attendo chiusura // chiudo #if false // Stop Threads ThreadsHandler.Close(); // Stop messageservice listeners ListenersHandler.Stop(); // Close WinForm ServerControlWindow.Stop(); #endif } #endregion Public Methods #region Private Methods private static void CreateDirectories() { #if false if (!Directory.Exists(MAINTENANCE_ATTACHMENT_PATH)) Directory.CreateDirectory(MAINTENANCE_ATTACHMENT_PATH); if (!Directory.Exists(ALARM_ATTACHMENT_PATH)) Directory.CreateDirectory(ALARM_ATTACHMENT_PATH); if (!Directory.Exists(TEMP_PP_FOLDER)) Directory.CreateDirectory(TEMP_PP_FOLDER); if (!Directory.Exists(PART_PRG_IMAGES)) Directory.CreateDirectory(PART_PRG_IMAGES); if (!Directory.Exists(JOB_TMP_DIRECTORY)) Directory.CreateDirectory(JOB_TMP_DIRECTORY); if (!Directory.Exists(QUEUE_TMP_FOLDER)) Directory.CreateDirectory(QUEUE_TMP_FOLDER); if (!Directory.Exists(SCADA_DIRECTORY)) Directory.CreateDirectory(SCADA_DIRECTORY); #endif } private static void ExceptionHandler(object sender, UnhandledExceptionEventArgs args) { #if false using (NcAdapter ncAdapter = new NcAdapter()) { if (ncAdapter.numericalControl.NC_IsConnected()) { ncAdapter.Disconnect(); } } var exc = (Exception)args.ExceptionObject; ManageException(ERROR_LEVEL.FATAL, exc); #endif } private static bool ValidateAddress(string Addr) { //If is an asterisk is OK if (string.IsNullOrWhiteSpace(Addr) || Addr == "*") return true; #if false //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; } #endif return false; } #endregion Private Methods } }