Exemple #1
0
        private static void _bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            string    mac = e.Argument.ToString();
            IPAddress ip  = IPAddress.Parse(GetLocalIPAddress());

            byte[] ipArray   = ip.GetAddressBytes();
            byte   currentip = ipArray[3];

            e.Result = null;

            for (byte x = 2; x <= 254; x++)
            {
                if (_bgw.CancellationPending)
                {
                    log.Info("Bridge lookup cancelled.");
                    e.Cancel = true;
                    break;
                }


                if (x == currentip)
                {
                    continue;
                }
                ipArray[3] = x;
                _bgw.ReportProgress(0, new ProgressReport(new IPAddress(ipArray), x));

                HueHttpClient.Timeout = 50;
                if (_bgw.CancellationPending)
                {
                    break;
                }

                HttpResult comres = HueHttpClient.SendRequest(new Uri($@"http://{new IPAddress(ipArray)}/api/config"), WebRequestType.Get);
                Philips_Hue.BridgeObject.BridgeObjects.BridgeSettings desc = new Philips_Hue.BridgeObject.BridgeObjects.BridgeSettings();

                if (comres.Success)
                {
                    desc = Serializer.DeserializeToObject <Philips_Hue.BridgeObject.BridgeObjects.BridgeSettings>(comres.Data); // try to deserialize the received message.
                    if (desc == null)
                    {
                        continue;               // if the deserialisation didn't work it means this is not a bridge continue with next ip.
                    }
                    if (desc.mac == mac)
                    {
                        e.Result = new IPAddress(ipArray);
                        break;
                    }
                }

                if (e.Result != null)
                {
                    _bgw.ReportProgress(0, new ProgressReport(new IPAddress(ipArray), 254));
                    break;
                }
            }

            // Restore the timeout to the original value
            HueHttpClient.Timeout = WinHueSettings.settings.Timeout;
        }
        /// <summary>
        /// Get a list of specified objects from the bridge async.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <returns>BridgeCommResult</returns>
        public async Task <List <T> > GetListObjectsAsync <T>(bool showmyhidden = false, bool getgroupzero = false) where T : IHueObject
        {
            string     typename = typeof(T).Name.ToLower() + "s";
            string     url      = BridgeUrl + $"/{typename}";
            HttpResult comres   = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Get);

            if (comres.Success)
            {
                Dictionary <string, T> data = Serializer.DeserializeToObject <Dictionary <string, T> >(comres.Data);
                if (data != null)
                {
                    List <T> listdata = data.Select(x => { x.Value.Id = x.Key; return(x.Value); }).ToList();
                    if (!showmyhidden)
                    {
                        RemoveHiddenObjects(ref listdata, WinHueSettings.bridges.BridgeInfo[Mac].hiddenobjects);
                    }

                    if (typeof(T) == typeof(Group) && getgroupzero)
                    {
                        listdata.Add(await GetObjectAsync <T>("0"));
                    }

                    if (typeof(T) == typeof(Scene) && !WinHueSettings.settings.ShowHiddenScenes)
                    {
                        listdata.RemoveAll(x => x.GetType() == typeof(Scene) && x.name.StartsWith("HIDDEN"));
                    }

                    return(data.Select(x => { x.Value.Id = x.Key; return x.Value; }).ToList());
                }
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(null);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(null);
        }
        /// <summary>
        /// Set Entertrainment Group stream status
        /// </summary>
        /// <param name="id">ID of the group</param>
        /// <param name="status">Status of the stream</param>
        /// <returns></returns>
        public async Task <bool> SetEntertrainementGroupStreamStatus(string id, bool status)
        {
            string url = BridgeUrl + $@"/groups/{id}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, "{\"stream\":{\"active\":" + status.ToString().ToLower() + "}}");

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Update Light location for group : {id}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
        /// <summary>
        /// Set Entertrainment Group light location
        /// </summary>
        /// <param name="id">ID of the group</param>
        /// <param name="loc">Location information</param>
        /// <returns></returns>
        public bool SetEntertrainementLightLocation(string id, Location loc)
        {
            string     url = BridgeUrl + $@"/groups/{id}";
            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Put, Serializer.SerializeJsonObject(loc));
                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Update Light location for group : {id}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
        /// <summary>
        /// Rename a specified object on the bridge.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="id">ID of the specified object to rename.</param>
        /// <param name="newname">New name of the object.</param>
        /// <returns>BridgeHttpResult</returns>
        public bool RenameObject(IHueObject hueobj)
        {
            string typename = hueobj.GetType().Name.ToLower() + "s";
            string url      = BridgeUrl + $@"/{typename}/{hueobj.Id}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Put, $@"{{""name"":""{hueobj.name}""}}");
                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Rename object : {hueobj.ToString()}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
        /// <summary>
        /// Set the Power configuration mode of the light.
        /// </summary>
        /// <param name="powermode">Power mode : powerfail or Safety</param>
        /// <param name="id">Id of the bulb</param>
        /// <returns>True or false</returns>
        public async Task <bool> SetPowerConfigAsyncTask(string powermode, string id)
        {
            HttpResult comres;

            string url = BridgeUrl + $"/lights/{id}/config";

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, "{\"startup\" : {\"mode\" : \"" + powermode + "\"}}");

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = powermode
                });
                return(LastCommandMessages.Success);
            }

            return(false);
        }
        /// <summary>
        /// Activate a scene async.
        /// </summary>
        /// <param name="id">Id of the scene.</param>
        /// <returns>BridgeHttpResult</returns>
        public async Task <bool> ActivateSceneAsyncTask(string id)
        {
            string     url = BridgeUrl + "/groups/0/action";
            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, "{\"scene\":\"" + id + "\"}");

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Set Virtual scene : {id}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
        /// <summary>
        /// Create a new object on the bridge. Won't work for Lights.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="newobject">New object to create on the bridge.</param>
        /// <returns>HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</returns>
        public bool CreateObject(IHueObject newobject)
        {
            string     typename = newobject.GetType().Name.ToLower() + "s";
            IHueObject clone    = (IHueObject)newobject.Clone();
            string     url      = BridgeUrl + $@"/{typename}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Post, Serializer.CreateJsonObject(clone));
                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Created Virtual object : {newobject.ToString()}"
                });
                return(LastCommandMessages.Success);
            }

            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
        /// <summary>
        /// Change the sensor's state.
        /// </summary>
        /// <param name="id">id of the sensor</param>
        /// <param name="newstate">New state of the sensor</param>
        /// <returns>Success or error</returns>
        public async Task <bool> ChangeSensorStateAsyncTask(string id, object newstate)
        {
            string     url = BridgeUrl + $@"/sensors/{id}/state";
            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(newstate));

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Modified Virtual sensor state : {id},{newstate.ToString()}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #10
0
        /// <summary>
        /// Remove the specified object from the bridge.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="obj">Object to modify</param>
        /// <returns>HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</returns>
        public bool RemoveObject(string Id, HueObjectType type)
        {
            string typename = type.ToString();
            string url      = BridgeUrl + $@"/{typename}/{Id}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Delete);
                if (comres.Success)
                {
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Deleted Virtual object : {Id},{type.ToString()}"
                });
                return(LastCommandMessages.Success);
            }

            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #11
0
        /// <summary>
        /// Remove the specified object from the bridge async.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="obj">Object to modify</param>
        /// <returns>HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</returns>
        public async Task <bool> RemoveObjectAsyncTask(IHueObject obj)
        {
            string     typename = obj.GetType().Name.ToLower() + "s";
            string     url      = BridgeUrl + $@"/{typename}/{obj.Id}";
            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Delete);

                if (comres.Success)
                {
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Deleted Virtual object : {obj.ToString()}"
                });
                return(LastCommandMessages.Success);
            }

            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #12
0
        /// <summary>
        /// Set the light state of a scene async.
        /// </summary>
        /// <param name="sceneid">Id of the scene.</param>
        /// <param name="lightid">Id of the light.</param>
        /// <param name="state">State of the light.</param>
        /// <returns>BrideHttpResult</returns>
        public async Task <bool> SetSceneLightStateAsyncTask(string sceneid, string lightid, IBaseProperties state)
        {
            string     url = BridgeUrl + $"/scenes/{sceneid}/lightstates/{lightid}";
            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(state));

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Set Virtual scene state : {sceneid}, {lightid}, {state.ToString()}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #13
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 async Task <List <string> > GetTimeZonesAsyncTask()
        {
            Version api   = Version.Parse(ApiVersion);
            Version limit = Version.Parse("1.15.0");

            if (api > limit)
            {
                Capabilities cap = await GetBridgeCapabilitiesAsyncTask();

                return(cap?.timezones.values);
            }
            else
            {
                HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + "/info/timezones"), WebRequestType.Get);

                if (comres.Success)
                {
                    List <string> timezones = Serializer.DeserializeToObject <List <string> >(comres.Data);
                    if (timezones != null)
                    {
                        return(timezones);
                    }
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(null);
                }
                BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + "/info/timezones", WebExceptionStatus.NameResolutionFailure));
                return(null);
            }
        }
Exemple #14
0
        /// <summary>
        /// Modify the specified object in the bridge.
        /// </summary>
        /// <typeparam name="T">HueObject (Light,Group,Sensor,Rule,Schedule,Scene)</typeparam>
        /// <param name="modifiedobject">The new modified object.</param>
        /// <param name="id">Id of the object.</param>
        /// <returns>BridgeHttpResult</returns>
        public async Task <bool> ModifyObjectAsyncTask(IHueObject modifiedobject)
        {
            string     typename = modifiedobject.GetType().Name.ToLower() + "s";
            IHueObject clone    = (IHueObject)modifiedobject.Clone();
            string     url      = BridgeUrl + $@"/{typename}/{modifiedobject.Id}";

            if (typename == null)
            {
                return(false);
            }

            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(modifiedobject));

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = $"Modified Virtual object : {modifiedobject.ToString()}"
                });
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #15
0
        public async Task <bool> SetPowerCustomSettingsAsyncTask(PowerCustomSettings state, string id)
        {
            HttpResult comres;

            string url = BridgeUrl + $"/lights/{id}/config/";

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, "{\"startup\": { \"customsettings\" : " + Serializer.SerializeJsonObject(state) + "}}");

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = state.ToString()
                });
                return(LastCommandMessages.Success);
            }

            return(false);
        }
Exemple #16
0
        /// <summary>
        /// Get the specified object freom the bridge in async.
        /// </summary>
        /// <typeparam name="T">Type of object to deserialize to</typeparam>
        /// <param name="id">Id of the object to get</param>
        /// <returns>BridgeCommResult</returns>
        public async Task <T> GetObjectAsync <T>(string id) where T : IHueObject
        {
            string typename = typeof(T).Name.ToLower() + "s";

            if (typename == string.Empty || typename == "s")
            {
                return(default(T));
            }
            string     url    = BridgeUrl + $"/{typename}/{id}";
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Get);

            if (comres.Success)
            {
                T data = Serializer.DeserializeToObject <T>(comres.Data);
                if (data != null)
                {
                    data.Id = id;
                    return(data);
                }

                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(default(T));
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(default(T));
        }
Exemple #17
0
        /// <summary>
        /// Allows the user to set some configuration values.
        /// </summary>
        /// <param name="settings">Settings of the bridge.</param>
        /// <return>The new settings of the bridge.</return>
        public async Task <bool> SetBridgeSettingsAsyncTask(BridgeSettings settings)
        {
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + "/config"), WebRequestType.Put, Serializer.ModifyJsonObject(settings));

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + "/config", WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #18
0
        public async Task <bool> TouchLink()
        {
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + "/config"), WebRequestType.Put, "{\"touchlink\":true}");

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(true);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + "/config", WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #19
0
        public async Task <bool> RemoveUserAsyncTask(string username)
        {
            string     url    = BridgeUrl + "/config/whitelist/" + username;
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Delete);

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #20
0
        /// <summary>
        /// Get Bridge capabilities
        /// </summary>
        /// <returns></returns>
        public Capabilities GetBridgeCapabilities()
        {
            string     url    = BridgeUrl + "/capabilities";
            HttpResult comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Get);

            if (comres.Success)
            {
                Capabilities cap = Serializer.DeserializeToObject <Capabilities>(comres.Data);
                return(cap);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(null);
        }
Exemple #21
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 async Task <BasicConfig> GetBridgeBasicConfigAsyncTask()
        {
            string     url    = BridgeUrl + "/config";
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Get);

            if (comres.Success)
            {
                BasicConfig config = Serializer.DeserializeToObject <BasicConfig>(comres.Data);
                return(config);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(null);
        }
Exemple #22
0
        /// <summary>
        /// Set notify to false once the update has been done.
        /// </summary>
        /// <returns>True or false if the operation was successful or not</returns>
        public bool SetNotify()
        {
            HttpResult comres = HueHttpClient.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.Put, "{\"swupdate\": {\"notify\":false}}");

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + "/config", WebExceptionStatus.NameResolutionFailure));

            return(false);
        }
Exemple #23
0
        public async Task <bool> StartNewObjectsSearchAsyncTask(Type objecttype)
        {
            string typename = objecttype.Name.ToLower() + "s";

            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(BridgeUrl + $"/{typename}"), WebRequestType.Post);

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(true);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, BridgeUrl + $"/{typename}", WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #24
0
        /// <summary>
        /// Send a raw json command to the bridge without altering the bridge lastmessages
        /// </summary>
        /// <param name="url">url to send the command to.</param>
        /// <param name="data">raw json data string</param>
        /// <param name="type">type of command.</param>
        /// <returns>json test resulting of the command.</returns>
        public async Task <string> SendRawCommandAsyncTask(string url, string data, WebRequestType type)
        {
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), type, data);

            if (comres.Success)
            {
                if (type != WebRequestType.Get)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                }
                return(comres.Data);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(null);
        }
Exemple #25
0
        /// <summary>
        /// Change the name of the bridge.
        /// </summary>
        /// <param name="name">New name of the bridge.</param>
        /// <returns>BridgeHttpResult if the operation is successfull</returns>
        public bool ChangeBridgeName(string name)
        {
            string     url    = BridgeUrl + "/config";
            HttpResult comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(new BridgeSettings()
            {
                name = name
            }));

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #26
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 async Task <string> CreateUserAsyncTask(string deviceType)
        {
            string     url    = "http://" + _ipAddress + "/api";
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Post, Serializer.CreateJsonObject(new User()
            {
                devicetype = deviceType
            }));

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(LastCommandMessages.Success ? LastCommandMessages.LastSuccess.value : null);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(null);
        }
Exemple #27
0
        /// <summary>
        /// Tell the bridge to store the current state of the lights of the scene.
        /// </summary>
        /// <param name="id">ID of the scene.</param>
        /// <returns>BrideHttpResult</returns>
        public async Task <bool> StoreCurrentLightStateAsyncTask(string id)
        {
            string     url    = BridgeUrl + $"/scenes/{id}";
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(new Scene()
            {
                storelightstate = true
            }));

            if (comres.Success)
            {
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(LastCommandMessages.Success);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #28
0
        /// <summary>
        /// Set the state of a light or group on the bridge.
        /// </summary>
        /// <typeparam name="T">Any class the derives from CommonProperties</typeparam>
        /// <param name="state">New state of the object.</param>
        /// <param name="id">ID of the specified object.</param>
        /// <returns>BridgeHttpResult</returns>
        public async Task <bool> SetStateAsyncTask(IBaseProperties state, string id)
        {
            string typename = null;
            string type     = null;

            if (state is State)
            {
                typename = "lights";
                type     = "state";
            }
            if (state is Action)
            {
                typename = "groups";
                type     = "action";
            }
            if (typename == null)
            {
                return(false);
            }
            string url = BridgeUrl + $@"/{typename}/{id}/{type}";

            HttpResult comres;

            if (!Virtual)
            {
                comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Put, Serializer.ModifyJsonObject(state));

                if (comres.Success)
                {
                    LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                    return(LastCommandMessages.Success);
                }
            }
            else
            {
                LastCommandMessages.AddMessage(new Success()
                {
                    Address = url, value = state.ToString()
                });
                return(LastCommandMessages.Success);
            }


            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(false);
        }
Exemple #29
0
        /// <summary>
        /// Get the list of users.
        /// </summary>
        /// <returns>The List of user or null on error.</returns>
        public async Task <Dictionary <string, Whitelist> > GetUserListAsyncTask()
        {
            string     url    = BridgeUrl + "/config";
            HttpResult comres = await HueHttpClient.SendRequestAsyncTask(new Uri(url), WebRequestType.Get);

            if (comres.Success)
            {
                BridgeSettings brs = Serializer.DeserializeToObject <BridgeSettings>(comres.Data);
                if (brs != null)
                {
                    return(brs.whitelist);
                }
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(null);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(null);
        }
Exemple #30
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()
        {
            string     url    = BridgeUrl + "/info/timezones";
            HttpResult comres = HueHttpClient.SendRequest(new Uri(url), WebRequestType.Get);

            if (comres.Success)
            {
                List <string> timezones = Serializer.DeserializeToObject <List <string> >(comres.Data);
                if (timezones != null)
                {
                    return(timezones);
                }
                LastCommandMessages.AddMessage(Serializer.DeserializeToObject <List <IMessage> >(comres.Data));
                return(null);
            }
            BridgeNotResponding?.Invoke(this, new BridgeNotRespondingEventArgs(this, url, WebExceptionStatus.NameResolutionFailure));
            return(null);
        }