PRSService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Reflection;
  7. using System.ServiceProcess;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.Logging;
  13. using Timer = System.Timers.Timer;
  14. namespace PRSServer
  15. {
  16. public class PRSService : ServiceBase
  17. {
  18. private IEngine? _engine;
  19. private string _servicename;
  20. private Thread? _thread;
  21. //private CancellationTokenSource Source = new();
  22. // TODO: Is this necessary?
  23. private App app;
  24. public PRSService(string serviceName, App app)
  25. {
  26. this.app = app;
  27. CanPauseAndContinue = false;
  28. CanShutdown = true;
  29. if (string.IsNullOrWhiteSpace(serviceName))
  30. _servicename = GetServiceName();
  31. else
  32. _servicename = serviceName;
  33. ServiceName = _servicename;
  34. }
  35. private string GetServiceName()
  36. {
  37. if (string.IsNullOrWhiteSpace(_servicename))
  38. {
  39. var processId = Process.GetCurrentProcess().Id;
  40. var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service where ProcessId = " + processId);
  41. var collection = searcher.Get();
  42. var services = collection.Cast<ManagementBaseObject>();
  43. _servicename = services.Any() ? (string)services.First()["Name"] : "";
  44. Logger.Send(LogType.Information, "", string.Format("Service Name is [{0}]", _servicename));
  45. }
  46. return _servicename;
  47. }
  48. public static LocalConfiguration<T> GetConfiguration<T>(string section = "")
  49. where T : LocalConfigurationSettings, new()
  50. {
  51. // TODO : Remove the fallback location
  52. var configuration = new LocalConfiguration<T>(CoreUtils.GetCommonAppData(), section);
  53. if (!File.Exists(configuration.GetFileName()))
  54. {
  55. var oldsettings = new LocalConfiguration<T>(
  56. Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location),
  57. ""
  58. );
  59. if (File.Exists(oldsettings.GetFileName()))
  60. {
  61. // Transition old data to new location
  62. var settings = oldsettings.LoadAll();
  63. configuration.SaveAll(settings);
  64. File.Delete(oldsettings.GetFileName());
  65. }
  66. }
  67. return configuration;
  68. }
  69. public static LocalConfiguration<ServerSettings> GetConfiguration(string section = "") => GetConfiguration<ServerSettings>(section);
  70. public void Run(string servicename)
  71. {
  72. _servicename = servicename;
  73. OnStart(new string[] { });
  74. }
  75. public void Halt()
  76. {
  77. OnStop();
  78. }
  79. protected override void OnStart(string[] args)
  80. {
  81. Logger.OnLog += MainLogger.Send;
  82. //InABox.Logging.NamedPipeLogger.Start(CoreUtils.GetPath());
  83. var svcname = GetServiceName();
  84. var settings = GetConfiguration(svcname).Load();
  85. if (settings != null)
  86. {
  87. var server = settings.CreateServer(svcname);
  88. if (settings.Type == ServerType.Database)
  89. _engine = new DatabaseEngine();
  90. else if (settings.Type == ServerType.GPS)
  91. _engine = new GPSEngine();
  92. else if (settings.Type == ServerType.AutoDiscovery)
  93. _engine = new AutoDiscoveryEngine();
  94. else if (settings.Type == ServerType.Schedule)
  95. _engine = new ScheduleEngine();
  96. else if (settings.Type == ServerType.Web)
  97. _engine = new WebEngine();
  98. else if (settings.Type == ServerType.Certificate)
  99. _engine = new CertificateEngine();
  100. if (_engine != null)
  101. {
  102. _engine.ServiceName = svcname;
  103. _engine.Version = CoreUtils.GetVersion();
  104. _engine.Configure(server);
  105. /*_task = Task.Run(() =>
  106. {
  107. _engine.Run();
  108. }, Source.Token);*/
  109. _thread = new Thread(() => { _engine.Run(); });
  110. _thread.IsBackground = true;
  111. _thread.Start();
  112. }
  113. }
  114. }
  115. protected override void OnStop()
  116. {
  117. Logger.Send(LogType.Information, "", "Shutting down Service: " + _servicename);
  118. _engine?.Stop();
  119. /*var timer = new Timer { Interval = 1000 };
  120. timer.Elapsed += (o, e) =>
  121. {
  122. try
  123. {
  124. Logger.Send(LogType.Information, "", "Terminating Process..");
  125. MainLogger.Stop();
  126. Environment.Exit(0);
  127. }
  128. catch (Exception err)
  129. {
  130. Logger.Send(LogType.Error, "", "Exception while killing process: " + err.Message);
  131. }
  132. };
  133. timer.AutoReset = false;
  134. timer.Enabled = true;*/
  135. Logger.Send(LogType.Information, "", "Terminating Process..");
  136. MainLogger.Stop();
  137. }
  138. }
  139. }