陈嘉乐
2020-09-02 652243206427f35a256400a149a1734085824cb9
ZigbeeApp/Shared/Phone/ZigBee/Device/BindObj.cs
@@ -8,145 +8,25 @@
    public class BindObj : CommonDevice
    {
        #region 设备绑定
        /// <summary>
        ///设备绑定
        /// </summary>
        public async System.Threading.Tasks.Task<AddedDeviceBindResponseAllData> AddDeviceBindAsync(AddBindData addBindData)
        {
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                AddedDeviceBindResponseAllData result = null;
                var mainGateway = ZbGateway.MainGateWay;
                if (mainGateway == null)
                {
                    result = new AddedDeviceBindResponseAllData { errorMessageBase = "当前没有主网关" };
                    return result;
                }
                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 gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = ZbGateway.MainGateWay.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            result.errorMessageBase = "网关错误回复,且数据是空";
                        }
                        else
                        {
                            result.errorResponData = temp;
                            result.errorMessageBase = ErrorMess(temp.Error);
                        }
                    }
                    else if (topic == gatewayID + "/" + "Bind/SetBind_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = ZbGateway.MainGateWay.getGatewayBaseInfo.gwID };
                        var tempResult = Newtonsoft.Json.JsonConvert.DeserializeObject<AddedDeviceBindResponseData>(jobject["Data"].ToString());
                        if (tempResult == null)
                        {
                            result = new AddedDeviceBindResponseAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            result = new AddedDeviceBindResponseAllData { addedDeviceBindResponseData = tempResult };
                            DebugPrintLog($"UI收到通知后的主题_{topic}");
                        }
                    }
                };
                mainGateway.Actions += action;
                DebugPrintLog("Bind/SetBind_Actions 启动" + "_" + System.DateTime.Now.ToLongTimeString() + " " + System.DateTime.Now.Millisecond);
                try
                {
                    if (addBindData != null)
                    {
                        var jObject = new JObject { { "DeviceAddr", addBindData.DeviceAddr }, { "Epoint", addBindData.Epoint }, { "Cluster_ID", 0 }, { "Command", 5001 } };
                        var bindList = new JArray { };
                        foreach (var bindInfo in addBindData.BindList)
                        {
                            if (bindInfo.BindType == 0)
                            {
                                var dInfo = new JObject{
                            { "BindMacAddr",bindInfo.BindMacAddr},
                            { "BindEpoint", bindInfo.BindEpoint} ,
                            { "BindCluster", bindInfo.BindCluster} ,
                            { "BindType",bindInfo.BindType}
                            };
                                bindList.Add(dInfo);
                            }
                            else if (bindInfo.BindType == 1)
                            {
                                var dInfo = new JObject{
                             { "BindCluster", bindInfo.BindCluster} ,
                             { "BindType",bindInfo.BindType},
                             { "BindScenesId", bindInfo.BindScenesId}
                            };
                                bindList.Add(dInfo);
                            }
                        }
                        var data = new JObject{
                            {"BindName",addBindData.BindName},
                             { "BindList", bindList }
                    };
                        jObject.Add("Data", data);
                        mainGateway.Send("Bind/SetBind", jObject.ToString());
                    }
                }
                catch
                {
                }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < 20 * 1000)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (result != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > 20 * 1000)
                {
                    result = new AddedDeviceBindResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                mainGateway.Actions -= action;
                System.Console.WriteLine($"Bind/SetBind_Actions 退出 { System.DateTime.Now.ToLongTimeString() + " " + System.DateTime.Now.Millisecond}");
                return result;
            });
        }
        /// <summary>
        /// 绑定设备信息,网关反馈信息
        /// </summary>
        public AddedDeviceBindResponseAllData addedDeviceBindResponseAllData;
        [System.Serializable]
        public class AddedDeviceBindResponseAllData
        public class AddedDeviceBindResponseAllData : ErrorResponCommon
        {
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errorMessageBase;
            /// <summary>
            /// 网关信息错误反馈
            /// <para>当网关接收到客户端信息后,出现以下异常情况将反馈错误。</para>
            /// </summary>
            public ErrorResponData errorResponData;
            /// <summary>
            /// 网关直接反馈绑定设备信息
            /// </summary>
            public AddedDeviceBindResponseData addedDeviceBindResponseData;
            /// <summary>
            /// 按键设备的确认反馈绑定设备信息
            /// </summary>
            public AddBindResultResponseData addBindResultResponseData;
        }
        /// <summary>
        /// 绑定设备信息
        /// </summary>
        public AddedDeviceBindResponseData addedDeviceBindResponseData;
        [System.Serializable]
        public class AddedDeviceSingleBindResponseAllData : ErrorResponCommon
        {
            /// <summary>
            /// 网关直接反馈绑定设备信息
            /// </summary>
            public AddedDeviceSingleBindResponseData addedDeviceSingleBindResponseData;
        }
        [System.Serializable]
        public class AddedDeviceBindResponseData
        {
@@ -164,6 +44,56 @@
            /// </summary>
            public int Result = -1;
        }
        [System.Serializable]
        public class AddedDeviceSingleBindResponseData
        {
            /// <summary>
            /// 控制设备节点的mac地址
            /// </summary>
            public string DeviceAddr;
            /// <summary>
            /// 控制设备节点的端口号
            /// </summary>
            public int Epoint;
            /// <summary>
            ///绑定名称
            /// </summary>
            public string BindName;
            /// <summary>
            /// 0:成功,针对场景和跨网关绑定
            ///<para>1:失败,节点设备或场景不存在。</para>
            ///<para>2:同网关设备绑定,等待控制源设备写入反馈结果;以下面主题结果为准</para>
            /// </summary>
            public int Result = -1;
            /// <summary>
            /// 绑定类型
            ///<para>0:绑定设备 </para>
            ///<para>1:绑定场景</para>
            /// </summary>
            public int BindType;
            /// <summary>
            ///绑定的cluster数组
            /// </summary>
            public List<int> BindClusterList = new List<int>();
            /// <summary>
            /// 绑定设备Mac地址, 当BindType=0时存在
            /// </summary>
            public string BindMacAddr;
            /// <summary>
            /// 绑定设备的端口号,当BindType=0时存在
            /// </summary>
            public int BindEpoint;
            /// <summary>
            /// 绑定场景,当BindType=2时存在
            /// </summary>
            public int BindScenesId;
            /// <summary>
            ///绑定的设备或场景的名称
            /// </summary>
            public string ESName;
        }
        /// <summary>
        /// 绑定列表的信息
@@ -218,7 +148,6 @@
        /// <summary>
        /// 绑定设备的数据
        /// </summary>
        public AddBindData addBindData;
        [System.Serializable]
        public class AddBindData
        {
@@ -270,295 +199,60 @@
            /// </summary>
            public int BindScenesId;
        }
        /// <summary>
        /// 单个设备的数据
        /// </summary>
        [System.Serializable]
        public class AddSingleBindData
        {
            /// <summary>
            /// 控制设备节点的mac地址
            /// </summary>
            public string DeviceAddr;
            /// <summary>
            /// 错误信息
            /// </summary>
            public int Epoint;
            /// <summary>
            ///绑定名称,不修改名称时忽略该选项
            /// </summary>
            public string BindName;
            /// <summary>
            /// 绑定类型
            ///<para>0:绑定设备 </para>
            ///<para>1:绑定场景</para>
            /// </summary>
            public int BindType;
            /// <summary>
            ///绑定的clusters数组,需要控制设备的OutCluster列表中存在该cluster。如绑定某个设备的on/off,为6。如绑定场景,填族Id为6
            /// </summary>
            public List<int> BindClusterList = new List<int>();
            /// <summary>
            /// 绑定设备Mac地址, 当BindType=0时存在
            /// </summary>
            public string BindMacAddr;
            /// <summary>
            /// 绑定设备的端口号,当BindType=0时存在
            /// </summary>
            public int BindEpoint;
            /// <summary>
            /// 绑定场景,当BindType=1时存在
            /// </summary>
            public int BindScenesId;
        }
        #endregion
        #region 解除绑定
        /// <summary>
        /// 解除绑定
        /// </summary>
        //public async System.Threading.Tasks.Task<DelDeviceBindResponseAllData> DelDeviceBindAsync(DelDeviceBindData delDeviceBindData)
        //{
        //    return await System.Threading.Tasks.Task.Run(async () =>
        //    {
        //        DelDeviceBindResponseAllData responseData = null;
        //        var mainGateway = ZbGateway.MainGateWay;
        //        if (mainGateway == null)
        //        {
        //            responseData = new DelDeviceBindResponseAllData { errorMessageBase = "当前没有主网关" };
        //        }
        //        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 gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = mainGateway.getGatewayBaseInfo.gwID };
        //                var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
        //                if (temp == null)
        //                {
        //                    responseData = new DelDeviceBindResponseAllData { errorMessageBase = "网关错误回复,且数据是空" };
        //                }
        //                else
        //                {
        //                    responseData = new DelDeviceBindResponseAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
        //                }
        //            }
        //            //场景移除
        //            if (topic == gatewayID + "/" + "Bind/RemoveBind_Respon")
        //            {
        //                var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
        //                var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<DelDeviceBindResponseData>(jobject["Data"].ToString());
        //                if (tempData == null)
        //                {
        //                    responseData = new DelDeviceBindResponseAllData { errorMessageBase = "网关返回的数据为空" };
        //                }
        //                else
        //                {
        //                    responseData = new DelDeviceBindResponseAllData { delDeviceBindResponseData = tempData };
        //                    System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
        //                }
        //            }
        //            //设备移除
        //            if (topic == gatewayID + "/" + "Bind/RemoveBindResult")
        //            {
        //                var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = mainGateway.getGatewayBaseInfo.gwID };
        //                var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<RemoveBindResultResponseData>(jobject["Data"].ToString());
        //                if (tempData != null)
        //                {
        //                    responseData = new DelDeviceBindResponseAllData { removeBindResultResponseData = tempData };
        //                    System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
        //                }
        //            }
        //        };
        //        mainGateway.Actions += action;
        //        System.Console.WriteLine("DeviceBind/DelBind_Actions 启动" + "_" + System.DateTime.Now.ToString());
        //        try
        //        {
        //            if (delDeviceBindData != null)
        //            {
        //                var jObject = new JObject { { "DeviceAddr", delDeviceBindData.DeviceAddr }, { "Epoint", delDeviceBindData.Epoint }, { "Cluster_ID", 0 }, { "Command", 5003 } };
        //                var removeBindList = new JArray { };
        //                foreach (var removeBindInfo in delDeviceBindData.RemoveBindList)
        //                {
        //                    if (removeBindInfo.BindType == 0)
        //                    {
        //                        var dInfo = new JObject{
        //                    { "BindMacAddr",removeBindInfo.BindMacAddr},
        //                 { "BindEpoint", removeBindInfo.BindEpoint} ,
        //                 { "BindCluster", removeBindInfo.BindCluster} ,
        //                 { "BindType",removeBindInfo.BindType}
        //                    };
        //                        removeBindList.Add(dInfo);
        //                    }
        //                    else if (removeBindInfo.BindType == 1)
        //                    {
        //                        var dInfo = new JObject{
        //                 { "BindCluster", removeBindInfo.BindCluster} ,
        //                 { "BindType",removeBindInfo.BindType},
        //                 { "BindScenesId", removeBindInfo.BindScenesId}
        //                    };
        //                        removeBindList.Add(dInfo);
        //                    }
        //                }
        //                var data = new JObject {
        //            {"RemoveBindList",removeBindList}
        //             };
        //                jObject.Add("Data", data);
        //                mainGateway.Send("Bind/RemoveBind", jObject.ToString());
        //            }
        //        }
        //        catch { }
        //        var dateTime = DateTime.Now;
        //        while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
        //        {
        //            await System.Threading.Tasks.Task.Delay(10);
        //            if (responseData != null)
        //            {
        //                break;
        //            }
        //        }
        //        if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
        //        {
        //            responseData = new DelDeviceBindResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
        //        }
        //        mainGateway.Actions -= action;
        //        System.Console.WriteLine("DeviceBind/DelBind_Actions 退出" + System.DateTime.Now.ToString());
        //        return responseData;
        //    });
        //}
        //之前2种方式反馈结果
        public async System.Threading.Tasks.Task<DelDeviceBindResponseAllData> DelDeviceBindAsync(DelDeviceBindData delDeviceBindData)
        {
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                var responseData = new DelDeviceBindResponseAllData();
                var mainGateway = ZbGateway.MainGateWay;
                if (mainGateway == null)
                {
                    responseData.errorMessageBase = "当前没有主网关";
                    return responseData;
                }
                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 gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = mainGateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            responseData.errorMessageBase = "网关错误回复,且数据是空";
                        }
                        else
                        {
                            responseData.errorResponData = temp;
                            responseData.errorMessageBase = ErrorMess(temp.Error);
                        }
                    }
                    if (topic == gatewayID + "/" + "Bind/RemoveBind_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<DelDeviceBindResponseData>(jobject["Data"].ToString());
                        if (tempData == null)
                        {
                            responseData.errorMessageBase = "网关返回的数据为空";
                        }
                        else
                        {
                            responseData.delDeviceBindResponseData = tempData;
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                    if (topic == gatewayID + "/" + "Bind/RemoveBindResult")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = mainGateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<RemoveBindResultResponseData>(jobject["Data"].ToString());
                        if (tempData != null)
                        {
                            responseData.removeBindResultResponseData = tempData;
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                mainGateway.Actions += action;
                System.Console.WriteLine("DeviceBind/DelBind_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    if (delDeviceBindData != null)
                    {
                        var jObject = new JObject { { "DeviceAddr", delDeviceBindData.DeviceAddr }, { "Epoint", delDeviceBindData.Epoint }, { "Cluster_ID", 0 }, { "Command", 5003 } };
                        var removeBindList = new JArray { };
                        foreach (var removeBindInfo in delDeviceBindData.RemoveBindList)
                        {
                            if (removeBindInfo.BindType == 0)
                            {
                                var dInfo = new JObject{
                            { "BindMacAddr",removeBindInfo.BindMacAddr},
                         { "BindEpoint", removeBindInfo.BindEpoint} ,
                         { "BindCluster", removeBindInfo.BindCluster} ,
                         { "BindType",removeBindInfo.BindType}
                            };
                                removeBindList.Add(dInfo);
                            }
                            else if (removeBindInfo.BindType == 1)
                            {
                                var dInfo = new JObject{
                         { "BindCluster", removeBindInfo.BindCluster} ,
                         { "BindType",removeBindInfo.BindType},
                         { "BindScenesId", removeBindInfo.BindScenesId}
                            };
                                removeBindList.Add(dInfo);
                            }
                        }
                        var data = new JObject {
                    {"RemoveBindList",removeBindList}
                     };
                        jObject.Add("Data", data);
                        //ZbGateway.MainGateWay.CurrentGateWayId = ZbGateway.MainGateWay.getGatewayBaseInfo?.gwID;
                        mainGateway.Send("Bind/RemoveBind", jObject.ToString());
                    }
                }
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(100);
                    if (responseData == null || responseData.delDeviceBindResponseData == null)
                    {
                        continue;
                    }
                    if (0 < responseData.delDeviceBindResponseData.RemoveBindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 1).Count)
                    {
                        if (responseData.removeBindResultResponseData != null)
                        {
                            break;
                        }
                    }
                    else if (0 < responseData.delDeviceBindResponseData.RemoveBindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 3).Count)
                    {
                        if (responseData.removeBindResultResponseData != null)
                        {
                            break;
                        }
                    }
                    else if (0 < responseData.delDeviceBindResponseData.RemoveBindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 2).Count)
                    {
                        if (responseData.removeBindResultResponseData != null)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    responseData.errorMessageBase = "回复超时,请重新操作";
                }
                mainGateway.Actions -= action;
                System.Console.WriteLine("DeviceBind/DelBind_Actions 退出" + System.DateTime.Now.ToString());
                return responseData;
            });
        }
        /// <summary>
        ///解除绑定数据,网关反馈信息
        /// </summary>
        public DelDeviceBindResponseAllData delDeviceBindResponseAllData;
        /// <summary>
        ///解除绑定数据,网关反馈信息
        /// </summary>
        [System.Serializable]
        public class DelDeviceBindResponseAllData
        public class DelDeviceBindResponseAllData : ErrorResponCommon
        {
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errorMessageBase;
            /// <summary>
            /// 网关信息错误反馈
            /// <para>当网关接收到客户端信息后,出现以下异常情况将反馈错误。</para>
            /// </summary>
            public ErrorResponData errorResponData;
            /// <summary>
            /// 解除绑定数据信息
            /// </summary>
@@ -570,10 +264,6 @@
            public RemoveBindResultResponseData removeBindResultResponseData;
        }
        /// <summary>
        /// 解除绑定数据
        /// </summary>
        public DelDeviceBindResponseData delDeviceBindResponseData;
        /// <summary>
        /// 解除绑定数据
        /// </summary>
@@ -637,10 +327,6 @@
        /// <summary>
        /// 解除绑定数据
        /// </summary>
        public DelDeviceBindData delDeviceBindData;
        /// <summary>
        /// 解除绑定数据
        /// </summary>
        [System.Serializable]
        public class DelDeviceBindData
        {
@@ -691,103 +377,13 @@
        #endregion
        #region (从网关中)获取所有绑定
        /// <summary>
        ///获取所有绑定
        /// </summary>
        public async System.Threading.Tasks.Task<GetDeviceBindResponseAllData> GetDeviceBindAsync()
        {
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                GetDeviceBindResponseAllData d = null;
                var mainGateway = ZbGateway.MainGateWay;
                if (mainGateway == null)
                {
                    d = new GetDeviceBindResponseAllData { errorMessageBase = "当前没有主网关" };
                    return d;
                }
                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 gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = mainGateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            d = new GetDeviceBindResponseAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new GetDeviceBindResponseAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
                    if (topic == gatewayID + "/" + "Bind/GetDeviceBind_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = mainGateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<GetDeviceBindResponseData>(jobject["Data"].ToString());
                        //var tempBindList = Newtonsoft.Json.Linq.JArray.Parse(jobject["Data"]["BindList"].ToString());
                        if (tempData == null)
                        {
                            d = new GetDeviceBindResponseAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new GetDeviceBindResponseAllData { getAllBindResponseData = tempData };
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                mainGateway.Actions += action;
                try
                {
                    System.Console.WriteLine("Bind/GetDeviceBind_Actions 启动" + "_" + System.DateTime.Now.ToString());
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 0 }, { "Command", 5007 } };
                    mainGateway.Send("Bind/GetDeviceBind", 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 GetDeviceBindResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                mainGateway.Actions -= action;
                System.Console.WriteLine("Bind/GetDeviceBind_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
        /// <summary>
        ///获取所有绑定数据,网关反馈信息
        /// </summary>
        public GetDeviceBindResponseAllData getAllBindResponseAllData;
        [System.Serializable]
        public class GetDeviceBindResponseAllData
        public class GetDeviceBindResponseAllData : ErrorResponCommon
        {
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errorMessageBase;
            /// <summary>
            /// 网关信息错误反馈
            /// <para>当网关接收到客户端信息后,出现以下异常情况将反馈错误。</para>
            /// </summary>
            public ErrorResponData errorResponData;
            /// <summary>
            /// 控制设备绑定的设备列表回复数据
            /// </summary>
@@ -797,7 +393,6 @@
        /// <summary>
        /// 获取所有绑定回复的数据
        /// </summary>
        public GetDeviceBindResponseData getAllBindResponseData;
        [System.Serializable]
        public class GetDeviceBindResponseData
        {
@@ -819,9 +414,9 @@
        {
            /// <summary>
            /// 绑定类型
            ///<para>0:移除设备,同网关设备间的移除</para>
            ///<para>1:移除设备,跨网关设备间的移除</para>
            ///<para>2:移除绑定场景</para>
            ///<para>0:同网关设备</para>
            ///<para>1:跨网关设备间</para>
            ///<para>2:场景</para>
            /// </summary>
            public int BindType;
            /// <summary>
@@ -845,106 +440,114 @@
            /// </summary>
            public string ESName;
        }
        /// <summary>
        /// 获取所有绑定的信息
        /// </summary>
        public class BindListAllInfo
        {
            /// <summary>
            /// 控制源设备Mac地址
            /// </summary>
            public string KeyMacAddr;
            /// <summary>
            /// 控制源设备的端口号
            /// </summary>
            public int KeyEpoint;
            /// <summary>
            /// 绑定类型
            ///<para>0:同网关设备</para>
            ///<para>1:跨网关设备间</para>
            ///<para>2:场景</para>
            /// </summary>
            public int BindType;
            /// <summary>
            /// 绑定的cluster
            /// </summary>
            public int BindCluster;
            /// <summary>
            /// 绑定设备Mac地址, 当BindType=0时存在
            /// </summary>
            public string BindMacAddr;
            /// <summary>
            /// 绑定设备的端口号,当BindType=0时存在
            /// </summary>
            public int BindEpoint;
            /// <summary>
            /// 绑定场景,当BindType=1时存在
            /// </summary>
            public int BindScenesId;
            /// <summary>
            /// 绑定的设备或场景名称
            /// </summary>
            public string ESName;
        }
        /// <summary>
        /// 获取面板绑定回复的数据【通过mac获取的回复]
        /// </summary>
        [System.Serializable]
        public class GetPanelBindResponseDataByMac
        {
            /// <summary>
            /// 0:成功
            ///<para>1:绑定不存在</para>
            /// </summary>
            public int Result;
            /// <summary>
            /// MaxNumItems
            /// </summary>
            public int MaxNumItems;
            /// <summary>
            /// StartIndex
            /// </summary>
            public int StartIndex;
            /// <summary>
            /// ReturnBindNum
            /// </summary>
            public int ReturnBindNum;
            /// <summary>
            /// 绑定列表 ,当Status=0时存在。
            /// </summary>
            public List<BindDeviceByMacObj> BindList = new List<BindDeviceByMacObj>();
        }
        /// <summary>
        /// 获取面板绑定回复的数据【通过mac获取的回复]
        /// </summary>
        public class BindDeviceByMacObj
        {
            /// <summary>
            /// ControllerEpoint
            /// </summary>
            public int ControllerEpoint;
            /// <summary>
            /// 绑定的cluster
            /// </summary>
            public int BindCluster;
            /// <summary>
            /// 绑定设备Mac地址, 当BindType=0时存在
            /// </summary>
            public string BindMacAddr;
            /// <summary>
            /// 绑定设备的端口号,当BindType=0时存在
            /// </summary>
            public int BindEpoint;
            /// <summary>
            /// BindMode
            /// </summary>
            public int BindMode;
        }
        #endregion
        #region 从节点设备本地记录读取绑定列表信息
        /// <summary>
        ///从节点设备本地记录读取绑定列表信息
        /// </summary>
        public async System.Threading.Tasks.Task<GetDeviceLocalBindResponseAllData> GetDeviceLocalBindAsync(int startIndex = 0)
        {
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                GetDeviceLocalBindResponseAllData d = null;
                var mainGateway = ZbGateway.MainGateWay;
                if (mainGateway == null)
                {
                    d = new GetDeviceLocalBindResponseAllData { errorMessageBase = "当前没有主网关" };
                    return d;
                }
                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 gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = mainGateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            d = new GetDeviceLocalBindResponseAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new GetDeviceLocalBindResponseAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
                    if (topic == gatewayID + "/" + "Bind/GetDeviceLocalBind_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = mainGateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<GetDeviceLocalBindResponseData>(jobject["Data"].ToString());
                        if (tempData == null)
                        {
                            d = new GetDeviceLocalBindResponseAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new GetDeviceLocalBindResponseAllData { getDeviceLocalBindResponseData = tempData };
                            DebugPrintLog($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                mainGateway.Actions += action;
                try
                {
                    DebugPrintLog("Bind/GetDeviceLocalBind_Actions 启动" + "_" + System.DateTime.Now.ToString());
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Cluster_ID", 0 }, { "Command", 5008 } };
                    var data = new JObject { { "StartIndex", startIndex } };
                    mainGateway.Send("Bind/GetDeviceLocalBind", 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 GetDeviceLocalBindResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                mainGateway.Actions -= action;
                DebugPrintLog("Bind/GetDeviceLocalBind_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
        /// <summary>
        ///从节点设备本地记录读取绑定列表信息回复的数据,网关反馈信息
        /// </summary>
        public GetDeviceLocalBindResponseAllData getDeviceLocalBindResponseAllData;
        [System.Serializable]
        public class GetDeviceLocalBindResponseAllData
        public class GetDeviceLocalBindResponseAllData : ErrorResponCommon
        {
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errorMessageBase;
            /// <summary>
            /// 网关信息错误反馈
            /// <para>当网关接收到客户端信息后,出现以下异常情况将反馈错误。</para>
            /// </summary>
            public ErrorResponData errorResponData;
            /// <summary>
            /// 从节点设备本地记录读取绑定列表信息回复的数据
            /// </summary>
@@ -954,7 +557,6 @@
        /// <summary>
        /// 从节点设备本地记录读取绑定列表信息回复的数据
        /// </summary>
        public GetDeviceLocalBindResponseData getDeviceLocalBindResponseData;
        [System.Serializable]
        public class GetDeviceLocalBindResponseData
        {
@@ -1016,100 +618,13 @@
        #endregion
        #region 删除一个控制设备所有绑定信息
        /// <summary>
        ///删除一个控制设备所有绑定信息
        /// </summary>
        public async System.Threading.Tasks.Task<ClearBindInfoResponseAllData> ClearBindInfoAsync()
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                ClearBindInfoResponseAllData 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 gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            d = new ClearBindInfoResponseAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new ClearBindInfoResponseAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
                    if (topic == gatewayID + "/" + "Bind/ClearBindInfo_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClearBindInfoResponseData>(jobject["Data"].ToString());
                        if (tempData == null)
                        {
                            d = new ClearBindInfoResponseAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new ClearBindInfoResponseAllData { clearBindInfoResponseData = tempData };
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                DebugPrintLog("Bind/ClearBindInfo_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 0 }, { "Command", 5006 } };
                    Gateway.Send("Bind/ClearBindInfo", jObject.ToString());
                }
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < 25 * 1000)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                //一键清除目标,会因为目标数量多而加长时间,目前暂定20秒
                if ((DateTime.Now - dateTime).TotalMilliseconds > 25 * 1000)
                {
                    d = new ClearBindInfoResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                DebugPrintLog("Bind/ClearBindInfo_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
        /// <summary>
        ///删除一个控制设备所有绑定数据,网关反馈信息
        /// </summary>
        public ClearBindInfoResponseAllData clearBindInfoResponseAllData;
        [System.Serializable]
        public class ClearBindInfoResponseAllData
        public class ClearBindInfoResponseAllData : ErrorResponCommon
        {
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errorMessageBase;
            /// <summary>
            /// 网关信息错误反馈
            /// <para>当网关接收到客户端信息后,出现以下异常情况将反馈错误。</para>
            /// </summary>
            public ErrorResponData errorResponData;
            /// <summary>
            /// 控制设备绑定的设备列表回复数据
            /// </summary>
@@ -1119,7 +634,6 @@
        /// <summary>
        /// 删除一个控制设备所有绑定信息回复的数据
        /// </summary>
        public ClearBindInfoResponseData clearBindInfoResponseData;
        [System.Serializable]
        public class ClearBindInfoResponseData
        {