Пример #1
0
        /// <summary>
        /// Sets up device based on config values
        /// </summary>
        void SetUpDevice()
        {
            Room = DeviceManager.GetDeviceForKey(PropertiesConfig.RoomKey) as EssentialsRoomBase;

            if (Room != null)
            {
                try
                {
                    FeatureEnabledTime = DateTime.Parse(PropertiesConfig.OccupancyStartTime);

                    if (FeatureEnabledTime != null)
                    {
                        Debug.Console(1, this, "Enabled Time: {0}", FeatureEnabledTime.ToString());
                    }
                    else
                    {
                        Debug.Console(1, this, "Unable to parse {0} to DateTime", PropertiesConfig.OccupancyStartTime);
                    }
                }
                catch (Exception e)
                {
                    Debug.Console(1, this, "Unable to parse OccupancyStartTime property: {0} \n Error: {1}", PropertiesConfig.OccupancyStartTime, e);
                }

                try
                {
                    FeatureDisabledTime = DateTime.Parse(PropertiesConfig.OccupancyEndTime);

                    if (FeatureDisabledTime != null)
                    {
                        Debug.Console(1, this, "Disabled Time: {0}", FeatureDisabledTime.ToString());
                    }
                    else
                    {
                        Debug.Console(1, this, "Unable to parse {0} to DateTime", PropertiesConfig.OccupancyEndTime);
                    }
                }
                catch (Exception e)
                {
                    Debug.Console(1, this, "Unable to parse a DateTime config value \n Error: {1}", e);
                }

                if (!PropertiesConfig.EnableRoomOnWhenOccupied)
                {
                    FeatureEventGroup.ClearAllEvents();
                }
                else
                {
                    AddEnableEventToGroup();

                    AddDisableEventToGroup();

                    FeatureEventGroup.UserGroupCallBack += new ScheduledEventGroup.UserEventGroupCallBack(FeatureEventGroup_UserGroupCallBack);

                    FeatureEventGroup.EnableAllEvents();
                }

                FeatureEnabled = CheckIfFeatureShouldBeEnabled();
            }
            else
            {
                Debug.Console(1, this, "Unable to get room from Device Manager with key: {0}", PropertiesConfig.RoomKey);
            }
        }
Пример #2
0
        /// <summary>
        ///  Walks down a dotted object path, starting with a Device, and returns the object
        ///  at the end of the path
        /// </summary>
        public static object FindObjectOnPath(string deviceObjectPath)
        {
            var path = deviceObjectPath.Split('.');

            var dev = DeviceManager.GetDeviceForKey(path[0]);

            if (dev == null)
            {
                Debug.Console(0, "Device {0} not found", path[0]);
                return(null);
            }

            // loop through any dotted properties
            object obj = dev;

            if (path.Length > 1)
            {
                for (int i = 1; i < path.Length; i++)
                {
                    var    objName   = path[i];
                    string indexStr  = null;
                    var    indexOpen = objName.IndexOf('[');
                    if (indexOpen != -1)
                    {
                        var indexClose = objName.IndexOf(']');
                        if (indexClose == -1)
                        {
                            Debug.Console(0, dev, "ERROR Unmatched index brackets");
                            return(null);
                        }
                        // Get the index and strip quotes if any
                        indexStr = objName.Substring(indexOpen + 1, indexClose - indexOpen - 1).Replace("\"", "");
                        objName  = objName.Substring(0, indexOpen);
                        Debug.Console(0, dev, "  Checking for collection '{0}', index '{1}'", objName, indexStr);
                    }

                    CType oType = obj.GetType();
                    var   prop  = oType.GetProperty(objName);
                    if (prop == null)
                    {
                        Debug.Console(0, dev, "Property {0} not found on {1}", objName, path[i - 1]);
                        return(null);
                    }
                    // if there's an index, try to get the property
                    if (indexStr != null)
                    {
                        if (!typeof(ICollection).IsAssignableFrom(prop.PropertyType))
                        {
                            Debug.Console(0, dev, "Property {0} is not collection", objName);
                            return(null);
                        }
                        var collection = prop.GetValue(obj, null) as ICollection;
                        // Get the indexed items "property"
                        var indexedPropInfo = prop.PropertyType.GetProperty("Item");
                        // These are the parameters for the indexing. Only care about one
                        var indexParams = indexedPropInfo.GetIndexParameters();
                        if (indexParams.Length > 0)
                        {
                            Debug.Console(0, "  Indexed, param type: {0}", indexParams[0].ParameterType.Name);
                            var properParam = Convert.ChangeType(indexStr, indexParams[0].ParameterType,
                                                                 System.Globalization.CultureInfo.InvariantCulture);
                            try
                            {
                                obj = indexedPropInfo.GetValue(collection, new object[] { properParam });
                            }
                            // if the index is bad, catch it here.
                            catch (Crestron.SimplSharp.Reflection.TargetInvocationException e)
                            {
                                if (e.InnerException is ArgumentOutOfRangeException)
                                {
                                    Debug.Console(0, "  Index Out of range");
                                }
                                else if (e.InnerException is KeyNotFoundException)
                                {
                                    Debug.Console(0, "  Key not found");
                                }
                                return(null);
                            }
                        }
                    }
                    else
                    {
                        obj = prop.GetValue(obj, null);
                    }
                }
            }
            return(obj);
        }