コード例 #1
0
ファイル: Bridge_Rules.cs プロジェクト: abhaypsingh/WinHue3
        /// <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);
        }
コード例 #2
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);
        }
コード例 #3
0
        /// <summary>
        /// Force the bridge to check for an update.
        /// </summary>
        /// <returns>True or false if the operation is successful (does not return if there is an update)</returns>
        public bool ForceCheckForUpdate()
        {
            bool result = false;

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.PUT, "{\"swupdate\": {\"checkforupdate\":true}}");

            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);
        }
コード例 #4
0
ファイル: Bridge_Groups.cs プロジェクト: Vuille/WinHue3
        /// <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);
        }
コード例 #5
0
ファイル: Bridge_Scenes.cs プロジェクト: abhaypsingh/WinHue3
        /// <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);
        }
コード例 #6
0
ファイル: Bridge.cs プロジェクト: abhaypsingh/WinHue3
        /// <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);
        }
コード例 #7
0
        /// <summary>
        /// Gets a list of lights that were discovered the last time a search for new lights was performed. The list of new lights is always deleted when a new search is started.
        /// </summary>
        /// <returns>Returns a list of new lights detected otherwise will return null</returns>
        public SearchResult GetNewLights()
        {
            SearchResult newLights = new SearchResult();
            CommResult   comres    = Communication.SendRequest(new Uri(BridgeUrl + "/lights/new"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                newLights = Serializer.DeserializeSearchResult(comres.data);
                if (newLights != null)
                {
                    return(newLights);
                }
                newLights = new SearchResult();
                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(newLights);
        }
コード例 #8
0
        /// <summary>
        /// Create a new scene in the bridge.
        /// </summary>
        /// <param name="newScene">New Scene to create.</param>
        /// <returns>The ID of the created scene.</returns>
        public string CreateScene(Scene newScene)
        {
            string     sceneId = "";
            CommResult comres  = Communication.SendRequest(new Uri(BridgeUrl + "/scenes/"), WebRequestType.POST, Serializer.SerializeToJson <Scene>(newScene));

            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);
                    sceneId      = lastMessages.FailureCount >= 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(sceneId);
        }
コード例 #9
0
ファイル: Bridge_Rules.cs プロジェクト: Vuille/WinHue3
        /// <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);
        }
コード例 #10
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);
        }
コード例 #11
0
ファイル: Hue.cs プロジェクト: abhaypsingh/WinHue3
        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);
        }
コード例 #12
0
ファイル: Hue.cs プロジェクト: abhaypsingh/WinHue3
        private static void _ipscanBgw_DoWork(object sender, DoWorkEventArgs e)
        {
            IPAddress ip = IPAddress.Parse(GetLocalIPAddress());

            byte[] ipArray   = ip.GetAddressBytes();
            byte   currentip = ipArray[3];
            ObservableCollection <Bridge> newlist = new ObservableCollection <Bridge>();

            Ping pingSender = new Ping()
            {
            };
            BridgeSettings desc = new BridgeSettings();

            for (byte x = 2; x <= 254; x++)
            {
                if (_ipscanBgw.CancellationPending)
                {
                    break;
                }
                _ipscanBgw.ReportProgress(0, x);
                if (x == currentip)
                {
                    continue;
                }
                ipArray[3] = x;

                Communication.Timeout = 50;
                if (_ipscanBgw.CancellationPending)
                {
                    break;
                }

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

                switch (comres.status)
                {
                case WebExceptionStatus.Success:
                    desc = Serializer.DeserializeToObject <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 (newlist.Count > 0)
                    {
                        if (!newlist.Any(y => Equals(y.IpAddress, ipArray)))
                        {
                            newlist.Add(new Bridge()
                            {
                                IpAddress = new IPAddress(ipArray), ApiVersion = desc.apiversion, Mac = desc.mac
                            });
                        }
                    }
                    else
                    {
                        newlist.Add(new Bridge()
                        {
                            IpAddress = new IPAddress(ipArray), ApiVersion = desc.apiversion, Mac = desc.mac
                        });
                    }
                    break;

                case WebExceptionStatus.Timeout:

                    break;

                default:

                    break;
                }
            }

            e.Result = newlist;
        }
コード例 #13
0
ファイル: Bridge.cs プロジェクト: abhaypsingh/WinHue3
        /// <summary>
        /// Send a raw json command to the bridge.
        /// </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 string SendRawCommand(string url, string data, WebRequestType type)
        {
            CommResult comres = Communication.SendRequest(new Uri(url), type, data);

            return(comres.data);
        }