public ColorService(ControlService controlService) { controlService.ColorService = this; _watch = new Stopwatch(); _streamTokenSource = new CancellationTokenSource(); _targetTokenSource = new CancellationTokenSource(); _sDevices = Array.Empty <IColorTarget>(); _systemData = DataUtil.GetSystemData(); LedColors = new Color[_systemData.LedCount]; SectorColors = new Color[+_systemData.SectorCount]; _enableAutoDisable = _systemData.EnableAutoDisable; _streams = new Dictionary <string, ColorSource>(); ControlService = controlService; Counter = new FrameCounter(this); ControlService.SetModeEvent += Mode; ControlService.DeviceReloadEvent += RefreshDeviceData; ControlService.RefreshLedEvent += ReloadLedData; ControlService.RefreshSystemEvent += ReloadSystemData; ControlService.TestLedEvent += LedTest; ControlService.FlashDeviceEvent += FlashDevice; ControlService.FlashSectorEvent += FlashSector; ControlService.DemoLedEvent += Demo; _splitter = new FrameSplitter(this); LoadServices(); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await Initialize(); Log.Debug("Starting main Color Service loop..."); await Task.Run(async() => { var cTask = ControlService.Execute(stoppingToken); var loopWatch = new Stopwatch(); loopWatch.Start(); while (!stoppingToken.IsCancellationRequested) { await CheckAutoDisable().ConfigureAwait(false); // Save a frame every 5 seconds if (loopWatch.Elapsed >= TimeSpan.FromSeconds(5)) { FrameSaveEvent.Invoke(); loopWatch.Restart(); } if (!ColorsUpdated) { continue; } if (!_demoComplete || _stream == null) { return; } Counter.Tick(""); ColorsUpdated = false; await SendColors(LedColors, SectorColors); } if (cTask.IsCompleted) { Log.Debug("CTask canceled."); } loopWatch.Stop(); Log.Information("Send loop canceled."); _streamTask?.Dispose(); DataUtil.Dispose(); }, CancellationToken.None); }
public DiscoveryService(ControlService cs) { cs.DeviceRescanEvent += TriggerRefresh; cs.SetModeEvent += UpdateMode; cs.RefreshSystemEvent += RefreshSystem; _discoveryInterval = DataUtil.GetItem <int>("AutoDiscoveryFrequency"); if (_discoveryInterval < 15) { _discoveryInterval = 15; } var classnames = SystemUtil.GetClasses <IColorDiscovery>(); _discoverables = new List <IColorDiscovery>(); _syncSource = new CancellationTokenSource(); foreach (var dev in classnames.Select(c => Activator.CreateInstance(Type.GetType(c) !, cs.ColorService)) .Where(obj => obj != null).Cast <IColorDiscovery?>()) { if (dev != null) { _discoverables.Add(dev); } } }
public StatService(IHubContext <SocketServer> hubContext, ControlService cs) { _hubContext = hubContext; _colorService = cs.ColorService; cs.SetModeEvent += Mode; }
private async Task CheckAutoDisable() { // Don't do anything if auto-disable isn't enabled if (!_enableAutoDisable) { if (!_autoDisabled) { return; } _autoDisabled = false; DataUtil.SetItem("AutoDisabled", _autoDisabled); return; } var sourceActive = _stream?.SourceActive ?? false; if (sourceActive) { // If our source is active and we're auto-disabled, turn it off. if (_autoDisabled) { Log.Information("Auto-enabling stream."); _autoDisabled = false; DataUtil.SetItem("AutoDisabled", _autoDisabled); ControlService.SetModeEvent -= Mode; _deviceMode = _systemData.PreviousMode; await ControlService.SetMode(_deviceMode); await StartStream(); ControlService.SetModeEvent += Mode; } _watch.Reset(); } else { if (_autoDisabled) { return; } if (!_watch.IsRunning) { _watch.Restart(); } if (_watch.ElapsedMilliseconds >= _autoDisableDelay * 1000f) { _autoDisabled = true; Counter.Reset(); DataUtil.SetItem("AutoDisabled", _autoDisabled); ControlService.SetModeEvent -= Mode; await ControlService.SetMode(DeviceMode.Off, true); ControlService.SetModeEvent += Mode; await SendColors(ColorUtil.EmptyColors(_systemData.LedCount), ColorUtil.EmptyColors(_systemData.SectorCount), 0, true); Log.Information( $"Auto-disabling stream {_watch.ElapsedMilliseconds} vs {_autoDisableDelay * 1000}."); _watch.Reset(); await StopStream(); } } }