private IEnumerator ChangeLightStateRoutine(int lightNumber, string newState, string url, Action <ChangeLightStateResponse> callback = null)
    {
        byte[] myData = System.Text.Encoding.UTF8.GetBytes(newState);
        Debug.Log(url + "lights/" + lightNumber + "/state");
        using (UnityWebRequest www = UnityWebRequest.Put(url + "lights/" + lightNumber + "/state", myData))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Status Code: " + www.responseCode);
                Debug.Log("response: " + www.downloadHandler.text);



                ChangeLightStateResponse response = null;


                if (!ChangeLightStateResponse.CreateFromJSON(www.downloadHandler.text, out response))
                {
                    Debug.Log("Data not in JSON format");
                    //Nothing more as I create the object an fill in the details.
                    //But you could like adding some extra processing in your own code
                }



                if (callback != null)
                {
                    callback(response);
                }
            }
        }



        ////JSONUtility does not support list as in Hue format.
        //// For now removing the [] and later will parse JSON with simpleJSON


        //CreateUserResponse response = null;


        //if (!CreateUserResponse.CreateFromJSON(request.downloadHandler.text, out response))
        //{
        //    Debug.Log("Data not in JSON format");
        //    //Nothing more as I create the object an fill in the details.
        //    //But you could like adding some extra processing in your own code
        //}



        ////HueCreateUserResponse response = HueCreateUserResponse.CreateFromJSon(data);
        //if (callback != null)
        //callback(response);
    }
    public static bool CreateFromJSON(string jsonString, out ChangeLightStateResponse response)
    {
        response = new ChangeLightStateResponse();

        try
        {
            JSONNode root = JSON.Parse(jsonString);
            response.Unformatted = jsonString;
            if (root.IsArray) //Should return an array with every states changed
            {
                JSONNode mainElement = root[0];

                if (mainElement.HasKey("success"))
                {
                    response.IsSuccess = true;
                }
                else if (mainElement.HasKey("error"))
                {
                    response.IsSuccess        = false;
                    response.Error            = TypeOfError.known;
                    response.ErrorType        = mainElement["error"]["type"].AsInt;
                    response.ErrorAddress     = mainElement["error"]["adress"];
                    response.ErrorDescription = mainElement["error"]["description"];
                }
                else
                { //undefined error
                    response.IsSuccess = false;
                    response.Error     = TypeOfError.unknown;
                }
            }
            else
            {
                return(false);
            }
        }
        catch (System.Exception ex)
        {
            response.IsSuccess   = false;
            response.Error       = TypeOfError.unknown;
            response.Unformatted = jsonString;
            response.exception   = ex.ToString();
            return(false);
        }

        return(true);
    }
 public void OnChangedState(ChangeLightStateResponse response)
 {
     if (response.IsSuccess)
     {
         logText.text = " Light changed state succesfully.";
     }
     else
     {
         if (response.Error == ChangeLightStateResponse.TypeOfError.known)
         {
             logText.text = "Cannot change light state.\nReason is : " + response.ErrorDescription;
         }
         else
         {
             logText.text = "Cannot  change light state.\n answer is : " + response.Unformatted + "\n Exception could be : " + response.exception;
         }
     }
 }