using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Management; using System.Reflection; using System.ServiceProcess; using System.Threading; using InABox.Configuration; using InABox.Core; using InABox.Logging; using InABox.IPC; namespace PRSServer { public class PRSService : ServiceBase { private IEngine? _engine; private string _servicename; private Thread? _thread; //private CancellationTokenSource Source = new(); // TODO: Is this necessary? private App app; public PRSService(string serviceName, App app) { this.app = app; CanPauseAndContinue = false; CanShutdown = true; if (string.IsNullOrWhiteSpace(serviceName)) _servicename = GetServiceName(); else _servicename = serviceName; ServiceName = _servicename; } private string GetServiceName() { if (string.IsNullOrWhiteSpace(_servicename)) { var processId = Process.GetCurrentProcess().Id; var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service where ProcessId = " + processId); var collection = searcher.Get(); var services = collection.Cast(); _servicename = services.Any() ? (string)services.First()["Name"] : ""; Logger.Send(LogType.Information, "", string.Format("Service Name is [{0}]", _servicename)); } return _servicename; } public static LocalConfiguration GetConfiguration(string section = "") where T : ILocalConfigurationSettings, new() { // TODO : Remove the fallback location var configuration = new LocalConfiguration(CoreUtils.GetCommonAppData(), section); if (!File.Exists(configuration.GetFileName())) { var oldsettings = new LocalConfiguration( Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location), "" ); if (File.Exists(oldsettings.GetFileName())) { // Transition old data to new location var settings = oldsettings.LoadAll(); configuration.SaveAll(settings); File.Delete(oldsettings.GetFileName()); } } return configuration; } public static LocalConfiguration GetConfiguration(string section = "") => GetConfiguration(section); public void Run(string servicename) { _servicename = servicename; OnStart(new string[] { }); } public void Halt() { OnStop(); } protected override void OnStart(string[] args) { Logger.OnLog += MainLogger.Send; //InABox.Logging.NamedPipeLogger.Start(CoreUtils.GetPath()); var svcname = GetServiceName(); var settings = GetConfiguration(svcname).Load(); if (settings != null) { var server = settings.CreateServer(svcname); if (settings.Type == ServerType.Database) _engine = new DatabaseEngine(); else if (settings.Type == ServerType.GPS) _engine = new GPSEngine(); else if (settings.Type == ServerType.AutoDiscovery) _engine = new AutoDiscoveryEngine(); else if (settings.Type == ServerType.Schedule) _engine = new ScheduleEngine(); else if (settings.Type == ServerType.Web) _engine = new WebEngine(); else if (settings.Type == ServerType.Certificate) _engine = new CertificateEngine(); if (_engine != null) { _engine.ServiceName = svcname; _engine.Version = CoreUtils.GetVersion(); _engine.Configure(server); /*_task = Task.Run(() => { _engine.Run(); }, Source.Token);*/ _thread = new Thread(() => { _engine.Run(); }); _thread.IsBackground = true; _thread.Start(); } } } protected override void OnStop() { Logger.Send(LogType.Information, "", "Shutting down Service: " + _servicename); _engine?.Stop(); /*var timer = new Timer { Interval = 1000 }; timer.Elapsed += (o, e) => { try { Logger.Send(LogType.Information, "", "Terminating Process.."); MainLogger.Stop(); Environment.Exit(0); } catch (Exception err) { Logger.Send(LogType.Error, "", "Exception while killing process: " + err.Message); } }; timer.AutoReset = false; timer.Enabled = true;*/ Logger.Send(LogType.Information, "", "Terminating Process.."); MainLogger.Stop(); } } }