public StateChangedEventArgs(AqaraDevice device, string stateName, string oldData, string newData)
 {
     this.Device    = device;
     this.StateName = stateName;
     this.OldData   = oldData;
     this.NewData   = newData;
 }
Пример #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);
                    device.Name = item.Name;
                    dicDevices.Add(systemid, device);
                }
            }
        }
Пример #3
0
        public void SendWriteCommand(AqaraDevice device, IEnumerable <KeyValuePair <string, dynamic> > arguments = null)
        {
            AqaraGateway gateway = device.Gateway;

            var dicData = new Dictionary <string, dynamic>();

            if (arguments != null)
            {
                foreach (var pair in arguments)
                {
                    dicData.Add(pair.Key, pair.Value);
                }
            }
            var key = Encrypt(gateway.Password, gateway.Token);

            dicData.Add("key", key);

            dynamic message = new {
                cmd      = "write",
                model    = device.Model?.Name,
                sid      = device.Id,
                short_id = device.ShortId,
                //key = key,
                data = JsonConvert.SerializeObject(dicData),
            };

            string jsonString = JsonConvert.SerializeObject(message);

            this.SendCommand(gateway, jsonString);
        }
Пример #4
0
        void ProcessMessage_GetIdListAck(AqaraGateway gateway, dynamic message, DateTime timestamp)
        {
            string cmd = message.cmd;

            if (cmd != "get_id_list_ack")
            {
                return;
            }
            string gateway_sid = message.sid;
            string token       = message.token;
            string jsonString  = message.data;

            if (gateway != null && gateway?.Id != gateway_sid)
            {
                throw new Exception();
            }
            if (!dicGateways.ContainsKey(gateway_sid))
            {
                return;
            }
            gateway = dicGateways[gateway_sid];
            gateway.UpdateToken(token);

            List <string> list = new List <string>();
            dynamic       data = JsonConvert.DeserializeObject(jsonString);

            foreach (string sid in data)
            {
                var deviceId = sid;

                if (!gateway.Devices.ContainsKey(deviceId))
                {
                    AqaraDevice device = new AqaraDevice(this, gateway, sid, null);
                    gateway.Devices.Add(deviceId, device);

                    log.Info($"GATEWAY[{device.Gateway.Id}] device added: sid='{device.Id}'.");
                }

                SendCommand(gateway, string.Format("{{\"cmd\" : \"read\", \"sid\": \"{0}\"}}", sid));
            }
        }
Пример #5
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);
            }
        }
Пример #6
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);
                }
            }
        }