HostLifetime.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. namespace MinicsConsole;
  2. class HostLifetime(
  3. HardwareFileLoader hardwareFileLoader,
  4. HardwareDBLoader hardwareDBLoader,
  5. ConfigFileLoader configFileLoader,
  6. AddressFileLoader addressFileLoader,
  7. HardWareMonitor hardWareMonitor,
  8. ConfigUpdater configUpdater,
  9. BasicInfo basicInfo,
  10. OrmCollections ormCollections,
  11. DataBaseCleaner dataBaseCleaner,
  12. RTNotifier rtNotifer,
  13. PLCNotifier plcNotifier,
  14. DailyRoutinHelper dailyRoutinHelper,
  15. ITlvProvider tlvProvider,
  16. ILog log) : IHostedService
  17. {
  18. //Do all Module Initializations here
  19. async Task IHostedService.StartAsync(CancellationToken cancellationToken)
  20. {
  21. log.Initialize("General");
  22. //Connect to DataBase Server
  23. ormCollections.MainORM = new SqlSugarCustom();
  24. if (string.IsNullOrEmpty(basicInfo.DBConnectionString) ||
  25. !ormCollections.MainORM.Initialize() ||
  26. !ormCollections.MainORM.Open(basicInfo.DBConnectionString, DbType.PostgreSQL))
  27. {
  28. log.Fatal($"Connect DB Failed");
  29. Environment.Exit(0);
  30. }
  31. if (!hardwareDBLoader.Load())
  32. {
  33. hardwareFileLoader.Load();
  34. hardwareDBLoader.Save();
  35. }
  36. configFileLoader.Load();
  37. addressFileLoader.Load();
  38. addressFileLoader.LoadPLC();
  39. //Perparing for data receiver
  40. if (!plcNotifier.StartService())
  41. {
  42. log.Fatal($"PLC configFile Not Exist");
  43. Environment.Exit(0);
  44. }
  45. //Start RT Server
  46. if (string.IsNullOrEmpty(basicInfo.RTServerAddress) ||
  47. !rtNotifer.Initialize(tlvProvider) ||
  48. !rtNotifer.Open(basicInfo.RTServerAddress, basicInfo.RTServerPort))
  49. {
  50. log.Fatal($"Open RT Server {basicInfo.RTServerAddress}:{basicInfo.RTServerPort} Failed");
  51. Environment.Exit(0);
  52. }
  53. log.Info($"Start RT Server {basicInfo.RTServerAddress}:{basicInfo.RTServerPort} success");
  54. //Connect to Mini8s
  55. if (!hardWareMonitor.ParallelCreateConnections())
  56. log.Warning($"Mini8 Addresses error Unable to connect to mini8s");
  57. //Read Realtime data from mini8 & Read ChannelMode from hareware configruation
  58. if (configUpdater.ReadConfigFromMini8(out TemperatureConfig? temperatureConfig) && temperatureConfig is not null)
  59. configUpdater.SetConfigFile(temperatureConfig, false, out _);
  60. //Start Collecting Mini8s Realtime Data
  61. hardWareMonitor.ParallelStartDataCollecion();
  62. dailyRoutinHelper.AddorUpdateRoutinWork("CleanDataBase", dataBaseCleaner.CleanDB);
  63. dailyRoutinHelper.AddorUpdateRoutinWork("CleanLogFile", ((LogSender)log).CleanLog);
  64. dailyRoutinHelper.StartService(TimeSpan.FromDays(1));
  65. await Task.CompletedTask;
  66. }
  67. private readonly AutoResetEvent _AutoResetEvent = new(true);
  68. async Task IHostedService.StopAsync(CancellationToken cancellationToken)
  69. {
  70. if (!_AutoResetEvent.WaitOne(0))
  71. await Task.CompletedTask;
  72. log.Info("Minics Console has been shutted down by UI request");
  73. //log.Info("Saving setting files..");
  74. //hardwareFileLoader.Save();
  75. //configFileLoader.Save();
  76. //addressFileLoader.Save();
  77. //BaseConfigFileLoader.Save(basicInfo);
  78. log.Info("Closing all mini8 connections..");
  79. hardWareMonitor.CloseConnections();
  80. log.Info("Closing RT server connections..");
  81. rtNotifer?.Dispose();
  82. log.Info("Closing PLC connections..");
  83. plcNotifier?.Dispose();
  84. log.Info("Closing Database connections..");
  85. ormCollections?.MainORM?.Dispose();
  86. dailyRoutinHelper?.Dispose();
  87. log.Info("Finish Stopping..");
  88. log.Dispose();
  89. await Task.CompletedTask;
  90. }
  91. }