Пример #1
0
        public void MiHomeRequest_ProvidedID_Update()
        {
            var device = new MiDevice("192.168.0.119", "aabbcc");

            device._deviceId        = new byte[] { 0x00, 0x11, 0x22, 0x33 };
            device._serverTimestamp = new Protocol.ServerTimestamp(91);

            var request = new MiHomeRequest(device._miToken, device._deviceId, device._serverTimestamp, device.NetworkOptions, new
            {
                method = "app_start",
                id     = "hello"
            });

            // This should not affect the ID when it was provided in the original payload
            request.RequestId++;

            var json = request.CreateJson();

            var jobj = JObject.Parse(json);

            Assert.AreEqual(2, jobj.Children().Count());
            Assert.IsNotNull(jobj.SelectToken("$.method"));
            Assert.AreEqual("app_start", (jobj.SelectToken("$.method") as JValue).Value);
            Assert.AreEqual("hello", (jobj.SelectToken("$.id") as JValue).Value);
        }
Пример #2
0
        public MiHomeResponse Send(string methodName, params object[] parameters)
        {
            var miRequest = new MiHomeRequest(_miToken, _deviceId, _serverTimestamp, NetworkOptions, new
            {
                method  = methodName,
                @params = parameters
            });

            return(Send(miRequest));
        }
Пример #3
0
        public void MiHomeRequest_RequestID()
        {
            var device = new MiDevice("192.168.0.119", "aabbcc");

            device._deviceId        = new byte[] { 0x00, 0x11, 0x22, 0x33 };
            device._serverTimestamp = new Protocol.ServerTimestamp(91);

            var request = new MiHomeRequest(device._miToken, device._deviceId, device._serverTimestamp, device.NetworkOptions, new
            {
                method = "app_start"
            });

            var json = request.CreateJson();

            var jobj = JObject.Parse(json);

            Assert.AreEqual(2, jobj.Children().Count());
            Assert.IsNotNull(jobj.SelectToken("$.method"));
            Assert.AreEqual("app_start", (jobj.SelectToken("$.method") as JValue).Value);
            Assert.AreEqual((Int64)0, (jobj.SelectToken("$.id") as JValue).Value);
        }
Пример #4
0
        private MiHomeResponse Send(MiHomeRequest request)
        {
            if (!request.IsHandshake)
            {
                Connect();
                NextId();

                // Update the request with the updated values from the handshake
                request.UpdateDeviceId(_deviceId);
                request.UpdateTimestamp(_serverTimestamp);
                request.RequestId = _requestId;
            }

            int retryCount = request.NetworkOptions.RetryCount;

            do
            {
                var requestPayload  = request.GetBytes();
                var responsePayload = _socket.Send(requestPayload, request.NetworkOptions);
                if (responsePayload != null)
                {
                    var miResponse = new MiHomeResponse(_miToken, responsePayload);
                    if (miResponse.Success)
                    {
                        // We need Device ID and server timestamp for subsequent requests
                        _serverTimestamp = miResponse.Timestamp;
                        if (_deviceId == null && miResponse.DeviceId != null)
                        {
                            _deviceId = miResponse.DeviceId;
                        }
                        return(miResponse);
                    }
                }
                request.RequestId++;
                retryCount--;
            } while (retryCount > 0);

            return(new MiHomeResponse());
        }
Пример #5
0
        private MiHomeResponse Handshake()
        {
            var miRequest = new MiHomeRequest(NetworkOptions);

            return(Send(miRequest));
        }
Пример #6
0
        public MiHomeResponse Send(object data)
        {
            var myRequest = new MiHomeRequest(_miToken, _deviceId, _serverTimestamp, NetworkOptions, data);

            return(Send(myRequest));
        }