PRSService.cs 5.4 KB

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