xm
2020-12-10 3aa397ab145382935492b11c1f18c9634e69910b
ZigbeeApp/Shared/Phone/MainPage/HomeMainPageForm.cs
old mode 100755 new mode 100644
@@ -7,1292 +7,1297 @@
using ZigBee.Device;
namespace Shared.Phone.MainPage
{
{
  /// <summary>
  /// 主页(注意,BodyFrameLyout是场景和功能的桌布)
  /// </summary>
  public class HomeMainPageForm : EditorCommonForm
  {
    #region ■ 变量声明___________________________
    /// <summary>
    /// 主页(注意,BodyFrameLyout是场景和功能的桌布)
    /// The instance.
    /// </summary>
    public class HomeMainPageForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// The instance.
        /// </summary>
        public static HomeMainPageForm Instance;
        /// <summary>
        /// 整个界面的上下滑动控件
        /// </summary>
        private VerticalFrameControl listBodyContr = null;
        /// <summary>
        /// 房间的滑动容器控件
        /// </summary>
        private HorizontalPages roomPageView = null;
        /// <summary>
        /// 设备卡片控件(主键为:设备主键)
        /// </summary>
        private Dictionary<string, Controls.DeviceCardCommon> dicDeviceCardControl = new Dictionary<string, Controls.DeviceCardCommon>();
        /// <summary>
        /// 场景卡片控件(主键为:场景ID)
        /// </summary>
        private Dictionary<int, Controls.SceneCardControl> dicSceneCardControl = new Dictionary<int, Controls.SceneCardControl>();
        /// <summary>
        /// 楼层名字控件
        /// </summary>
        private NormalViewControl btnFloorName = null;
        /// <summary>
        /// 房间正在变更中
        /// </summary>
        private bool isRoomChanging = false;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 界面显示
        /// </summary>
        public void ShowForm()
        {
            this.FormID = "HomeMainPageForm";
            Instance = this;
            //先初始化左滑房间列表界面(在反射里面初始化)
            if (LeftListRoomViewFrom.Instance == null) { }
            //整个背景的上下滑动控件
            if (this.listBodyContr == null)
            {
                this.listBodyContr = new VerticalFrameControl();
                listBodyContr.Height = this.Height;
                listBodyContr.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
                this.AddChidren(listBodyContr);
            }
            else
            {
                //清除全部
                this.listBodyContr.frameTable.RemoveAll();
                listBodyContr.RecoverTableHeight();
            }
            //初始化头部控件
            this.InitTopFrameLayoutControl();
            //初始化中间控件
            this.InitMidFrameLayoutControl();
            //添加设备状态上报事件
            this.AddNormalDeviceReportEvent();
            //添加传感器状态上报事件
            this.AddSensorDeviceReportEvent();
            //开启传感器状态还原的线程
            this.StartRecoverSenorStatuThread();
        }
        /// <summary>
        /// 初始化头部控件
        /// </summary>
        private void InitTopFrameLayoutControl()
        {
            //头部容器
            base.topFrameLayout = new NormalFrameLayout();
            topFrameLayout.Y = Application.GetRealHeight(60);
            topFrameLayout.Height = Application.GetRealHeight(127);
            topFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
            listBodyContr.frameTable.AddChidren(topFrameLayout);
            //楼层图标
            var btnFloorIcon = new IconViewControl(69);
            btnFloorIcon.X = ControlCommonResourse.XXLeft;
            btnFloorIcon.Gravity = Gravity.CenterVertical;
            btnFloorIcon.UnSelectedImagePath = "Item/Floor.png";
            topFrameLayout.AddChidren(btnFloorIcon);
            //拥有楼层
            this.btnFloorName = null;
            if (Config.Instance.Home.FloorDics.Count > 0)
            {
                //拥有楼层的时候,点击才有效
                btnFloorIcon.ButtonClickEvent += (sender, e) =>
                {
                    //显示选择楼层的界面
                    this.ShowSelectFloorForm(btnFloorName);
                };
                //楼层名
                this.btnFloorName = new NormalViewControl(500, 100, true);
                btnFloorName.X = btnFloorIcon.Right;
                btnFloorName.Gravity = Gravity.CenterVertical;
                btnFloorName.TextColor = ZigbeeColor.Current.GXCTextColor;
                btnFloorName.Text = Config.Instance.Home.GetCurrentFloorName;
                btnFloorName.IsBold = true;
                btnFloorName.Width = btnFloorName.GetRealWidthByText() + Application.GetRealWidth(20);
                topFrameLayout.AddChidren(btnFloorName);
                btnFloorName.ButtonClickEvent += (sender, e) =>
                {
                    //显示选择楼层的界面
                    this.ShowSelectFloorForm(btnFloorName);
                };
            }
            //可视对讲快捷方式
            Shared.Phone.Device.VideoIntercom.VideoMachine.AccountSupportVideo(topFrameLayout);
            //安防快捷方式
            if (UserCenterResourse.ResidenceOption.SafetyShortcut)
            {
                var btnSafety = new Controls.SafetyShortcutControl();
                btnSafety.X = Application.GetRealWidth(860);
                btnSafety.Gravity = Gravity.CenterVertical;
                topFrameLayout.AddChidren(btnSafety);
            }
            //消息图标
            var btnMessage = new MessageManagementControl();
            btnMessage.X = Application.GetRealWidth(953);
            btnMessage.Gravity = Gravity.CenterVertical;
            btnMessage.UnSelectedImagePath = "Item/Message.png";
            btnMessage.SelectedImagePath = "Item/MessageSelected.png";
            topFrameLayout.AddChidren(btnMessage);
            //住宅名字
            var btnHouseName = new NormalViewControl(700, 95, true);
            btnHouseName.X = ControlCommonResourse.XXLeft;
            btnHouseName.Y = Application.GetRealHeight(161);
            btnHouseName.TextSize = 24;
            btnHouseName.TextColor = ZigbeeColor.Current.GXCTextDeepBlackColor;
            btnHouseName.IsBold = true;
            btnHouseName.Text = Config.Instance.Home.Name;
            btnHouseName.Width = btnHouseName.GetRealWidthByText() + Application.GetRealWidth(100);
            this.listBodyContr.frameTable.AddChidren(btnHouseName);
            //切换住宅
            btnHouseName.MouseLongEventHandler += (sender, e) =>
            {
                if (Config.Instance.HomeFilePathList.Count == 0)
                {
                    //当前住宅为空,请先建立住宅
                    this.ShowMassage(ShowMsgType.Remind, Language.StringByID(R.MyInternationalizationString.CurrentlyTheUserIshHouseIsEmptyPleaseBuildANewHouseFirst));
                }
                else
                {
                    var selectHouse = new Category.SelectHouseForm();
                    selectHouse.Init();
                    selectHouse.HouseAction = (houseId) =>
                    {
                        //切换住宅
                        this.ChangeResidence(HdlResidenceLogic.Current.GetHouseByHouseId(houseId));
                    };
                }
            };
        }
        /// <summary>
        /// 初始化中间控件
        /// </summary>
        private void InitMidFrameLayoutControl()
        {
            //成员身份不允许添加网关
            if (UserCenterResourse.UserInfo.AuthorityNo == 3)
            {
                //初始化场景,功能的切换控件
                this.InitSceneFunctionSwitchControl();
                //初始化房间滑动控件
                this.InitRoomHorizontalPagesControl();
                return;
            }
            //检测是否绑定有网关
            if (Common.Config.Instance.Home.IsVirtually == false && this.CheckHadBindGateway() == false)
            {
                //显示没有绑定过网关的界面
                this.ShowNoGatewayTip();
            }
            else
            {
                //初始化场景,功能的切换控件
                this.InitSceneFunctionSwitchControl();
                //初始化房间滑动控件
                this.InitRoomHorizontalPagesControl();
            }
        }
        /// <summary>
        /// 初始化场景,功能的切换控件
        /// </summary>
        private void InitSceneFunctionSwitchControl()
        {
            //功能和场景的背景图
            var functionSceneView = new FrameLayout();
            functionSceneView.Y = Application.GetRealHeight(861);
            functionSceneView.Width = Application.GetRealWidth(832);
            functionSceneView.Height = Application.GetRealHeight(167);
            functionSceneView.Gravity = Gravity.CenterHorizontal;
            functionSceneView.BackgroundImagePath = "Item/SceneFunctionBG.png";
            this.listBodyContr.frameTable.AddChidren(functionSceneView);
            //场景
            var btnScene = new NormalViewControl(350, 100, true);
            btnScene.X = Application.GetRealWidth(30);
            btnScene.Y = Application.GetRealHeight(12);
            btnScene.TextColor = ZigbeeColor.Current.GXCTextGrayColor3;
            btnScene.SelectedTextColor = ZigbeeColor.Current.GXCTextWhiteColor;
            btnScene.TextID = R.MyInternationalizationString.Scence;
            btnScene.TextSize = 15;
            btnScene.TextAlignment = TextAlignment.Center;
            functionSceneView.AddChidren(btnScene);
            //功能
            var btnFunction = new NormalViewControl(360, 100, true);
            btnFunction.X = Application.GetRealWidth(430);
            btnFunction.Y = Application.GetRealHeight(12);
            btnFunction.TextColor = ZigbeeColor.Current.GXCTextGrayColor3;
            btnFunction.SelectedTextColor = ZigbeeColor.Current.GXCTextWhiteColor;
            btnFunction.TextSize = 15;
            btnFunction.TextID = R.MyInternationalizationString.Function;
            btnFunction.TextAlignment = TextAlignment.Center;
            functionSceneView.AddChidren(btnFunction);
            if (UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex == 1)
            {
                btnScene.IsSelected = true;
                btnScene.TextSize = 17;
                btnScene.IsBold = true;
            }
            else if (UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex == 2)
            {
                btnFunction.IsSelected = true;
                btnFunction.TextSize = 17;
                btnFunction.IsBold = true;
            }
            //功能和场景bodyView
            this.bodyFrameLayout = new NormalFrameLayout();
            bodyFrameLayout.Y = functionSceneView.Bottom;
            bodyFrameLayout.Height = this.listBodyContr.frameTable.Height - functionSceneView.Bottom;
            bodyFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
            this.listBodyContr.frameTable.AddChidren(bodyFrameLayout);
            //选择功能分栏
            btnFunction.ButtonClickEvent += (sender, e) =>
            {
                if (btnFunction.IsSelected == true)
                {
                    return;
                }
                //功能分支选择
                UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex = 2;
                btnScene.IsSelected = false;
                btnScene.TextSize = 15;
                btnScene.IsBold = false;
                btnFunction.IsSelected = true;
                btnFunction.TextSize = 17;
                btnFunction.IsBold = true;
                //刷新设备桌布控件
                this.RefreshBodyView();
            };
            //选择场景分栏
            btnScene.ButtonClickEvent += (sender, e) =>
            {
                if (btnScene.IsSelected == true)
                {
                    return;
                }
                //场景分支选择
                UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex = 1;
                btnScene.IsSelected = true;
                btnScene.TextSize = 17;
                btnScene.IsBold = true;
                btnFunction.IsSelected = false;
                btnFunction.TextSize = 15;
                btnFunction.IsBold = false;
                //刷新设备桌布控件
                this.RefreshBodyView();
            };
        }
        /// <summary>
        /// 提示用户没有网关,并快速跳转到添加网关界面
        /// </summary>
        private void ShowNoGatewayTip()
        {
            var bg = new PicViewControl(717, 478);
            bg.Y = Application.GetRealHeight(302);
            bg.Gravity = Gravity.CenterHorizontal;
            bg.UnSelectedImagePath = "Item/NoBindGW.png";
            this.listBodyContr.frameTable.AddChidren(bg);
            var tip = new NormalViewControl(this.GetPictrueRealSize(717), this.GetPictrueRealSize(58), false);
            tip.Y = Application.GetRealHeight(815);
            tip.Gravity = Gravity.CenterHorizontal;
            tip.TextID = R.MyInternationalizationString.NewAccountNeedBingGW;
            tip.TextColor = ZigbeeColor.Current.GXCTextGrayColor;
            tip.TextAlignment = TextAlignment.Center;
            this.listBodyContr.frameTable.AddChidren(tip);
            var addBG = new PicViewControl(971, 366);
            addBG.Y = Application.GetRealHeight(1048);
            addBG.UnSelectedImagePath = "Item/VirtualFrame.png";
            addBG.Gravity = Gravity.CenterHorizontal;
            this.listBodyContr.frameTable.AddChidren(addBG);
            var addGatewayBtn = new IconViewControl(89);
            addGatewayBtn.Y = Application.GetRealHeight(1143);
            addGatewayBtn.UnSelectedImagePath = "Item/Add_GW.png";
            addGatewayBtn.Gravity = Gravity.CenterHorizontal;
            this.listBodyContr.frameTable.AddChidren(addGatewayBtn);
            addGatewayBtn.ButtonClickEvent += (sender, e) =>
            {
                var form = new UserCenter.GatewayAdd.NewGateWayMenuSelectForm();
                form.AddForm();
            };
            var addTip = new PicViewControl(717, 58);
            addTip.Y = Application.GetRealHeight(1256);
            addTip.Gravity = Gravity.CenterHorizontal;
            addTip.TextID = R.MyInternationalizationString.AddSmartGW;
            addTip.TextColor = ZigbeeColor.Current.GXCTextBlackColor;
            addTip.TextAlignment = TextAlignment.Center;
            this.listBodyContr.frameTable.AddChidren(addTip);
            addTip.ButtonClickEvent += (sender, e) =>
            {
                var form = new UserCenter.GatewayAdd.NewGateWayMenuSelectForm();
                form.AddForm();
            };
        }
        #endregion
        #region ■ 初始化房间滑动控件_________________
        /// <summary>
        /// 初始化房间滑动控件
        /// </summary>
        private void InitRoomHorizontalPagesControl()
        {
            //左右滑动控件的背景容器
            var frameBack = new FrameLayout();
            frameBack.Y = Application.GetRealHeight(302);
            frameBack.Width = this.Width;
            frameBack.Height = Application.GetRealHeight(478);
            this.listBodyContr.frameTable.AddChidren(frameBack);
            //房间滑动控件左边的虚拟图片
            var mLeftImageView = new ImageView();
            mLeftImageView.ImagePath = "RoomIcon/19.jpg";
            mLeftImageView.Radius = (uint)Application.GetRealHeight(17);
            mLeftImageView.Width = Application.GetRealWidth(717);
            mLeftImageView.Height = Application.GetRealHeight(478);
            //房间滑动控件右边的虚拟图片
            var mRightImageView = new ImageView();
            mRightImageView.ImagePath = "RoomIcon/19.jpg";
            mRightImageView.Radius = (uint)Application.GetRealHeight(17);
            mRightImageView.Width = mLeftImageView.Width;
            mRightImageView.Height = mLeftImageView.Height;
            this.roomPageView = new HorizontalPages();
            roomPageView.Width = frameBack.Width;
            roomPageView.Height = frameBack.Height;
            frameBack.AddChidren(roomPageView);
            //突出边距
            roomPageView.TCBJ = Application.GetRealWidth(124);
            //两个page之间的间距
            roomPageView.JMBJ = Application.GetRealWidth(58);
            //在这个控件的下面添加阴影效果
            var btnRoomBottomShadow = new NormalViewControl(717, 81, true);
            btnRoomBottomShadow.Y = frameBack.Bottom;
            btnRoomBottomShadow.Gravity = Gravity.CenterHorizontal;
            btnRoomBottomShadow.UnSelectedImagePath = "Room/RoomCardShadow.png";
            this.listBodyContr.frameTable.AddChidren(btnRoomBottomShadow);
            //设置控制最左和最右2边填充的假背景View,不设置不显示,需要在AddChidren 前调用该方法
            roomPageView.SetLeftAndRightImageView(mLeftImageView, mRightImageView);
            //当前房间
            var listRoom = HdlRoomLogic.Current.GetRoomsByFloorIdAppendLoveRoom(Config.Instance.Home.CurrentFloorId);
            if (HdlRoomLogic.Current.NowMainPageRoom == null)
            {
                HdlRoomLogic.Current.NowMainPageRoom = listRoom[0];
            }
            var curIndex = 0;
            for (int i = 0; i < listRoom.Count; i++)
            {
                var roomView = new RoomView(0, 0);
                roomPageView.AddChidren(roomView);
                roomView.Init(listRoom[i]);
                if (listRoom[i].Id == HdlRoomLogic.Current.NowMainPageRoom.Id)
                {
                    //当前房间,则显示那些数据出来
                    roomView.HideName(false);
                    //当前索引
                    curIndex = i;
                }
                else
                {
                    roomView.HideName(true);
                }
            }
            //在房间卡片左边添加遮罩
            var btnRoomLeftShadow = new FrameLayout();
            btnRoomLeftShadow.Width = Application.GetRealWidth(124);
            btnRoomLeftShadow.Height = roomPageView.Height - roomPageView.JMBJ * 2;
            btnRoomLeftShadow.Gravity = Gravity.CenterVertical;
            btnRoomLeftShadow.BackgroundColor = 0x1c000000;
            btnRoomLeftShadow.SetCornerWithSameRadius(Application.GetRealHeight(12), HDLUtils.RectCornerTopRight | HDLUtils.RectCornerBottomRight);
            //frameBack.AddChidren(btnRoomLeftShadow);//暂时不加遮罩
            //在房间卡片右边添加遮罩
            var btnRoomRightShadow = new FrameLayout();
            btnRoomRightShadow.Width = btnRoomLeftShadow.Width;
            btnRoomRightShadow.Height = btnRoomLeftShadow.Height;
            btnRoomRightShadow.X = Application.GetRealWidth(956);
            btnRoomRightShadow.Gravity = Gravity.CenterVertical;
            btnRoomRightShadow.BackgroundColor = btnRoomLeftShadow.BackgroundColor;
            btnRoomRightShadow.SetCornerWithSameRadius(Application.GetRealHeight(12), HDLUtils.RectCornerTopLeft | HDLUtils.RectCornerBottomLeft);
            //frameBack.AddChidren(btnRoomRightShadow);//暂时不加遮罩
            //控件开始滑动的事件
            roomPageView.StartScrollAction += () =>
            {
                this.isRoomChanging = true;
                //开始滑动时,隐藏当前的数据显示
                (roomPageView.GetChildren(roomPageView.PageIndex) as RoomView).HideName(true);
                btnRoomLeftShadow.Visible = false;
                btnRoomRightShadow.Visible = false;
                btnRoomBottomShadow.Visible = false;
            };
            roomPageView.EndScrollAction += () =>
            {
                curIndex = roomPageView.PageIndex;
                //前一张卡片
                if (roomPageView.GetChildren(curIndex - 1) != null)
                {
                    (roomPageView.GetChildren(curIndex - 1) as RoomView).HideName(true);
                }
                //当前卡片
                if (roomPageView.GetChildren(curIndex) != null)
                {
                    (roomPageView.GetChildren(curIndex) as RoomView).HideName(false);
                }
                //下一张卡片
                if (roomPageView.GetChildren(curIndex + 1) != null)
                {
                    (roomPageView.GetChildren(curIndex + 1) as RoomView).HideName(true);
                }
                btnRoomLeftShadow.Visible = true;
                btnRoomRightShadow.Visible = true;
                btnRoomBottomShadow.Visible = true;
                //切换当前房间
                HdlRoomLogic.Current.NowMainPageRoom = listRoom[curIndex];
                //刷新设备桌布控件
                this.isRoomChanging = false;
                this.RefreshBodyView();
            };
            //设置房间的初始选择
            roomPageView.PageIndex = curIndex;
            //如果索引是第一个时,它触发不了事件,需要手动触发
            if (curIndex == 0)
            {
                roomPageView.EndScrollAction();
            }
        }
        /// <summary>
        /// 设置房间的滑动容器控件选择指定的房间(供外部调用)
        /// </summary>
        /// <param name="room"></param>
        public void SetRoomPageViewSelect(Room room)
        {
            if (roomPageView == null)
            {
                //当没有绑定有网关,左滑时,点击房间列表,则不鸟它
                return;
            }
            var listRoom = HdlRoomLogic.Current.GetRoomsByFloorIdAppendLoveRoom(Config.Instance.Home.CurrentFloorId);
            for (int i = 0; i < listRoom.Count; i++)
            {
                if (room.Id == listRoom[i].Id)
                {
                    if (roomPageView.PageIndex != i)
                    {
                        //不相同时才刷新
                        roomPageView.PageIndex = i;
                    }
                    break;
                }
            }
            //重置一下楼层名字
            if (this.btnFloorName != null)
            {
                btnFloorName.Text = Config.Instance.Home.GetCurrentFloorName;
            }
        }
        #endregion
        #region ■ 功能分支___________________________
        /// <summary>
        /// 显示功能
        /// </summary>
        private void ShowFunction()
        {
            //先清空缓存
            bodyFrameLayout.RemoveAll();
            bodyFrameLayout.Height = Application.GetRealHeight(750);
            this.listBodyContr.RecoverTableHeight();
            this.dicSceneCardControl = new Dictionary<int, Controls.SceneCardControl>();
            this.dicDeviceCardControl = new Dictionary<string, Controls.DeviceCardCommon>();
            var listDevice = new List<CommonDevice>();
            foreach (var mainkeys in HdlRoomLogic.Current.NowMainPageRoom.ListDevice)
            {
                var device = LocalDevice.Current.GetDevice(mainkeys);
                //判断该设备能否显示在主页
                if (LocalDevice.Current.CanShowInHomeHomeMainPage(device) == false)
                {
                    continue;
                }
                listDevice.Add(device);
            }
            //当前房间的设备数
            if (listDevice.Count == 0)
            {
                //还没有功能哦
                this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.NoFunction_Tip));
                return;
            }
            //设备列表容器控件
            var listDeviceBackContr = new FrameLayout();
            listDeviceBackContr.Height = bodyFrameLayout.Height;
            bodyFrameLayout.AddChidren(listDeviceBackContr);
            HdlThreadLogic.Current.RunThread(() =>
            {
                //初始化设备卡片列表控件
                this.InitDeviceListCardControl(listDeviceBackContr, listDevice);
            });
        }
        /// <summary>
        /// 初始化设备卡片列表控件
        /// </summary>
        /// <param name="listDeviceBackContr"></param>
        private void InitDeviceListCardControl(FrameLayout listDeviceBackContr, List<CommonDevice> listDevice)
        {
            //设备计数
            int contrCount = 0;
            //X轴坐标(图片左边有余白)
            int XX = this.GetPictrueRealSize(44);
            //Y轴坐标
            int YY = 0;
            var listContr = new List<Controls.DeviceCardCommon>();
            foreach (var device in listDevice)
            {
                if (this.isRoomChanging == true || listDeviceBackContr.Parent == null)
                {
                    //放弃初始化控件
                    return;
                }
                HdlThreadLogic.Current.RunMain(() =>
                {
                    Controls.DeviceCardCommon cardContr = null;
                    //窗帘
                    if (device.Type == DeviceType.WindowCoveringDevice)
                    {
                        cardContr = new Controls.DeviceCurtainCardControl();
                    }
                    //继电器
                    else if (device.Type == DeviceType.OnOffOutput)
                    {
                        cardContr = new Controls.DeviceRelayCardControl();
                    }
                    //空气开关
                    else if (device.Type == DeviceType.AirSwitch)
                    {
                        cardContr = new Controls.DeviceAirSwitchCardControl();
                    }
                    //空调
                    else if (device.Type == DeviceType.Thermostat)
                    {
                        cardContr = new Controls.DeviceAcCardControl();
                    }
                    //新风
                    else if (device.Type == DeviceType.FreshAir)
                    {
                        cardContr = new Controls.DeviceFreshAirCardControl();
                    }
                    // PM2.5
                    else if (device.Type == DeviceType.PMSensor)
                    {
                        cardContr = new Controls.DevicePmSensorCardControl();
                    }
                    //彩灯(调光器)
                    else if (device.Type == DeviceType.DimmableLight)
                    {
                        cardContr = new Controls.DeviceColorLightCardControl();
                    }
                    //传感器
                    else if (device.Type == DeviceType.IASZone)
                    {
                        cardContr = new Controls.DeviceSensorCardControl();
                    }
                    //温湿度
                    else if (device.Type == DeviceType.TemperatureSensor)
                    {
                        cardContr = new Controls.DeviceTemperatureCardControl();
                    }
                    //门锁
                    else if (device.Type == DeviceType.DoorLock)
                    {
                        cardContr = new Controls.DeviceDoorLockCardControl();
                    }
                    //色温灯
                    else if (device.Type == DeviceType.ColorTemperatureLight)
                    {
                        cardContr = new Controls.DeviceColorTemperatureCardControl();
                    }
                    //无法识别
                    else
                    {
                        cardContr = new Controls.DeviceCardCommon();
                    }
                    //初始化卡片
                    cardContr.X = XX;
                    cardContr.Y = YY;
                    listDeviceBackContr.AddChidren(cardContr);
                    cardContr.InitControl(device, HdlRoomLogic.Current.NowMainPageRoom);
                    //卡片需要被移除的事件
                    cardContr.CardNeedRemoveEvent += () =>
                    {
                        //重置设备卡片控件坐标(卡片需要被删除专用)
                        this.ResetDeviceCardControlLocation(LocalDevice.Current.GetDeviceMainKeys(device));
                    };
                    //设备计数
                    contrCount++;
                    //卡片左右两边有14的余白,每两个后,X轴重置
                    XX = contrCount % 2 == 0 ? this.GetPictrueRealSize(44) : cardContr.Right + this.GetPictrueRealSize(20);
                    if (contrCount % 2 == 0)
                    {
                        //没两个之后,Y轴递增
                        YY = cardContr.Bottom + this.GetPictrueRealSize(15);
                    }
                    //控件记录到缓存中
                    this.dicDeviceCardControl[LocalDevice.Current.GetDeviceMainKeys(device)] = cardContr;
                    //加缓存,然后发命令
                    listContr.Add(cardContr);
                    if (contrCount == listDevice.Count)
                    {
                        //调整桌布大小
                        if (contrCount > 4)
                        {
                            bodyFrameLayout.Height = listDeviceBackContr.GetChildren(listDeviceBackContr.ChildrenCount - 1).Bottom;
                            listDeviceBackContr.Height = bodyFrameLayout.Height;
                            this.listBodyContr.frameTable.Height = bodyFrameLayout.Bottom;
                        }
                        HdlThreadLogic.Current.RunThread(() =>
                        {
                            while (listDeviceBackContr.Parent != null)
                            {
                                bool hadSend = false;
                                for (int i = 0; i < listContr.Count; i++)
                                {
                                    if (listDeviceBackContr.Parent == null)
                                    {
                                        return;
                                    }
                                    //如果还没有接收到数据,则再次发送
                                    if (listContr[i].ReceiveResponeResultStatu() == false)
                                    {
                                        //发送获取状态的命令
                                        listContr[i].SendStatuComand();
                                        hadSend = true;
                                        System.Threading.Thread.Sleep(200);
                                    }
                                }
                                if (hadSend == false)
                                {
                                    //如果已经全部回路都接收了网关回复,则不再二次发送
                                    break;
                                }
                                System.Threading.Thread.Sleep(2000);
                            }
                        }, ShowErrorMode.NO);
                    }
                });
                if (contrCount > 6)
                {
                    //让它呈现一种直接一次性刷出来的效果
                    System.Threading.Thread.Sleep(50);
                }
            }
        }
        #endregion
        #region ■ 重置设备卡片控件坐标_______________
        /// <summary>
        /// 重置设备卡片控件坐标(卡片需要被删除专用)
        /// </summary>
        /// <param name="deleteKey">需要被删除的卡片主键</param>
        private void ResetDeviceCardControlLocation(string deleteKey)
        {
            if (this.dicDeviceCardControl.ContainsKey(deleteKey) == false)
            {
                //如果列表里面没有这个控件的话
                return;
            }
            var deleteContr = this.dicDeviceCardControl[deleteKey];
            //设备计数
            int contrCount = 0;
            bool canMove = false;
            int YY = 0;
            foreach (var mainkey in this.dicDeviceCardControl.Keys)
            {
                if (deleteKey == mainkey)
                {
                    //如果已经到了要删除的卡片,删除掉它,然后它之后的卡片全部向上移动
                    canMove = true;
                    continue;
                }
                //设备计数
                contrCount++;
                //卡片左右两边有14的余白,每两个后,X轴重置
                int XX = 0;
                if (contrCount % 2 == 1)
                {
                    //第一个固定44
                    XX = this.GetPictrueRealSize(44);
                }
                else
                {
                    //第二个是44+控件宽度+20余白
                    XX = this.GetPictrueRealSize(44) + this.dicDeviceCardControl[mainkey].Width + this.GetPictrueRealSize(20);
                }
                //每两个之后,并且是第一个的时候,Y轴递增
                if (contrCount > 2 && contrCount % 2 == 1)
                {
                    //求商
                    int value = contrCount / 2;
                    //控件的底部有15的间隔
                    YY = value * (this.dicDeviceCardControl[mainkey].Height + this.GetPictrueRealSize(15));
                }
                if (canMove == true)
                {
                    //移动控件
                    this.dicDeviceCardControl[mainkey].X = XX;
                    this.dicDeviceCardControl[mainkey].Y = YY;
                }
            }
            //去找一下这个控件的列表容器
            var listView = deleteContr.Parent as FrameLayout;
            //删除掉那张卡片的主键
            deleteContr.RemoveFromParent();
            this.dicDeviceCardControl.Remove(deleteKey);
            //调整桌布高度
            if (listView.ChildrenCount >= 4)
            {
                bodyFrameLayout.Height = listView.GetChildren(listView.ChildrenCount - 1).Bottom;
                listView.Height = bodyFrameLayout.Height;
                this.listBodyContr.frameTable.Height = bodyFrameLayout.Bottom;
            }
        }
        #endregion
        #region ■ 场景分支___________________________
        /// <summary>
        /// 显示场景
        /// </summary>
        private void ShowScene()
        {
            //先清空缓存
            bodyFrameLayout.RemoveAll();
            bodyFrameLayout.Height = Application.GetRealHeight(750);
            this.listBodyContr.RecoverTableHeight();
            this.dicSceneCardControl = new Dictionary<int, Controls.SceneCardControl>();
            this.dicDeviceCardControl = new Dictionary<string, Controls.DeviceCardCommon>();
            var listScene = new List<SceneUI>();
            foreach (int sceneId in HdlRoomLogic.Current.NowMainPageRoom.ListSceneId)
            {
                var sceneUi = HdlSceneLogic.Current.GetSceneUIBySceneId(sceneId);
                if (sceneUi == null)
                {
                    //这个场景不见了
                    continue;
                }
                listScene.Add(sceneUi);
            }
            if (listScene.Count == 0)
            {
                //还没有场景哦
                this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.NoScene_Tip));
                return;
            }
            //列表控件
            var listSceneBackContr = new FrameLayout();
            listSceneBackContr.Height = bodyFrameLayout.Height;
            bodyFrameLayout.AddChidren(listSceneBackContr);
            HdlThreadLogic.Current.RunThread(() =>
            {
                //初始化场景卡片列表控件
                this.InitSceneListCardControl(listSceneBackContr, listScene);
            });
        }
        /// <summary>
        /// 初始化场景卡片列表控件
        /// </summary>
        /// <param name="listSceneBackContr"></param>
        private void InitSceneListCardControl(FrameLayout listSceneBackContr, List<SceneUI> listScene)
        {
            //场景计数
            int contrCount = 0;
            //X轴坐标(图片左边有余白)
            int XX = this.GetPictrueRealSize(44);
            //Y轴坐标
            int YY = 0;
            foreach (var sceneUi in listScene)
            {
                if (this.isRoomChanging == true || listSceneBackContr.Parent == null)
                {
                    //放弃初始化控件
                    return;
                }
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //初始化卡片
                    var cardContr = new Controls.SceneCardControl();
                    cardContr.X = XX;
                    cardContr.Y = YY;
                    listSceneBackContr.AddChidren(cardContr);
                    cardContr.InitControl(sceneUi);
                    //卡片需要被移除的事件
                    cardContr.CardNeedRemoveEvent += () =>
                    {
                        //重置设备卡片控件坐标(卡片需要被删除专用)
                        this.ResetSceneCardControlLocation(sceneUi.Id);
                    };
                    //设备计数
                    contrCount++;
                    //每两个后,X轴重置
                    XX = contrCount % 2 == 0 ? this.GetPictrueRealSize(44) : cardContr.Right + this.GetPictrueRealSize(20);
                    if (contrCount % 2 == 0)
                    {
                        //没两个之后,Y轴递增
                        YY = cardContr.Bottom + this.GetPictrueRealSize(15);
                    }
                    //控件记录到缓存中
                    this.dicSceneCardControl[sceneUi.Id] = cardContr;
                    if (contrCount == listScene.Count)
                    {
                        //调整桌布高度
                        if (contrCount > 4)
                        {
                            bodyFrameLayout.Height = listSceneBackContr.GetChildren(listSceneBackContr.ChildrenCount - 1).Bottom;
                            listSceneBackContr.Height = bodyFrameLayout.Height;
                            this.listBodyContr.frameTable.Height = bodyFrameLayout.Bottom;
                        }
                        HdlThreadLogic.Current.RunThread(() =>
                        {
                            //设置目前场景的延迟状态
                            this.SetSceneDelayTime(listSceneBackContr);
                        });
                    }
                });
                if (contrCount > 6)
                {
                    //让它呈现一种一次性显示出来的效果
                    System.Threading.Thread.Sleep(50);
                }
            }
        }
        /// <summary>
        /// 设置场景的延迟状态
        /// </summary>
        /// <param name="listSceneBackContr"></param>
        private async void SetSceneDelayTime(FrameLayout listSceneBackContr)
        {
            await System.Threading.Tasks.Task.Delay(1500);
            if (listSceneBackContr.Parent == null)
            {
                //控件已经被移除
                return;
            }
            //等待1.5秒之后,获取延迟状态
            var result = await Scene.CatDelaySceneAsync();
            if (result == null || result.catDelaySceneResponseData == null)
            {
                return;
            }
            if (listSceneBackContr.Parent == null)
            {
                //控件已经被移除
                return;
            }
            foreach (var data in result.catDelaySceneResponseData.DelayScenesList)
            {
                var sceneUi = HdlSceneLogic.Current.GetSceneUIBySceneId(data.ScenesId);
                if (sceneUi == null)
                {
                    continue;
                }
                if (this.dicSceneCardControl.ContainsKey(data.ScenesId) == false)
                {
                    continue;
                }
                //修改剩余延迟时间
                sceneUi.RemainTime = data.RemainTime;
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //刷新卡片信息
                    this.dicSceneCardControl[data.ScenesId].RefreshControlInfo(sceneUi);
                });
            }
        }
        #endregion
        #region ■ 重置场景卡片控件坐标_______________
        /// <summary>
        /// 重置场景卡片控件坐标(卡片需要被删除专用)
        /// </summary>
        /// <param name="deleteKey">需要被删除的卡片主键</param>
        private void ResetSceneCardControlLocation(int deleteId)
        {
            if (this.dicSceneCardControl.ContainsKey(deleteId) == false)
            {
                //如果列表里面没有这个控件的话
                return;
            }
            var deleteContr = this.dicSceneCardControl[deleteId];
            //设备计数
            int contrCount = 0;
            bool canMove = false;
            int YY = 0;
            foreach (var sceneId in this.dicSceneCardControl.Keys)
            {
                if (deleteId == sceneId)
                {
                    //如果已经到了要删除的卡片,删除掉它,然后它之后的卡片全部向上移动
                    canMove = true;
                    continue;
                }
                //设备计数
                contrCount++;
                //每两个后,X轴重置
                int XX = 0;
                if (contrCount % 2 == 1)
                {
                    //第一个固定44
                    XX = this.GetPictrueRealSize(44);
                }
                else
                {
                    //第二个是44+控件宽度+20余白
                    XX = this.GetPictrueRealSize(44) + this.dicSceneCardControl[sceneId].Width + this.GetPictrueRealSize(20);
                }
                //每两个之后,并且是第一个时,Y轴递增
                if (contrCount > 2 && contrCount % 2 == 1)
                {
                    //求商
                    int value = contrCount / 2;
                    //控件的底部有15的间隔
                    YY = value * (this.dicSceneCardControl[sceneId].Height + this.GetPictrueRealSize(15));
                }
                if (canMove == true)
                {
                    //移动控件
                    this.dicSceneCardControl[sceneId].X = XX;
                    this.dicSceneCardControl[sceneId].Y = YY;
                }
            }
            //去找一下这个控件的列表容器
            var listView = deleteContr.Parent as FrameLayout;
            //删除掉那张卡片的主键
            deleteContr.RemoveFromParent();
            this.dicSceneCardControl.Remove(deleteId);
            //调整桌布高度
            if (listView.ChildrenCount >= 4)
            {
                bodyFrameLayout.Height = listView.GetChildren(listView.ChildrenCount - 1).Bottom;
                listView.Height = bodyFrameLayout.Height;
                this.listBodyContr.frameTable.Height = bodyFrameLayout.Bottom;
            }
        }
        #endregion
        #region ■ 刷新桌布控件_______________________
        /// <summary>
        /// 刷新设备桌布控件
        /// </summary>
        public void RefreshBodyView()
        {
            bodyFrameLayout.RemoveAll();
            //让界面别闪烁
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                if (UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex == 2)
                {
                    //功能
                    this.ShowFunction();
                }
                else
                {
                    //场景
                    this.ShowScene();
                }
            });
        }
        #endregion
        #region ■ 切换住宅___________________________
        /// <summary>
        /// 切换住宅
        /// </summary>
        /// <param name="home">Home.</param>
        private void ChangeResidence(House home)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                this.ShowProgressBar();
                //切换住宅
                var result = HdlResidenceLogic.Current.SwitchResidence(home.Id);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    if (result == true)
                    {
                        //刷新界面
                        this.ShowForm();
                    }
                    this.CloseProgressBar();
                });
            });
        }
        #endregion
        #region ■ 一般设备状态上报___________________
        /// <summary>
        /// 添加一般设备状态上报事件
        /// </summary>
        private void AddNormalDeviceReportEvent()
        {
            //设备属性上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceStatus", ReceiveComandDiv.A设备属性上报, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //处理一般设备的上报数据
                    string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                    if (this.dicDeviceCardControl.ContainsKey(mainKeys) == true)
                    {
                        //刷新卡片信息
                        var locadevice = LocalDevice.Current.GetDevice(mainKeys);
                        //已经接收到网关的反馈 2020.05.09:删除Ack主题
                        this.dicDeviceCardControl[mainKeys].SetHadGetResponeResultStatu();
                        //刷新控件
                        if (locadevice.Type != DeviceType.IASZone)
                        {
                            //传感器不需要属性上报(但是那个球型传感器居然会属性上报)
                            this.dicDeviceCardControl[mainKeys].RefreshControlInfo(locadevice);
                        }
                        else if (report.DeviceStatusReport.CluterID == 1)
                        {
                            //这个是电量推送
                            this.dicDeviceCardControl[mainKeys].RefreshBatteryStatu();
                        }
                    }
                }, ShowErrorMode.NO);
            });
            //设备在线上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceOnline", ReceiveComandDiv.A设备在线上报, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                    if (this.dicDeviceCardControl.ContainsKey(mainKeys) == true)
                    {
                        //刷新卡片状态
                        bool bolOnline = Common.LocalDevice.Current.CheckDeviceIsOnline(report);
                        this.dicDeviceCardControl[mainKeys].SetCardOnlineStatu(bolOnline);
                        return;
                    }
                }, ShowErrorMode.NO);
            });
        }
        #endregion
        #region ■ 传感器状态上报_____________________
        /// <summary>
        /// 添加传感器状态上报事件
        /// </summary>
        private void AddSensorDeviceReportEvent()
        {
            //传感器上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewSensor", ReceiveComandDiv.A传感器上报, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //处理传感器上报数据
                    string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                    if (this.dicDeviceCardControl.ContainsKey(mainKeys) == true)
                    {
                        //开启卡片闪烁特效
                        this.dicDeviceCardControl[mainKeys].StartCardLightAppeal();
                    }
                }, ShowErrorMode.NO);
            });
        }
        #endregion
        #region ■ 传感器状态还原_____________________
        /// <summary>
        /// 开启传感器状态还原的线程
        /// </summary>
        private void StartRecoverSenorStatuThread()
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                while (this.Parent != null)
                {
                    try
                    {
                        var dicControl = this.dicDeviceCardControl;
                        foreach (var contr in dicControl.Values)
                        {
                            //如果是传感器,则刷新状态
                            if (contr.device.Type == DeviceType.IASZone && contr.Parent != null
                              && ((IASZone)contr.device).iASInfo == null)
                            {
                                HdlThreadLogic.Current.RunMain(() =>
                                {
                                    //设备状态必须刷新
                                    string statuText = HdlDeviceOtherLogic.Current.GetDeviceStatu(contr.device);
                                    contr.SetDeviceStatuText(statuText);
                                    //传感器切换为灰色状态
                                    contr.SwitchCardStatuAppeal(false);
                                }, ShowErrorMode.NO);
                            }
                        }
                        System.Threading.Thread.Sleep(8000);
                    }
                    catch { System.Threading.Thread.Sleep(3000); }
                }
            });
        }
        #endregion
        #region ■ 切换楼层___________________________
        /// <summary>
        /// 显示选择楼层的界面
        /// </summary>
        private void ShowSelectFloorForm(NormalViewControl btnFloor)
        {
            var floorFL = new Category.SelectFloorForm();
            floorFL.CurFloorId = Config.Instance.Home.CurrentFloorId;
            this.AddChidren(floorFL);
            floorFL.Init(35, 153);
            floorFL.FloorAction = (floorId) =>
            {
                btnFloor.Text = HdlResidenceLogic.Current.GetFloorNameById(floorId);
                Config.Instance.Home.CurrentFloorId = floorId;
                HdlRoomLogic.Current.NowMainPageRoom = HdlRoomLogic.Current.GetLoveRoom();
                //分类界面需要刷新
                UserView.UserPage.Instance.RefreshCategoryForm = true;
                this.ShowForm();
            };
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            //把静态变量的这个东西置空
            Instance = null;
            HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewDeviceOnline");
            HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewDeviceStatus");
            HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewSensor");
            base.CloseFormBefore();
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 检测是否绑定有网关
        /// </summary>
        private bool CheckHadBindGateway()
        {
            //获取本地已经绑定的网关
            var gatewayList = HdlGatewayLogic.Current.GetAllLocalGateway();
            if (gatewayList.Count == 0)
            {
                return false;
            }
            return true;
        }
        #endregion
        #region ■ 界面重新激活事件___________________
        /// <summary>
        /// 自身的上层界面关闭后,它自身处于最上层时,触发的事件
        /// </summary>
        public override int FormActionAgainEvent()
        {
            CommonPage.Instance.IsDrawerLockMode = false;
            return 1;
        }
        #endregion
    }
    public static HomeMainPageForm Instance;
    /// <summary>
    /// 整个界面的上下滑动控件
    /// </summary>
    private VerticalFrameControl listBodyContr = null;
    /// <summary>
    /// 房间的滑动容器控件
    /// </summary>
    private HorizontalPages roomPageView = null;
    /// <summary>
    /// 设备卡片控件(主键为:设备主键)
    /// </summary>
    private Dictionary<string, Controls.DeviceCardCommon> dicDeviceCardControl = new Dictionary<string, Controls.DeviceCardCommon>();
    /// <summary>
    /// 场景卡片控件(主键为:场景ID)
    /// </summary>
    private Dictionary<int, Controls.SceneCardControl> dicSceneCardControl = new Dictionary<int, Controls.SceneCardControl>();
    /// <summary>
    /// 楼层名字控件
    /// </summary>
    private NormalViewControl btnFloorName = null;
    /// <summary>
    /// 房间正在变更中
    /// </summary>
    private bool isRoomChanging = false;
    #endregion
    #region ■ 初始化_____________________________
    /// <summary>
    /// 界面显示
    /// </summary>
    public void ShowForm()
    {
      this.FormID = "HomeMainPageForm";
      Instance = this;
      //先初始化左滑房间列表界面(在反射里面初始化)
      if (LeftListRoomViewFrom.Instance == null) { }
      //整个背景的上下滑动控件
      if (this.listBodyContr == null)
      {
        this.listBodyContr = new VerticalFrameControl();
        listBodyContr.Height = this.Height;
        listBodyContr.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
        this.AddChidren(listBodyContr);
      }
      else
      {
        //清除全部
        this.listBodyContr.frameTable.RemoveAll();
        listBodyContr.RecoverTableHeight();
      }
      //初始化头部控件
      this.InitTopFrameLayoutControl();
      //初始化中间控件
      this.InitMidFrameLayoutControl();
      //添加设备状态上报事件
      this.AddNormalDeviceReportEvent();
      //添加传感器状态上报事件
      this.AddSensorDeviceReportEvent();
      //开启传感器状态还原的线程
      this.StartRecoverSenorStatuThread();
    }
    /// <summary>
    /// 初始化头部控件
    /// </summary>
    private void InitTopFrameLayoutControl()
    {
      //头部容器
      base.topFrameLayout = new NormalFrameLayout();
      topFrameLayout.Y = Application.GetRealHeight(60);
      topFrameLayout.Height = Application.GetRealHeight(127);
      topFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
      listBodyContr.frameTable.AddChidren(topFrameLayout);
      //楼层图标
      var btnFloorIcon = new IconViewControl(69);
      btnFloorIcon.X = ControlCommonResourse.XXLeft;
      btnFloorIcon.Gravity = Gravity.CenterVertical;
      btnFloorIcon.UnSelectedImagePath = "Item/Floor.png";
      topFrameLayout.AddChidren(btnFloorIcon);
      //拥有楼层
      this.btnFloorName = null;
      if (Config.Instance.Home.FloorDics.Count > 0)
      {
        //拥有楼层的时候,点击才有效
        btnFloorIcon.ButtonClickEvent += (sender, e) =>
        {
          //显示选择楼层的界面
          this.ShowSelectFloorForm(btnFloorName);
        };
        //楼层名
        this.btnFloorName = new NormalViewControl(500, 100, true);
        btnFloorName.X = btnFloorIcon.Right;
        btnFloorName.Gravity = Gravity.CenterVertical;
        btnFloorName.TextColor = ZigbeeColor.Current.GXCTextColor;
        btnFloorName.Text = Config.Instance.Home.GetCurrentFloorName;
        btnFloorName.IsBold = true;
        btnFloorName.Width = btnFloorName.GetRealWidthByText() + Application.GetRealWidth(20);
        topFrameLayout.AddChidren(btnFloorName);
        btnFloorName.ButtonClickEvent += (sender, e) =>
        {
          //显示选择楼层的界面
          this.ShowSelectFloorForm(btnFloorName);
        };
      }
      //可视对讲快捷方式
      Shared.Phone.Device.VideoIntercom.VideoMachine.AccountSupportVideo(topFrameLayout);
      //安防快捷方式
      if (UserCenterResourse.ResidenceOption.SafetyShortcut)
      {
        var btnSafety = new Controls.SafetyShortcutControl();
        btnSafety.X = Application.GetRealWidth(860);
        btnSafety.Gravity = Gravity.CenterVertical;
        topFrameLayout.AddChidren(btnSafety);
      }
      //消息图标
      var btnMessage = new MessageManagementControl();
      btnMessage.X = Application.GetRealWidth(953);
      btnMessage.Gravity = Gravity.CenterVertical;
      btnMessage.UnSelectedImagePath = "Item/Message.png";
      btnMessage.SelectedImagePath = "Item/MessageSelected.png";
      topFrameLayout.AddChidren(btnMessage);
      //住宅名字
      var btnHouseName = new NormalViewControl(700, 95, true);
      btnHouseName.X = ControlCommonResourse.XXLeft;
      btnHouseName.Y = Application.GetRealHeight(161);
      btnHouseName.TextSize = 24;
      btnHouseName.TextColor = ZigbeeColor.Current.GXCTextDeepBlackColor;
      btnHouseName.IsBold = true;
      btnHouseName.Text = Config.Instance.Home.Name;
      btnHouseName.Width = btnHouseName.GetRealWidthByText() + Application.GetRealWidth(100);
      this.listBodyContr.frameTable.AddChidren(btnHouseName);
      //切换住宅
      btnHouseName.MouseLongEventHandler += (sender, e) =>
      {
        if (Config.Instance.HomeFilePathList.Count == 0)
        {
          //当前住宅为空,请先建立住宅
          this.ShowMassage(ShowMsgType.Remind, Language.StringByID(R.MyInternationalizationString.CurrentlyTheUserIshHouseIsEmptyPleaseBuildANewHouseFirst));
        }
        else
        {
          var selectHouse = new Category.SelectHouseForm();
          selectHouse.Init();
          selectHouse.HouseAction = (houseId) =>
                {
                  //切换住宅
                  this.ChangeResidence(HdlResidenceLogic.Current.GetHouseByHouseId(houseId));
                };
        }
      };
    }
    /// <summary>
    /// 初始化中间控件
    /// </summary>
    private void InitMidFrameLayoutControl()
    {
      //成员身份不允许添加网关
      if (UserCenterResourse.UserInfo.AuthorityNo == 3)
      {
        //初始化场景,功能的切换控件
        this.InitSceneFunctionSwitchControl();
        //初始化房间滑动控件
        this.InitRoomHorizontalPagesControl();
        return;
      }
      //检测是否绑定有网关
      if (Common.Config.Instance.Home.IsVirtually == false && this.CheckHadBindGateway() == false)
      {
        //显示没有绑定过网关的界面
        this.ShowNoGatewayTip();
      }
      else
      {
        //初始化场景,功能的切换控件
        this.InitSceneFunctionSwitchControl();
        //初始化房间滑动控件
        this.InitRoomHorizontalPagesControl();
      }
    }
    /// <summary>
    /// 初始化场景,功能的切换控件
    /// </summary>
    private void InitSceneFunctionSwitchControl()
    {
      //功能和场景的背景图
      var functionSceneView = new FrameLayout();
      functionSceneView.Y = Application.GetRealHeight(861);
      functionSceneView.Width = Application.GetRealWidth(832);
      functionSceneView.Height = Application.GetRealHeight(167);
      functionSceneView.Gravity = Gravity.CenterHorizontal;
      functionSceneView.BackgroundImagePath = "Item/SceneFunctionBG.png";
      this.listBodyContr.frameTable.AddChidren(functionSceneView);
      //场景
      var btnScene = new NormalViewControl(350, 100, true);
      btnScene.X = Application.GetRealWidth(30);
      btnScene.Y = Application.GetRealHeight(12);
      btnScene.TextColor = ZigbeeColor.Current.GXCTextGrayColor3;
      btnScene.SelectedTextColor = ZigbeeColor.Current.GXCTextWhiteColor;
      btnScene.TextID = R.MyInternationalizationString.Scence;
      btnScene.TextSize = 15;
      btnScene.TextAlignment = TextAlignment.Center;
      functionSceneView.AddChidren(btnScene);
      //功能
      var btnFunction = new NormalViewControl(360, 100, true);
      btnFunction.X = Application.GetRealWidth(430);
      btnFunction.Y = Application.GetRealHeight(12);
      btnFunction.TextColor = ZigbeeColor.Current.GXCTextGrayColor3;
      btnFunction.SelectedTextColor = ZigbeeColor.Current.GXCTextWhiteColor;
      btnFunction.TextSize = 15;
      btnFunction.TextID = R.MyInternationalizationString.Function;
      btnFunction.TextAlignment = TextAlignment.Center;
      functionSceneView.AddChidren(btnFunction);
      if (UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex == 1)
      {
        btnScene.IsSelected = true;
        btnScene.TextSize = 17;
        btnScene.IsBold = true;
      }
      else if (UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex == 2)
      {
        btnFunction.IsSelected = true;
        btnFunction.TextSize = 17;
        btnFunction.IsBold = true;
      }
      //功能和场景bodyView
      this.bodyFrameLayout = new NormalFrameLayout();
      bodyFrameLayout.Y = functionSceneView.Bottom;
      bodyFrameLayout.Height = this.listBodyContr.frameTable.Height - functionSceneView.Bottom;
      bodyFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
      this.listBodyContr.frameTable.AddChidren(bodyFrameLayout);
      //选择功能分栏
      btnFunction.ButtonClickEvent += (sender, e) =>
      {
        if (btnFunction.IsSelected == true)
        {
          return;
        }
        //功能分支选择
        UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex = 2;
        btnScene.IsSelected = false;
        btnScene.TextSize = 15;
        btnScene.IsBold = false;
        btnFunction.IsSelected = true;
        btnFunction.TextSize = 17;
        btnFunction.IsBold = true;
        //刷新设备桌布控件
        this.RefreshBodyView();
      };
      //选择场景分栏
      btnScene.ButtonClickEvent += (sender, e) =>
      {
        if (btnScene.IsSelected == true)
        {
          return;
        }
        //场景分支选择
        UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex = 1;
        btnScene.IsSelected = true;
        btnScene.TextSize = 17;
        btnScene.IsBold = true;
        btnFunction.IsSelected = false;
        btnFunction.TextSize = 15;
        btnFunction.IsBold = false;
        //刷新设备桌布控件
        this.RefreshBodyView();
      };
    }
    /// <summary>
    /// 提示用户没有网关,并快速跳转到添加网关界面
    /// </summary>
    private void ShowNoGatewayTip()
    {
      var bg = new PicViewControl(717, 478);
      bg.Y = Application.GetRealHeight(302);
      bg.Gravity = Gravity.CenterHorizontal;
      bg.UnSelectedImagePath = "Item/NoBindGW.png";
      this.listBodyContr.frameTable.AddChidren(bg);
      var tip = new NormalViewControl(this.GetPictrueRealSize(717), this.GetPictrueRealSize(58), false);
      tip.Y = bg.Bottom + Application.GetRealHeight(35);
      tip.Gravity = Gravity.CenterHorizontal;
      tip.TextID = R.MyInternationalizationString.NewAccountNeedBingGW;
      tip.TextColor = ZigbeeColor.Current.GXCTextGrayColor;
      tip.TextAlignment = TextAlignment.Center;
      this.listBodyContr.frameTable.AddChidren(tip);
      var addBG = new PicViewControl(971, 366);
      addBG.Y = tip.Bottom + Application.GetRealHeight(176);
      addBG.UnSelectedImagePath = "Item/VirtualFrame.png";
      addBG.Gravity = Gravity.CenterHorizontal;
      this.listBodyContr.frameTable.AddChidren(addBG);
      var addGatewayBtn = new IconViewControl(89);
      addGatewayBtn.Y = tip.Bottom + Application.GetRealHeight(271);
      addGatewayBtn.UnSelectedImagePath = "Item/Add_GW.png";
      addGatewayBtn.Gravity = Gravity.CenterHorizontal;
      this.listBodyContr.frameTable.AddChidren(addGatewayBtn);
      addGatewayBtn.ButtonClickEvent += (sender, e) =>
      {
        var form = new UserCenter.GatewayAdd.NewGateWayMenuSelectForm();
        form.AddForm();
      };
      var addTip = new PicViewControl(717, 58);
      addTip.Y = addGatewayBtn.Bottom + Application.GetRealHeight(23);
      addTip.Gravity = Gravity.CenterHorizontal;
      addTip.TextID = R.MyInternationalizationString.AddSmartGW;
      addTip.TextColor = ZigbeeColor.Current.GXCTextBlackColor;
      addTip.TextAlignment = TextAlignment.Center;
      this.listBodyContr.frameTable.AddChidren(addTip);
      addTip.ButtonClickEvent += (sender, e) =>
      {
        var form = new UserCenter.GatewayAdd.NewGateWayMenuSelectForm();
        form.AddForm();
      };
    }
    #endregion
    #region ■ 初始化房间滑动控件_________________
    /// <summary>
    /// 初始化房间滑动控件
    /// </summary>
    private void InitRoomHorizontalPagesControl()
    {
      //左右滑动控件的背景容器
      var frameBack = new FrameLayout();
      frameBack.Y = Application.GetRealHeight(302);
      frameBack.Width = this.Width;
      frameBack.Height = Application.GetRealHeight(478);
      this.listBodyContr.frameTable.AddChidren(frameBack);
      //房间滑动控件左边的虚拟图片
      var mLeftImageView = new ImageView();
      mLeftImageView.ImagePath = "RoomIcon/19.jpg";
      mLeftImageView.Radius = (uint)Application.GetRealHeight(17);
      mLeftImageView.Width = Application.GetRealWidth(717);
      mLeftImageView.Height = Application.GetRealHeight(478);
      //房间滑动控件右边的虚拟图片
      var mRightImageView = new ImageView();
      mRightImageView.ImagePath = "RoomIcon/19.jpg";
      mRightImageView.Radius = (uint)Application.GetRealHeight(17);
      mRightImageView.Width = mLeftImageView.Width;
      mRightImageView.Height = mLeftImageView.Height;
      this.roomPageView = new HorizontalPages();
      roomPageView.Width = frameBack.Width;
      roomPageView.Height = frameBack.Height;
      frameBack.AddChidren(roomPageView);
      //突出边距
      roomPageView.TCBJ = Application.GetRealWidth(124);
      //两个page之间的间距
      roomPageView.JMBJ = Application.GetRealWidth(58);
      //在这个控件的下面添加阴影效果
      var btnRoomBottomShadow = new NormalViewControl(717, 81, true);
      btnRoomBottomShadow.Y = frameBack.Bottom;
      btnRoomBottomShadow.Gravity = Gravity.CenterHorizontal;
      btnRoomBottomShadow.UnSelectedImagePath = "Room/RoomCardShadow.png";
      this.listBodyContr.frameTable.AddChidren(btnRoomBottomShadow);
      //设置控制最左和最右2边填充的假背景View,不设置不显示,需要在AddChidren 前调用该方法
      roomPageView.SetLeftAndRightImageView(mLeftImageView, mRightImageView);
      //当前房间
      var listRoom = HdlRoomLogic.Current.GetRoomsByFloorIdAppendLoveRoom(Config.Instance.Home.CurrentFloorId);
      if (HdlRoomLogic.Current.NowMainPageRoom == null)
      {
        HdlRoomLogic.Current.NowMainPageRoom = listRoom[0];
      }
      var curIndex = 0;
      for (int i = 0; i < listRoom.Count; i++)
      {
        var roomView = new RoomView(0, 0);
        roomPageView.AddChidren(roomView);
        roomView.Init(listRoom[i]);
        if (listRoom[i].Id == HdlRoomLogic.Current.NowMainPageRoom.Id)
        {
          //当前房间,则显示那些数据出来
          roomView.HideName(false);
          //当前索引
          curIndex = i;
        }
        else
        {
          roomView.HideName(true);
        }
      }
      //在房间卡片左边添加遮罩
      var btnRoomLeftShadow = new FrameLayout();
      btnRoomLeftShadow.Width = Application.GetRealWidth(124);
      btnRoomLeftShadow.Height = roomPageView.Height - roomPageView.JMBJ * 2;
      btnRoomLeftShadow.Gravity = Gravity.CenterVertical;
      btnRoomLeftShadow.BackgroundColor = 0x1c000000;
      btnRoomLeftShadow.SetCornerWithSameRadius(Application.GetRealHeight(12), HDLUtils.RectCornerTopRight | HDLUtils.RectCornerBottomRight);
      //frameBack.AddChidren(btnRoomLeftShadow);//暂时不加遮罩
      //在房间卡片右边添加遮罩
      var btnRoomRightShadow = new FrameLayout();
      btnRoomRightShadow.Width = btnRoomLeftShadow.Width;
      btnRoomRightShadow.Height = btnRoomLeftShadow.Height;
      btnRoomRightShadow.X = Application.GetRealWidth(956);
      btnRoomRightShadow.Gravity = Gravity.CenterVertical;
      btnRoomRightShadow.BackgroundColor = btnRoomLeftShadow.BackgroundColor;
      btnRoomRightShadow.SetCornerWithSameRadius(Application.GetRealHeight(12), HDLUtils.RectCornerTopLeft | HDLUtils.RectCornerBottomLeft);
      //frameBack.AddChidren(btnRoomRightShadow);//暂时不加遮罩
      //控件开始滑动的事件
      roomPageView.StartScrollAction += () =>
      {
        this.isRoomChanging = true;
        //开始滑动时,隐藏当前的数据显示
        (roomPageView.GetChildren(roomPageView.PageIndex) as RoomView).HideName(true);
        btnRoomLeftShadow.Visible = false;
        btnRoomRightShadow.Visible = false;
        btnRoomBottomShadow.Visible = false;
      };
      roomPageView.EndScrollAction += () =>
      {
        curIndex = roomPageView.PageIndex;
        //前一张卡片
        if (roomPageView.GetChildren(curIndex - 1) != null)
        {
          (roomPageView.GetChildren(curIndex - 1) as RoomView).HideName(true);
        }
        //当前卡片
        if (roomPageView.GetChildren(curIndex) != null)
        {
          (roomPageView.GetChildren(curIndex) as RoomView).HideName(false);
        }
        //下一张卡片
        if (roomPageView.GetChildren(curIndex + 1) != null)
        {
          (roomPageView.GetChildren(curIndex + 1) as RoomView).HideName(true);
        }
        btnRoomLeftShadow.Visible = true;
        btnRoomRightShadow.Visible = true;
        btnRoomBottomShadow.Visible = true;
        //切换当前房间
        HdlRoomLogic.Current.NowMainPageRoom = listRoom[curIndex];
        //刷新设备桌布控件
        this.isRoomChanging = false;
        this.RefreshBodyView();
      };
      //设置房间的初始选择
      roomPageView.PageIndex = curIndex;
      //如果索引是第一个时,它触发不了事件,需要手动触发
      if (curIndex == 0)
      {
        roomPageView.EndScrollAction();
      }
    }
    /// <summary>
    /// 设置房间的滑动容器控件选择指定的房间(供外部调用)
    /// </summary>
    /// <param name="room"></param>
    public void SetRoomPageViewSelect(Room room)
    {
      if (roomPageView == null)
      {
        //当没有绑定有网关,左滑时,点击房间列表,则不鸟它
        return;
      }
      var listRoom = HdlRoomLogic.Current.GetRoomsByFloorIdAppendLoveRoom(Config.Instance.Home.CurrentFloorId);
      for (int i = 0; i < listRoom.Count; i++)
      {
        if (room.Id == listRoom[i].Id)
        {
          if (roomPageView.PageIndex != i)
          {
            //不相同时才刷新
            roomPageView.PageIndex = i;
          }
          break;
        }
      }
      //重置一下楼层名字
      if (this.btnFloorName != null)
      {
        btnFloorName.Text = Config.Instance.Home.GetCurrentFloorName;
      }
    }
    #endregion
    #region ■ 功能分支___________________________
    /// <summary>
    /// 显示功能
    /// </summary>
    private void ShowFunction()
    {
      //先清空缓存
      bodyFrameLayout.RemoveAll();
      bodyFrameLayout.Height = Application.GetRealHeight(750);
      this.listBodyContr.RecoverTableHeight();
      this.dicSceneCardControl = new Dictionary<int, Controls.SceneCardControl>();
      this.dicDeviceCardControl = new Dictionary<string, Controls.DeviceCardCommon>();
      var listDevice = new List<CommonDevice>();
      foreach (var mainkeys in HdlRoomLogic.Current.NowMainPageRoom.ListDevice)
      {
        var device = LocalDevice.Current.GetDevice(mainkeys);
        //判断该设备能否显示在主页
        if (LocalDevice.Current.CanShowInHomeHomeMainPage(device) == false)
        {
          continue;
        }
        listDevice.Add(device);
      }
      //当前房间的设备数
      if (listDevice.Count == 0)
      {
        //还没有功能哦
        this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.NoFunction_Tip));
        return;
      }
      //设备列表容器控件
      var listDeviceBackContr = new FrameLayout();
      listDeviceBackContr.Height = bodyFrameLayout.Height;
      bodyFrameLayout.AddChidren(listDeviceBackContr);
      HdlThreadLogic.Current.RunThread(() =>
      {
        //初始化设备卡片列表控件
        this.InitDeviceListCardControl(listDeviceBackContr, listDevice);
      });
    }
    /// <summary>
    /// 初始化设备卡片列表控件
    /// </summary>
    /// <param name="listDeviceBackContr"></param>
    private void InitDeviceListCardControl(FrameLayout listDeviceBackContr, List<CommonDevice> listDevice)
    {
      //设备计数
      int contrCount = 0;
      //X轴坐标(图片左边有余白)
      int XX = this.GetPictrueRealSize(44);
      //Y轴坐标
      int YY = 0;
      var listContr = new List<Controls.DeviceCardCommon>();
      foreach (var device in listDevice)
      {
        if (this.isRoomChanging == true || listDeviceBackContr.Parent == null)
        {
          //放弃初始化控件
          return;
        }
        HdlThreadLogic.Current.RunMain(() =>
        {
          Controls.DeviceCardCommon cardContr = null;
          //窗帘
          if (device.Type == DeviceType.WindowCoveringDevice)
          {
            cardContr = new Controls.DeviceCurtainCardControl();
          }
          //继电器
          else if (device.Type == DeviceType.OnOffOutput)
          {
            cardContr = new Controls.DeviceRelayCardControl();
          }
          //空气开关
          else if (device.Type == DeviceType.AirSwitch)
          {
            cardContr = new Controls.DeviceAirSwitchCardControl();
          }
          //空调
          else if (device.Type == DeviceType.Thermostat)
          {
            cardContr = new Controls.DeviceAcCardControl();
          }
          //新风
          else if (device.Type == DeviceType.FreshAir)
          {
            cardContr = new Controls.DeviceFreshAirCardControl();
          }
          // PM2.5
          else if (device.Type == DeviceType.PMSensor)
          {
            cardContr = new Controls.DevicePmSensorCardControl();
          }
          // 晾衣架
          else if (device.Type == DeviceType.Airer)
          {
            cardContr = new Controls.DeviceAirerCardControl();
          }
          //彩灯(调光器)
          else if (device.Type == DeviceType.DimmableLight)
          {
            cardContr = new Controls.DeviceColorLightCardControl();
          }
          //传感器
          else if (device.Type == DeviceType.IASZone)
          {
            cardContr = new Controls.DeviceSensorCardControl();
          }
          //温湿度
          else if (device.Type == DeviceType.TemperatureSensor)
          {
            cardContr = new Controls.DeviceTemperatureCardControl();
          }
          //门锁
          else if (device.Type == DeviceType.DoorLock)
          {
            cardContr = new Controls.DeviceDoorLockCardControl();
          }
          //色温灯
          else if (device.Type == DeviceType.ColorTemperatureLight)
          {
            cardContr = new Controls.DeviceColorTemperatureCardControl();
          }
          //无法识别
          else
          {
            cardContr = new Controls.DeviceCardCommon();
          }
          //初始化卡片
          cardContr.X = XX;
          cardContr.Y = YY;
          listDeviceBackContr.AddChidren(cardContr);
          cardContr.InitControl(device, HdlRoomLogic.Current.NowMainPageRoom);
          //卡片需要被移除的事件
          cardContr.CardNeedRemoveEvent += () =>
          {
            //重置设备卡片控件坐标(卡片需要被删除专用)
            this.ResetDeviceCardControlLocation(LocalDevice.Current.GetDeviceMainKeys(device));
          };
          //设备计数
          contrCount++;
          //卡片左右两边有14的余白,每两个后,X轴重置
          XX = contrCount % 2 == 0 ? this.GetPictrueRealSize(44) : cardContr.Right + this.GetPictrueRealSize(20);
          if (contrCount % 2 == 0)
          {
            //没两个之后,Y轴递增
            YY = cardContr.Bottom + this.GetPictrueRealSize(15);
          }
          //控件记录到缓存中
          this.dicDeviceCardControl[LocalDevice.Current.GetDeviceMainKeys(device)] = cardContr;
          //加缓存,然后发命令
          listContr.Add(cardContr);
          if (contrCount == listDevice.Count)
          {
            //调整桌布大小
            if (contrCount > 4)
            {
              bodyFrameLayout.Height = listDeviceBackContr.GetChildren(listDeviceBackContr.ChildrenCount - 1).Bottom;
              listDeviceBackContr.Height = bodyFrameLayout.Height;
              this.listBodyContr.frameTable.Height = bodyFrameLayout.Bottom;
            }
            HdlThreadLogic.Current.RunThread(() =>
                    {
                      while (listDeviceBackContr.Parent != null)
                      {
                        bool hadSend = false;
                        for (int i = 0; i < listContr.Count; i++)
                        {
                          if (listDeviceBackContr.Parent == null)
                          {
                            return;
                          }
                          //如果还没有接收到数据,则再次发送
                          if (listContr[i].ReceiveResponeResultStatu() == false)
                          {
                            //发送获取状态的命令
                            listContr[i].SendStatuComand();
                            hadSend = true;
                            System.Threading.Thread.Sleep(200);
                          }
                        }
                        if (hadSend == false)
                        {
                          //如果已经全部回路都接收了网关回复,则不再二次发送
                          break;
                        }
                        System.Threading.Thread.Sleep(2000);
                      }
                    }, ShowErrorMode.NO);
          }
        });
        if (contrCount > 6)
        {
          //让它呈现一种直接一次性刷出来的效果
          System.Threading.Thread.Sleep(50);
        }
      }
    }
    #endregion
    #region ■ 重置设备卡片控件坐标_______________
    /// <summary>
    /// 重置设备卡片控件坐标(卡片需要被删除专用)
    /// </summary>
    /// <param name="deleteKey">需要被删除的卡片主键</param>
    private void ResetDeviceCardControlLocation(string deleteKey)
    {
      if (this.dicDeviceCardControl.ContainsKey(deleteKey) == false)
      {
        //如果列表里面没有这个控件的话
        return;
      }
      var deleteContr = this.dicDeviceCardControl[deleteKey];
      //设备计数
      int contrCount = 0;
      bool canMove = false;
      int YY = 0;
      foreach (var mainkey in this.dicDeviceCardControl.Keys)
      {
        if (deleteKey == mainkey)
        {
          //如果已经到了要删除的卡片,删除掉它,然后它之后的卡片全部向上移动
          canMove = true;
          continue;
        }
        //设备计数
        contrCount++;
        //卡片左右两边有14的余白,每两个后,X轴重置
        int XX = 0;
        if (contrCount % 2 == 1)
        {
          //第一个固定44
          XX = this.GetPictrueRealSize(44);
        }
        else
        {
          //第二个是44+控件宽度+20余白
          XX = this.GetPictrueRealSize(44) + this.dicDeviceCardControl[mainkey].Width + this.GetPictrueRealSize(20);
        }
        //每两个之后,并且是第一个的时候,Y轴递增
        if (contrCount > 2 && contrCount % 2 == 1)
        {
          //求商
          int value = contrCount / 2;
          //控件的底部有15的间隔
          YY = value * (this.dicDeviceCardControl[mainkey].Height + this.GetPictrueRealSize(15));
        }
        if (canMove == true)
        {
          //移动控件
          this.dicDeviceCardControl[mainkey].X = XX;
          this.dicDeviceCardControl[mainkey].Y = YY;
        }
      }
      //去找一下这个控件的列表容器
      var listView = deleteContr.Parent as FrameLayout;
      //删除掉那张卡片的主键
      deleteContr.RemoveFromParent();
      this.dicDeviceCardControl.Remove(deleteKey);
      //调整桌布高度
      if (listView.ChildrenCount >= 4)
      {
        bodyFrameLayout.Height = listView.GetChildren(listView.ChildrenCount - 1).Bottom;
        listView.Height = bodyFrameLayout.Height;
        this.listBodyContr.frameTable.Height = bodyFrameLayout.Bottom;
      }
    }
    #endregion
    #region ■ 场景分支___________________________
    /// <summary>
    /// 显示场景
    /// </summary>
    private void ShowScene()
    {
      //先清空缓存
      bodyFrameLayout.RemoveAll();
      bodyFrameLayout.Height = Application.GetRealHeight(750);
      this.listBodyContr.RecoverTableHeight();
      this.dicSceneCardControl = new Dictionary<int, Controls.SceneCardControl>();
      this.dicDeviceCardControl = new Dictionary<string, Controls.DeviceCardCommon>();
      var listScene = new List<SceneUI>();
      foreach (int sceneId in HdlRoomLogic.Current.NowMainPageRoom.ListSceneId)
      {
        var sceneUi = HdlSceneLogic.Current.GetSceneUIBySceneId(sceneId);
        if (sceneUi == null)
        {
          //这个场景不见了
          continue;
        }
        listScene.Add(sceneUi);
      }
      if (listScene.Count == 0)
      {
        //还没有场景哦
        this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.NoScene_Tip));
        return;
      }
      //列表控件
      var listSceneBackContr = new FrameLayout();
      listSceneBackContr.Height = bodyFrameLayout.Height;
      bodyFrameLayout.AddChidren(listSceneBackContr);
      HdlThreadLogic.Current.RunThread(() =>
      {
        //初始化场景卡片列表控件
        this.InitSceneListCardControl(listSceneBackContr, listScene);
      });
    }
    /// <summary>
    /// 初始化场景卡片列表控件
    /// </summary>
    /// <param name="listSceneBackContr"></param>
    private void InitSceneListCardControl(FrameLayout listSceneBackContr, List<SceneUI> listScene)
    {
      //场景计数
      int contrCount = 0;
      //X轴坐标(图片左边有余白)
      int XX = this.GetPictrueRealSize(44);
      //Y轴坐标
      int YY = 0;
      foreach (var sceneUi in listScene)
      {
        if (this.isRoomChanging == true || listSceneBackContr.Parent == null)
        {
          //放弃初始化控件
          return;
        }
        HdlThreadLogic.Current.RunMain(() =>
        {
          //初始化卡片
          var cardContr = new Controls.SceneCardControl();
          cardContr.X = XX;
          cardContr.Y = YY;
          listSceneBackContr.AddChidren(cardContr);
          cardContr.InitControl(sceneUi);
          //卡片需要被移除的事件
          cardContr.CardNeedRemoveEvent += () =>
          {
            //重置设备卡片控件坐标(卡片需要被删除专用)
            this.ResetSceneCardControlLocation(sceneUi.Id);
          };
          //设备计数
          contrCount++;
          //每两个后,X轴重置
          XX = contrCount % 2 == 0 ? this.GetPictrueRealSize(44) : cardContr.Right + this.GetPictrueRealSize(20);
          if (contrCount % 2 == 0)
          {
            //没两个之后,Y轴递增
            YY = cardContr.Bottom + this.GetPictrueRealSize(15);
          }
          //控件记录到缓存中
          this.dicSceneCardControl[sceneUi.Id] = cardContr;
          if (contrCount == listScene.Count)
          {
            //调整桌布高度
            if (contrCount > 4)
            {
              bodyFrameLayout.Height = listSceneBackContr.GetChildren(listSceneBackContr.ChildrenCount - 1).Bottom;
              listSceneBackContr.Height = bodyFrameLayout.Height;
              this.listBodyContr.frameTable.Height = bodyFrameLayout.Bottom;
            }
            HdlThreadLogic.Current.RunThread(() =>
                    {
                      //设置目前场景的延迟状态
                      this.SetSceneDelayTime(listSceneBackContr);
                    });
          }
        });
        if (contrCount > 6)
        {
          //让它呈现一种一次性显示出来的效果
          System.Threading.Thread.Sleep(50);
        }
      }
    }
    /// <summary>
    /// 设置场景的延迟状态
    /// </summary>
    /// <param name="listSceneBackContr"></param>
    private async void SetSceneDelayTime(FrameLayout listSceneBackContr)
    {
      await System.Threading.Tasks.Task.Delay(1500);
      if (listSceneBackContr.Parent == null)
      {
        //控件已经被移除
        return;
      }
      //等待1.5秒之后,获取延迟状态
      var result = await Scene.CatDelaySceneAsync();
      if (result == null || result.catDelaySceneResponseData == null)
      {
        return;
      }
      if (listSceneBackContr.Parent == null)
      {
        //控件已经被移除
        return;
      }
      foreach (var data in result.catDelaySceneResponseData.DelayScenesList)
      {
        var sceneUi = HdlSceneLogic.Current.GetSceneUIBySceneId(data.ScenesId);
        if (sceneUi == null)
        {
          continue;
        }
        if (this.dicSceneCardControl.ContainsKey(data.ScenesId) == false)
        {
          continue;
        }
        //修改剩余延迟时间
        sceneUi.RemainTime = data.RemainTime;
        HdlThreadLogic.Current.RunMain(() =>
        {
          //刷新卡片信息
          this.dicSceneCardControl[data.ScenesId].RefreshControlInfo(sceneUi);
        });
      }
    }
    #endregion
    #region ■ 重置场景卡片控件坐标_______________
    /// <summary>
    /// 重置场景卡片控件坐标(卡片需要被删除专用)
    /// </summary>
    /// <param name="deleteKey">需要被删除的卡片主键</param>
    private void ResetSceneCardControlLocation(int deleteId)
    {
      if (this.dicSceneCardControl.ContainsKey(deleteId) == false)
      {
        //如果列表里面没有这个控件的话
        return;
      }
      var deleteContr = this.dicSceneCardControl[deleteId];
      //设备计数
      int contrCount = 0;
      bool canMove = false;
      int YY = 0;
      foreach (var sceneId in this.dicSceneCardControl.Keys)
      {
        if (deleteId == sceneId)
        {
          //如果已经到了要删除的卡片,删除掉它,然后它之后的卡片全部向上移动
          canMove = true;
          continue;
        }
        //设备计数
        contrCount++;
        //每两个后,X轴重置
        int XX = 0;
        if (contrCount % 2 == 1)
        {
          //第一个固定44
          XX = this.GetPictrueRealSize(44);
        }
        else
        {
          //第二个是44+控件宽度+20余白
          XX = this.GetPictrueRealSize(44) + this.dicSceneCardControl[sceneId].Width + this.GetPictrueRealSize(20);
        }
        //每两个之后,并且是第一个时,Y轴递增
        if (contrCount > 2 && contrCount % 2 == 1)
        {
          //求商
          int value = contrCount / 2;
          //控件的底部有15的间隔
          YY = value * (this.dicSceneCardControl[sceneId].Height + this.GetPictrueRealSize(15));
        }
        if (canMove == true)
        {
          //移动控件
          this.dicSceneCardControl[sceneId].X = XX;
          this.dicSceneCardControl[sceneId].Y = YY;
        }
      }
      //去找一下这个控件的列表容器
      var listView = deleteContr.Parent as FrameLayout;
      //删除掉那张卡片的主键
      deleteContr.RemoveFromParent();
      this.dicSceneCardControl.Remove(deleteId);
      //调整桌布高度
      if (listView.ChildrenCount >= 4)
      {
        bodyFrameLayout.Height = listView.GetChildren(listView.ChildrenCount - 1).Bottom;
        listView.Height = bodyFrameLayout.Height;
        this.listBodyContr.frameTable.Height = bodyFrameLayout.Bottom;
      }
    }
    #endregion
    #region ■ 刷新桌布控件_______________________
    /// <summary>
    /// 刷新设备桌布控件
    /// </summary>
    public void RefreshBodyView()
    {
      bodyFrameLayout.RemoveAll();
      //让界面别闪烁
      HdlThreadLogic.Current.RunMainInThread(() =>
      {
        if (UserCenterResourse.ResidenceOption.HomeMainPageSwitchIndex == 2)
        {
          //功能
          this.ShowFunction();
        }
        else
        {
          //场景
          this.ShowScene();
        }
      });
    }
    #endregion
    #region ■ 切换住宅___________________________
    /// <summary>
    /// 切换住宅
    /// </summary>
    /// <param name="home">Home.</param>
    private void ChangeResidence(House home)
    {
      HdlThreadLogic.Current.RunThread(() =>
      {
        this.ShowProgressBar();
        //切换住宅
        var result = HdlResidenceLogic.Current.SwitchResidence(home.Id);
        HdlThreadLogic.Current.RunMain(() =>
              {
                if (result == true)
                {
                  //刷新界面
                  this.ShowForm();
                }
                this.CloseProgressBar();
              });
      });
    }
    #endregion
    #region ■ 一般设备状态上报___________________
    /// <summary>
    /// 添加一般设备状态上报事件
    /// </summary>
    private void AddNormalDeviceReportEvent()
    {
      //设备属性上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
      HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceStatus", ReceiveComandDiv.A设备属性上报, (report) =>
      {
        HdlThreadLogic.Current.RunMain(() =>
              {
                //处理一般设备的上报数据
                string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                if (this.dicDeviceCardControl.ContainsKey(mainKeys) == true)
                {
                  //刷新卡片信息
                  var locadevice = LocalDevice.Current.GetDevice(mainKeys);
                  //已经接收到网关的反馈 2020.05.09:删除Ack主题
                  this.dicDeviceCardControl[mainKeys].SetHadGetResponeResultStatu();
                  //刷新控件
                  if (locadevice.Type != DeviceType.IASZone)
                  {
                    //传感器不需要属性上报(但是那个球型传感器居然会属性上报)
                    this.dicDeviceCardControl[mainKeys].RefreshControlInfo(locadevice);
                  }
                  else if (report.DeviceStatusReport.CluterID == 1)
                  {
                    //这个是电量推送
                    this.dicDeviceCardControl[mainKeys].RefreshBatteryStatu();
                  }
                }
              }, ShowErrorMode.NO);
      });
      //设备在线上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
      HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceOnline", ReceiveComandDiv.A设备在线上报, (report) =>
      {
        HdlThreadLogic.Current.RunMain(() =>
              {
                string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                if (this.dicDeviceCardControl.ContainsKey(mainKeys) == true)
                {
                  //刷新卡片状态
                  bool bolOnline = Common.LocalDevice.Current.CheckDeviceIsOnline(report);
                  this.dicDeviceCardControl[mainKeys].SetCardOnlineStatu(bolOnline);
                  return;
                }
              }, ShowErrorMode.NO);
      });
    }
    #endregion
    #region ■ 传感器状态上报_____________________
    /// <summary>
    /// 添加传感器状态上报事件
    /// </summary>
    private void AddSensorDeviceReportEvent()
    {
      //传感器上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
      HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewSensor", ReceiveComandDiv.A传感器上报, (report) =>
      {
        HdlThreadLogic.Current.RunMain(() =>
              {
                //处理传感器上报数据
                string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                if (this.dicDeviceCardControl.ContainsKey(mainKeys) == true)
                {
                  //开启卡片闪烁特效
                  this.dicDeviceCardControl[mainKeys].StartCardLightAppeal();
                }
              }, ShowErrorMode.NO);
      });
    }
    #endregion
    #region ■ 传感器状态还原_____________________
    /// <summary>
    /// 开启传感器状态还原的线程
    /// </summary>
    private void StartRecoverSenorStatuThread()
    {
      HdlThreadLogic.Current.RunThread(() =>
      {
        while (this.Parent != null)
        {
          try
          {
            var dicControl = this.dicDeviceCardControl;
            foreach (var contr in dicControl.Values)
            {
              //如果是传感器,则刷新状态
              if (contr.device.Type == DeviceType.IASZone && contr.Parent != null
                && ((IASZone)contr.device).iASInfo == null)
              {
                HdlThreadLogic.Current.RunMain(() =>
                      {
                        //设备状态必须刷新
                        string statuText = HdlDeviceOtherLogic.Current.GetDeviceStatu(contr.device);
                        contr.SetDeviceStatuText(statuText);
                        //传感器切换为灰色状态
                        contr.SwitchCardStatuAppeal(false);
                      }, ShowErrorMode.NO);
              }
            }
            System.Threading.Thread.Sleep(8000);
          }
          catch { System.Threading.Thread.Sleep(3000); }
        }
      });
    }
    #endregion
    #region ■ 切换楼层___________________________
    /// <summary>
    /// 显示选择楼层的界面
    /// </summary>
    private void ShowSelectFloorForm(NormalViewControl btnFloor)
    {
      var floorFL = new Category.SelectFloorForm();
      floorFL.CurFloorId = Config.Instance.Home.CurrentFloorId;
      this.AddChidren(floorFL);
      floorFL.Init(35, 153);
      floorFL.FloorAction = (floorId) =>
      {
        btnFloor.Text = HdlResidenceLogic.Current.GetFloorNameById(floorId);
        Config.Instance.Home.CurrentFloorId = floorId;
        HdlRoomLogic.Current.NowMainPageRoom = HdlRoomLogic.Current.GetLoveRoom();
        //分类界面需要刷新
        UserView.UserPage.Instance.RefreshCategoryForm = true;
        this.ShowForm();
      };
    }
    #endregion
    #region ■ 界面关闭___________________________
    /// <summary>
    /// 界面关闭
    /// </summary>
    public override void CloseFormBefore()
    {
      //把静态变量的这个东西置空
      Instance = null;
      HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewDeviceOnline");
      HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewDeviceStatus");
      HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewSensor");
      base.CloseFormBefore();
    }
    #endregion
    #region ■ 一般方法___________________________
    /// <summary>
    /// 检测是否绑定有网关
    /// </summary>
    private bool CheckHadBindGateway()
    {
      //获取本地已经绑定的网关
      var gatewayList = HdlGatewayLogic.Current.GetAllLocalGateway();
      if (gatewayList.Count == 0)
      {
        return false;
      }
      return true;
    }
    #endregion
    #region ■ 界面重新激活事件___________________
    /// <summary>
    /// 自身的上层界面关闭后,它自身处于最上层时,触发的事件
    /// </summary>
    public override int FormActionAgainEvent()
    {
      CommonPage.Instance.IsDrawerLockMode = false;
      return 1;
    }
    #endregion
  }
}