Inheritance: IControlToken
示例#1
0
        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();
                }
            };
        }
示例#2
0
 private void Release()
 {
     if (this.groupControlToken != null)
     {
         this.groupControlToken.Dispose();
         this.groupControlToken = null;
     }
 }
示例#3
0
        public IControlToken TakeControl(int priority = 1, [System.Runtime.CompilerServices.CallerMemberName] string name = "")
        {
            lock (this.members)
            {
                var memberTokens = new Dictionary <IOwnedDevice, IControlToken>();
                foreach (var device in this.members)
                {
                    memberTokens.Add(device, device.TakeControl(priority, name));
                }

                var ownerCandidate = new GroupControlToken(
                    memberTokens,
                    true,
                    cToken =>
                {
                    IControlToken nextOwner;

                    lock (this.members)
                    {
                        this.owners.Remove(cToken);

                        nextOwner = this.owners.LastOrDefault();

                        this.currentOwner = nextOwner;

                        Executor.Current.SetControlToken(this, nextOwner);
                    }
                },
                    priority);

                // Insert new owner
                lock (this)
                {
                    int pos = -1;
                    for (int i = 0; i < this.owners.Count; i++)
                    {
                        if (this.owners[i].Priority < priority)
                        {
                            continue;
                        }

                        pos = i;
                        break;
                    }
                    if (pos == -1)
                    {
                        this.owners.Add(ownerCandidate);
                    }
                    else
                    {
                        this.owners.Insert(pos, ownerCandidate);
                    }
                }

                // Grab the owner with the highest priority (doesn't have to be the candidate)
                var newOwner = this.owners.Last();

                this.currentOwner = newOwner;

                Executor.Current.SetControlToken(this, newOwner);

                return(ownerCandidate);
            }
        }
示例#4
0
        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);
            }

            this.groupControlToken = new GroupControlToken(heldLocks, disposeLocks: true, priority: this.lockPriority);
            this.groupControlToken.AutoAddDevices = this.autoAddDevices;
        }