public OberonDevice(OberonSettings settings) { _settings = settings ?? throw new ArgumentNullException(nameof(settings)); Id = _settings.Id; }
public void Run(CancellationToken cToken) { // Begin Oberon Activities _logger.LogInformation("Beginning Oberon Activities..."); // Build Oberon device list: try { foreach (var device in _appSettings.Devicelist.JunoDevices) { if (device.Id.StartsWith("Oberon")) { var settings = new OberonSettings() { Id = device.Id, Name = device.Name, SerialNumber = device.SerialNumber, ProvisionDate = device.ProvisionDate, IpAddress = device.IpAddress, Location = device.Location, TimeSettings = device.Settings }; _oberonDevices.Add(new OberonDevice(settings)); } } // Initialize the devices found: // We must wait for this to complete before proceeding InitDevicesAsync(cToken).Wait(); _logger.LogInformation("Device initialization Completed!"); _logger.LogInformation($"{_oberonDevices.Count} active Oberon devices(s) detected during initialization!"); var oberonTasks = new List <Task>(); // Launch ping routines for all the initialized devices: _oberonDevices.ForEach(d => { if (cToken.IsCancellationRequested) { return; } var pt = Task.Run(() => d.StartPingRoutineAsync(new Progress <DeviceProgress>(LogProgress), cToken)); _logger.LogInformation($"Ping routine for Oberon device :{d.Id} started!"); oberonTasks.Add(pt); Task.Delay(2000, cToken).Wait(); }); // Launch Monitor routines for all the initialized devices: _oberonDevices.ForEach(d => { if (cToken.IsCancellationRequested) { return; } var mt = Task.Run(() => d.StartMonitorRoutineAsync(RefreshSunsetTime, new Progress <DeviceProgress>(LogProgress), cToken)); _logger.LogInformation($"Monitor routine for Oberon device :{d.Id} started!"); oberonTasks.Add(mt); Task.Delay(2000, cToken).Wait(); }); Task.WaitAll(oberonTasks.ToArray()); } catch (Exception x) { _logger.LogError("Exception while running Oberon Tasks!"); _logger.LogError(x.Message); _logger.LogError(x.InnerException.Message); } }