xm
2020-07-21 9a4b76398009cf76c508d61f7e48fb6f5cb7ac2d
ZigbeeApp/Shared/Phone/ZigBee/Device/CommonDevice.cs
old mode 100644 new mode 100755
@@ -85,6 +85,7 @@
        /// <para>持续数据上报(MomentStatus=0   TriggerZoneStatus>=1<报警>  TriggerZoneStatus=0<取消报警>)</para>
        /// <para>21: Door/Window 门窗传感器(有21和22,这里没有写错)</para>
        /// <para>22:Door/Window 门窗传感器(有21和22,这里没有写错</para>
        /// <para>541 球型移动传感器</para>
        /// </summary>
        public int IasDeviceType;
        /// <summary>
@@ -199,12 +200,12 @@
                {
                    return null;
                }
                var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.getGatewayBaseInfo != null) && (obj.getGatewayBaseInfo.gwID == CurrentGateWayId));
                var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == CurrentGateWayId));
                if (gateWay == null)
                {
                    gateWay = new ZbGateway { IsVirtual = true, };
                    gateWay.getGatewayBaseInfo.gwID = CurrentGateWayId;
                    gateWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId;
                    gateWay.GwId = CurrentGateWayId;
                    gateWay.HomeId = Shared.Common.Config.Instance.HomeId;
                    ZbGateway.GateWayList.Add(gateWay);
                }
@@ -248,9 +249,21 @@
            {
                device = Newtonsoft.Json.JsonConvert.DeserializeObject<ToggleLight>(strDeviceByte);
            }
            else if (strDeviceType == ZigBee.Device.DeviceType.ColorDimmerSwitch.ToString())
            {
                device = Newtonsoft.Json.JsonConvert.DeserializeObject<ColorDimmerSwitch>(strDeviceByte);
            }
            else if (strDeviceType == ZigBee.Device.DeviceType.LevelControlSwitch.ToString())
            {
                device = Newtonsoft.Json.JsonConvert.DeserializeObject<LevelControlSwitch>(strDeviceByte);
            }
            else if (strDeviceType == ZigBee.Device.DeviceType.WindowCoveringDevice.ToString())
            {
                device = Newtonsoft.Json.JsonConvert.DeserializeObject<Rollershade>(strDeviceByte);
            }
            else if (strDeviceType == ZigBee.Device.DeviceType.WindowCoveringController.ToString())
            {
                device = Newtonsoft.Json.JsonConvert.DeserializeObject<WindowCoveringController>(strDeviceByte);
            }
            else if (strDeviceType == ZigBee.Device.DeviceType.OnOffSwitch.ToString())
            {
@@ -300,7 +313,34 @@
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<PMSensor>(strDeviceByte);
            }
            return null;
            else if (strDeviceType == ZigBee.Device.DeviceType.ColorTemperatureLight.ToString())
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<ColorTemperatureLight>(strDeviceByte);
            }
            else if (strDeviceType == ZigBee.Device.DeviceType.Buzzer.ToString())
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<Buzzer>(strDeviceByte);
            }
            else { return null; }
            //能少存一个变量就少存一个
            device.Type = (DeviceType)device.DeviceID;
            return device;
        }
        /// <summary>
        /// 由设备路径恢复设备对象
        /// </summary>
        /// <returns>The device by file path.</returns>
        /// <param name="deviceFilePath">Device file path.</param>
        public static CommonDevice CommonDeviceByFilePath(string deviceFilePath)
        {
            var v = deviceFilePath.Split('_');
            if (v.Length < 3)
            {
                return null;
            }
            return CommonDeviceByByteString(v[1], System.Text.Encoding.UTF8.GetString(Shared.Common.Global.ReadFileByHomeId(deviceFilePath)));
        }
        /// <summary>
@@ -350,6 +390,11 @@
                {
                    //调光器
                    IconPath = "Device/Light.png";
                }
                else if (this.Type == DeviceType.ColorTemperatureLight)
                {
                    //色温灯
                    IconPath = "Device/ColorLightTemperature.png";
                }
                else if (this.Type == DeviceType.OnOffOutput)
                {
@@ -358,17 +403,17 @@
                }
                else if (this.Type == DeviceType.Thermostat)
                {
                    //空调
                    //空调
                    IconPath = "Device/AirConditionerEpoint.png";
                }
                else if (this.Type == DeviceType.FreshAir)
                {
                    //新风
                    //新风
                    IconPath = "Device/FreshAirEpoint.png";
                }
                else if (this.Type == DeviceType.PMSensor)
                {
                    //PM2.5空气质量传感器
                    //PM2.5空气质量传感器
                    IconPath = "Device/AirQualitySensorEpoint.png";
                }
                else if (this.Type == DeviceType.FreshAirHumiditySensor)
@@ -457,6 +502,8 @@
            /// 温度测量功能的设备如:温度传感器。。。
            /// <para>1029:Relative Humidity Measurement,设备支持“湿度测量功能”</para>
            /// 湿度测量功能的设备如:湿度传感器。。。
            /// <para>1066:Pm2.5  Measurement,设备支持“pm2.5测量功能”</para>
            /// Pm2.5测量功能的设备如:Pm2.5传感器。。。
            /// </summary>
            public int InCluster;
        }
@@ -641,6 +688,10 @@
            /// 好像是序列号
            /// </summary>
            public string ProductCode = string.Empty;
            /// <summary>
            /// 设备功能类型(空气开关和继电器专用)
            /// </summary>
            public int FunctionType = -1;
            /// <summary>
            /// 输入簇列表
            /// </summary>
@@ -727,80 +778,6 @@
        #endregion
        #region 修改设备端口名称
        ///<summary >
        /// 修改设备端口(按键)名称
        /// <para>Gateway:设备所属网关(调用方法:device.Gateway)</para>
        /// <para>deviceName:按键名称</para>
        /// </summary>
        public async System.Threading.Tasks.Task<DeviceRenameAllData> RenameDeviceNameAsync(string deviceAddr, int deviceEpoint, string deviceName)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                DeviceRenameAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            d = new DeviceRenameAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new DeviceRenameAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
                    if (topic == gatewayID + "/" + "DeviceRenameRespon")
                    {
                        var receiptData = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceRenameResponseData>(jobject["Data"].ToString());
                        d = new DeviceRenameAllData { deviceRenameData = receiptData };
                    }
                };
                Gateway.Actions += action;
                System.Console.WriteLine("DeviceRename_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var bytes = new byte[64];
                    var reamarkGwBytes = Encoding.UTF8.GetBytes(deviceName);
                    System.Array.Copy(reamarkGwBytes, 0, bytes, 0, 64 < reamarkGwBytes.Length ? 64 : reamarkGwBytes.Length);
                    deviceName = Encoding.UTF8.GetString(bytes);
                    var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 96 } };
                    var data = new JObject { { "DeviceName", deviceName } };
                    jObject.Add("Data", data);
                    Gateway?.Send(("DeviceRename"), jObject.ToString());
                }
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new DeviceRenameAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                System.Console.WriteLine("DeviceRename_Actions退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
        /// <summary>
        /// 重命名设备,网关反馈具体信息
@@ -842,80 +819,6 @@
        #endregion
        #region 修改设备mac名称
        ///<summary >
        /// 修改设备mac名称
        /// <para>macName:设备名称</para>
        /// </summary>
        public async System.Threading.Tasks.Task<RenameDeviceMacNameAllData> RenameDeviceMacNameAsync(string deviceAddr, int deviceEpoint, string macName)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                RenameDeviceMacNameAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            d = new RenameDeviceMacNameAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new RenameDeviceMacNameAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
                    if (topic == gatewayID + "/" + "MacRename_Respon")
                    {
                        var deviceID = jobject.Value<int>("Device_ID");
                        d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject<ToggleLight.RenameDeviceMacNameData>(jobject["Data"].ToString()) };
                    }
                };
                Gateway.Actions += action;
                System.Console.WriteLine("MacRename_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var bytes = new byte[64];
                    var reamarkGwBytes = Encoding.UTF8.GetBytes(macName);
                    System.Array.Copy(reamarkGwBytes, 0, bytes, 0, 64 < reamarkGwBytes.Length ? 64 : reamarkGwBytes.Length);
                    macName = Encoding.UTF8.GetString(bytes);
                    var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 100 } };
                    var data = new JObject { { "MacName", macName } };
                    jObject.Add("Data", data);
                    Gateway?.Send(("MacRename"), jObject.ToString());
                }
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new RenameDeviceMacNameAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                System.Console.WriteLine("MacRename_Action退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
        /// <summary>
        /// 修改设备mac名称数据,网关反馈具体信息
@@ -944,6 +847,109 @@
            /// 修改后的设备名称
            /// </summary>
            public string MacName;
        }
        #endregion
        #region 一键更新四寸屏按键属性
        /// <summary>
        /// 同步设备功能
        /// </summary>
        /// <returns></returns>
        public async System.Threading.Tasks.Task<SynchronizationDeviceResponseAllData> SyncMsgToBindSource(string deviceAddr, int deviceEpoint)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SynchronizationDeviceResponseAllData resContent = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            resContent = new SynchronizationDeviceResponseAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            resContent = new SynchronizationDeviceResponseAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
                    if (topic == gatewayID + "/" + "Bind/SyncMsgToBindSourceRespon")
                    {
                        var res = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(jobject["Data"]["Result"].ToString());
                        if (res == null)
                        {
                            resContent = new SynchronizationDeviceResponseAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            resContent = new SynchronizationDeviceResponseAllData { result = res };
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions = action;
                DebugPrintLog("Bind/SyncMsgToBindSourceRespon_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 5010 } };
                    Gateway.Send("Bind/SyncMsgToBindSource", jObject.ToString());
                }
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (resContent != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    resContent = new SynchronizationDeviceResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                DebugPrintLog("Bind/SyncMsgToBindSource_Actions 退出" + System.DateTime.Now.ToString());
                return resContent;
            });
        }
        /// <summary>
        /// 同步设备,网关反馈具体信息
        /// </summary>
        [System.Serializable]
        public class SynchronizationDeviceResponseAllData
        {
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errorMessageBase;
            /// <summary>
            /// 网关信息错误反馈
            /// <para>当网关接收到客户端信息后,出现以下异常情况将反馈错误。</para>
            /// </summary>
            public ErrorResponData errorResponData;
            /// <summary>
            /// 同步结果
            /// 0:成功:网关内部自动写入设备目标名字、设备目标功能类型、场景目标名字、utc时间写入4寸屏
            /// 1:失败
            /// </summary>
            public int result;
        }
        #endregion
@@ -997,7 +1003,7 @@
                    }
                };
                Gateway.Actions += action;
                System.Console.WriteLine("RemoveDevice_Actions 启动" + "_" + System.DateTime.Now.ToString());
                DebugPrintLog("RemoveDevice_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
@@ -1035,7 +1041,7 @@
                    d = new RemoveDeviceResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                System.Console.WriteLine("RemoveDevice_Actions 退出" + System.DateTime.Now.ToString());
                DebugPrintLog("RemoveDevice_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });