Exemplo n.º 1
0
        private void DeviceQuery(ref Gateway gateway, string ipAddress = "")
        {
            // Detect if it's a manual connection. If does, set the IP.
            var manualConnect = !string.IsNullOrEmpty(ipAddress);

            if (manualConnect)
            {
                gateway.GatewayIpAddr = ipAddress;
            }

            // Query sub devices (ZigBee devices)
            var miHome            = new MiHome();
            var responseStr       = miHome.PerformQuery(new ListDeviceQuery(), gateway.GatewayIpAddr);
            var devieListResponse = JsonConvert.DeserializeObject <DeviceIdListResponse>(responseStr);

            if (manualConnect)
            {
                gateway.GatewayId = devieListResponse.GatewayId;
            }

            // Iterate all the devices found from the gateway
            foreach (var deviceId in devieListResponse.GetDeviceIdList())
            {
                responseStr = miHome.PerformQuery(new ReadDeviceQuery(deviceId), gateway.GatewayIpAddr);
                var deviceResponse = JsonConvert.DeserializeObject <DeviceResponse>(responseStr);
                gateway.Devices.Add(new Device()
                {
                    DeviceId    = deviceResponse.DeviceId,
                    Model       = Enum.GetName(typeof(ProductModel), deviceResponse.ProductModel),
                    DataPayload = deviceResponse.RawDataPayload
                });
            }

            // Add gateway as root
            GatewayList.Add(gateway);
            DeviceTree.ItemsSource = GatewayList;
        }
Exemplo n.º 2
0
        private void AutoConnectButton_Click(object sender, RoutedEventArgs e)
        {
            // Perform query
            MiHome miHome      = new MiHome();
            var    responseStr = miHome.PerformQuery(new FindGatewayQuery());

            Console.WriteLine($"Got gateway response: {responseStr}");

            var gatewayResponse = JsonConvert.DeserializeObject <GatewayResponse>(responseStr);
            var gateway         = new Gateway
            {
                GatewayIpAddr = gatewayResponse.IpAddress,
                GatewayId     = gatewayResponse.DeviceId
            };

            DeviceQuery(ref gateway);
        }