/// <summary> /// Encodes the request to import the list of associated names /// </summary> /// <returns></returns> IEnumerator sendConfigurationRequest() { SmartPlugCommand cmd = new SmartPlugCommand(); cmd.type = "SmartPlugDiscovery"; string json = JsonUtility.ToJson(cmd); byte[] myData = System.Text.Encoding.UTF8.GetBytes(json); UnityWebRequest www = UnityWebRequest.Put(address, myData); yield return(www.Send()); if (www.isNetworkError) { if (www.error == "Cannot connect to destination host") { MagicRoomAppliancesManager_active = false; } } else { Debug.Log(www.downloadHandler.text); ServerSmartPlugConfiguration conf = new ServerSmartPlugConfiguration(); conf = JsonUtility.FromJson <ServerSmartPlugConfiguration>(www.downloadHandler.text); listofAssociatedNames = conf.configuration; string log = ""; foreach (string s in listofAssociatedNames) { log += "Found " + s + " on the network, "; } Logger.addToLogNewLine("ServerSP", log); } }
/// <summary> /// send the http request to obtain all the carpet command available /// </summary> /// <returns></returns> IEnumerator sendConfigurationRequest() { SmartPlugCommand cmd = new SmartPlugCommand(); cmd.type = "SmartCarpetDiscovery"; string json = JsonUtility.ToJson(cmd); byte[] myData = System.Text.Encoding.UTF8.GetBytes(json); UnityWebRequest www = UnityWebRequest.Put(address, myData); yield return(www.Send()); if (www.isNetworkError) { if (www.error == "Cannot connect to destination host") { MagicRoomCarpetManager_active = false; } } else { Debug.Log(www.downloadHandler.text); ServerSmartPlugConfiguration conf = new ServerSmartPlugConfiguration(); conf = JsonUtility.FromJson <ServerSmartPlugConfiguration>(www.downloadHandler.text); listofAssociatedNames = conf.configuration; } }
// Use this for initialization void Awake() { instance = this; address = "http://localhost:7071"; command = new SmartPlugCommand(); command.type = "SmartPlugCommand"; MagicRoomAppliancesManager_active = true; }
private void SendConfigurationRequest() { SmartPlugCommand cmd = new SmartPlugCommand { type = "SmartPlugDiscovery" }; StartCoroutine(SendCommand(cmd, (body) => { Debug.Log(body); ServerSmartPlugConfiguration conf = JsonUtility.FromJson <ServerSmartPlugConfiguration>(body); Appliances.Clear(); Appliances.AddRange(conf.configuration); MagicRoomManager.instance.Logger.AddToLogNewLine("ServerSP", Appliances.ToString()); })); }
public void SendChangeCommand(string appliance, string cmd) { cmd = cmd.ToUpper(); if (cmd != "ON" && cmd != "OFF") { return; } SmartPlugCommand command = new SmartPlugCommand() { type = "SmartPlugCommand", command = cmd, id = appliance }; MagicRoomManager.instance.Logger.AddToLogNewLine(appliance, cmd.ToUpper()); StartCoroutine(SendCommand(command)); }
private IEnumerator SendCommand(SmartPlugCommand command, MagicRoomManager.WebCallback callback = null) { string json = JsonUtility.ToJson(command); byte[] body = new System.Text.UTF8Encoding().GetBytes(json); UnityWebRequest request = new UnityWebRequest(address, "POST") { uploadHandler = new UploadHandlerRaw(body), downloadHandler = new DownloadHandlerBuffer() }; request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); if (!request.isNetworkError) { callback?.Invoke(request.downloadHandler.text); } }