Exemple #1
0
        /// <summary>
        /// Set the parameters of a light in a scene.
        /// </summary>
        /// <param name="sceneName">The name of the scene.</param>
        /// <param name="lightId">The light ID.</param>
        /// <param name="newState">The desired state of the light.</param>
        /// <returns>A list of MessageCollection from the bridge.</returns>
        public MessageCollection SetSceneLightState(string sceneName, string lightId, State newState)
        {
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/scenes/" + sceneName + "/lightstates/" + lightId), WebRequestType.PUT, Serializer.SerializeToJson(newState));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                lastMessages = lstmsg == null ? new MessageCollection {
                    new UnkownError(comres)
                } : new MessageCollection(lstmsg);
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(lastMessages);
        }
Exemple #2
0
        /// <summary>
        /// Try to contact the specified bridge to get the basic config.
        /// </summary>
        /// <param name="BridgeIP">IP Address of the bridge.</param>
        /// <returns>The basic configuration of the bridge.</returns>
        public static BasicConfig GetBridgeBasicConfig(IPAddress BridgeIP)
        {
            BasicConfig config = new BasicConfig();

            CommResult comres = Communication.SendRequest(new Uri($@"http://{BridgeIP}/api/config"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                config = Serializer.DeserializeToObject <BasicConfig>(comres.data);
                if (config != null)
                {
                    return(config);
                }
                config = new BasicConfig();
                break;

            case WebExceptionStatus.Timeout:
                break;

            default:
                break;
            }

            return(config);
        }
Exemple #3
0
        /// <summary>
        /// Set the state object of a light.
        /// </summary>
        /// <param name="ID">ID of the light to change state.</param>
        /// <param name="newState">The new state of the light to change.</param>
        /// <returns>A list of MessageCollection from the bridge.</returns>
        public HueResult SetLightState(string ID, State newState)
        {
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/lights/" + ID + "/state"), WebRequestType.PUT, Serializer.SerializeToJson <State>(newState));
            HueResult  result = new HueResult();

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                lastMessages = lstmsg == null ? new MessageCollection {
                    new UnkownError(comres)
                } : new MessageCollection(lstmsg);
                result.Success = true;
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }
            result.messages = lastMessages;
            return(result);
        }
Exemple #4
0
        /// <summary>
        /// Set the flag of a sensor to the desired value.
        /// </summary>
        /// <param name="id">Id of the sensor.</param>
        /// <param name="config">Config of the sensor</param>
        /// <returns></returns>
        public MessageCollection SetSensorFlag(string id, SensorState config)
        {
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/sensors/" + id.ToString() + "/state"), WebRequestType.PUT, Serializer.SerializeToJson <SensorState>(config));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(lastMessages);
        }
Exemple #5
0
        /// <summary>
        /// Change an existing Group.
        /// </summary>
        /// <param name="newgroup">Old group with new properties.</param>
        /// <returns>A collection of messages.</returns>
        public MessageCollection ChangeGroup(Group newgroup)
        {
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/groups/" + newgroup.Id), WebRequestType.PUT, Serializer.SerializeToJson <Group>(newgroup));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(lastMessages);
        }
Exemple #6
0
        /// <summary>
        /// Rename a sensor.
        /// </summary>
        /// <param name="id">ID of the sensor to rename.</param>
        /// <param name="sensor">The new value of the sensor.</param>
        /// <returns>True or False the sensor has been renamed.</returns>
        public bool UpdateSensor(string id, Sensor sensor)
        {
            bool result = false;

            sensor.state.lastupdated = null;
            sensor.name             = null;
            sensor.modelid          = null;
            sensor.type             = null;
            sensor.manufacturername = null;
            sensor.swversion        = null;
            sensor.uniqueid         = null;
            if (sensor.config != null)
            {
                sensor.config.on        = null;
                sensor.config.reachable = null;
            }

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/sensors/" + id.ToString()), WebRequestType.PUT, Serializer.SerializeToJson <Sensor>(sensor));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.FailureCount == 0)
                    {
                        result = true;
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(result);
        }
Exemple #7
0
        /// <summary>
        /// Check if there is an update for the bridge.
        /// </summary>
        /// <returns>Software Update or null.</returns>
        public bool GetSwUpdate()
        {
            SwUpdate swu = new SwUpdate();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                if (!comres.data.Contains("\"error\""))
                {
                    Match mt = Regex.Match(comres.data, "\"swupdate\":{(.*?)}");
                    swu = Serializer.DeserializeToObject <SwUpdate>(mt.Value.Remove(0, 11) + "}");
                    return(swu.updatestate == 2);
                }
                else
                {
                    List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                    if (lstmsg == null)
                    {
                        goto default;
                    }
                    else
                    {
                        lastMessages = new MessageCollection(lstmsg);
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(false);
        }
Exemple #8
0
        /// <summary>
        /// Store current light state in the selected scene.
        /// </summary>
        /// <param name="id">ID of the scene to change the name.</param>
        /// <param name="newlightsList">New List of lights</param>
        /// <returns>the id of the modified scene or null</returns>
        public string SetScene(string id)
        {
            string     modifiedid = "";
            CommResult comres     = Communication.SendRequest(new Uri(BridgeUrl + "/scenes/" + id), WebRequestType.PUT, Serializer.SerializeToJson <Scene>(new Scene()
            {
                storelightstate = true
            }));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 2)
                    {
                        modifiedid = id;
                    }
                }
                lastMessages = new MessageCollection();
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(modifiedid);
        }
Exemple #9
0
        /// <summary>
        /// Send the command to the bridge to search for new lights.
        /// </summary>
        /// <returns>True OR False command sent succesfully</returns>
        public bool SearchNewLights()
        {
            bool result = false;

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/lights"), WebRequestType.POST);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);

                if (lstmsg != null)
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 1)
                    {
                        result = true;
                    }
                }
                else
                {
                    lastMessages = new MessageCollection {
                        new UnkownError(comres)
                    };
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection
                {
                    new UnkownError(comres)
                };
                break;
            }
            return(result);
        }
Exemple #10
0
        /// <summary>
        /// Creates a new user / Register a new user. The link button on the bridge must be pressed and this command executed within 30 seconds.
        /// </summary>
        /// <returns>Contains a list with a single item that details whether the user was added successfully along with the username parameter. Note: If the requested username already exists then the response will report a success.</returns>
        /// <param name="DeviceType">Description of the type of device associated with this username. This field must contain the name of your app.</param>
        /// <return>The new API Key.</return>
        public string CreateUser(string DeviceType)
        {
            string apikey = string.Empty;

            CommResult comres = Communication.SendRequest(new Uri("http://" + _ipAddress + "/api"), WebRequestType.POST, Serializer.SerializeToJson <User>(new User()
            {
                devicetype = DeviceType
            }));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 1)
                    {
                        apikey = ((Success)lastMessages[0]).Value;
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(apikey);
        }
Exemple #11
0
        /// <summary>
        /// Get the specified schedule attributes.
        /// </summary>
        /// <param name="id">Id of the light.</param>
        /// <returns>Return the requested schedule.</returns>
        public Schedule GetSchedule(string id)
        {
            Schedule result = new Schedule();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/schedules/" + id), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                result = Serializer.DeserializeToObject <Schedule>(comres.data);
                if (result != null)
                {
                    return(result);
                }
                result = new Schedule();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(result);
        }
Exemple #12
0
        /// <summary>
        /// Create a new group
        /// </summary>
        /// <param name="Name">Name of the new group.</param>
        /// <param name="lightList">A List of lights in the group.</param>
        /// <return>Return the ID of the group created</return>
        public byte CreateGroup(string Name, List <string> lightList)
        {
            byte       Id     = 0;
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/groups"), WebRequestType.POST, Serializer.SerializeToJson <Group>(new Group()
            {
                name = Name, lights = lightList
            }));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 1)
                    {
                        Id = byte.Parse(((CreationSuccess)lastMessages[0]).id);
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(Id);
        }
Exemple #13
0
        /// <summary>
        /// Delete the selected light from the bridge.
        /// </summary>
        /// <param name="ID">ID of the light to delete.</param>
        /// <returns>True for success or false for error.</returns>
        public bool DeleteLight(string ID)
        {
            bool id = false;

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/lights/" + ID), WebRequestType.DELETE);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 1)
                    {
                        id = lastMessages[0].GetType() == typeof(DeletionSuccess) ? true : false;
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }


            return(id);
        }
Exemple #14
0
        /// <summary>
        /// Modify an already existing rule.
        /// </summary>
        /// <param name="id">ID of the rule to be modified.</param>
        /// <param name="modifiedRule">The desired modification to the rule.</param>
        /// <returns>The id of the modified rule if successful</returns>
        public string ModifyRule(string id, Rule modifiedRule)
        {
            string result = "";

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/rules/" + id.ToString()), WebRequestType.PUT, Serializer.SerializeToJson <Rule>(modifiedRule));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount > 0)
                    {
                        result = ((Success)lastMessages[0]).id;
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(result);
        }
Exemple #15
0
        /// <summary>
        /// Update the bridge firmware.
        /// </summary>
        /// <returns>True or False command sent succesfully.</returns>
        public bool DoSwUpdate()
        {
            bool result = false;

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.PUT, "{\"swupdate\":" + Serializer.SerializeToJson <SwUpdate>(new SwUpdate()
            {
                updatestate = 3
            }) + "}");

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                lastMessages = new MessageCollection(lstmsg);
                if (lastMessages.SuccessCount == 1)
                {
                    result = true;
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(result);
        }
Exemple #16
0
        /// <summary>
        /// Delete the specified rule.
        /// </summary>
        /// <param name="id">ID of the rule to be deleted.</param>
        /// <returns>True or False the rule has been deleted.</returns>
        public bool DeleteRule(string id)
        {
            bool result = false;

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/rules/" + id.ToString()), WebRequestType.DELETE);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 1)
                    {
                        result = true;
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(result);
        }
Exemple #17
0
        /// <summary>
        /// Activate a scene on the bridge.
        /// </summary>
        /// <param name="sceneId">ID of the scene to activate.</param>
        /// <returns>True or false if the scene has been applied.</returns>
        public bool ActivateScene(string sceneId)
        {
            bool       result = false;
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/groups/0/action"), WebRequestType.PUT, "{\"scene\":\"" + sceneId + "\"}");

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 1)
                    {
                        result = true;
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(result);
        }
Exemple #18
0
        /// <summary>
        /// Create a new rule on the bridge.
        /// </summary>
        /// <param name="newRule">The new rule to be created.</param>
        /// <returns>The ID of the created rule if succesful.</returns>
        public string CreateRule(Rule newRule)
        {
            string     id     = "";
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/rules"), WebRequestType.POST, Serializer.SerializeToJson <Rule>(newRule));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    if (lastMessages.SuccessCount == 1)
                    {
                        id = ((CreationSuccess)lastMessages[0]).id;
                    }
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(id);
        }
Exemple #19
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Message msg = null;
            JObject obj = serializer.Deserialize <JObject>(reader);

            foreach (var o in obj)
            {
                if (o.Key == "error")
                {
                    string str = o.Value.ToString().Replace("\r\n", "");
                    msg = JsonConvert.DeserializeObject <Error>(str);
                }
                else
                {
                    if (o.Value.ToString().Contains("deleted"))
                    {
                        msg = new DeletionSuccess()
                        {
                            success = o.Value.ToString()
                        }
                    }
                    ;
                    else if (o.Value.ToString().Contains("id"))
                    {
                        JObject jo = Serializer.DeserializeToObject <JObject>(o.Value.ToString());
                        msg = new CreationSuccess()
                        {
                            id = jo["id"].Value <string>()
                        };
                    }
                    else
                    {
                        msg = Serializer.DeserializeToObject <Success>(o.Value.ToString());
                    }
                }
            }

            return(msg);
        }
Exemple #20
0
        /// <summary>
        /// Get a list of newly discovered sensors.
        /// </summary>
        /// <returns>A list of sensors.</returns>
        public SearchResult GetNewSensors()
        {
            SearchResult newSensors = new SearchResult();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/lights/new"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                newSensors = Serializer.DeserializeSearchResult(comres.data);
                if (newSensors != null)
                {
                    return(newSensors);
                }
                newSensors = new SearchResult();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                lastMessages = lstmsg != null ? new MessageCollection(lstmsg) : new MessageCollection {
                    new UnkownError(comres)
                };
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }


            return(newSensors);
        }
Exemple #21
0
        /// <summary>
        /// Get The list of scenes from the bridge.
        /// </summary>
        /// <returns>a list of scenes available in the bridge.</returns>
        public Dictionary <string, Scene> GetScenesList()
        {
            Dictionary <string, Scene> listScenes = new Dictionary <string, Scene>();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/scenes"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                listScenes = Serializer.DeserializeToObject <Dictionary <string, Scene> >(comres.data);
                if (listScenes == null)
                {
                    listScenes = new Dictionary <string, Scene>();
                    List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                    lastMessages = lstmsg == null ? new MessageCollection {
                        new UnkownError(comres)
                    } : new MessageCollection(lstmsg);
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection
                {
                    new UnkownError(comres)
                };
                break;
            }

            return(listScenes);
        }
Exemple #22
0
        /// <summary>
        /// Change the name of a Rule.
        /// </summary>
        /// <param name="id">ID of the rule to change the name.</param>
        /// <param name="newName">New name of the rule.</param>
        /// <returns>A message collection.</returns>
        public MessageCollection ChangeRuleName(string id, string newName)
        {
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/rules/" + id.ToString()), WebRequestType.PUT, Serializer.SerializeToJson <Rule>(new Rule()
            {
                name = newName
            }));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(lastMessages);
        }
Exemple #23
0
        /// <summary>
        /// Get the bridge Settings.
        /// </summary>
        /// <returns>The Settings of the bridge or null.</returns>
        public BridgeSettings GetBridgeSettings()
        {
            BridgeSettings bridgeSettings = new BridgeSettings();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                bridgeSettings = Serializer.DeserializeToObject <BridgeSettings>(comres.data);
                if (bridgeSettings != null)
                {
                    return(bridgeSettings);
                }
                bridgeSettings = new BridgeSettings();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                lastMessages = lstmsg != null ? new MessageCollection(lstmsg) : new MessageCollection {
                    new UnkownError(comres)
                };
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(bridgeSettings);
        }
Exemple #24
0
        /// <summary>
        ///  Get all the timezones that are supported by the bridge.
        /// </summary>
        /// <returns>a list of all the timezones supported by the bridge.</returns>
        public List <string> GetTimeZones()
        {
            List <string> timezones = new List <string>();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/info/timezones"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                timezones = Serializer.DeserializeToObject <List <string> >(comres.data);
                if (timezones != null)
                {
                    return(timezones);
                }
                timezones = new List <string>();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                lastMessages = lstmsg != null ? new MessageCollection(lstmsg) : new MessageCollection {
                    new UnkownError(comres)
                };
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(timezones);
        }
Exemple #25
0
        /// <summary>
        /// Get all objects from the bridge.
        /// </summary>
        /// <returns>A DataStore of objects from the bridge.</returns>
        public DataStore GetBridgeDataStore()
        {
            DataStore listObjets = new DataStore();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                listObjets = Serializer.DeserializeToObject <DataStore>(comres.data);
                if (listObjets != null)
                {
                    return(listObjets);
                }
                listObjets = new DataStore();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                lastMessages = lstmsg != null ? new MessageCollection(lstmsg) : new MessageCollection {
                    new UnkownError(comres)
                };
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(listObjets);
        }
Exemple #26
0
        /// <summary>
        /// Get the attributes of the specified group.
        /// </summary>
        /// <param name="ID">ID of the group.</param>
        /// <returns>The requested group.</returns>
        public Group GetGroup(string ID)
        {
            Group      result = new Group();
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/groups/" + ID), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                result = Serializer.DeserializeToObject <Group>(comres.data);
                if (result != null)
                {
                    return(result);
                }
                result = new Group();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                lastMessages = lstmsg != null ? new MessageCollection(lstmsg) : new MessageCollection {
                    new UnkownError(comres)
                };
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }


            return(result);
        }
Exemple #27
0
        /// <summary>
        /// Get a sensor by it's ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns>The requested sensor.</returns>
        public Sensor GetSensor(string id)
        {
            Sensor sensor = new Sensor();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/sensors/" + id.ToString()), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                sensor = Serializer.DeserializeToObject <Sensor>(comres.data);
                if (sensor != null)
                {
                    return(sensor);
                }
                sensor = new Sensor();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                lastMessages = lstmsg != null ? new MessageCollection(lstmsg) : new MessageCollection {
                    new UnkownError(comres)
                };
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(sensor);
        }
Exemple #28
0
        /// <summary>
        /// Create a new sensor on the bridge.
        /// </summary>
        /// <param name="newSensor"></param>
        /// <returns>The created sensor id or null.</returns>
        public string CreateSensor(Sensor newSensor)
        {
            string     result = "";
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/sensors"), WebRequestType.POST, Serializer.SerializeToJson <Sensor>(newSensor));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(comres.data);
                if (lstmsg == null)
                {
                    goto default;
                }
                else
                {
                    lastMessages = new MessageCollection(lstmsg);
                    result       = lastMessages.SuccessCount == 1 ? ((CreationSuccess)_lastmessages[0]).id : "";
                }
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(result);
        }
Exemple #29
0
        /// <summary>
        /// Return the desired rule from the bridge.
        /// </summary>
        /// <param name="id">ID of the desired rule</param>
        /// <returns>A rule from the bridge.</returns>
        public Rule GetRule(string id)
        {
            Rule       rule   = new Rule();
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/rules/" + id.ToString()), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                rule = Serializer.DeserializeToObject <Rule>(comres.data);
                if (rule != null)
                {
                    return(rule);
                }
                rule = new Rule();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                lastMessages = lstmsg == null ? new MessageCollection {
                    new UnkownError(comres)
                } : new MessageCollection(lstmsg);
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(rule);
        }
Exemple #30
0
        private static void _detectionBgw_DoWork(object sender, DoWorkEventArgs e)
        {
            Dictionary <string, BasicConfig> newdetectedBridge = new Dictionary <string, BasicConfig>();

            // Detect using UPNP
            bool finished;

            List <ManagedUPnP.Device> upnpDevices =
                Discovery.FindDevices(null, 3000, int.MaxValue, out finished).ToList();

            foreach (ManagedUPnP.Device dev in upnpDevices)
            {
                if (dev.ModelName.Contains("Philips hue bridge"))
                {
                    BasicConfig bc = GetBridgeBasicConfig(IPAddress.Parse(dev.RootHostName));
                    if (bc == null)
                    {
                        continue;
                    }

                    newdetectedBridge.Add(dev.RootHostName, bc);
                }
            }


            // If not bridge are found via upnp try the portal.
            if (newdetectedBridge.Count == 0)
            {
                try
                {
                    // Detect using Portal
                    CommResult comres = Communication.SendRequest(new Uri("http://www.meethue.com/api/nupnp"), WebRequestType.GET);
                    if (comres.status == WebExceptionStatus.Success)
                    {
                        List <Device> portalDevices = Serializer.DeserializeToObject <List <Device> >(comres.data);
                        foreach (Device dev in portalDevices)
                        {
                            if (newdetectedBridge.ContainsKey(dev.internalipaddress))
                            {
                                continue;
                            }
                            BasicConfig bc = GetBridgeBasicConfig(IPAddress.Parse(dev.internalipaddress));
                            newdetectedBridge.Add(dev.internalipaddress, bc);
                        }
                    }
                }
                catch (System.TimeoutException ex)
                {
                    OnPortalDetectionTimedOut?.Invoke(null, new DetectionErrorEventArgs(ex));
                    OnBridgeDetectionFailed?.Invoke(null, new DetectionErrorEventArgs(ex));
                    return;
                }
                catch (Exception ex)
                {
                    OnPortalDetectionError?.Invoke(null, new DetectionErrorEventArgs(ex));
                    OnBridgeDetectionFailed?.Invoke(null, new DetectionErrorEventArgs(ex));
                    return;
                }
            }


            IList <Bridge> bridges = newdetectedBridge.Select(kvp => new Bridge
            {
                IpAddress  = IPAddress.Parse(kvp.Key),
                Mac        = kvp.Value.mac,
                ApiVersion = kvp.Value.apiversion,
                SwVersion  = kvp.Value.swversion,
                Name       = kvp.Value.name ?? ""
            }).ToList();

            // Process all bridges to get needed settings.

            e.Result = new ObservableCollection <Bridge>(bridges);
        }