Пример #1
0
        private void DiscoverGatewayAndDevices(ResponseCommand cmd)
        {
            if (_gatewaySid != null && _gatewaySid != cmd.Sid)
            {
                throw new Exception("Gateway is not discovered, make sure that it is powered on");
            }

            _transport.Token = cmd.Token;

            _gateway = new Gateway(cmd.Sid, _transport);
            OnGateway?.Invoke(this, _gateway);

            _transport.SendCommand(new ReadDeviceCommand(cmd.Sid));

            foreach (var sid in JArray.Parse(cmd.Data))
            {
                _transport.SendCommand(new ReadDeviceCommand(sid.ToString()));

                Task.Delay(ReadDeviceInterval).Wait(); // need some time in order not to loose message
            }
        }
Пример #2
0
        public MiHome(string gatewayPassword = null, string gatewaySid = null)
        {
            _gatewaySid = gatewaySid;

            _commandsToActions = new Dictionary <ResponseCommandType, Action <ResponseCommand> >
            {
                { ResponseCommandType.GetIdListAck, DiscoverGatewayAndDevices },
                { ResponseCommandType.ReadAck, ProcessReadAck },
                { ResponseCommandType.Report, ProcessReport },
                { ResponseCommandType.Hearbeat, ProcessHeartbeat },
            };

            _deviceEvents = new Dictionary <Type, Action <MiHomeDevice> >
            {
                { typeof(AqaraCubeSensor), x => OnAqaraCubeSensor?.Invoke(this, x as AqaraCubeSensor) },
                { typeof(AqaraMotionSensor), x => OnAqaraMotionSensor?.Invoke(this, x as AqaraMotionSensor) },
                { typeof(AqaraOpenCloseSensor), x => OnAqaraOpenCloseSensor?.Invoke(this, x as AqaraOpenCloseSensor) },
                { typeof(DoorWindowSensor), x => OnDoorWindowSensor?.Invoke(this, x as DoorWindowSensor) },
                { typeof(MotionSensor), x => OnMotionSensor?.Invoke(this, x as MotionSensor) },
                { typeof(SmokeSensor), x => OnSmokeSensor?.Invoke(this, x as SmokeSensor) },
                { typeof(SocketPlug), x => OnSocketPlug?.Invoke(this, x as SocketPlug) },
                { typeof(Switch), x => OnSwitch?.Invoke(this, x as Switch) },
                { typeof(ThSensor), x => OnThSensor?.Invoke(this, x as ThSensor) },
                { typeof(WaterLeakSensor), x => OnWaterLeakSensor?.Invoke(this, x as WaterLeakSensor) },
                { typeof(WeatherSensor), x => OnWeatherSensor?.Invoke(this, x as WeatherSensor) },
                { typeof(WiredDualWallSwitch), x => OnWiredDualWallSwitch?.Invoke(this, x as WiredDualWallSwitch) },
                { typeof(WirelessDualWallSwitch), x => OnWirelessDualWallSwitch?.Invoke(this, x as WirelessDualWallSwitch) },
            };

            _transport = new UdpTransport(gatewayPassword);

            _miHomeDeviceFactory = new MiHomeDeviceFactory(_transport);

            _receiveTask = Task.Run(() => StartReceivingMessagesAsync(_cts.Token), _cts.Token);

            _transport.SendCommand(new DiscoverGatewayCommand());
        }