Example #1
0
        void ProcessMessage_ReadAck(AqaraGateway gateway, dynamic message, DateTime timestamp)
        {
            string cmd = message.cmd;

            if (cmd != "read_ack")
            {
                return;
            }
            string sid        = message.sid;
            string model      = message.model;
            long   short_id   = message.short_id;
            string jsonString = message.data;

            if (!gateway.Devices.ContainsKey(sid))
            {
                return;
            }
            var deviceId = sid;

            if (!gateway.Devices.ContainsKey(deviceId))
            {
                return;
            }

            AqaraDevice device = gateway.Devices[deviceId] as AqaraDevice;

            device.Update(model, short_id);
            switch (model)
            {
            case "magnet":    //a.窗磁传感器
                break;

            case "motion":    //人体传感器
                break;

            case "switch":    //无线开关传感器
                if (!(device is SwitchDevice))
                {
                    device = new SwitchDevice(this, device.Gateway, device.Id, device.Config);
                    gateway.Devices[deviceId] = device;
                    device.Update(model, short_id);
                }
                break;

            case "plug":    //智能插座
                if (!(device is PlugDevice))
                {
                    device = new PlugDevice(this, device.Gateway, device.Id, device.Config);
                    gateway.Devices[deviceId] = device;
                    device.Update(model, short_id);
                }
                break;

            case "ctrl_neutral1":    //单火开关单键
                break;

            case "ctrl_neutral2":    //单火开关双键
                if (!(device is CtrlNeutral2Device))
                {
                    device = new CtrlNeutral2Device(this, device.Gateway, device.Id, device.Config);
                    gateway.Devices[deviceId] = device;
                    device.Update(model, short_id);
                }
                break;

            case "86sw1":    //无线开关单键
                break;

            case "86sw2":    //无线开关双键
                break;

            case "sensor_ht":    //温湿度传感器
                break;

            case "rgbw_light":    //j.LUMI.LIGHT.RGBW
                break;

            default:
                device.Update(model, short_id);
                break;
            }

            log.Info($"GATEWAY[{device.Gateway.Id}] device updated: sid='{device.Id}' model='{device.Model}' shortId='{device.ShortId}'.");

            dynamic data = JsonConvert.DeserializeObject(jsonString);

            foreach (var item in data)
            {
                string key   = item.Name;
                string value = item.Value;

                if (!device.States.ContainsKey(key))
                {
                    device.States.Add(key, new DeviceState(key));
                }

                device.States[key].SetValue(value);
            }
        }
Example #2
0
        public AqaraGateway(AqaraClient client, string sid, string password, AqaraDeviceConfig[] devices)
        {
            this.client   = client;
            this.sid      = sid;
            this.password = password;
            if (devices != null)
            {
                foreach (var item in devices)
                {
                    string systemid = item.DeviceId;
                    if (systemid.StartsWith("lumi.", StringComparison.OrdinalIgnoreCase))
                    {
                        systemid = systemid.Substring(5);
                    }

                    if (dicDevices.ContainsKey(systemid))
                    {
                        throw new ArgumentException("设备清单中已存在具有相同键的元素");
                    }

                    //var device = new AqaraDevice(client, this, systemid, item);

                    AqaraDevice device = new AqaraDevice(client, this, systemid, item);
                    if (device.Model is DeviceModel)
                    {
                        switch (device.Model.Name)
                        {
                        case "cube":    //a.窗磁传感器
                            device = new CubeDevice(client, this, systemid, item);
                            break;

                        case "magnet":    //a.窗磁传感器
                            device = new MagnetDevice(client, this, systemid, item);
                            break;

                        case "motion":    //人体传感器
                            device = new MotionDevice(client, this, systemid, item);
                            break;

                        case "switch":    //无线开关传感器
                            device = new SwitchDevice(client, this, systemid, item);
                            break;

                        case "plug":    //智能插座
                            device = new PlugDevice(client, this, systemid, item);
                            break;

                        case "ctrl_neutral1":    //单火开关单键
                            break;

                        case "ctrl_neutral2":    //单火开关双键
                            device = new CtrlNeutral2Device(client, this, systemid, item);
                            break;

                        case "86sw1":    //无线开关单键
                            break;

                        case "86sw2":    //无线开关双键
                            break;

                        case "sensor_ht":    //温湿度传感器
                            device = new SensorHTDevice(client, this, systemid, item);
                            break;

                        case "rgbw_light":    //j.LUMI.LIGHT.RGBW
                            break;

                        case "gateway":    //MiJia/XiaoMi/Aqara Gateway
                            device = new MiJiaGatewayDevice(client, this, systemid, item);
                            break;

                        default:
                            break;
                        }
                    }
                    device.Name = item.Name;
                    dicDevices.Add(systemid, device);
                }
            }
        }