Files
cms_thermo_active/Thermo.Active/program.cs
T
2021-02-01 14:33:49 +01:00

178 lines
6.2 KiB
C#

using Microsoft.Owin.Hosting;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using TeamDev.SDK.MVVM;
using Thermo.Active.Config;
using Thermo.Active.Controllers.WebApi;
using Thermo.Active.Core;
using Thermo.Active.Database;
using Thermo.Active.Listeners;
using Thermo.Active.NC;
using Thermo.Active.UI;
using Thermo.Active.Utils;
using static Thermo.Active.Config.ServerConfig;
using static Thermo.Active.Model.Constants;
using static Thermo.Active.Utils.ExceptionManager;
using static Thermo.Active.Utils.ThermoActiveLogger;
namespace Thermo.Active
{
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);
// Create directories if they don't exist
CreateDirectories();
// Read config
ServerConfigController.ReadStartupConfig();
// Start WinForm
ServerControlWindow.Start();
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();
});
// Start server services
if (!ValidateAddress(ServerStartupConfig.ServerAddress))
ExceptionManager.ManageError(ERROR_LEVEL.FATAL, "IP Address not valid (must be one configured in Windows-OS)!",true);
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());
if (Config.ServerConfig.ServerStartupConfig.CmsConnectReady)
{
ThreadsFunctions.SetupCmsConnect();
}
// read and save last CURRENT RECIPE data...
NcFileAdapter.ReadLastRecipe();
RecipeController.WriteCurrentRecipeToPlc();
//starts threads
using (WebApp.Start<App_Start.Startup>(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");
}
// 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 (NcAdapter ncAdapter = new NcAdapter())
{
if (ncAdapter.numericalControl.NC_IsConnected())
{
ncAdapter.Disconnect();
}
}
var exc = (Exception)args.ExceptionObject;
ManageException(ERROR_LEVEL.FATAL, exc);
}
private static void CreateDirectories()
{
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);
}
}
}