gxc
2020-01-02 00ab3ddb140ba8bb88b5cf572b004a85e1da85e9
ZigbeeApp/Shared/Phone/ZigBee/Device/BindObj.cs
@@ -15,11 +15,11 @@
        {
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                var result = new AddedDeviceBindResponseAllData();
                AddedDeviceBindResponseAllData result = null;
                var mainGateway = ZbGateway.MainGateWay;
                if (mainGateway == null)
                {
                    result.errorMessageBase = "当前没有主网关";
                    result = new AddedDeviceBindResponseAllData { errorMessageBase = "当前没有主网关" };
                    return result;
                }
                Action<string, string> action = (topic, message) =>
@@ -44,37 +44,38 @@
                    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 tempBindList = Newtonsoft.Json.Linq.JArray.Parse(jobject["Data"]["BindList"].ToString());
                        if (tempBindList == null)
                        //var tempBindList = Newtonsoft.Json.Linq.JArray.Parse(jobject["Data"]["BindList"].ToString());
                        var tempResult = Newtonsoft.Json.JsonConvert.DeserializeObject<AddedDeviceBindResponseData>(jobject["Data"].ToString());
                        if (tempResult == null)
                        {
                            result.errorMessageBase = "网关返回的数据为空";
                            result = new AddedDeviceBindResponseAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            var dataBindRes = new AddedDeviceBindResponseData();
                            for (int m = 0; tempBindList != null && m < tempBindList.Count; m++)
                            {
                                var tempBind = tempBindList[m];
                                dataBindRes.BindList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<AddBindListResponseObj>(tempBind.ToString()));
                            }
                            if (dataBindRes != null)
                            {
                                result.addedDeviceBindResponseData = dataBindRes;
                                DebugPrintLog($"UI收到通知后的主题_{topic}");
                            }
                            //var dataBindRes = new AddedDeviceBindResponseData();
                            //for (int m = 0; tempBindList != null && m < tempBindList.Count; m++)
                            //{
                            //    var tempBind = tempBindList[m];
                            //    dataBindRes.BindList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<AddBindListResponseObj>(tempBind.ToString()));
                            //}
                            //if (dataBindRes != null)
                            //{
                            result = new AddedDeviceBindResponseAllData { addedDeviceBindResponseData = tempResult };
                            DebugPrintLog($"UI收到通知后的主题_{topic}");
                            //}
                        }
                    }
                    else if (topic == gatewayID + "/" + "Bind/BindResult")
                    {
                        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 tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<AddBindResultResponseData>(jobject["Data"].ToString());
                    //else if (topic == gatewayID + "/" + "Bind/BindResult")
                    //{
                    //    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 tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<AddBindResultResponseData>(jobject["Data"].ToString());
                        if (tempData != null)
                        {
                            result.addBindResultResponseData = tempData;
                        }
                        DebugPrintLog($"UI收到通知后的主题_{topic}");
                    }
                    //    if (tempData != null)
                    //    {
                    //        result = new AddedDeviceBindResponseAllData { addBindResultResponseData = tempData };
                    //    }
                    //    DebugPrintLog($"UI收到通知后的主题_{topic}");
                    //}
                };
                mainGateway.Actions += action;
                DebugPrintLog("Bind/SetBind_Actions 启动" + "_" + System.DateTime.Now.ToLongTimeString() + " " + System.DateTime.Now.Millisecond);
@@ -111,7 +112,6 @@
                             { "BindList", bindList }
                    };
                        jObject.Add("Data", data);
                        //ZbGateway.MainGateWay.CurrentGateWayId = ZbGateway.MainGateWay.getGatewayBaseInfo?.gwID;
                        mainGateway.Send("Bind/SetBind", jObject.ToString());
                    }
                }
@@ -120,42 +120,54 @@
                }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                while ((DateTime.Now - dateTime).TotalMilliseconds < 20 * 1000)
                {
                    await System.Threading.Tasks.Task.Delay(100);
                    if (result == null || result.addedDeviceBindResponseData == null)
                    {
                        continue;
                    }
                    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)
                        {
                            break;
                        }
                    }
                    else
                    await System.Threading.Tasks.Task.Delay(10);
                    if (result != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    result.errorMessageBase = " 回复超时,请重新操作";
                    result = new AddedDeviceBindResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                //while ((DateTime.Now - dateTime).TotalMilliseconds < 20*1000)
                //{
                //await System.Threading.Tasks.Task.Delay(100);
                //if (result == null || result.addedDeviceBindResponseData == null)
                //{
                //    continue;
                //}
                //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)
                //    {
                //        break;
                //    }
                //}
                //else
                //{
                //    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}");
@@ -203,7 +215,12 @@
            /// <summary>
            ///绑定列表 ,当Status=0时存在
            /// </summary>
            public List<AddBindListResponseObj> BindList = new List<AddBindListResponseObj>();
            public List<BindListResponseObj> BindList = new List<BindListResponseObj>();
            /// <summary>
            /// 0:绑定所有目标成功
            ///<para>1:绑定目标失败,绑定设备目标有漏掉</para>
            /// </summary>
            public int Result = -1;
        }
        /// <summary>
@@ -317,6 +334,124 @@
        /// <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 () =>
@@ -441,7 +576,7 @@
                            break;
                        }
                    }
                    else if (0 < responseData.delDeviceBindResponseData.RemoveBindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 4).Count)
                    else if (0 < responseData.delDeviceBindResponseData.RemoveBindList.FindAll((obj) => obj.BindType == 0 && obj.Result == 2).Count)
                    {
                        if (responseData.removeBindResultResponseData != null)
                        {
@@ -519,7 +654,7 @@
            /// 0:移除成功(该状态只适用用于跨网关绑定、绑定场景、失效设备(设备已经从网关的设备列表中删除)。同网关设备间的解除绑定需要节点设备的确认成功信息,不会直接反馈成功。)
            ///<para>1:失败,设备不在绑定列表中</para>
            ///<para>3:失败,在等待节点设备确认是否解除绑定成功(当网关还在等待某节点设备确认是否解除绑定成功的反馈信息时,客户端再次发送解除绑定该节点设备的指令,将反馈该状态。)</para>
            ///<para>4:未知,由节点设备反馈发送“Bind/RemoveBindResult”主题消息确定是否成功。</para>
            ///<para>2:未知,由节点设备反馈发送“Bind/RemoveBindResult”主题消息确定是否成功。</para>
            /// </summary>
            public int Result;
@@ -997,7 +1132,7 @@
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                while ((DateTime.Now - dateTime).TotalMilliseconds < 25 * 1000)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
@@ -1005,7 +1140,8 @@
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                //一键清除目标,会因为目标数量多而加长时间,目前暂定20秒
                if ((DateTime.Now - dateTime).TotalMilliseconds > 25 * 1000)
                {
                    d = new ClearBindInfoResponseAllData { errorMessageBase = " 回复超时,请重新操作" };
                }