Beispiel #1
0
        /// <summary>
        /// Gets a source from config list SourceListKey and dynamically build and executes the
        /// route or commands
        /// </summary>
        /// <param name="name"></param>
        public void RunRouteAction(string routeKey, Action successCallback)
        {
            // Run this on a separate thread
            new CTimer(o =>
            {
                Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Run route action '{0}'", routeKey);
                var dict = ConfigReader.ConfigObject.GetSourceListForKey(SourceListKey);
                if (dict == null)
                {
                    Debug.Console(1, this, "WARNING: Config source list '{0}' not found", SourceListKey);
                    return;
                }

                // Try to get the list item by it's string key
                if (!dict.ContainsKey(routeKey))
                {
                    Debug.Console(1, this, "WARNING: No item '{0}' found on config list '{1}'",
                                  routeKey, SourceListKey);
                    return;
                }

                var item = dict[routeKey];
                //Debug.Console(2, this, "Action {0} has {1} steps",
                //    item.SourceKey, item.RouteList.Count);

                // End usage timer on last source
                if (!string.IsNullOrEmpty(LastSourceKey))
                {
                    var lastSource = dict[LastSourceKey].SourceDevice;

                    try
                    {
                        if (lastSource != null && lastSource is IUsageTracking)
                        {
                            (lastSource as IUsageTracking).UsageTracker.EndDeviceUsage();
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.Console(1, this, "*#* EXCEPTION in end usage tracking (257):\r{0}", e);
                    }
                }

                // Let's run it
                if (routeKey.ToLower() != "roomoff")
                {
                    LastSourceKey = routeKey;
                }
                else
                {
                    CurrentSourceInfoKey = null;
                }

                foreach (var route in item.RouteList)
                {
                    // if there is a $defaultAll on route, run two separate
                    if (route.DestinationKey.Equals("$defaultAll", StringComparison.OrdinalIgnoreCase))
                    {
                        // Going to assume a single-path route for now
                        var tempVideo = new SourceRouteListItem
                        {
                            DestinationKey = "$defaultDisplay",
                            SourceKey      = route.SourceKey,
                            Type           = eRoutingSignalType.Video
                        };
                        DoRoute(tempVideo);

                        //var tempAudio = new SourceRouteListItem
                        //{
                        //    DestinationKey = "$defaultAudio",
                        //    SourceKey = route.SourceKey,
                        //    Type = eRoutingSignalType.Audio
                        //};
                        //DoRoute(tempAudio);
                        //continue; -- not sure why this was here
                    }
                    else
                    {
                        DoRoute(route);
                    }
                }

                // Start usage timer on routed source
                if (item.SourceDevice is IUsageTracking)
                {
                    (item.SourceDevice as IUsageTracking).UsageTracker.StartDeviceUsage();
                }



                // Set volume control, using default if non provided
                IBasicVolumeControls volDev = null;
                // Handle special cases for volume control
                if (string.IsNullOrEmpty(item.VolumeControlKey) ||
                    item.VolumeControlKey.Equals("$defaultAudio", StringComparison.OrdinalIgnoreCase))
                {
                    volDev = DefaultVolumeControls;
                }
                else if (item.VolumeControlKey.Equals("$defaultDisplay", StringComparison.OrdinalIgnoreCase))
                {
                    volDev = DefaultDisplay as IBasicVolumeControls;
                }
                // Or a specific device, probably rarely used.
                else
                {
                    var dev = DeviceManager.GetDeviceForKey(item.VolumeControlKey);
                    if (dev is IBasicVolumeControls)
                    {
                        volDev = dev as IBasicVolumeControls;
                    }
                    else if (dev is IHasVolumeDevice)
                    {
                        volDev = (dev as IHasVolumeDevice).VolumeDevice;
                    }
                }

                if (volDev != CurrentVolumeControls)
                {
                    // zero the volume on the device we are leaving.
                    // Set the volume to default on device we are entering
                    if (ZeroVolumeWhenSwtichingVolumeDevices && CurrentVolumeControls is IBasicVolumeWithFeedback)
                    {
                        var vd = CurrentVolumeControls as IBasicVolumeWithFeedback;
                        SavedVolumeLevels[vd] = (uint)vd.VolumeLevelFeedback.IntValue;
                        vd.SetVolume(0);
                    }
                    CurrentVolumeControls = volDev;
                    if (ZeroVolumeWhenSwtichingVolumeDevices && CurrentVolumeControls is IBasicVolumeWithFeedback)
                    {
                        var vd     = CurrentVolumeControls as IBasicVolumeWithFeedback;
                        ushort vol = (SavedVolumeLevels.ContainsKey(vd) ? (ushort)SavedVolumeLevels[vd] : DefaultVolume);
                        vd.SetVolume(vol);
                    }
                }



                // store the name and UI info for routes
                if (item.SourceKey == "$off")
                {
                    CurrentSourceInfoKey = routeKey;
                    CurrentSourceInfo    = null;
                }
                else if (item.SourceKey != null)
                {
                    CurrentSourceInfoKey = routeKey;
                    CurrentSourceInfo    = item;
                }
                // And finally, set the "control".  This will trigger event
                //CurrentControlDevice = DeviceManager.GetDeviceForKey(item.SourceKey) as Device;

                OnFeedback.FireUpdate();

                // report back when done
                if (successCallback != null)
                {
                    successCallback();
                }
            }, 0);                     // end of CTimer
        }
 public AudioChangeEventArgs(IBasicVolumeControls device, AudioChangeType changeType)
 {
     ChangeType  = changeType;
     AudioDevice = device;
 }
        /// <summary>
        /// Gets a source from config list SourceListKey and dynamically build and executes the
        /// route or commands
        /// </summary>
        /// <param name="name"></param>
        public void RunRouteAction(string routeKey, Action successCallback)
        {
            // Run this on a separate thread
            new CTimer(o =>
            {
                // try to prevent multiple simultaneous selections
                SourceSelectLock.TryEnter();

                try
                {
                    Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Run route action '{0}'", routeKey);
                    var dict = ConfigReader.ConfigObject.GetSourceListForKey(SourceListKey);
                    if (dict == null)
                    {
                        Debug.Console(1, this, "WARNING: Config source list '{0}' not found", SourceListKey);
                        return;
                    }

                    // Try to get the list item by it's string key
                    if (!dict.ContainsKey(routeKey))
                    {
                        Debug.Console(1, this, "WARNING: No item '{0}' found on config list '{1}'",
                                      routeKey, SourceListKey);
                        return;
                    }

                    // End usage timer on last source
                    if (!string.IsNullOrEmpty(LastSourceKey))
                    {
                        var usageLastSource = dict[LastSourceKey].SourceDevice as IUsageTracking;
                        if (usageLastSource != null && usageLastSource.UsageTracker != null)
                        {
                            try
                            {
                                // There MAY have been failures in here.  Protect
                                usageLastSource.UsageTracker.EndDeviceUsage();
                            }
                            catch (Exception e)
                            {
                                Debug.Console(1, this, "*#* EXCEPTION in end usage tracking:\r{0}", e);
                            }
                        }
                    }

                    // Let's run it
                    var item = dict[routeKey];
                    if (routeKey.ToLower() != "roomoff")
                    {
                        LastSourceKey = routeKey;
                    }
                    else
                    {
                        CurrentSourceInfoKey = null;
                    }

                    // hand off the individual routes to this helper
                    foreach (var route in item.RouteList)
                    {
                        DoRouteItem(route);
                    }

                    // Start usage timer on routed source
                    var usageNewSource = item.SourceDevice as IUsageTracking;
                    if (usageNewSource != null && usageNewSource.UsageTracker != null) // Have to make sure there is a usage tracker!
                    {
                        (item.SourceDevice as IUsageTracking).UsageTracker.StartDeviceUsage();
                    }

                    // See if this can be moved into common, base-class method -------------


                    // Set volume control, using default if non provided
                    IBasicVolumeControls volDev = null;
                    // Handle special cases for volume control
                    if (string.IsNullOrEmpty(item.VolumeControlKey) ||
                        item.VolumeControlKey.Equals("$defaultAudio", StringComparison.OrdinalIgnoreCase))
                    {
                        volDev = DefaultVolumeControls;
                    }
                    else if (item.VolumeControlKey.Equals("$defaultDisplay", StringComparison.OrdinalIgnoreCase))
                    {
                        volDev = DefaultDisplay as IBasicVolumeControls;
                    }
                    // Or a specific device, probably rarely used.
                    else
                    {
                        var dev = DeviceManager.GetDeviceForKey(item.VolumeControlKey);
                        if (dev is IBasicVolumeControls)
                        {
                            volDev = dev as IBasicVolumeControls;
                        }
                        else if (dev is IHasVolumeDevice)
                        {
                            volDev = (dev as IHasVolumeDevice).VolumeDevice;
                        }
                    }

                    if (volDev != CurrentVolumeControls)
                    {
                        // zero the volume on the device we are leaving.
                        // Set the volume to default on device we are entering
                        if (ZeroVolumeWhenSwtichingVolumeDevices && CurrentVolumeControls is IBasicVolumeWithFeedback)
                        {
                            var vd = CurrentVolumeControls as IBasicVolumeWithFeedback;
                            SavedVolumeLevels[vd] = (uint)vd.VolumeLevelFeedback.IntValue;
                            vd.SetVolume(0);
                        }

                        CurrentVolumeControls = volDev;
                        if (ZeroVolumeWhenSwtichingVolumeDevices && CurrentVolumeControls is IBasicVolumeWithFeedback)
                        {
                            var vd     = CurrentVolumeControls as IBasicVolumeWithFeedback;
                            ushort vol = (SavedVolumeLevels.ContainsKey(vd) ? (ushort)SavedVolumeLevels[vd] : DefaultVolume);
                            vd.SetVolume(vol);
                        }
                    }
                    // -----------------------------------------------------------------------



                    // store the name and UI info for routes
                    if (item.SourceKey == "$off")
                    {
                        CurrentSourceInfoKey = routeKey;
                        CurrentSourceInfo    = null;
                    }
                    else if (item.SourceKey != null)
                    {
                        CurrentSourceInfoKey = routeKey;
                        CurrentSourceInfo    = item;
                    }

                    OnFeedback.FireUpdate();

                    // report back when done
                    if (successCallback != null)
                    {
                        successCallback();
                    }
                }
                catch (Exception e)
                {
                    Debug.Console(1, this, "ERROR in routing: {0}", e);
                }

                SourceSelectLock.Leave();
            }, 0); // end of CTimer
        }
 public VolumeDeviceChangeEventArgs(IBasicVolumeControls oldDev, IBasicVolumeControls newDev, ChangeType type)
 {
     OldDev = oldDev;
     NewDev = newDev;
     Type   = type;
 }