protected virtual void LockDevices(params IOwnedDevice[] devices) { // Dispose if we already have a lock this.controlToken?.Dispose(); this.controlToken = new GroupControlToken(new List <IOwnedDevice>(devices), null, Name); }
private void Lock() { var heldLocks = new Dictionary <IOwnedDevice, IControlToken>(); foreach (var handleLock in this.handleLocks) { var control = handleLock.TakeControl(priority: this.lockPriority, name: Name); heldLocks.Add(handleLock, control); } if (this.externalControlToken == null) { this.groupControlToken = new GroupControlToken(heldLocks, disposeLocks: true, priority: this.lockPriority); this.groupControlToken.AutoAddDevices = this.autoAddDevices; } }
public IEffect Stop() { this.isRunning = false; this.timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite); foreach (var device in this.devices) { device.Observer = null; } if (this.token != null) { this.token.Dispose(); this.token = null; } return(this); }
public IEffect Start(int priority = 1, IControlToken token = null) { if (this.token == null) { IControlToken controlToken = token; if (controlToken == null) { this.token = new GroupControlToken(this.devices.Select(x => x.Device), null, Name, priority); controlToken = this.token; } foreach (var device in this.devices) { device.Observer = device.Device.GetDataObserver(controlToken); } } this.timer.Change(TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(-1)); this.isRunning = true; return(this); }
protected void PopulateTimeline() { foreach (var kvp in this.mappedDevices) { AddEffectData(kvp.Key, kvp.Value, ChannelEffectInstance.DeviceType.Brightness); } foreach (var kvp in this.mappedRgbDevices) { var id = kvp.Key; AddEffectData(id.R, kvp.Value, ChannelEffectInstance.DeviceType.ColorR); AddEffectData(id.G, kvp.Value, ChannelEffectInstance.DeviceType.ColorG); AddEffectData(id.B, kvp.Value, ChannelEffectInstance.DeviceType.ColorB); } timeline.Setup(() => { if (this.token == null) { this.token = new GroupControlToken(this.devices.Select(x => x.Device), null, this.name, this.priority); foreach (var device in this.devices) { device.Observer = device.Device.GetDataObserver(this.token); device.Observer.SetDataFromIData(device.AdditionalData); } } //foreach (var device in this.mappedDevices.SelectMany(x => x.Value)) //{ // if (!this.brightnessObservers.ContainsKey(device)) // { // var observer = device.GetBrightnessObserver(); // this.brightnessObservers.Add(device, observer); // } //} //foreach (var device in this.mappedRgbDevices.SelectMany(x => x.Value)) //{ // if (!this.rgbObservers.ContainsKey(device)) // { // var observer = device.GetRgbObserver(); // this.rgbObservers.Add(device, observer); // } //} }); timeline.TearDown(() => { foreach (var device in this.devices) { device.Observer = null; } if (this.token != null) { this.token.Dispose(); this.token = null; } //foreach (var controlledDevice in this.controlledDevices) // controlledDevice.TurnOff(); //// Release locks //foreach (var observer in this.brightnessObservers.Values) // observer.Dispose(); //this.brightnessObservers.Clear(); //foreach (var observer in this.rgbObservers.Values) // observer.Dispose(); //this.rgbObservers.Clear(); }); timeline.MultiTimelineTrigger += (sender, e) => { if ((e.ElapsedMs - this.lastProgressReport) > 1000) { // Update progress this.lastProgressReport = e.ElapsedMs; this.progress.OnNext(e.ElapsedMs); } foreach (var controlledDevice in this.controlledDevices) { controlledDevice.Suspend(); } try { foreach (var effectInstance in e.Code) { if (effectInstance.Type == ChannelEffectInstance.DeviceType.Brightness) { foreach (var device in effectInstance.Devices) { var receivesBrightness = device.Device as IReceivesBrightness; if (receivesBrightness != null) { effectInstance.Effect.Execute(receivesBrightness, this.token); } } } else if (effectInstance.Type == ChannelEffectInstance.DeviceType.ColorR) { foreach (var device in effectInstance.Devices) { var receivesColor = device.Device as IReceivesColor; if (receivesColor != null) { effectInstance.Effect.Execute(receivesColor, effectInstance.Type, this.token); } } } else if (effectInstance.Type == ChannelEffectInstance.DeviceType.ColorG) { foreach (var device in effectInstance.Devices) { var receivesColor = device.Device as IReceivesColor; if (receivesColor != null) { effectInstance.Effect.Execute(receivesColor, effectInstance.Type, this.token); } } } else if (effectInstance.Type == ChannelEffectInstance.DeviceType.ColorB) { foreach (var device in effectInstance.Devices) { var receivesColor = device.Device as IReceivesColor; if (receivesColor != null) { effectInstance.Effect.Execute(receivesColor, effectInstance.Type, this.token); } } } } } finally { foreach (var controlledDevice in this.controlledDevices) { controlledDevice.Resume(); } } }; }
public void Dispose() { this.controlToken?.Dispose(); this.controlToken = null; }
protected virtual void UnlockDevices() { this.controlToken?.Dispose(); this.controlToken = null; }
private void Release() { this.groupControlToken?.Dispose(); this.groupControlToken = null; }