HostLifetime.cs 4.0 KB

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