黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
ZigbeeApp/Shared/Phone/UserCenter/Device/Panel/PanelSimpleMutilfunctionTargetsForm.cs
@@ -21,16 +21,9 @@
        /// 构造函数
        /// </summary>
        /// <param name="deviceMac">简约多功能面板Mac</param> 
        public PanelSimpleMutilfunctionTargetsForm(string deviceMac)
        public PanelSimpleMutilfunctionTargetsForm(Panel controlDev)
        {
            curControlDev = new Panel();
            var dev = Common.LocalDevice.Current.GetDevice(deviceMac, 62);//简约面板随便一个回路来获取设备
            if (dev != null)
            {
                curControlDev.CurrentGateWayId = dev.CurrentGateWayId;
            }
            curControlDev.Type = DeviceType.OnOffSwitch;
            curControlDev.DeviceAddr = deviceMac;
            this.curControlDev = controlDev;
        }
        #endregion
@@ -88,8 +81,6 @@
        /// 特殊类型的个数【开关/插座/灯光】需要计算显示的个数 
        /// </summary>
        int specialTypeCount = 0;
        private Action bindReceiveAction = null;
        /// <summary>
        /// 回调绑定目标页面刷新
        /// </summary>
@@ -111,7 +102,7 @@
            CommonPage.Loading.Start("");
            InitLocalDeviceList();
            if (MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.Count == 0)
            if (curControlDev.bindTargetsFromMutilfunctionPanelList.Count == 0)
            {
                IsRead = true;
            }
@@ -157,7 +148,7 @@
            btnAddFrameLayout.AddChidren(btnBindAdd);
            EventHandler<MouseEventArgs> eHandlerAdd = (sender, e) =>
            {
                var simpleMutilfunctionBindTargetsForm = new Shared.Phone.UserCenter.DeviceBind.PanelSimpleMutilfunctionAddTargetsForm(curControlDev.DeviceAddr, curBindType);
                var simpleMutilfunctionBindTargetsForm = new Shared.Phone.UserCenter.DeviceBind.PanelSimpleMutilfunctionAddTargetsForm(curControlDev, curBindType);
                Shared.Phone.UserView.HomePage.Instance.AddChidren(simpleMutilfunctionBindTargetsForm);
                Shared.Phone.UserView.HomePage.Instance.PageIndex += 1;
                simpleMutilfunctionBindTargetsForm.Show();
@@ -374,7 +365,25 @@
        void RefreshBindListUI()
        {
            midVerticalScrolViewLayout.RemoveAll();
            curBindTypeList = MutilfunctionPanelMethod.GetMatchBindList(curBindType);
            curBindTypeList = MutilfunctionPanelMethod.GetMatchBindList(curControlDev, curBindType);
            //注意!同个按键端点如果支持绑定多种功能类型,需要注意如下处理
            //当更换设备功能类型后
            // 绑定的开关、插座、灯光所用的按键回路互相关联,所以这里重新获取将这三种类型的其他2种,为了获取正确的开关、插座、灯光绑定的个数
            switch (curBindType)
            {
                case 1:
                    MutilfunctionPanelMethod.GetMatchBindList(curControlDev, 2);
                    MutilfunctionPanelMethod.GetMatchBindList(curControlDev, 3);
                    break;
                case 2:
                    MutilfunctionPanelMethod.GetMatchBindList(curControlDev, 1);
                    MutilfunctionPanelMethod.GetMatchBindList(curControlDev, 3);
                    break;
                case 3:
                    MutilfunctionPanelMethod.GetMatchBindList(curControlDev, 1);
                    MutilfunctionPanelMethod.GetMatchBindList(curControlDev, 2);
                    break;
            }
            int curIndex = 0;
            for (int i = 0; i < curBindTypeList.Count; i++)
            {
@@ -434,7 +443,7 @@
                {
                    //被绑定设备图片【可能和外面设备等图片不同,因为这里是以功能来绑定的】
                    //devicePic.UnSelectedImagePath = tempDev.IconPath;
                    var device = LocalDevice.Current.GetDevice(bindObj.BindMacAddr, bindObj.BindEpoint);
                    var device = HdlDeviceCommonLogic.Current.GetDevice(bindObj.BindMacAddr, bindObj.BindEpoint);
                    switch (curBindType)
                    {
                        case 1:
@@ -474,7 +483,7 @@
                    if (device != null)
                    {
                        //设备名字
                        btnBindNameText = Common.LocalDevice.Current.GetDeviceEpointName(device);
                        btnBindNameText = HdlDeviceCommonLogic.Current.GetDeviceEpointName(device);
                        //获取设备所属房间
                        var tempDevRoom = HdlRoomLogic.Current.GetRoomByDevice(device);
@@ -528,7 +537,7 @@
                var btnDel = rowLayout.AddDeleteControl();
                btnDel.ButtonClickEvent += (sender, e) =>
                {
                    RemoveTargets(bindObj, btnDel);
                    ClearTargets(bindObj);
                };
                #endregion
            }
@@ -562,7 +571,7 @@
            localDeviceList.Clear();
            scList.Clear();
            //设备
            foreach (var dev in Shared.Common.LocalDevice.Current.listAllDevice)
            foreach (var dev in HdlDeviceCommonLogic.Current.listAllDevice)
            {
                localDeviceList.Add(dev);
            }
@@ -582,7 +591,7 @@
                    GetDeviceBindResponseAllData getBindList = null;
                    if (IsRead)
                    {
                        MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.Clear();
                        curControlDev.bindTargetsFromMutilfunctionPanelList.Clear();
                        var epointList = new List<int>();
                        for (int i = 2; i < 62; i++)
                        {
@@ -614,11 +623,10 @@
                                    curD.BindScenesId = dev.BindScenesId;
                                    curD.BindMacAddr = dev.BindMacAddr;
                                    curD.BindEpoint = dev.BindEpoint;
                                    curD.ESName = dev.ESName;
                                    curD.BindType = dev.BindType;
                                    bList.Add(curD);
                                }
                                MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.Add(curControlDev.DeviceAddr + curControlDev.DeviceEpoint, bList);
                                curControlDev.bindTargetsFromMutilfunctionPanelList.Add(curControlDev.DeviceAddr + curControlDev.DeviceEpoint, bList);
                                oldReadEpoint++;
                            }
@@ -647,7 +655,7 @@
                        //获取所有类型的绑定目标个数
                        for (int i = 0; i < 7; i++)
                        {
                            curBindTypeList = MutilfunctionPanelMethod.GetMatchBindList(i);
                            curBindTypeList = MutilfunctionPanelMethod.GetMatchBindList(curControlDev, i);
                        }
                        Application.RunOnMainThread(() =>
@@ -688,7 +696,7 @@
        /// <param name="bindDevice"></param>
        /// <param name="btnDel"></param>
        /// <returns></returns>
        private void RemoveTargets(BindListAllInfo bindDevice, Button btnDel)
        private void RemoveTargets(BindListAllInfo bindDevice)
        {
            var delDevice = new DelDeviceBindData();
            delDevice.DeviceAddr = bindDevice.KeyMacAddr;
@@ -707,7 +715,7 @@
                switch (curBindType)
                {
                    case 3:
                        var device = LocalDevice.Current.GetDevice(bindDevice.BindMacAddr, bindDevice.BindEpoint);
                        var device = HdlDeviceCommonLogic.Current.GetDevice(bindDevice.BindMacAddr, bindDevice.BindEpoint);
                        if (device != null)
                        {
                            if (device.Type == DeviceType.DimmableLight)
@@ -771,9 +779,9 @@
                                {
                                    if (d.Result == 0 || d.Result == 1)
                                    {
                                        if (MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.ContainsKey(bindDevice.KeyMacAddr + bindDevice.KeyEpoint))
                                        if (curControlDev.bindTargetsFromMutilfunctionPanelList.ContainsKey(bindDevice.KeyMacAddr + bindDevice.KeyEpoint))
                                        {
                                            MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.Remove(bindDevice.KeyMacAddr + bindDevice.KeyEpoint);
                                            curControlDev.bindTargetsFromMutilfunctionPanelList.Remove(bindDevice.KeyMacAddr + bindDevice.KeyEpoint);
                                        }
                                        Application.RunOnMainThread(() =>
                                        {
@@ -806,9 +814,9 @@
                        {
                            if (delResult.removeBindResultResponseData.Result == 0)
                            {
                                if (MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.ContainsKey(bindDevice.KeyMacAddr + bindDevice.KeyEpoint))
                                if (curControlDev.bindTargetsFromMutilfunctionPanelList.ContainsKey(bindDevice.KeyMacAddr + bindDevice.KeyEpoint))
                                {
                                    MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.Remove(bindDevice.KeyMacAddr + bindDevice.KeyEpoint);
                                    curControlDev.bindTargetsFromMutilfunctionPanelList.Remove(bindDevice.KeyMacAddr + bindDevice.KeyEpoint);
                                }
                                Application.RunOnMainThread(() =>
                                {
@@ -839,7 +847,68 @@
            });
        }
        /// <summary>
        /// 清除多功能面板的目标
        /// </summary>
        /// <param name="bindDevice"></param>
        /// <param name="btnDel"></param>
        /// <returns></returns>
        private void ClearTargets(BindListAllInfo bindDevice)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    Application.RunOnMainThread(() =>
                    {
                        CommonPage.Loading.Start("");
                    });
                    var currentKey = new Panel();
                    currentKey.DeviceAddr = bindDevice.KeyMacAddr;
                    currentKey.DeviceEpoint = bindDevice.KeyEpoint;
                    currentKey.CurrentGateWayId = curControlDev.CurrentGateWayId;
                    var bindResult = HdlDeviceBindLogic.Current.ClearBindInfoAsync(currentKey);
                    if (bindResult != null && bindResult.clearBindInfoResponseData != null)
                    {
                        if (bindResult.clearBindInfoResponseData.Result != 0)
                        {
                            Application.RunOnMainThread(() =>
                            {
                                new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.RemoveFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(CommonPage.Instance);
                                CommonPage.Loading.Hide();
                            });
                        }
                        else
                        {
                            if (curControlDev.bindTargetsFromMutilfunctionPanelList.ContainsKey(bindDevice.KeyMacAddr + bindDevice.KeyEpoint))
                            {
                                curControlDev.bindTargetsFromMutilfunctionPanelList.Remove(bindDevice.KeyMacAddr + bindDevice.KeyEpoint);
                            }
                            Application.RunOnMainThread(() =>
                            {
                                RefreshBindListUI();
                                CommonPage.Loading.Hide();
                            });
                        }
                    }
                    else
                    {
                        Application.RunOnMainThread(() =>
                        {
                            new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime) + "(" + "5006" + ")", Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(CommonPage.Instance);
                        });
                    }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        CommonPage.Loading.Hide();
                    });
                }
            });
        }
        /// <summary>
        /// 匹配的类型列表
@@ -858,148 +927,6 @@
            return textTypelist;
        }
        #endregion
        #region 其他方法
        /// <summary>
        /// 显示绑定设备线程是否已经开启
        /// </summary>
        private bool isDeviceThreadStart = false;
        /// <summary>
        /// 主题超时的线程是否开启
        /// </summary>
        private bool isTopicTimeOutThreadStart = false;
        /// <summary>
        /// 等待设备的回馈的超时时间(单位:百毫秒)
        /// </summary>
        private int waitDeviceTimeOut = 20;
        /// <summary>
        /// 网关ID
        /// </summary>
        private string gatewayId = string.Empty;
        /// <summary>
        /// 接收个数
        /// </summary>
        private int countBind = 0;
        /// <summary>
        /// 新上报的设备
        /// </summary>
        private Dictionary<int, GetPanelBindResponseDataByMac> dicPanelResInfo = new Dictionary<int, GetPanelBindResponseDataByMac>();
        ///<summary >
        /// 发送获取面板命令[通过DeviceAddr获取]
        /// </summary>
        private void SendPanelCommand()
        {
            var jObject = new JObject { { "DeviceAddr", curControlDev.DeviceAddr }, { "Cluster_ID", 0 }, { "Command", 5009 } };
            curControlDev.Gateway.Send(("SearchNewDevice"), jObject.ToString());
        }
        /// <summary>
        /// 检测设备绑定的主题
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="resultData"></param>
        /// <returns></returns>
        public bool CheckIsDeviceBindTopic(string topic)
        {
            if (topic == gatewayId + "Bind/GetDeviceLocalBind_Respon")
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 处理面板绑定的设备,和端点没有关系
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="resultData">上报数据</param>
        private void AdjustGatewayResultData(string topic, string resultData)
        {
            //接收绑定设备,开启接收绑定设备信息的线程(里面会等待三秒这样)
            this.ReceiveSuccessFormThread();
            var result = this.CheckIsDeviceBindTopic(topic);
            if (result)
            {
                lock (dicPanelResInfo)
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                    var info = Newtonsoft.Json.JsonConvert.DeserializeObject<GetPanelBindResponseDataByMac>(jobject["Data"].ToString());
                    countBind = info.MaxNumItems;
                    if (info != null)
                    {
                        foreach (var bDev in info.BindList)
                        {
                            if (this.dicPanelResInfo.ContainsKey(bDev.ControllerEpoint) == false)
                            {
                                this.dicPanelResInfo[bDev.ControllerEpoint] = info;
                            }
                        }
                    }
                    if (this.dicPanelResInfo.Count == countBind)
                    {
                        //停止接收
                        curControlDev.Gateway.GwResDataAction -= this.AdjustGatewayResultData;
                        //刷新超时时间
                        this.waitDeviceTimeOut = -1;
                    }
                    else
                    {
                        //刷新超时时间
                        this.waitDeviceTimeOut = 20;
                    }
                }
            }
        }
        /// <summary>
        /// 开启接收绑定设备的超时线程
        /// </summary>
        private void ReceiveSuccessFormThread()
        {
            if (this.isDeviceThreadStart == true)
            {
                //线程已经开启
                return;
            }
            this.isDeviceThreadStart = true;
            HdlThreadLogic.Current.RunThread(() =>
            {
                System.Console.WriteLine($"1AAAAA");
                while (this.waitDeviceTimeOut >= 0)
                {
                    //等待下一个回路
                    System.Threading.Thread.Sleep(100);
                    this.waitDeviceTimeOut--;
                    System.Console.WriteLine($"2AAAAA");
                }
                System.Console.WriteLine($"3AAAAA");
                //停止接收
                bindReceiveAction.Invoke();
                curControlDev.Gateway.GwResDataAction -= this.AdjustGatewayResultData;
                System.Console.WriteLine($"4AAAAA");
                System.Threading.Thread.Sleep(200);
                System.Console.WriteLine($"5AAAAA");
                //目前就弄一个
                Application.RunOnMainThread(() =>
                {
                    if (this.dicPanelResInfo.Count < countBind)
                    {
                        //响应超时,请重新下拉刷新获取绑定信息
                        new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndReAccessNetwork), Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(Common.CommonPage.Instance);
                        //停止接收
                        curControlDev.Gateway.GwResDataAction -= this.AdjustGatewayResultData;
                    }
                    System.Console.WriteLine($"6AAAAA");
                });
            });
        }
        #endregion
    }