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); } } }
/// <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); }
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. }
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); } }
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); } }
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); }
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); } }
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); } }
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); }
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); }
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); }
public void SendRequest(WWWWrapper request, Action <string> successCallback, Action <List <HueErrorInfo> > errorCallback = null) { StartCoroutine(SendRequestEnumerator(request, successCallback, errorCallback)); }