Ejemplo n.º 1
0
        IEnumerator CreateUserEnumerator(
            string applicationName,
            string deviceName,
            Action <string> generatedUserName,
            Action <List <HueErrorInfo> > errorCallback = null
            )
        {
            var body = new Dictionary <string, object>()
            {
                { HueKeys.DEVICE_TYPE, string.Format("{0}#{1}", applicationName, deviceName) }
            };

            var www = new WWWWrapper(BaseURL, body);

            yield return(www.waitToFinish());

            if (isValidJsonResponse(www, errorCallback))
            {
                JSON response = www.responseJSON.getJsonArray("rootArray")[0].getJSON("success");
                var  userName = response.getString(HueKeys.USER_NAME, "");
                if (userName != "")
                {
                    generatedUserName(userName);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Modifies the group name and lights array both at once
        /// If you just want to modify one property set newGroupName
        /// to null or don't supply any hue lights (See SetName or SetLights)
        /// </summary>
        /// <param name="successCallback">Success callback.</param>
        /// <param name="errorCallback">Error callback.</param>
        /// <param name="newGroupName">New group name.</param>
        /// <param name="lights">Lights.</param>
        public void ModifyGroup(
            Action <string> successCallback,
            Action <List <HueErrorInfo> > errorCallback,
            string newGroupName = null,
            params HueLight[] lights
            )
        {
            string url  = HueBridge.instance.BaseURLWithUserName + "/groups/" + id;
            var    list = new List <string>();

            foreach (var item in lights)
            {
                list.Add(item.id);
            }

            var body = new Dictionary <string, object>();

            if (!string.IsNullOrEmpty(newGroupName))
            {
                body[HueKeys.NAME] = newGroupName;
            }
            if (list.Count > 0)
            {
                body[HueKeys.LIGHTS] = list;
            }

            var www = new WWWWrapper(url, body, method: HTTPMethod.PUT);

            HueBridge.instance.SendRequest(www, successCallback, errorCallback);
        }
Ejemplo n.º 3
0
        IEnumerator DeleteEnumerator(string url, Action <List <HueErrorInfo> > errorCallback = null)
        {
            var www = new WWWWrapper(url, method: HTTPMethod.DELETE);

            yield return(www.waitToFinish());

            isValidJsonResponse(www, errorCallback);                    // Just check for errors.
        }
Ejemplo n.º 4
0
        IEnumerator SendRequestEnumerator(WWWWrapper www, Action <string> successCallback,
                                          Action <List <HueErrorInfo> > errorCallback = null)
        {
            yield return(www.waitToFinish());

            if (isValidJsonResponse(www, errorCallback) && successCallback != null)
            {
                successCallback(www.responseText);
            }
        }
Ejemplo n.º 5
0
        IEnumerator GetBridgesEnumerator(Action <List <HueBridgeInfo> > bridgesCallback, Action <List <HueErrorInfo> > errorCallback = null)
        {
            var www = new WWWWrapper(hueDiscoveryServer);

            yield return(www.waitToFinish());

            if (isValidJsonResponse(www, errorCallback))
            {
                ProcessBridges(www.responseJSON, bridgesCallback);
            }
        }
Ejemplo n.º 6
0
        public void SetName(string lightName, Action <string> successCallback = null, Action <List <HueErrorInfo> > errorCallback = null)
        {
            string url = string.Format("{0}/lights/{1}", HueBridge.instance.BaseURLWithUserName, id);

            var body = new Dictionary <string, object>()
            {
                { HueKeys.NAME, lightName }
            };

            var www = new WWWWrapper(url, body, method: HTTPMethod.PUT);

            HueBridge.instance.SendRequest(www, successCallback, errorCallback);
        }
Ejemplo n.º 7
0
        IEnumerator DiscoverGroupsEnumerator(Action <List <HueGroup> > groupsCallback, Action <List <HueErrorInfo> > errorCallback)
        {
            string url = string.Format("{0}/{1}/groups", BaseURL, currentBridge.userName);

            var www = new WWWWrapper(url);

            yield return(www.waitToFinish());

            if (isValidJsonResponse(www, errorCallback))
            {
                ProcessGroups(www.responseJSON, groupsCallback);
            }
        }
Ejemplo n.º 8
0
        IEnumerator UpdateLightFromBridgeEnumerator(string id, HueLight lightToUpdate, Action <List <HueErrorInfo> > errorCallback = null)
        {
            string url = string.Format("{0}/lights/{1}", BaseURLWithUserName, id);

            var www = new WWWWrapper(url);

            yield return(www.waitToFinish());

            if (isValidJsonResponse(www, errorCallback))
            {
                ProcessLightUpdate(www.responseJSON, lightToUpdate);
            }
        }
Ejemplo n.º 9
0
        public void SetState(Action <string> successCallback,
                             Action <List <HueErrorInfo> > errorCallback,
                             params KeyValuePair <string, object>[] parameters
                             )
        {
            string url = string.Format("{0}/groups/{1}/action", HueBridge.instance.BaseURLWithUserName, id);

            var body = new Dictionary <string, object>();

            foreach (var kvp in parameters)
            {
                body.Add(kvp.Key, kvp.Value);
            }

            var www = new WWWWrapper(url, body, method: HTTPMethod.PUT);

            HueBridge.instance.SendRequest(www, successCallback, errorCallback);
        }
Ejemplo n.º 10
0
        private bool isValidJsonResponse(WWWWrapper www, System.Action <List <HueErrorInfo> > errorCallback)
        {
            var  errorInfos = new List <HueErrorInfo>();
            JSON response   = www.responseJSON;

            if (response == null)
            {
                errorInfos.Add(new HueErrorInfo(www.error, null));
            }
            else if (!response.isValid)
            {
                errorInfos.Add(new HueErrorInfo(null, www.responseText));
            }
            else
            {
                if (response.hasKey("rootArray"))
                {
                    foreach (JSON json in response.getJsonArray("rootArray"))
                    {
                        if (json.hasKey(HueKeys.ERROR))
                        {
                            errorInfos.Add(new HueErrorInfo(json));
                        }
                    }
                }
                else if (response.hasKey(HueKeys.ERROR))
                {
                    if (response.hasKey(HueKeys.ERROR))
                    {
                        errorInfos.Add(new HueErrorInfo(response));
                    }
                }
            }

            if (errorInfos.Count > 0 && errorCallback != null)
            {
                errorCallback(errorInfos);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 11
0
        public static void CreateHueGroup(Action <string> sucessCallback, Action <List <HueErrorInfo> > errorCallback,
                                          string groupName, List <string> ids)
        {
            string url = string.Format("{0}/groups", HueBridge.instance.BaseURLWithUserName);

            var body = new Dictionary <string, object>()
            {
                { HueKeys.NAME, groupName },
                { HueKeys.LIGHTS, ids }
            };

            var www = new WWWWrapper(url, body);

            //UnityWebRequest stateRequest = UnityWebrequestHelper.NonURLEncodedPost(url,
            //JsonHelper.CreateJsonParameterString(
            //	new JsonParameter(HueKeys.NAME, groupName),
            //	new JsonParameter(HueKeys.LIGHTS, ids as object)
            //));

            HueBridge.instance.SendRequest(www, sucessCallback, errorCallback);
        }
Ejemplo n.º 12
0
 public void SendRequest(WWWWrapper request, Action <string> successCallback, Action <List <HueErrorInfo> > errorCallback = null)
 {
     StartCoroutine(SendRequestEnumerator(request, successCallback, errorCallback));
 }