PRSService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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<ServerSettings> GetConfiguration(string section = "")
  49. {
  50. // TODO : Remove the fallback location
  51. var configuration = new LocalConfiguration<ServerSettings>(CoreUtils.GetCommonAppData(), section);
  52. if (!File.Exists(configuration.GetFileName()))
  53. {
  54. var oldsettings = new LocalConfiguration<ServerSettings>(
  55. Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location),
  56. ""
  57. );
  58. if (File.Exists(oldsettings.GetFileName()))
  59. {
  60. // Transition old data to new location
  61. var settings = oldsettings.LoadAll();
  62. configuration.SaveAll(settings);
  63. File.Delete(oldsettings.GetFileName());
  64. }
  65. }
  66. return configuration;
  67. }
  68. public void Run(string servicename)
  69. {
  70. _servicename = servicename;
  71. OnStart(new string[] { });
  72. }
  73. public void Halt()
  74. {
  75. OnStop();
  76. }
  77. protected override void OnStart(string[] args)
  78. {
  79. Logger.OnLog += MainLogger.Send;
  80. //InABox.Logging.NamedPipeLogger.Start(CoreUtils.GetPath());
  81. var svcname = GetServiceName();
  82. var settings = GetConfiguration(svcname).Load();
  83. if (settings != null)
  84. {
  85. var server = settings.CreateServer(svcname);
  86. if (settings.Type == ServerType.Database)
  87. _engine = new DatabaseEngine();
  88. else if (settings.Type == ServerType.GPS)
  89. _engine = new GPSEngine();
  90. else if (settings.Type == ServerType.AutoDiscovery)
  91. _engine = new AutoDiscoveryEngine();
  92. else if (settings.Type == ServerType.Schedule)
  93. _engine = new ScheduleEngine();
  94. else if (settings.Type == ServerType.Web)
  95. _engine = new WebEngine();
  96. else if (settings.Type == ServerType.Certificate)
  97. _engine = new CertificateEngine();
  98. if (_engine != null)
  99. {
  100. _engine.ServiceName = svcname;
  101. _engine.Version = CoreUtils.GetVersion();
  102. _engine.Configure(server);
  103. /*_task = Task.Run(() =>
  104. {
  105. _engine.Run();
  106. }, Source.Token);*/
  107. _thread = new Thread(() => { _engine.Run(); });
  108. _thread.IsBackground = true;
  109. _thread.Start();
  110. }
  111. }
  112. }
  113. protected override void OnStop()
  114. {
  115. Logger.Send(LogType.Information, "", "Shutting down Service: " + _servicename);
  116. _engine?.Stop();
  117. /*var timer = new Timer { Interval = 1000 };
  118. timer.Elapsed += (o, e) =>
  119. {
  120. try
  121. {
  122. Logger.Send(LogType.Information, "", "Terminating Process..");
  123. MainLogger.Stop();
  124. Environment.Exit(0);
  125. }
  126. catch (Exception err)
  127. {
  128. Logger.Send(LogType.Error, "", "Exception while killing process: " + err.Message);
  129. }
  130. };
  131. timer.AutoReset = false;
  132. timer.Enabled = true;*/
  133. Logger.Send(LogType.Information, "", "Terminating Process..");
  134. MainLogger.Stop();
  135. }
  136. }
  137. }