コード例 #1
0
        public void Update()
        {
            using (var mi = new MiHome())
            {
                Task.Delay(5000).Wait();
                this.temperatures.Clear();

                foreach (var temperatureConfig in this.temperatureConfigs)
                {
                    var sensor = mi.GetDeviceBySid <ThSensor>(temperatureConfig.Sid);

                    if (sensor != null)
                    {
                        this.temperatures.Add(
                            new Temperature()
                        {
                            NameBg     = temperatureConfig.NameBg,
                            NameEn     = temperatureConfig.NameEn,
                            Temerature = sensor.Temperature.Value,
                            Humidity   = sensor.Humidity.Value
                        });
                    }
                }
            }
        }
コード例 #2
0
 public XElement ToXml()
 {
     return(new XElement("Configuration",
                         MiHome?.ToXml(),
                         Http?.ToXml(),
                         Virtual?.ToXml()));
 }
コード例 #3
0
        public static void Main(string[] args)
        {
            //Action<ILoggingBuilder> loggingBuilder =
            //    builder => builder.AddConsole(x =>
            //    {
            //        x.DisableColors = true;
            //        x.Format = ConsoleLoggerFormat.Systemd;
            //        x.TimestampFormat = " yyyy-MM-d [HH:mm:ss] - ";
            //    });

            //MiHome.LoggerFactory = LoggerFactory.Create(loggingBuilder);
            //MiHome.LogRawCommands = true;

            // pwd of your gateway (optional, needed only to send commands to your devices)
            // and sid of your gateway (optional, use only when you have 2 gateways in your LAN)
            //using var miHome = new MiHome("pwd", "sid")
            using var miHome = new MiHome();

            miHome.OnAnyDevice += (_, device) =>
            {
                Console.WriteLine($"{device.Sid}, {device.GetType()}, {device}"); // all discovered devices
            };

            Console.ReadLine();
        }
コード例 #4
0
ファイル: SmokeSensorTests.cs プロジェクト: xukaige/mi-home
 public void TestMethod1()
 {
     using (var miHome = new MiHome())
     {
         Assert.IsTrue(1 == 1);
     }
 }
コード例 #5
0
        public async Task <bool> FindDevicesAsync(HardwareConfig config)
        {
            var modified = false;

            if (config.MiHome != null)
            {
                foreach (var gatewayConfig in config.MiHome.Gateways)
                {
                    var miHome = new MiHome(gatewayConfig.Password, gatewayConfig.Id);
                    m_MiHomeObjects.Add(miHome);
                    await Task.Delay(5000);

                    var gateway = miHome.GetGateway();
                    if (gateway != null)
                    {
                        AddDevice(gatewayConfig.Name, gatewayConfig.Description, gateway);
                        foreach (var device in miHome.GetDevices())
                        {
                            var deviceConfig = gatewayConfig.Devices.SingleOrDefault(deviceConfig => deviceConfig.Id == device.Sid);
                            if (deviceConfig == null)
                            {
                                deviceConfig = CreateDeviceConfig(device);
                                gatewayConfig.Devices.Add(deviceConfig);
                                modified = true;
                            }
                            AddDevice(deviceConfig.Name, deviceConfig.Description, device);
                        }
                    }
                }
            }

            if (config.Http != null)
            {
                foreach (var deviceConfig in config.Http.Devices)
                {
                    var device = HttpDevice.Create(deviceConfig.Type);
                    if (device != null)
                    {
                        device.Host = deviceConfig.Host;
                        AddDevice(deviceConfig.Name, deviceConfig.Description, device);
                    }
                }
            }

            if (config.Virtual != null)
            {
                foreach (var switchConfig in config.Virtual.Switches)
                {
                    var @switch = new Virtual.Switch();
                    @switch.SetStatus(switchConfig.Status);
                    AddDevice(switchConfig.Name, switchConfig.Description, @switch);
                }
            }

            return(modified);
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: mulflar/mi-home
        public static void Main(string[] args)
        {
            // pwd of your gateway (optional, needed only to send commands to your devices)
            // and sid of your gateway (optional, use only when you have 2 gateways in your LAN)
            using (var miHome = new MiHome())
            {
                Task.Delay(5000).Wait();

                foreach (var miHomeDevice in miHome.GetDevices())
                {
                    Console.WriteLine(miHomeDevice); // all discovered devices
                }

                Console.ReadLine();
            }
        }
コード例 #7
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);
        }
コード例 #8
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;
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: ermac0/mi-home
        public static void Main(string[] args)
        {
            //Console.ReadLine();

            _InitializeMiLight();
            Devices.Initialize(_miLightGateway);

            _miHome = new MiHome(
                gatewayPassword: "******",
                gatewaySid: "34ce00fa5c3c",
                wellKwownDevices: Devices.MiHomeDevices);



            Devices.LightHallway.SwitchOff();

            //miHome.GetGateway().StartPlayMusic(3);
            //Task.Delay(2000).Wait();
            //miHome.GetGateway().StartPlayMusic(2);
            //Task.Delay(2000).Wait();
            //miHome.GetGateway().StartPlayMusic(5);
            //miHome.GetGateway().EnableLight(255, 0, 255, 1000);



            Devices.TempSensorLivingRoom.OnTemperatureChange += (sender, eventArgs) =>
            {
                Console.WriteLine($"Temperature: {eventArgs.Temperature} °C");
            };

            Devices.MotionHall.MotionDetected += (sender, eventArgs) =>
            {
                Console.WriteLine("Motion Hall");
                Devices.LightHallway.SwitchOn(TimeSpan.FromSeconds(90));
            };

            Devices.MotionLivingRoom.MotionDetected += (sender, eventArgs) =>
            {
                Console.WriteLine("Motion Living Room");
            };
            Devices.MotionBathroom.MotionDetected += (sender, eventArgs) =>
            {
                Console.WriteLine("Motion Bathroom");
            };

            var foo = new LightSceneSwitcher(new []
            {
                new LightScene(new []
                {
                    new LightSetting(LivingRoom.MainLight, 0),
                    new LightSetting(LivingRoom.LampLight, 0),
                }),
                new LightScene(new []
                {
                    new LightSetting(LivingRoom.MainLight, 50),
                    new LightSetting(LivingRoom.LampLight, 50),
                }),
                new LightScene(new []
                {
                    new LightSetting(LivingRoom.MainLight, 100),
                    new LightSetting(LivingRoom.LampLight, 50),
                }),
                new LightScene(new []
                {
                    new LightSetting(LivingRoom.MainLight, 50),
                    new LightSetting(LivingRoom.LampLight, 100),
                }),
                new LightScene(new []
                {
                    new LightSetting(LivingRoom.MainLight, 100),
                    new LightSetting(LivingRoom.LampLight, 100),
                }),
            }, Devices.SwitchA);


            Devices.SwitchA.Clicked += (sender, eventArgs) =>
            {
                Console.WriteLine($"Switch A clicked");
            };
            Devices.SwitchB.Clicked += (sender, eventArgs) =>
            {
                Console.WriteLine($"Switch B clicked");
            };
            Devices.SwitchC.Clicked += (sender, eventArgs) =>
            {
                Console.WriteLine($"Switch C clicked");
                Devices.BulbA.Toggle();
                Devices.LightHallway.SwitchOff();
                Devices.ChristmasLights.Toggle();
            };

            Devices.SwitchD.Clicked += (sender, eventArgs) =>
            {
                Console.WriteLine($"Switch D clicked");
                Devices.BulbC.Toggle();
                Devices.LightHallway.SwitchOn();
                Devices.KitchenLights.Toggle();
            };
            LivingRoom.LightSwitch.OnLeftKeyClicked += (sender, eventArgs) =>
            {
                Console.WriteLine($"Left Key Clicked");
                LivingRoom.MainLight.Toggle();
            };
            LivingRoom.LightSwitch.OnRightKeyClicked += (sender, eventArgs) =>
            {
                Console.WriteLine($"Right Key Clicked");
                LivingRoom.LampLight.Toggle();
            };

            Console.ReadLine();
            _miHome.Dispose();
        }