gxc
2019-12-25 944b87b6bcccb095cd73f13f4410fb20faf48f74
ZigbeeApp/Shared/Phone/ZigBee/Device/BindObj.cs
@@ -5,7 +5,7 @@
namespace ZigBee.Device
{
    [System.Serializable]
    public class BindObj :CommonDevice
    public class BindObj : CommonDevice
    {
        #region 设备绑定
        /// <summary>
@@ -13,7 +13,6 @@
        /// </summary>
        public async System.Threading.Tasks.Task<AddedDeviceBindResponseAllData> AddDeviceBindAsync(AddBindData addBindData)
        {
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                var result = new AddedDeviceBindResponseAllData();
@@ -74,10 +73,11 @@
                        {
                            result.addBindResultResponseData = tempData;
                        }
                        DebugPrintLog($"UI收到通知后的主题_{topic}");
                    }
                };
                mainGateway.Actions += action;
                DebugPrintLog("Bind/SetBind_Actions 启动" + "_" + System.DateTime.Now.ToString());
                DebugPrintLog("Bind/SetBind_Actions 启动" + "_" + System.DateTime.Now.ToLongTimeString() + " " + System.DateTime.Now.Millisecond);
                try
                {
                    if (addBindData != null)
@@ -90,9 +90,9 @@
                            {
                                var dInfo = new JObject{
                            { "BindMacAddr",bindInfo.BindMacAddr},
                         { "BindEpoint", bindInfo.BindEpoint} ,
                         { "BindCluster", bindInfo.BindCluster} ,
                         { "BindType",bindInfo.BindType}
                            { "BindEpoint", bindInfo.BindEpoint} ,
                            { "BindCluster", bindInfo.BindCluster} ,
                            { "BindType",bindInfo.BindType}
                            };
                                bindList.Add(dInfo);
                            }
@@ -100,15 +100,15 @@
                            {
                                var dInfo = new JObject{
                             { "BindCluster", bindInfo.BindCluster} ,
                         { "BindType",bindInfo.BindType},
                         { "BindScenesId", bindInfo.BindScenesId}
                             { "BindType",bindInfo.BindType},
                             { "BindScenesId", bindInfo.BindScenesId}
                            };
                                bindList.Add(dInfo);
                            }
                        }
                        var data = new JObject{
                    {"BindName",addBindData.BindName},
                    { "BindList", bindList }
                            {"BindName",addBindData.BindName},
                             { "BindList", bindList }
                    };
                        jObject.Add("Data", data);
                        //ZbGateway.MainGateWay.CurrentGateWayId = ZbGateway.MainGateWay.getGatewayBaseInfo?.gwID;
@@ -127,7 +127,21 @@
                    {
                        continue;
                    }
                    if (0 < result.addedDeviceBindResponseData.BindList.FindAll((obj) => obj.BindType == 0).Count)
                    if (0 < result.addedDeviceBindResponseData.BindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 1).Count)
                    {
                        if (result.addBindResultResponseData != null)
                        {
                            break;
                        }
                    }
                    else if (0 < result.addedDeviceBindResponseData.BindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 2).Count)
                    {
                        if (result.addBindResultResponseData != null)
                        {
                            break;
                        }
                    }
                    else if (0 < result.addedDeviceBindResponseData.BindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 3).Count)
                    {
                        if (result.addBindResultResponseData != null)
                        {
@@ -144,7 +158,7 @@
                    result.errorMessageBase = " 回复超时,请重新操作";
                }
                mainGateway.Actions -= action;
                System.Console.WriteLine($"Bind/SetBind_Actions 退出 {System.DateTime.Now}");
                System.Console.WriteLine($"Bind/SetBind_Actions 退出 { System.DateTime.Now.ToLongTimeString() + " " + System.DateTime.Now.Millisecond}");
                return result;
            });
        }
@@ -405,7 +419,6 @@
                }
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
@@ -414,7 +427,21 @@
                    {
                        continue;
                    }
                    if (0 < responseData.delDeviceBindResponseData.RemoveBindList.FindAll((obj) => obj.BindType == 0).Count)
                    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 == 4).Count)
                    {
                        if (responseData.removeBindResultResponseData != null)
                        {
@@ -426,14 +453,12 @@
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    responseData.errorMessageBase = "回复超时,请重新操作";
                }
                mainGateway.Actions -= action;
                System.Console.WriteLine("DeviceBind/DelBind_Actions 退出" + System.DateTime.Now.ToString());
                return responseData;
            });
        }
@@ -749,7 +774,7 @@
        /// <summary>
        ///从节点设备本地记录读取绑定列表信息
        /// </summary>
        public async System.Threading.Tasks.Task<GetDeviceLocalBindResponseAllData> GetDeviceLocalBindAsync(int startIndex=0)
        public async System.Threading.Tasks.Task<GetDeviceLocalBindResponseAllData> GetDeviceLocalBindAsync(int startIndex = 0)
        {
            return await System.Threading.Tasks.Task.Run(async () =>
            {
@@ -784,8 +809,8 @@
                    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)
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<GetDeviceLocalBindResponseData>(jobject["Data"].ToString());
                        if (tempData == null)
                        {
                            d = new GetDeviceLocalBindResponseAllData { errorMessageBase = "网关返回的数据为空" };
                        }
@@ -801,8 +826,8 @@
                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 } };
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Cluster_ID", 0 }, { "Command", 5008 } };
                    var data = new JObject { { "StartIndex", startIndex } };
                    mainGateway.Send("Bind/GetDeviceLocalBind", jObject.ToString());
                }
                catch { }
@@ -872,7 +897,7 @@
            /// <summary>
            /// 该次读取返回绑定列表个数
            /// </summary>
            public int ReturnBindNum;
            public int ReturnBindNum;
            /// <summary>
            /// 绑定列表,当Result=0时存在
            /// </summary>
@@ -912,5 +937,121 @@
            public int BindEpoint;
        }
        #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 < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    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
        {
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errorMessageBase;
            /// <summary>
            /// 网关信息错误反馈
            /// <para>当网关接收到客户端信息后,出现以下异常情况将反馈错误。</para>
            /// </summary>
            public ErrorResponData errorResponData;
            /// <summary>
            /// 控制设备绑定的设备列表回复数据
            /// </summary>
            public ClearBindInfoResponseData clearBindInfoResponseData;
        }
        /// <summary>
        /// 删除一个控制设备所有绑定信息回复的数据
        /// </summary>
        public ClearBindInfoResponseData clearBindInfoResponseData;
        [System.Serializable]
        public class ClearBindInfoResponseData
        {
            /// <summary>
            /// 0:成功
            ///<para>1:绑定信息不存在</para>
            /// </summary>
            public int Result;
        }
        #endregion
    }
}