|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Reactive.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Ares.Core.Analyzing; |
| 7 | +using Ares.Core.Device; |
| 8 | +using Ares.Core.Device.Remote; |
| 9 | +using Ares.Core.Grpc; |
| 10 | +using Ares.Core.Planning; |
| 11 | +using Ares.Services; |
| 12 | +using AresService.Data; |
| 13 | +using AresService.DeviceDbLoaders; |
| 14 | +using Microsoft.EntityFrameworkCore; |
| 15 | +using Microsoft.Extensions.Configuration; |
| 16 | + |
| 17 | +namespace AresService; |
| 18 | + |
| 19 | +public class AresStarter |
| 20 | +{ |
| 21 | + private readonly IRemoteAnalyzerManager _analyzerManager; |
| 22 | + private readonly IDbContextFactory<AresDbContext> _dbContextFactory; |
| 23 | + private readonly IDeviceCommandInterpreterRepo _deviceCommandInterpreterRepo; |
| 24 | + private readonly IEnumerable<IDeviceDbLoader> _deviceLoaders; |
| 25 | + private readonly IRemotePlannerManager _plannerManager; |
| 26 | + private readonly IConfiguration _configuration; |
| 27 | + private readonly IRemoteDeviceManager _remoteDeviceManager; |
| 28 | + private readonly string _dataPath; |
| 29 | + private readonly string _resultsPath; |
| 30 | + private readonly string _templatesPath; |
| 31 | + private readonly string _devicesPath; |
| 32 | + |
| 33 | + public AresStarter( |
| 34 | + IDeviceCommandInterpreterRepo deviceCommandInterpreterRepo, |
| 35 | + IDbContextFactory<AresDbContext> dbContextFactory, |
| 36 | + IRemotePlannerManager plannerManager, |
| 37 | + IRemoteAnalyzerManager analyzerManager, |
| 38 | + IEnumerable<IDeviceDbLoader> deviceLoaders, |
| 39 | + IConfiguration configuration, |
| 40 | + IRemoteDeviceManager remoteDeviceManager) |
| 41 | + { |
| 42 | + _deviceCommandInterpreterRepo = deviceCommandInterpreterRepo; |
| 43 | + _dbContextFactory = dbContextFactory; |
| 44 | + _plannerManager = plannerManager; |
| 45 | + _analyzerManager = analyzerManager; |
| 46 | + _deviceLoaders = deviceLoaders; |
| 47 | + _configuration = configuration; |
| 48 | + _remoteDeviceManager = remoteDeviceManager; |
| 49 | + _dataPath = _configuration.Get<AppSettings>()?.AresDataPath ?? ""; |
| 50 | + _resultsPath = Path.Combine(_dataPath, AppSettings.ResultsFolder); |
| 51 | + _templatesPath = Path.Combine(_dataPath, AppSettings.TemplatesFolder); |
| 52 | + _devicesPath = Path.Combine(_dataPath, AppSettings.DevicesFolder); |
| 53 | + } |
| 54 | + |
| 55 | + public async Task Start() |
| 56 | + { |
| 57 | + await EnsureDataPathsExist(); |
| 58 | + |
| 59 | + foreach(var deviceLoader in _deviceLoaders) |
| 60 | + await deviceLoader.Load(); |
| 61 | + |
| 62 | + await _plannerManager.LoadPlanners(); |
| 63 | + await _analyzerManager.LoadAnalyzers(); |
| 64 | + await _remoteDeviceManager.LoadDevices(); |
| 65 | + |
| 66 | + Observable.Interval(TimeSpan.FromSeconds(20)) |
| 67 | + .Take(1) |
| 68 | + .Subscribe(_ => ServerStatusHelper.ServerStatusSubject.OnNext(new ServerStatusResponse { ServerStatus = ServerStatus.Error, StatusMessage = "This is a test error from server." })); |
| 69 | + } |
| 70 | + |
| 71 | + public Task EnsureDataPathsExist() |
| 72 | + { |
| 73 | + Directory.CreateDirectory(_devicesPath); |
| 74 | + Directory.CreateDirectory(_resultsPath); |
| 75 | + Directory.CreateDirectory(_templatesPath); |
| 76 | + |
| 77 | + return Task.CompletedTask; |
| 78 | + } |
| 79 | +} |
0 commit comments