Esempio n. 1
0
        /// <summary>
        /// Loads hub data values.
        /// </summary>
        /// <param name="strLightsIP"></param>
        /// <param name="strUser"></param>
        /// <param name="iBulbCount"></param>
        //private void Load(IPAddress strLightsIP, string strUser, int iBulbCount)
        //{
        //    m_strLightsIP = strLightsIP;
        //    m_strUser = strUser;

        //    List<int> listIDs = new List<int>();
        //    for (int i = 0; i < iBulbCount; ++i)
        //    {
        //        m_listLightStates.Add(new LightState());
        //        listIDs.Add(i);
        //    }

        //    // Add the all set. Don't call the standard method for all lights so there isn't any accidents if there are more lights than the program knows about.
        //    LightGroup group = new LightGroup(0, "All", listIDs.ToArray());
        //    m_listLightGroup.Add(group);

        //    ResetAllBulbs();

        //    m_updateTimer.Elapsed += Update;
        //    m_updateTimer.Start();
        //}

        /// <summary>
        /// Initializes the light states
        /// </summary>
        public void Init()
        {
            string response = MakeLightRequest("/api/" + m_strUser, "GET");

            JObject jobject = JObject.Parse(response);

            foreach (var topChild in jobject.Children())
            {
                if (topChild.Type == JTokenType.Property && ((JProperty)topChild).Name.Equals("lights"))
                {
                    if (topChild.Children().Count() != 1)
                    {
                        logger.Log("I don't understand the Hue Bridge response!. Quitting");
                        return;
                    }

                    var lightList = topChild.Children().First();

                    foreach (var light in lightList.Children())
                    {
                        LightState lstate = new LightState();

                        lstate.Index = int.Parse(((JProperty)light).Name);

                        if (light.Children().Count() != 1)
                        {
                            logger.Log("I don't understand the Hue Bridge response for light object!. Quitting");
                            return;
                        }

                        var lightDetails = light.Children().First();

                        lstate.Name = lightDetails["name"].ToString();

                        lstate.ParseState(lightDetails["state"]);

                        m_listLightStates.Add(lstate);
                    }
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Assignment operator.
 /// </summary>
 /// <param name="state"></param>
 /// <returns></returns>
 public LightState Copy(LightState state)
 {
     m_color = state.Color;
     m_bEnabled = state.Enabled;
     m_iPriorityLock = state.PriorityLock;
     return this;
 }
Esempio n. 3
0
        ///// <summary>
        ///// The demultiplexing routing for incoming operations
        ///// </summary>
        ///// <param name="message"></param>
        //public IList<VParamType> OnOperationInvoke(string roleName, String opName, IList<VParamType> args)
        //{
        //    switch (opName.ToLower())
        //    {
        //        case RoleHueBridge.OpToggleAll:
        //            {
        //                logger.Log("{0} Got {1}", this.ToString(), opName.ToLower());

        //                lightManager.ToggleAllLights();

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpTurnOffAll:
        //            {
        //                logger.Log("{0} Got {1}", this.ToString(), opName.ToLower());

        //                lightManager.TurnAllLightsOff();

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpTurnOnAll:
        //            {
        //                logger.Log("{0} Got {1}", this.ToString(), opName.ToLower());

        //                lightManager.TurnAllLightsOn();

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpResetAll:
        //            {
        //                logger.Log("{0} Got {1}", this.ToString(), opName.ToLower());

        //                lightManager.ResetAllBulbs();

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpUnlockAll:
        //            {
        //                logger.Log("{0} Got {1}", this.ToString(), opName.ToLower());

        //                lightManager.SetAllLockLevel(0);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpSetColorAll:
        //            {
        //                int iRed = (int)args[0].Value();
        //                int iBlue = (int)args[1].Value();
        //                int iGreen = (int)args[2].Value();

        //                logger.Log("{0} Got {1} {2} {3} {4}", this.ToString(), opName.ToLower(), iRed.ToString(), iBlue.ToString(), iGreen.ToString());

        //                System.Drawing.Color color = System.Drawing.Color.FromArgb(iRed, iBlue, iGreen);

        //                lightManager.SetGroupColor("All", color);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpSetBrightnessAll:
        //            {

        //                float fBrightness = ((float)((int)args[0].Value())) / 1000.0f;

        //                logger.Log("{0} Got {1} {2}", this.ToString(), opName.ToLower(), fBrightness.ToString());

        //                lightManager.SetGroupBrightness("All", fBrightness);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpToggleBulb:
        //            {

        //                int bulbID = (int)args[0].Value();
        //                int lockLevel = (int)args[1].Value();
        //                logger.Log("{0} Got {1} {2} {3}", this.ToString(), opName.ToLower(), bulbID.ToString(), lockLevel.ToString());


        //                lightManager.ToggleBulb(bulbID, lockLevel);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpTurnOffBulb:
        //            {
        //                int bulbID = (int)args[0].Value();
        //                int lockLevel = (int)args[1].Value();
        //                logger.Log("{0} Got {1} {2} {3}", this.ToString(), opName.ToLower(), bulbID.ToString(), lockLevel.ToString());

        //                lightManager.TurnOffBulb(bulbID, lockLevel);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpTurnOnBulb:
        //            {

        //                int bulbID = (int)args[0].Value();
        //                int lockLevel = (int)args[1].Value();
        //                logger.Log("{0} Got {1} {2} {3}", this.ToString(), opName.ToLower(), bulbID.ToString(), lockLevel.ToString());


        //                lightManager.TurnOnBulb(bulbID, lockLevel);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpResetBulb:
        //            {
        //                int bulbID = (int)args[0].Value();
        //                logger.Log("{0} Got {1} {2}", this.ToString(), opName.ToLower(), bulbID.ToString());


        //                lightManager.ResetBulb(bulbID);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpUnlockBulb:
        //            {
        //                int bulbID = (int)args[0].Value();
        //                logger.Log("{0} Got {1} {2}", this.ToString(), opName.ToLower(), bulbID.ToString());


        //                lightManager.SetSingleLockLevel(bulbID, 0);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpSetColorBulb:
        //            {
        //                int bulbID = (int)args[0].Value();
        //                int lockLevel = (int)args[1].Value();
        //                int iRed = (int)args[2].Value();
        //                int iBlue = (int)args[3].Value();
        //                int iGreen = (int)args[4].Value();

        //                logger.Log("{0} Got {1} {2} {3} {4} {5} {6}", this.ToString(), opName.ToLower(), bulbID.ToString(), lockLevel.ToString(), iRed.ToString(), iBlue.ToString(), iGreen.ToString());


        //                System.Drawing.Color color = System.Drawing.Color.FromArgb(iRed, iBlue, iGreen);
        //                lightManager.SetLightColor(bulbID, color, lockLevel);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpGetColorBulb:
        //            {
        //                int bulbID = (int)args[0].Value();
        //                logger.Log("{0} Got {1} {2}", this.ToString(), opName.ToLower(), bulbID.ToString());


        //                System.Drawing.Color color = lightManager.GetLightColor(bulbID);

        //                List<VParamType> retVals = new List<VParamType>();
        //                retVals.Add(new ParamType(true));
        //                retVals.Add(new ParamType(ParamType.SimpleType.integer, "red", (int)color.R));
        //                retVals.Add(new ParamType(ParamType.SimpleType.integer, "green", (int)color.G));
        //                retVals.Add(new ParamType(ParamType.SimpleType.integer, "blue", (int)color.B));

        //                return retVals;
        //            }

        //        case RoleHueBridge.OpSetBrightnessBulb:
        //            {
        //                int bulbID = (int)args[0].Value();
        //                int lockLevel = (int)args[1].Value();
        //                float fBrightness = ((float)((int)args[2].Value())) / 1000.0f;

        //                logger.Log("{0} Got {1} {2} {3}", this.ToString(), opName.ToLower(), bulbID.ToString(), lockLevel.ToString(), fBrightness.ToString());

        //                lightManager.SetLightBrightness(bulbID, fBrightness, lockLevel);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }
        //        case RoleHueBridge.OpBumpBulb:
        //            {
        //                int bulbID = (int)args[0].Value();

        //                logger.Log("{0} Got {1} {2}", this.ToString(), opName.ToLower(), bulbID.ToString());

        //                lightManager.BumpBulb(bulbID);

        //                List<VParamType> retVals = new List<VParamType>() { new ParamType(true) };

        //                return retVals;
        //            }
        //        default:
        //            logger.Log("Invalid operation: {0}", opName);
        //            return null;
        //    }
        //}

        /// <summary>
        /// The demultiplexing routing for incoming operations
        /// </summary>
        /// <param name="message"></param>
        public IList <VParamType> OnOperationInvoke(Port targetLightPort, string roleName, String opName, IList <VParamType> args)
        {
            logger.Log("{0} Got {1} {2}", this.ToString(), opName.ToLower(), targetLightPort.ToString());


            //send back an error if we don't know of this port
            if (!portToLightState.ContainsKey(targetLightPort))
            {
                logger.Log("Got request for unknown light!");

                return(new List <VParamType>()
                {
                    new ParamType(ParamType.SimpleType.error, "unknown port " + targetLightPort.GetInfo().GetFriendlyName())
                });
            }

            LightState lstate = portToLightState[targetLightPort];

            switch (opName.ToLower())
            {
            case RoleSwitchMultiLevel.OpSetName:
            {
                double valToSetDbl = (double)args[0].Value();

                if (valToSetDbl > 1)
                {
                    valToSetDbl = 1;
                }

                byte valToSet = (byte)(valToSetDbl * 255);

                byte currVal = lstate.Brightness;

                //if (currVal != valToSet)
                //{
                lightManager.SetLightBrightness(lstate, valToSet);

                Notify(targetLightPort, RoleSwitchMultiLevel.Instance, RoleSwitchMultiLevel.OpGetName, new ParamType(valToSetDbl));

                logger.Log("{0}: issued notification for light {1}, value {2}", ToString(), targetLightPort.ToString(), valToSetDbl.ToString());
                //}

                return(new List <VParamType>());
            }

            case RoleSwitchMultiLevel.OpGetName:
            {
                byte value = lstate.Brightness;

                IList <VParamType> retVals = new List <VParamType>()
                {
                    new ParamType((double)value / 255.0)
                };

                return(retVals);
            }

            case RoleLightColor.OpSetName:
            {
                byte  red, green, blue;
                Color color;

                try
                {
                    red   = (byte)(int)args[0].Value();
                    green = (byte)(int)args[1].Value();
                    blue  = (byte)(int)args[2].Value();

                    color = Color.FromArgb(red, green, blue);
                }
                catch (Exception ex)
                {
                    logger.Log("Bad parameters for {0}", opName, ex.ToString());
                    return(new List <VParamType>()
                        {
                            new ParamType(ParamType.SimpleType.error, "bad parameters for " + opName)
                        });
                }

                if (!color.Equals(lstate.Color))
                {
                    lightManager.SetLightColor(lstate, color);

                    Notify(targetLightPort, RoleLightColor.Instance, RoleLightColor.OpGetName,
                           new ParamType(red), new ParamType(green), new ParamType(blue));

                    logger.Log("{0}: issued color notification for light {1}, value {2}", ToString(), targetLightPort.ToString(), color.ToString());
                }

                return(new List <VParamType>());
            }

            case RoleLightColor.OpGetName:
            {
                IList <VParamType> retVals = new List <VParamType>()
                {
                    new ParamType(lstate.Color.R), new ParamType(lstate.Color.G), new ParamType(lstate.Color.B)
                };

                return(retVals);
            }

            default:
                logger.Log("Unknown operation {0} for role {1}", opName, roleName);
                return(new List <VParamType>()
                {
                    new ParamType(ParamType.SimpleType.unsupported, "unknown operation " + opName)
                });
            }
        }
        internal void SetLightColor(LightState lstate, Color color)
        {
            //change the value first
            lstate.Color = color;

            MakeLightRequest("/api/" + m_strUser + "/lights/" + (lstate.Index) + "/state", "PUT", lstate.ToJSON());
        }
        internal void SetLightBrightness(LightState lstate, byte bValue)
        {

            //change the value first
            lstate.Brightness = bValue;

            MakeLightRequest("/api/" + m_strUser + "/lights/" + (lstate.Index) + "/state", "PUT", lstate.ToJSON());
        }
        /// <summary>
        /// Loads hub data values.
        /// </summary>
        /// <param name="strLightsIP"></param>
        /// <param name="strUser"></param>
        /// <param name="iBulbCount"></param>
        //private void Load(IPAddress strLightsIP, string strUser, int iBulbCount)
        //{
        //    m_strLightsIP = strLightsIP;
        //    m_strUser = strUser;

        //    List<int> listIDs = new List<int>();
        //    for (int i = 0; i < iBulbCount; ++i)
        //    {
        //        m_listLightStates.Add(new LightState());
        //        listIDs.Add(i);
        //    }

        //    // Add the all set. Don't call the standard method for all lights so there isn't any accidents if there are more lights than the program knows about.
        //    LightGroup group = new LightGroup(0, "All", listIDs.ToArray());
        //    m_listLightGroup.Add(group);

        //    ResetAllBulbs();

        //    m_updateTimer.Elapsed += Update;
        //    m_updateTimer.Start();
        //}

        /// <summary>
        /// Initializes the light states
        /// </summary>
        public void Init()
        {

            string response = MakeLightRequest("/api/" + m_strUser, "GET");

            JObject jobject = JObject.Parse(response);

            foreach (var topChild in jobject.Children())
            {
                if (topChild.Type == JTokenType.Property && ((JProperty)topChild).Name.Equals("lights"))
                {

                    if (topChild.Children().Count() != 1)
                    {
                        logger.Log("I don't understand the Hue Bridge response!. Quitting");
                        return;
                    }

                    var lightList = topChild.Children().First();

                    foreach (var light in lightList.Children())
                    {
                        LightState lstate = new LightState();
                        
                        lstate.Index = int.Parse(((JProperty)light).Name);
                        
                        if (light.Children().Count() != 1)
                        {
                            logger.Log("I don't understand the Hue Bridge response for light object!. Quitting");
                            return;
                        }

                        var lightDetails = light.Children().First();

                        lstate.Name = lightDetails["name"].ToString();

                        lstate.ParseState(lightDetails["state"]);

                        m_listLightStates.Add(lstate);
                    }

                }
            }
        }