黄学彪
2020-07-01 304dca51c28183a9dfc192c6b93ea1c00bdd5d97
ZigbeeApp/Shared/Phone/UserCenter/Device/Panel/PanelSimpleMutilfunctionTargetsForm.cs
@@ -1,10 +1,1006 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;
using Shared.Common;
using Shared.Phone.UserCenter.Device.Bind;
using Shared.Phone.UserCenter.Safety;
using ZigBee.Device;
using static ZigBee.Device.BindObj;
using static ZigBee.Device.Panel;
namespace Shared.Phone.UserCenter.DeviceBind
{
    /// <summary>
    /// 简约多功能面板
    /// 当前界面默认选择第一个楼层,第一个类型
    /// </summary>
    public class PanelSimpleMutilfunctionTargetsForm : BindCommonLayout
    {
        public PanelSimpleMutilfunctionTargetsForm()
        #region  构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deviceMac">简约多功能面板Mac</param>
        public PanelSimpleMutilfunctionTargetsForm(string deviceMac)
        {
            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;
        }
        #endregion
        #region  变量申明
        /// <summary>
        /// 控制设备
        /// </summary>
        Panel curControlDev;
        /// <summary>
        /// 显示被绑定设备或场景的view
        /// </summary>
        VerticalFrameRefreshControl midVerticalScrolViewLayout;
        /// <summary>
        /// 显示绑定类型
        /// </summary>
        FrameLayout typeFrameLayout;
        /// <summary>
        /// 绑定类型提示文本
        /// </summary>
        Button btnTipText;
        /// <summary>
        /// 保存完成按钮
        /// </summary>
        Button btnFinifh;
        /// <summary>
        /// 本地设备列表
        /// </summary>
        private System.Collections.Generic.List<CommonDevice> localDeviceList = new System.Collections.Generic.List<CommonDevice>();
        /// <summary>
        /// 本地场景列表
        /// </summary>
        private System.Collections.Generic.List<SceneUI> scList = new System.Collections.Generic.List<SceneUI> { };
        /// <summary>
        /// 当前类型的绑定表
        /// </summary>
        public List<BindListAllInfo> curBindTypeList = new List<BindListAllInfo>();
        /// <summary>
        /// 当前类型
        /// 0:场景  1:开关  2:插座  3:灯光  4:遮阳 5:空调 6:新风
        /// </summary>
        int curBindType = 0;
        /// <summary>
        /// 是否支持读取
        /// </summary>
        bool IsRead = false;
        /// <summary>
        /// 是否支持再次读取【当目标读取不全情况下重新读取】
        /// </summary>
        bool IsAgain = false;
        /// <summary>
        /// 上一次读取端点 
        /// </summary>
        private int oldReadEpoint = 2;
        /// <summary>
        /// 特殊类型的个数【开关/插座/灯光】需要计算显示的个数 
        /// </summary>
        int specialTypeCount = 0;
        private Action bindReceiveAction = null;
        /// <summary>
        /// 回调绑定目标页面刷新
        /// </summary>
        public Action<List<BindListResponseObj>> action;
        #endregion
        #region UI设计
        /// <summary>
        /// 目标页显示
        /// </summary>
        public void Show()
        {
            //标题栏
            TitleUI();
            //中部UI【标题栏下的UI】
            MidFrameLayouUI();
            //类型栏UI
            BindTypeUI();
            CommonPage.Loading.Start("");
            InitLocalDeviceList();
            if (MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.Count == 0)
            {
                IsRead = true;
            }
            else
            {
                IsRead = false;
            }
            InitData();
        }
        /// <summary>
        /// 标题栏
        /// </summary>
        void TitleUI()
        {
            string titleText = "";
            titleText = Language.StringByID(R.MyInternationalizationString.uBindTargets);
            this.TopFrameLayout(this, titleText);
            EventHandler<MouseEventArgs> eHandlerBack = (sender, e) =>
            {
                RemoveFromParent();
            };
            this.btnTitle.Width = Application.GetRealWidth(1080 - 161 - 300);
            this.btnBack.MouseUpEventHandler += eHandlerBack;
            this.btnBackFrameLayout.MouseUpEventHandler += eHandlerBack;
            this.MidFrameLayout(this);
            var btnAddFrameLayout = new FrameLayout()
            {
                X = Application.GetRealWidth(772 - 58),
                Width = Application.GetRealWidth(192),
            };
            this.titleFrameLayout.AddChidren(btnAddFrameLayout);
            var btnBindAdd = new Button
            {
                X = Application.GetRealWidth(62),
                Height = Application.GetMinReal(72),
                Width = Application.GetMinReal(72),
                UnSelectedImagePath = "BindPic/BindAdd.png",
            };
            btnAddFrameLayout.AddChidren(btnBindAdd);
            EventHandler<MouseEventArgs> eHandlerAdd = (sender, e) =>
            {
                var simpleMutilfunctionBindTargetsForm = new Shared.Phone.UserCenter.DeviceBind.PanelSimpleMutilfunctionAddTargetsForm(curControlDev.DeviceAddr, curBindType);
                Shared.Phone.UserView.HomePage.Instance.AddChidren(simpleMutilfunctionBindTargetsForm);
                Shared.Phone.UserView.HomePage.Instance.PageIndex += 1;
                simpleMutilfunctionBindTargetsForm.Show();
                Action action = () =>
                {
                    RefreshBindListUI();
                };
                simpleMutilfunctionBindTargetsForm.actionRefreshBindList += action;
            };
            btnBindAdd.MouseDownEventHandler += eHandlerAdd;
            btnAddFrameLayout.MouseDownEventHandler += eHandlerAdd;
        }
        /// <summary>
        /// 中部UI【标题栏下的UI】
        /// </summary>
        void MidFrameLayouUI()
        {
            //类型栏1
            typeFrameLayout = new FrameLayout
            {
                X = Application.GetRealWidth(58 / 2),
                Height = Application.GetRealHeight(323),
            };
            this.midFrameLayout.AddChidren(typeFrameLayout);
            //设备栏
            midVerticalScrolViewLayout = new VerticalFrameRefreshControl()
            {
                Y = Application.GetRealHeight(323),
                Height = Application.GetRealHeight(1065),
            };
            this.midFrameLayout.AddChidren(midVerticalScrolViewLayout);
            midVerticalScrolViewLayout.BeginHeaderRefreshingAction += () =>
            {
                midVerticalScrolViewLayout.BeginHeaderRefreshing();
                IsRead = true;
                InitData();
            };
            //底部保存栏
            var bottomFrameLayout = new FrameLayout()
            {
                Width = LayoutParams.MatchParent,
                Height = Application.GetRealHeight(84 * 2 + 127),
                Y = Application.GetRealHeight(1388),
            };
            this.midFrameLayout.AddChidren(bottomFrameLayout);
            btnTipText = new Button()
            {
                Height = Application.GetRealHeight(84),
                TextID = R.MyInternationalizationString.Tip,
                TextColor = Shared.Common.ZigbeeColor.Current.XMGray3,
                TextSize = 12,
                Text = MutilfunctionPanelMethod.MatchTypeBindTextTip(curBindType),
                TextAlignment = TextAlignment.Center,
            };
            bottomFrameLayout.AddChidren(btnTipText);
            btnFinifh = new Button()
            {
                Width = Application.GetRealWidth(907),
                Height = Application.GetRealHeight(127),
                Y = Application.GetRealHeight(49 + 35),
                Gravity = Gravity.CenterHorizontal,
                Radius = (uint)Application.GetRealHeight(127) / 2,
                TextID = R.MyInternationalizationString.Save,
                BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack,
                TextColor = Shared.Common.ZigbeeColor.Current.XMWhite,
                IsBold = true,
                TextSize = 16,
            };
            bottomFrameLayout.AddChidren(btnFinifh);
            btnFinifh.MouseUpEventHandler += (sender, e) =>
            {
                SaveTarget();
            };
        }
        /// <summary>
        /// 类型栏UI
        /// </summary>
        void BindTypeUI()
        {
            //匹配的类型列表
            var typeList = GetTypeList();
            int index = 0;
            Button curentOldType = null;
            FrameLayout curentOldTypeLayout = null;
            foreach (var typeText in typeList)
            {
                var btnTypeLayout = new FrameLayout
                {
                    Height = Application.GetMinRealAverage(159),
                    Width = Application.GetMinRealAverage(255),
                    Y = Application.GetRealHeight(23),
                    BackgroundImagePath = "Item/RoomIconBackground.png",
                    BorderWidth = 1,
                };
                typeFrameLayout.AddChidren(btnTypeLayout);
                var btnType = new Button
                {
                    Height = Application.GetRealHeight(58),
                    Width = Application.GetRealWidth(127),
                    Y = Application.GetRealHeight(58),
                    X = Application.GetRealWidth(14),
                    Text = typeText,
                    TextSize = 12,
                    TextColor = Shared.Common.ZigbeeColor.Current.XMGray3,
                    Gravity = Gravity.Center,
                };
                btnTypeLayout.AddChidren(btnType);
                btnType.IsSelected = false;
                if (index == 0)
                {
                    btnTypeLayout.Y = Application.GetRealHeight(0);
                    btnTypeLayout.BackgroundImagePath = "Item/RoomIconBackgroundSelected.png";
                    btnType.TextColor = Shared.Common.ZigbeeColor.Current.XMWhite;
                    curentOldType = btnType;
                    curentOldTypeLayout = btnTypeLayout;
                }
                else
                {
                    btnTypeLayout.Y = Application.GetRealHeight(0);
                    btnTypeLayout.X = index * Application.GetRealWidth(260);
                    if (index > 3)
                    {
                        btnTypeLayout.Y = Application.GetRealHeight(58 + 78);
                        if (index == 4)
                        {
                            btnTypeLayout.X = Application.GetRealWidth(0);
                        }
                        else
                        {
                            btnTypeLayout.X = (index - 4) * Application.GetRealWidth(260);
                        }
                    }
                }
                EventHandler<MouseEventArgs> eHandlerRoom = (sender, e) =>
                {
                    if (!curentOldType.IsSelected)
                    {
                        if (curentOldType != null)
                        {
                            curentOldType.TextColor = Shared.Common.ZigbeeColor.Current.XMGray3;
                        }
                        curentOldType = btnType;
                        curentOldType.TextColor = Shared.Common.ZigbeeColor.Current.XMWhite;
                    }
                    if (btnTypeLayout.BorderWidth == 1)
                    {
                        if (curentOldTypeLayout != null)
                        {
                            curentOldTypeLayout.BorderWidth = 1;
                            curentOldTypeLayout.BorderColor = Shared.Common.ZigbeeColor.Current.XMOrange;
                            curentOldTypeLayout.BackgroundImagePath = "Item/RoomIconBackground.png";
                        }
                        curentOldTypeLayout = btnTypeLayout;
                        curentOldTypeLayout.BorderWidth = 0;
                        curentOldTypeLayout.BackgroundImagePath = "Item/RoomIconBackgroundSelected.png";
                    }
                    if (curentOldType.Text == Language.StringByID(R.MyInternationalizationString.scene))
                    {
                        curBindType = 0;
                        RefreshBindListUI();
                    }
                    else if (curentOldType.Text == Language.StringByID(R.MyInternationalizationString.uDeviceBelongId13))
                    {
                        curBindType = 1;
                        RefreshBindListUI();
                    }
                    else if (curentOldType.Text == Language.StringByID(R.MyInternationalizationString.uDeviceBelongId14))
                    {
                        curBindType = 2;
                        RefreshBindListUI();
                    }
                    else if (curentOldType.Text == Language.StringByID(R.MyInternationalizationString.uDeviceBelongId15))
                    {
                        curBindType = 3;
                        RefreshBindListUI();
                    }
                    else if (curentOldType.Text == Language.StringByID(R.MyInternationalizationString.uDeviceBelongId100))
                    {
                        curBindType = 4;
                        RefreshBindListUI();
                    }
                    else if (curentOldType.Text == Language.StringByID(R.MyInternationalizationString.uDeviceBelongId3600))
                    {
                        curBindType = 5;
                        RefreshBindListUI();
                    }
                    else if (curentOldType.Text == Language.StringByID(R.MyInternationalizationString.uDeviceBelongId2310))
                    {
                        curBindType = 6;
                        RefreshBindListUI();
                    }
                };
                btnType.MouseUpEventHandler += eHandlerRoom;
                btnTypeLayout.MouseUpEventHandler += eHandlerRoom;
                index++;
            }
        }
        /// <summary>
        /// 绑定表显示
        /// </summary>
        void RefreshBindListUI()
        {
            midVerticalScrolViewLayout.RemoveAll();
            curBindTypeList = MutilfunctionPanelMethod.GetMatchBindList(curBindType);
            int curIndex = 0;
            for (int i = 0; i < curBindTypeList.Count; i++)
            {
                curIndex = i;
                var bindObj = curBindTypeList[i];
                SceneUI curSceneUI = null;
                var rowLayout = new RowLayoutControl(midVerticalScrolViewLayout.rowSpace / 2);
                rowLayout.BackgroundColor = ZigbeeColor.Current.XMWhite;
                midVerticalScrolViewLayout.AddChidren(rowLayout);
                rowLayout.frameTable.UseClickStatu = false;
                var devicePic = rowLayout.frameTable.AddLeftIcon();
                devicePic.Y = Application.GetRealHeight(25);//49
                devicePic.UnSelectedImagePath = "DoorLock/DoorLockUserPic.png";
                #region 绑定数据处理
                int currentIndex = i;
                var btnBindNameText = "";
                var btnFloorRoomNameText = "";
                if (currentIndex == curBindTypeList.Count - 1)
                {
                    rowLayout.LineColor = Shared.Common.ZigbeeColor.Current.XMWhite;
                }
                if (curBindType == 0)
                {
                    //Scene
                    curSceneUI = HdlSceneLogic.Current.GetSceneUIBySceneId(bindObj.BindScenesId);
                    devicePic.UnSelectedImagePath = "Scene/SceneIcon.png";
                    if (curSceneUI == null)
                    {
                        if (string.IsNullOrEmpty(bindObj.ESName))
                        {
                            btnBindNameText = Language.StringByID(R.MyInternationalizationString.OffLineScene) + "_" + "ID" + "_" + bindObj.BindScenesId.ToString();
                        }
                        else
                        {
                            btnBindNameText = Language.StringByID(R.MyInternationalizationString.OffLineScene) + "_" + bindObj.ESName;
                        }
                        btnFloorRoomNameText = Language.StringByID(R.MyInternationalizationString.Undistributed);
                    }
                    else
                    {
                        btnBindNameText = curSceneUI.Name;
                        string myName = HdlSceneLogic.Current.GetZoneById(curSceneUI.Id);
                        if (myName != null)
                        {
                            btnFloorRoomNameText = myName;
                        }
                        else
                        {
                            btnFloorRoomNameText = Language.StringByID(R.MyInternationalizationString.Undistributed);
                        }
                    }
                }
                else
                {
                    //被绑定设备图片【可能和外面设备等图片不同,因为这里是以功能来绑定的】
                    //devicePic.UnSelectedImagePath = tempDev.IconPath;
                    var device = LocalDevice.Current.GetDevice(bindObj.BindMacAddr, bindObj.BindEpoint);
                    switch (curBindType)
                    {
                        case 1:
                            devicePic.UnSelectedImagePath = "BindPic/Switch.png";
                            break;
                        case 2:
                            devicePic.UnSelectedImagePath = "Device/Socket1.png";
                            break;
                        case 3:
                            devicePic.UnSelectedImagePath = "Device/Light.png";
                            break;
                        case 4:
                            if (device != null)
                            {
                                if (device.Type == DeviceType.WindowCoveringDevice)
                                {
                                    var tempCur = device as Rollershade;
                                    if (tempCur.WcdType == 4)
                                    {
                                        devicePic.UnSelectedImagePath = "BindPic/Curtain.png";
                                    }
                                    else
                                    {
                                        devicePic.UnSelectedImagePath = "BindPic/Roller.png";
                                    }
                                }
                            }
                            break;
                        case 5:
                            devicePic.UnSelectedImagePath = "Device/AirConditionerEpoint.png";
                            break;
                        case 6:
                            devicePic.UnSelectedImagePath = "Device/FreshAirEpoint.png";
                            break;
                    }
                    if (device != null)
                    {
                        //设备名字
                        btnBindNameText = Common.LocalDevice.Current.GetDeviceEpointName(device);
                        //获取设备所属房间
                        var tempDevRoom = HdlRoomLogic.Current.GetRoomByDevice(device);
                        if (tempDevRoom != null)
                        {
                            var tempDevFloorId = tempDevRoom.FloorId;
                            //获取房间的名字
                            var tempDevRoomName = tempDevRoom.Name;
                            //获取楼层的名字
                            var tempDevFloorName = HdlResidenceLogic.Current.GetFloorNameById(tempDevFloorId);
                            if (string.IsNullOrEmpty(tempDevFloorName))
                            {
                                if (string.IsNullOrEmpty(tempDevRoomName))
                                {
                                    btnFloorRoomNameText = Language.StringByID(R.MyInternationalizationString.Undistributed);
                                }
                                else
                                {
                                    btnFloorRoomNameText = tempDevRoomName;
                                }
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(tempDevRoomName))
                                {
                                    btnFloorRoomNameText = Language.StringByID(R.MyInternationalizationString.Undistributed);
                                }
                                else
                                {
                                    btnFloorRoomNameText = tempDevFloorName + "," + tempDevRoomName;
                                }
                            }
                        }
                        else
                        {
                            btnFloorRoomNameText = Language.StringByID(R.MyInternationalizationString.Undistributed);
                        }
                    }
                    else
                    {
                        devicePic.TextAlignment = TextAlignment.CenterLeft;
                        devicePic.TextColor = UserCenterColor.Current.Gray;
                        devicePic.Text = Language.StringByID(R.MyInternationalizationString.uOffLine);
                    }
                }
                var btnBindName = rowLayout.frameTable.AddTopView(btnBindNameText, 800);
                var btnFloorRoomName = rowLayout.frameTable.AddBottomView(btnFloorRoomNameText, 800);
                rowLayout.frameTable.AddBottomLine();
                var btnDel = rowLayout.AddDeleteControl();
                btnDel.ButtonClickEvent += (sender, e) =>
                {
                    RemoveTargets(bindObj, btnDel);
                };
                #endregion
            }
            switch (curBindType)
            {
                case 1:
                    btnTipText.Text = MutilfunctionPanelMethod.MatchTypeBindTextTip(curBindType, MutilfunctionPanelMethod.curSwitchCount + MutilfunctionPanelMethod.curSocketCount + MutilfunctionPanelMethod.curLightFromRelayCount);
                    break;
                case 2:
                    btnTipText.Text = MutilfunctionPanelMethod.MatchTypeBindTextTip(curBindType, MutilfunctionPanelMethod.curSwitchCount + MutilfunctionPanelMethod.curSocketCount + MutilfunctionPanelMethod.curLightFromRelayCount);
                    break;
                case 3:
                    btnTipText.Text = MutilfunctionPanelMethod.MatchTypeBindTextTip(curBindType, MutilfunctionPanelMethod.curSwitchCount + MutilfunctionPanelMethod.curSocketCount + MutilfunctionPanelMethod.curLightFromRelayCount + MutilfunctionPanelMethod.curLightCount);
                    break;
                default:
                    btnTipText.Text = MutilfunctionPanelMethod.MatchTypeBindTextTip(curBindType, curBindTypeList.Count);
                    break;
            }
            midVerticalScrolViewLayout.AdjustTableHeight(Application.GetRealHeight(23));
        }
        #endregion
        #region 数据处理
        /// <summary>
        /// 获取本地列表
        /// </summary>
        void InitLocalDeviceList()
        {
            localDeviceList.Clear();
            scList.Clear();
            //设备
            foreach (var dev in Shared.Common.LocalDevice.Current.listAllDevice)
            {
                localDeviceList.Add(dev);
            }
            //场景
            scList = HdlSceneLogic.Current.GetAllRoomSceneList();
        }
        /// <summary>
        /// 初始化设备数据
        /// </summary>
        void InitData()
        {
            System.Threading.Tasks.Task.Run(async () =>
            {
                try
                {
                    //读取按键当前绑定目标  多功能面板目标总共需要读取本页的目标60个,3秒左右(3秒+200毫秒)
                    GetDeviceBindResponseAllData getBindList = null;
                    if (IsRead)
                    {
                        MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.Clear();
                        var epointList = new List<int>();
                        for (int i = 2; i < 62; i++)
                        {
                            epointList.Add(i);
                        }
                        //读取上次没读全读目标
                        if (IsAgain)
                        {
                            epointList.Clear();
                            for (int i = oldReadEpoint; i < 62; i++)
                            {
                                epointList.Add(i);
                            }
                        }
                        foreach (var epoint in epointList)
                        {
                            curControlDev.DeviceEpoint = epoint;
                            getBindList = HdlDeviceBindLogic.Current.GetDeviceBindAsync(curControlDev);
                            if (getBindList != null && getBindList.getAllBindResponseData != null)
                            {
                                var bList = new List<BindListAllInfo>();
                                foreach (var dev in getBindList.getAllBindResponseData.BindList)
                                {
                                    var curD = new BindListAllInfo();
                                    curD.KeyMacAddr = curControlDev.DeviceAddr;
                                    curD.KeyEpoint = epoint;
                                    curD.BindCluster = dev.BindCluster;
                                    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);
                                oldReadEpoint++;
                            }
                            else
                            {
                                if (oldReadEpoint < 62)
                                {
                                    IsAgain = true;
                                }
                                Application.RunOnMainThread(() =>
                                {
                                    RefreshBindListUI();
                                    midVerticalScrolViewLayout.EndHeaderRefreshing();
                                    CommonPage.Loading.Hide();
                                    new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(Common.CommonPage.Instance);
                                });
                                break;
                            }
                        }
                        if (oldReadEpoint >= 61)
                        {
                            IsAgain = false;
                            oldReadEpoint = 2;
                        }
                        //获取所有类型的绑定目标个数
                        for (int i = 0; i < 7; i++)
                        {
                            curBindTypeList = MutilfunctionPanelMethod.GetMatchBindList(i);
                        }
                        Application.RunOnMainThread(() =>
                        {
                            RefreshBindListUI();
                            midVerticalScrolViewLayout.EndHeaderRefreshing();
                            CommonPage.Loading.Hide();
                        });
                    }
                    else
                    {
                        Application.RunOnMainThread(() =>
                        {
                            RefreshBindListUI();
                            CommonPage.Loading.Hide();
                        });
                    }
                }
                catch (Exception ex)
                {
                    var mess = ex.Message;
                }
            });
        }
        /// <summary>
        /// 保存目标
        /// </summary>
        /// <param name="curControlDev"></param>
        void SaveTarget()
        {
            RemoveFromParent();
        }
        /// <summary>
        /// 删除多功能面板的目标
        /// </summary>
        /// <param name="bindDevice"></param>
        /// <param name="btnDel"></param>
        /// <returns></returns>
        private void RemoveTargets(BindListAllInfo bindDevice, Button btnDel)
        {
            var delDevice = new DelDeviceBindData();
            delDevice.DeviceAddr = bindDevice.KeyMacAddr;
            delDevice.Epoint = bindDevice.KeyEpoint;
            if (bindDevice.BindType == 0 || bindDevice.BindType == 1)
            {
                var removeDevice = new RemoveBindListObj();
                removeDevice.BindCluster = bindDevice.BindCluster;
                removeDevice.BindType = 0;
                removeDevice.BindMacAddr = bindDevice.BindMacAddr;
                removeDevice.BindEpoint = bindDevice.BindEpoint;
                delDevice.RemoveBindList.Add(removeDevice);
                //部分目标需要二次绑定第二种功能
                switch (curBindType)
                {
                    case 3:
                        var device = LocalDevice.Current.GetDevice(bindDevice.BindMacAddr, bindDevice.BindEpoint);
                        if (device != null)
                        {
                            if (device.Type == DeviceType.DimmableLight)
                            {
                                //灯光为调光时需要移除6,8
                                var removeDevice2 = new RemoveBindListObj();
                                removeDevice2.BindCluster = 6;
                                removeDevice2.BindType = 0;
                                removeDevice2.BindMacAddr = bindDevice.BindMacAddr;
                                removeDevice2.BindEpoint = bindDevice.BindEpoint;
                                delDevice.RemoveBindList.Add(removeDevice2);
                            }
                        }
                        break;
                    case 5:
                        //要移除空调:需要移除513,514
                        var removeDevice3 = new RemoveBindListObj();
                        removeDevice3.BindCluster = 514;
                        removeDevice3.BindType = 0;
                        removeDevice3.BindMacAddr = bindDevice.BindMacAddr;
                        removeDevice3.BindEpoint = bindDevice.BindEpoint;
                        delDevice.RemoveBindList.Add(removeDevice3);
                        break;
                }
            }
            else if (bindDevice.BindType == 2)
            {
                var removeDevice = new RemoveBindListObj();
                removeDevice.BindCluster = bindDevice.BindCluster;
                removeDevice.BindType = 1;
                removeDevice.BindScenesId = bindDevice.BindScenesId;
                delDevice.RemoveBindList.Add(removeDevice);
            }
            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    Application.RunOnMainThread(() =>
                    {
                        CommonPage.Loading.Start("");
                    });
                    var delResult = new DelDeviceBindResponseAllData();
                    delResult = HdlDeviceBindLogic.Current.DelDeviceBindAsync(delDevice);
                    if (delResult == null)
                    {
                        Application.RunOnMainThread(() =>
                        {
                            new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.None, CloseTime = 2 }.Show(CommonPage.Instance);
                            CommonPage.Loading.Hide();
                        });
                        return;
                    }
                    else
                    {
                        if (delResult.removeBindResultResponseData == null)
                        {
                            if (delResult.delDeviceBindResponseData != null)
                            {
                                foreach (var d in delResult.delDeviceBindResponseData.RemoveBindList)
                                {
                                    if (d.Result == 0 || d.Result == 1)
                                    {
                                        if (MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.ContainsKey(bindDevice.KeyMacAddr + bindDevice.KeyEpoint))
                                        {
                                            MutilfunctionPanelMethod.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), Direction = AMPopTipDirection.None, CloseTime = 2 }.Show(CommonPage.Instance);
                                            CommonPage.Loading.Hide();
                                        });
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.None, CloseTime = 2 }.Show(CommonPage.Instance);
                                    CommonPage.Loading.Hide();
                                });
                                return;
                            }
                        }
                        else
                        {
                            if (delResult.removeBindResultResponseData.Result == 0)
                            {
                                if (MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.ContainsKey(bindDevice.KeyMacAddr + bindDevice.KeyEpoint))
                                {
                                    MutilfunctionPanelMethod.bindTargetsFromMutilfunctionPanelList.Remove(bindDevice.KeyMacAddr + bindDevice.KeyEpoint);
                                }
                                Application.RunOnMainThread(() =>
                                {
                                    RefreshBindListUI();
                                    CommonPage.Loading.Hide();
                                });
                            }
                            else
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(CommonPage.Instance);
                                    CommonPage.Loading.Hide();
                                });
                                return;
                            }
                        }
                    }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        CommonPage.Loading.Hide();
                    });
                }
            });
        }
        /// <summary>
        /// 匹配的类型列表
        /// </summary>
        /// <returns></returns>
        private List<string> GetTypeList()
        {
            List<string> textTypelist = new List<string> { };
            textTypelist.Add(Language.StringByID(R.MyInternationalizationString.scene));
            textTypelist.Add(Language.StringByID(R.MyInternationalizationString.uDeviceBelongId13));
            textTypelist.Add(Language.StringByID(R.MyInternationalizationString.uDeviceBelongId14));
            textTypelist.Add(Language.StringByID(R.MyInternationalizationString.uDeviceBelongId15));
            textTypelist.Add(Language.StringByID(R.MyInternationalizationString.uDeviceBelongId100));
            textTypelist.Add(Language.StringByID(R.MyInternationalizationString.uDeviceBelongId3600));
            textTypelist.Add(Language.StringByID(R.MyInternationalizationString.uDeviceBelongId2310));
            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
    }
}