Esempio n. 1
0
    /// <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);
        }
    }
Esempio n. 2
0
    /// <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;
        }
    }
Esempio n. 3
0
 // Use this for initialization
 void Awake()
 {
     instance     = this;
     address      = "http://localhost:7071";
     command      = new SmartPlugCommand();
     command.type = "SmartPlugCommand";
     MagicRoomAppliancesManager_active = true;
 }
Esempio n. 4
0
    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());
        }));
    }
Esempio n. 5
0
    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));
    }
Esempio n. 6
0
    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);
        }
    }