コード例 #1
0
ファイル: Bridge_Settings.cs プロジェクト: Vuille/WinHue3
        /// <summary>
        /// Change the name of the bridge.
        /// </summary>
        /// <param name="name">New name of the bridge.</param>
        /// <returns>BridgeCommResult if the operation is successfull</returns>
        public CommandResult ChangeBridgeName(string name)
        {
            CommandResult bresult = new CommandResult {
                Success = false
            };
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.PUT, Serializer.SerializeToJson <BridgeSettings>(new BridgeSettings()
            {
                name = name
            }));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                MessageCollection mc = new MessageCollection(Serializer.DeserializeToObject <List <Message> >(comres.data));
                lastMessages = mc;
                if (mc.FailureCount == 0)
                {
                    bresult.Success = true;
                }
                break;

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

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }
            bresult.resultobject = lastMessages;
            return(bresult);
        }
コード例 #2
0
ファイル: Bridge_ObjectSetter.cs プロジェクト: Vuille/WinHue3
        /// <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>BrideCommResult</returns>
        public CommandResult StoreCurrentLightState(string id)
        {
            CommandResult bresult = new CommandResult()
            {
                Success = false
            };
            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + $"/scenes/{id}"), WebRequestType.PUT, Serializer.SerializeToJson(new Scene()
            {
                storelightstate = true
            }));

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                lastMessages         = new MessageCollection(Serializer.DeserializeToObject <List <Message> >(comres.data));
                bresult.Success      = lastMessages.FailureCount == 0;
                bresult.resultobject = lastMessages;
                break;

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

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                bresult.resultobject = comres.data;
                break;
            }
            return(bresult);
        }
コード例 #3
0
ファイル: Bridge_Update.cs プロジェクト: Vuille/WinHue3
        /// <summary>
        /// Update the bridge firmware.
        /// </summary>
        /// <returns>True or False command sent succesfully.</returns>
        public CommandResult DoSwUpdate()
        {
            CommandResult bresult = new CommandResult {
                Success = 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:
                lastMessages = new MessageCollection(Serializer.DeserializeToObject <List <Message> >(comres.data));
                if (lastMessages.FailureCount == 0)
                {
                    bresult.Success = true;
                }
                break;

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

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }
            bresult.resultobject = lastMessages;
            return(bresult);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
ファイル: Hue.cs プロジェクト: Vuille/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"))
                {
                    continue;
                }

                CommandResult bresult = GetBridgeBasicConfig(IPAddress.Parse(dev.RootHostName));
                if (bresult.Success)
                {
                    newdetectedBridge.Add(dev.RootHostName, (BasicConfig)bresult.resultobject);
                }
            }


            // If not bridge are found via upnp try the portal.
            if (newdetectedBridge.Count == 0)
            {
                // Detect using Portal
                CommResult comres = Communication.SendRequest(new Uri("http://www.meethue.com/api/nupnp"), WebRequestType.GET);

                switch (comres.status)
                {
                case WebExceptionStatus.Success:
                    List <Device> portalDevices = Serializer.DeserializeToObject <List <Device> >(comres.data);
                    foreach (Device dev in portalDevices)
                    {
                        if (newdetectedBridge.ContainsKey(dev.internalipaddress))
                        {
                            continue;
                        }
                        CommandResult bresult = GetBridgeBasicConfig(IPAddress.Parse(dev.internalipaddress));
                        if (bresult.Success)
                        {
                            newdetectedBridge.Add(dev.internalipaddress, (BasicConfig)bresult.resultobject);
                        }
                    }
                    break;

                case WebExceptionStatus.Timeout:
                    OnPortalDetectionTimedOut?.Invoke(null, new DetectionErrorEventArgs(comres.data));
                    OnBridgeDetectionFailed?.Invoke(null, new DetectionErrorEventArgs(comres.data));
                    break;

                default:
                    OnPortalDetectionError?.Invoke(null, new DetectionErrorEventArgs(comres.data));
                    OnBridgeDetectionFailed?.Invoke(null, new DetectionErrorEventArgs(comres.data));
                    break;
                }
            }


            Dictionary <string, 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 ?? ""
            }).ToDictionary(p => p.Mac, p => p);

            // Process all bridges to get needed settings.
            e.Result = bridges;
        }
コード例 #6
0
ファイル: Hue.cs プロジェクト: Vuille/WinHue3
        private static void _ipscanBgw_DoWork(object sender, DoWorkEventArgs e)
        {
            IPAddress ip = IPAddress.Parse(GetLocalIPAddress());

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

            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.Value.IpAddress, ipArray)))
                        {
                            newlist.Add(desc.mac, new Bridge()
                            {
                                IpAddress = new IPAddress(ipArray), ApiVersion = desc.apiversion, Mac = desc.mac
                            });
                        }
                    }
                    else
                    {
                        newlist.Add(desc.mac, new Bridge()
                        {
                            IpAddress = new IPAddress(ipArray), ApiVersion = desc.apiversion, Mac = desc.mac
                        });
                    }
                    break;

                case WebExceptionStatus.Timeout:
                    break;

                default:

                    break;
                }
            }

            e.Result = newlist;
        }
コード例 #7
0
 public UnkownError(CommResult comres)
 {
     status      = comres.status;
     description = comres.data;
 }