HostLifetime.cs 4.1 KB

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