陈嘉乐
2020-07-10 48ba446936b51fffafa7c3600c0dadc6ac0e8c20
ZigbeeApp/Shared/Phone/Category/CategoryMainForm.cs
old mode 100644 new mode 100755
@@ -1,938 +1,996 @@
using System;
using System.Collections.Generic;
using Shared.Common;
using Shared.Phone.UserView;
using Shared.R;
using Shared.Phone.Device.CommonForm;
using Shared.Phone.UserCenter;
using ZigBee.Device;
namespace Shared.Phone.Category
{
    /// <summary>
    /// 分类的主界面
    /// </summary>
    public class CategoryMainForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// The instance.
        /// </summary>
        public static CategoryMainForm instance;
        /// <summary>
        /// 场景功能中部背景bodyView(高度为设备菜单的那个白色背景的上部到屏幕底部)
        /// </summary>
        public FrameLayout functionSceneBodyView;
        /// <summary>
        /// 中部背景bodyView(高度为场景功能切换控件的底部到屏幕底部)
        /// </summary>
        public FrameLayout functionSceneAutoBodyView;
        /// <summary>
        /// 右上角添加按钮
        /// </summary>
        private IconViewControl btnTopRightAdd = null;
        /// <summary>
        /// 楼层控件
        /// </summary>
        private NormalViewControl btnFloorName = null;
        /// <summary>
        /// 楼层图标控件
        /// </summary>
        private IconViewControl btnFloorIcon = null;
        /// <summary>
        /// 设备行控件
        /// </summary>
        private Dictionary<string, Controls.DeviceRowCommon> dicDeviceRowControl = new Dictionary<string, Controls.DeviceRowCommon>();
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 构造方法
        /// </summary>
        public CategoryMainForm()
        {
            BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
            instance = this;
        }
        /// <summary>
        /// 显示界面--默认场景界面
        /// </summary>
        public void ShowForm()
        {
            //先清除吧
            this.RemoveAll();
            //初始化头部控件
            this.InitTopControl();
            //初始化中间控件
            this.InitMidControls();
            //添加设备状态上报事件
            this.AddNormalDeviceReportEvent();
            //添加传感器状态上报事件
            this.AddSensorDeviceReportEvent();
        }
        /// <summary>
        /// 初始化头部控件
        /// </summary>
        private void InitTopControl()
        {
            //头部FrameLayout
            this.topFrameLayout = new FrameLayout();
            topFrameLayout.Y = Application.GetRealHeight(104);
            topFrameLayout.Height = Application.GetRealHeight(127);
            topFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
            this.AddChidren(topFrameLayout);
            //标题分类
            var btnTitle = new NormalViewControl(300, 95, true);
            btnTitle.X = ControlCommonResourse.XXLeft;
            btnTitle.Gravity = Gravity.CenterVertical;
            btnTitle.TextID = R.MyInternationalizationString.Category;
            btnTitle.TextSize = 24;
            btnTitle.TextColor = ZigbeeColor.Current.GXCTextDeepBlackColor;
            btnTitle.IsBold = true;
            topFrameLayout.AddChidren(btnTitle);
            //右上角添加按钮
            this.btnTopRightAdd = new IconViewControl(104);
            btnTopRightAdd.X = Application.GetRealWidth(953);
            btnTopRightAdd.Gravity = Gravity.CenterVertical;
            btnTopRightAdd.UnSelectedImagePath = "Item/Add_Category.png";
            topFrameLayout.AddChidren(btnTopRightAdd);
            btnTopRightAdd.ButtonClickEvent += (sender, e) =>
            {
                if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 0)
                {
                    //添加场景
                    Add_Scene();
                }
                else if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 2)
                {
                    //添加自动化
                    Add_Automation();
                }
            };
        }
        /// <summary>
        /// 初始化中间控件
        /// </summary>
        private void InitMidControls()
        {
            //应该是body吧
            this.bodyFrameLayout = new FrameLayout();
            bodyFrameLayout.Y = topFrameLayout.Bottom;
            bodyFrameLayout.Height = Application.GetRealHeight(1549);
            bodyFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
            this.AddChidren(bodyFrameLayout);
            //场景,功能,自动化的切换控件的容器
            var frameSwitchBack = new FrameLayout();
            frameSwitchBack.Height = Application.GetRealHeight(127);
            bodyFrameLayout.AddChidren(frameSwitchBack);
            //拥有楼层时
            this.btnFloorName = null;
            this.btnFloorIcon = null;
            if (Config.Instance.Home.FloorDics.Count > 0)
            {
                //楼层名
                this.btnFloorName = new NormalViewControl(200, 69, true);
                btnFloorName.X = frameSwitchBack.Width - Application.GetRealWidth(200 + 120);
                btnFloorName.Y = Application.GetRealHeight(17);
                btnFloorName.TextAlignment = TextAlignment.CenterRight;
                btnFloorName.IsBold = true;
                btnFloorName.Text = HdlResidenceLogic.Current.GetFloorNameById(Config.Instance.Home.CurrentFloorId);
                frameSwitchBack.AddChidren(btnFloorName);
                btnFloorName.ButtonClickEvent += (sender, e) =>
                {
                    var floors = new Device.Category.SelectFloor();
                    AddChidren(floors);
                    floors.Init(580, 330, Direction.Right);
                    floors.changeFloor = true;
                    floors.FloorAction += (floorId) =>
                    {
                        btnFloorName.Text = Config.Instance.Home.GetFloorNameById(floorId);
                        HdlRoomLogic.Current.CurrentRoom = HdlRoomLogic.Current.GetLoveRoom();
                        //刷新bodyView
                        this.RefreshBodyView();
                    };
                };
                //楼层图标
                this.btnFloorIcon = new IconViewControl(69);
                btnFloorIcon.X = Application.GetRealWidth(950);
                btnFloorIcon.Y = btnFloorName.Y;
                btnFloorIcon.UnSelectedImagePath = "Item/Drop_Down.png";
                frameSwitchBack.AddChidren(btnFloorIcon);
                btnFloorIcon.ButtonClickEvent += (sender, e) =>
                {
                    btnFloorName.ButtonClickEvent?.Invoke(sender, e);
                };
            }
            //目前不知道郭雪城的这个具体放在哪个的位置
            this.functionSceneAutoBodyView = new FrameLayout();
            functionSceneAutoBodyView.Y = frameSwitchBack.Bottom;
            functionSceneAutoBodyView.Height = Application.GetRealHeight(1423);
            bodyFrameLayout.AddChidren(functionSceneAutoBodyView);
            //场景,功能,自动化的切换控件
            var switchContr = new SceneFunctionSwitchControl();
            switchContr.Width = Application.GetRealWidth(650);
            switchContr.Gravity = Gravity.CenterVertical;
            frameSwitchBack.AddChidren(switchContr);
            var listTitle = new List<string>();
            listTitle.Add(Language.StringByID(R.MyInternationalizationString.uScence));
            listTitle.Add(Language.StringByID(R.MyInternationalizationString.uFunction));
            if (UserCenterResourse.UserInfo.AuthorityNo == 1 || UserCenterResourse.UserInfo.AuthorityNo == 2)
            {
                //成员没有自动化
                listTitle.Add(Language.StringByID(R.MyInternationalizationString.Automation));
            }
            //设置初始值
            switchContr.SetDefultIndex(UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex);
            //选择事件
            switchContr.SelectTabEvent += (selectIndex) =>
            {
                UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex = selectIndex;
                if (selectIndex == 2)
                {
                    Common.Logic.LogicDviceList.Clear();
                    if (Common.Logic.LogicDviceList.Count == 0)
                    {
                        Common.Logic.LogicDviceList.AddRange(LocalDevice.Current.listAllDevice.ToArray());
                    }
                }
                //刷新bodyView
                this.RefreshBodyView();
            };
            //开始初始化
            switchContr.InitControl(listTitle);
        }
        /// <summary>
        /// 刷新bodyView
        /// </summary>
        public void RefreshBodyView()
        {
            this.functionSceneAutoBodyView.RemoveAll();
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                bool floorVisible = true;
                //场景
                if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 0)
                {
                    //初始化房间菜单
                    this.InitRoomMenuControl();
                    //显示右上角的加号
                    this.btnTopRightAdd.Visible = true;
                }
                //功能
                else if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 1)
                {
                    //初始化房间菜单
                    this.InitRoomMenuControl();
                    //隐藏右上角的加号
                    this.btnTopRightAdd.Visible = false;
                }
                //自动化
                else if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 2)
                {
                    this.ShowAutotion();
                    //隐藏楼层
                    floorVisible = false;
                    //显示右上角的加号
                    this.btnTopRightAdd.Visible = true;
                }
                //修改楼层的显示效果
                if (this.btnFloorName != null)
                {
                    this.btnFloorName.Visible = floorVisible;
                    this.btnFloorIcon.Visible = floorVisible;
                }
            });
        }
        #endregion
        #region ■ 房间菜单控件_______________________
        /// <summary>
        /// 初始化房间菜单控件
        /// </summary>
        private void InitRoomMenuControl()
        {
            //当前楼层的全部房间
            var lisrRoom = HdlRoomLogic.Current.GetRoomsByCurrentFloorIdAppendLoveRoom();
            //房间菜单控件
            var roomSwitchContr = new RoomDeviceGroupMenuControl(lisrRoom);
            roomSwitchContr.Y = Application.GetRealHeight(-55);
            this.functionSceneAutoBodyView.AddChidren(roomSwitchContr);
            //设置初始值
            roomSwitchContr.SetDefultIndex(HdlRoomLogic.Current.CurrentRoom.Id);
            //选择事件
            roomSwitchContr.SelectRoomEvent += (selectRoom) =>
            {
                HdlRoomLogic.Current.CurrentRoom = selectRoom;
                //场景
                if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 0)
                {
                    //在外面清空(特效的问题)
                    this.functionSceneBodyView.RemoveAll();
                    HdlThreadLogic.Current.RunMainInThread(() =>
                    {
                        //刷新场景分支控件
                        this.RefreshSceneView(selectRoom);
                    });
                }
                //功能
                else if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 1)
                {
                    //在外面清空(特效的问题)
                    this.functionSceneBodyView.RemoveAll();
                    HdlThreadLogic.Current.RunMainInThread(() =>
                    {
                        //刷新设备分支控件
                        this.RefreshFunctionView(selectRoom);
                    });
                }
            };
            //功能和场景bodyView
            this.functionSceneBodyView = new FrameLayout();
            functionSceneBodyView.Y = roomSwitchContr.Bottom;
            functionSceneBodyView.Height = functionSceneAutoBodyView.Height - roomSwitchContr.Bottom;
            functionSceneAutoBodyView.AddChidren(functionSceneBodyView);
            //执行初始化
            roomSwitchContr.InitControl();
        }
        #endregion
        #region ■ 功能分支___________________________
        /// <summary>
        /// 刷新功能分支控件
        /// </summary>
        /// <param name="room"></param>
        private void RefreshFunctionView(Common.Room room)
        {
            //获取分组后的设备
            var dicGroupDevice = this.GetAllGroupDevice(room);
            if (dicGroupDevice.Count == 0)
            {
                this.ShowNoFunctionTip();
                return;
            }
            //设备菜单的白色背景
            var functionBack1 = new FrameLayout();
            functionBack1.X = ControlCommonResourse.XXLeft;
            functionBack1.Height = Application.GetRealHeight(160);
            functionBack1.Width = Application.GetRealWidth(1028);
            functionBack1.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
            functionSceneBodyView.AddChidren(functionBack1);
            functionBack1.SetCornerWithSameRadius(Application.GetRealHeight(17), HDLUtils.RectCornerTopLeft);
            var functionBack2 = new FrameLayout();
            functionBack2.X = ControlCommonResourse.XXLeft;
            functionBack2.Y = functionBack1.Bottom - Application.GetRealHeight(50);
            functionBack2.Height = Application.GetRealHeight(279 - 160 + 50);
            functionBack2.Width = Application.GetRealWidth(1028);
            functionBack2.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
            functionSceneBodyView.AddChidren(functionBack2);
            functionBack2.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerBottomLeft);
            //设备菜单的左右滑动的控件
            var HorizontalView = new HorizontalScrolViewLayout();
            HorizontalView.X = Application.GetRealWidth(CommonFormResouce.X_Left);
            HorizontalView.Height = Application.GetRealHeight(279);
            HorizontalView.Width = Application.GetRealWidth(1028);
            functionSceneBodyView.AddChidren(HorizontalView);
            //设备的背景容器
            var frameDeviceBack = new FrameLayout();
            frameDeviceBack.X = ControlCommonResourse.XXLeft;
            frameDeviceBack.Y = HorizontalView.Bottom + Application.GetRealHeight(35);
            frameDeviceBack.BackgroundColor = UserCenterColor.Current.White;
            frameDeviceBack.Width = bodyFrameLayout.Width;
            frameDeviceBack.Height = functionSceneBodyView.Height - HorizontalView.Bottom - Application.GetRealHeight(35);
            frameDeviceBack.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerTopLeft);
            functionSceneBodyView.AddChidren(frameDeviceBack);
            //设备列表控件
            var listDeviceView = new VerticalListControl(35);
            listDeviceView.Y = Application.GetRealHeight(11);
            listDeviceView.Width = Application.GetRealWidth(1022);
            listDeviceView.Height = frameDeviceBack.Height - Application.GetRealHeight(11);
            listDeviceView.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerTopLeft);
            frameDeviceBack.AddChidren(listDeviceView);
            //上一次选择的菜单
            MainPage.Controls.DeviceFunctionUnallocatedControl oldSelectContr = null;
            DeviceRowInfo nowSelectDeviceInfo = null;
            foreach (int Textid in dicGroupDevice.Keys)
            {
                var rowInfo = dicGroupDevice[Textid];
                //设备类型的容器
                var devieFrame = new FrameLayout();
                devieFrame.Width = Application.GetRealWidth(220);
                HorizontalView.AddChidren(devieFrame);
                //菜单图片控件
                var deviceObjContr = new MainPage.Controls.DeviceFunctionUnallocatedControl();
                devieFrame.AddChidren(deviceObjContr);
                deviceObjContr.InitControl(Language.StringByID(Textid), rowInfo.IconPath, rowInfo.IconPathSelected, rowInfo.listDeviceKeys);
                deviceObjContr.ButtonClickEvent += (sender, e) =>
                {
                    //选择的是同一个东西的话,不处理
                    if (nowSelectDeviceInfo.TextId != rowInfo.TextId)
                    {
                        //上一次的菜单取消,本次菜单选择
                        oldSelectContr.SetSelectStatu(false);
                        deviceObjContr.SetSelectStatu(true);
                        oldSelectContr = deviceObjContr;
                        nowSelectDeviceInfo = rowInfo;
                        HdlThreadLogic.Current.RunMainInThread(() =>
                        {
                            //初始化设备列表控件
                            this.InitListDeviceControls(listDeviceView, rowInfo);
                        });
                    }
                };
                if (nowSelectDeviceInfo == null)
                {
                    //设置初始选择
                    nowSelectDeviceInfo = rowInfo;
                    //记录初始选择的菜单控件
                    oldSelectContr = deviceObjContr;
                    deviceObjContr.SetSelectStatu(true);
                }
            }
            if (nowSelectDeviceInfo != null)
            {
                //初始化默认的设备列表控件
                this.InitListDeviceControls(listDeviceView, nowSelectDeviceInfo);
            }
        }
        /// <summary>
        /// 提示没有功能
        /// </summary>
        private void ShowNoFunctionTip()
        {
            var noFunction = new Button()
            {
                Y = Application.GetRealHeight(320),
                Width = Application.GetMinRealAverage(757),
                Height = Application.GetMinRealAverage(435),
                UnSelectedImagePath = "Item/NoFunction.png",
                Gravity = Gravity.CenterHorizontal
            };
            functionSceneBodyView.AddChidren(noFunction);
            var noFunctionTip = new Button()
            {
                Y = noFunction.Bottom + Application.GetRealHeight(32),
                Height = Application.GetRealHeight(200),
                Width = Application.GetRealWidth(700),
                Gravity = Gravity.CenterHorizontal,
                Text = Language.StringByID(R.MyInternationalizationString.NoFunction).Replace("{\\r\\n}", "\r\n"),
                TextColor = ZigbeeColor.Current.GXCPlaceHolderTextColor,
                TextAlignment = TextAlignment.Center,
                IsMoreLines = true
            };
            functionSceneBodyView.AddChidren(noFunctionTip);
        }
        #endregion
        #region ■ 初始化设备列表控件_________________
        /// <summary>
        /// 初始化设备列表控件
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="rowInfo"></param>
        private void InitListDeviceControls(VerticalListControl listView, DeviceRowInfo rowInfo)
        {
            //先清空
            listView.RemoveAll();
            var listDevice = new List<CommonDevice>();
            foreach (var mainkeys in rowInfo.listDeviceKeys)
            {
                var device = LocalDevice.Current.GetDevice(mainkeys);
                if (device != null)
                {
                    listDevice.Add(device);
                }
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                var listContr = new List<Controls.DeviceRowCommon>();
                foreach (var device in listDevice)
                {
                    Controls.DeviceRowCommon cardContr = null;
                    //窗帘
                    if (device.Type == DeviceType.WindowCoveringDevice)
                    {
                        cardContr = new Controls.DeviceCurtainRowControl(listView.rowSpace / 2);
                    }
                    //继电器
                    else if (device.Type == DeviceType.OnOffOutput)
                    {
                        cardContr = new Controls.DeviceRelayRowControl(listView.rowSpace / 2);
                    }
                    //空气开关
                    else if (device.Type == DeviceType.AirSwitch)
                    {
                        cardContr = new Controls.DeviceAirSwitchRowControl(listView.rowSpace / 2);
                    }
                    //空调
                    else if (device.Type == DeviceType.Thermostat)
                    {
                        cardContr = new Controls.DeviceAcRowControl(listView.rowSpace / 2);
                    }
                    //彩灯(调光器)
                    else if (device.Type == DeviceType.DimmableLight)
                    {
                        cardContr = new Controls.DeviceColorLightRowControl(listView.rowSpace / 2);
                    }
                    //传感器
                    else if (device.Type == DeviceType.IASZone)
                    {
                        cardContr = new Controls.DeviceSensorRowControl(listView.rowSpace / 2);
                    }
                    //温湿度
                    else if (device.Type == DeviceType.TemperatureSensor)
                    {
                        cardContr = new Controls.DeviceTemperatureRowControl(listView.rowSpace / 2);
                    }
                    //门锁
                    else if (device.Type == DeviceType.DoorLock)
                    {
                        cardContr = new Controls.DeviceDoorLockRowControl(listView.rowSpace / 2);
                    }
                    //无法识别
                    else
                    {
                        cardContr = new Controls.DeviceRowCommon(listView.rowSpace / 2);
                        //没有状态功能
                        cardContr.hadStatuFunction = false;
                    }
                    //初始化卡片
                    listView.AddChidren(cardContr);
                    cardContr.InitControl(device);
                    //控件记录到缓存中
                    this.dicDeviceRowControl[LocalDevice.Current.GetDeviceMainKeys(device)] = cardContr;
                    //加缓存,然后发命令
                    listContr.Add(cardContr);
                }
                HdlThreadLogic.Current.RunThread(() =>
                {
                    for (int i = 0; i < listContr.Count; i++)
                    {
                        if (listView.Parent == null)
                        {
                            return;
                        }
                        System.Threading.Thread.Sleep(20);
                        //发送获取状态的命令
                        listContr[i].SendStatuComand();
                    }
                }, ShowErrorMode.NO);
            });
        }
        #endregion
        #region ■ 场景分支___________________________
        /// <summary>
        /// 刷新场景分支控件
        /// </summary>
        /// <param name="room"></param>
        private void RefreshSceneView(Room room)
        {
            var listScene = new List<SceneUI>();
            foreach (var sceneId in room.ListSceneId)
            {
                var scene = HdlSceneLogic.Current.GetSceneUIBySceneId(sceneId);
                if (scene != null)
                {
                    listScene.Add(scene);
                }
            }
            if (listScene.Count == 0)
            {
                this.ShowNoSceneTip();
                return;
            }
            var dicSceneContr = new Dictionary<int, SceneCategoryView>();
            var listview = new VerticalListControl();
            functionSceneBodyView.AddChidren(listview);
            foreach (var scene in listScene)
            {
                //场景卡片控件
                var sceneView = new SceneCategoryView();
                listview.AddChidren(sceneView);
                sceneView.InitControl(scene, room);
                dicSceneContr[scene.Id] = sceneView;
            }
            HdlThreadLogic.Current.RunThread(() =>
            {
                //获取全部场景的延时时间
                this.GetAllDelayScene(dicSceneContr, listview);
            }, ShowErrorMode.NO);
        }
        /// <summary>
        /// 获取全部场景的延时时间
        /// </summary>
        /// <param name="dicSceneContr"></param>
        private async void GetAllDelayScene(Dictionary<int, SceneCategoryView> dicSceneContr, VerticalListControl listview)
        {
            //读取全部的延时时间
            var result = await Scene.CatDelaySceneAsync();
            if (result == null || result.catDelaySceneResponseData == null ||
                result.catDelaySceneResponseData.DelayScenesList.Count == 0)
            {
                //出错不鸟它
                return;
            }
            string hourText = Language.StringByID(R.MyInternationalizationString.Hour);
            string minuText = Language.StringByID(R.MyInternationalizationString.Minute);
            string secondText = Language.StringByID(R.MyInternationalizationString.Second);
            foreach (var data in result.catDelaySceneResponseData.DelayScenesList)
            {
                if (dicSceneContr.ContainsKey(data.ScenesId) == false)
                {
                    //当前的界面没有显示这个场景
                    continue;
                }
                var seceneContr = dicSceneContr[data.ScenesId];
                //保存缓存时间
                seceneContr.scene.RemainTime = data.RemainTime;
                int remainTime = data.RemainTime;
                if (remainTime <= 0)
                {
                    continue;
                }
                HdlThreadLogic.Current.RunThread(() =>
                {
                    //开启内部延时时间线程(旨在全部地方的同一场景时间同步)
                    HdlSceneLogic.Current.StartDelayTimeThread(seceneContr.scene);
                    while (remainTime > 0 && this.Parent != null)
                    {
                        System.Threading.Thread.Sleep(1000);
                        Application.RunOnMainThread(() =>
                        {
                            seceneContr.SetTimeText(this.GetTimeString(remainTime, hourText, minuText, secondText));
                        });
                        remainTime--;
                    }
                    Application.RunOnMainThread(() =>
                    {
                        if (listview.Parent != null)
                        {
                            seceneContr.SetTimeImage();
                            //开启Loading特效
                            seceneContr.StartLoadingApreal();
                        }
                    });
                });
            }
        }
        /// <summary>
        /// 显示没有场景
        /// </summary>
        private void ShowNoSceneTip()
        {
            var noFunction = new Button()
            {
                Y = Application.GetRealHeight(320),
                Width = Application.GetMinRealAverage(757),
                Height = Application.GetMinRealAverage(435),
                UnSelectedImagePath = "Item/NoFunction.png",
                Gravity = Gravity.CenterHorizontal
            };
            functionSceneBodyView.AddChidren(noFunction);
            var noScenceTip = new Button()
            {
                Y = noFunction.Bottom + Application.GetRealHeight(32),
                Height = Application.GetRealHeight(200),
                Width = Application.GetRealWidth(700),
                Gravity = Gravity.CenterHorizontal,
                Text = Language.StringByID(R.MyInternationalizationString.NoScene).Replace("{\\r\\n}", "\r\n"),
                TextColor = ZigbeeColor.Current.GXCPlaceHolderTextColor,
                TextAlignment = TextAlignment.Center,
                IsMoreLines = true
            };
            functionSceneBodyView.AddChidren(noScenceTip);
        }
        #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.dicDeviceRowControl.ContainsKey(mainKeys) == true)
                    {
                        //刷新卡片信息
                        var locadevice = LocalDevice.Current.GetDevice(mainKeys);
                        this.dicDeviceRowControl[mainKeys].RefreshControlInfo(locadevice);
                    }
                }, ShowErrorMode.NO);
            });
            //设备在线上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceOnline", ReceiveComandDiv.A设备在线上报, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                    if (this.dicDeviceRowControl.ContainsKey(mainKeys) == true)
                    {
                        //刷新卡片状态
                        this.dicDeviceRowControl[mainKeys].SetRowOnlineStatu(report.IsOnline == 1);
                        return;
                    }
                }, ShowErrorMode.NO);
            });
            //设备控制反馈上报
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceRespone", ReceiveComandDiv.A节点控制反馈, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                    if (this.dicDeviceRowControl.ContainsKey(mainKeys) == true)
                    {
                        //设备卡片
                        var deviceCardContr = this.dicDeviceRowControl[mainKeys];
                        //已经接收到网关的反馈
                        deviceCardContr.SetHadGetResponeResultStatu();
                    }
                }, 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.dicDeviceRowControl.ContainsKey(mainKeys) == true)
                    {
                        //本地设备对象
                        var locadevice = LocalDevice.Current.GetDevice(mainKeys);
                        //刷新卡片信息
                        this.dicDeviceRowControl[mainKeys].RefreshControlInfo(locadevice);
                    }
                }, ShowErrorMode.NO);
            });
        }
        #endregion
        #region ■ 整合设备___________________________
        /// <summary>
        /// 获取分组后的设备
        /// </summary>
        /// <returns></returns>
        private Dictionary<int, DeviceRowInfo> GetAllGroupDevice(Common.Room room)
        {
            //全部的设备
            var listDevice = HdlRoomLogic.Current.GetRoomListDevice(room);
            //根据设备所属类型排序
            listDevice = LocalDevice.Current.SortDeviceByBelongType(listDevice);
            var dic = new Dictionary<int, DeviceRowInfo>();
            foreach (var device in listDevice)
            {
                var typeInfo = LocalDevice.Current.GetDeviceBelongEnumInfo(device);
                //按所属ID分组
                if (dic.ContainsKey(typeInfo.BeloneTextId) == false)
                {
                    dic[typeInfo.BeloneTextId] = new DeviceRowInfo();
                    string path1 = string.Empty;
                    string path2 = string.Empty;
                    //获取图片
                    LocalDevice.Current.GetDeviceObjectIcon(typeInfo.ConcreteType, ref path1, ref path2);
                    dic[typeInfo.BeloneTextId].IconPath = path1;
                    dic[typeInfo.BeloneTextId].IconPathSelected = path2;
                    dic[typeInfo.BeloneTextId].TextId = typeInfo.BeloneTextId;
                }
                dic[typeInfo.BeloneTextId].listDeviceKeys.Add(LocalDevice.Current.GetDeviceMainKeys(device));
            }
            return dic;
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            //把静态变量的这个东西置空
            instance = null;
            base.CloseFormBefore();
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 获取时间的翻译文本
        /// </summary>
        /// <param name="second"></param>
        /// <param name="hourText">小时的文本</param>
        /// <param name="minuText">分的文本</param>
        /// <param name="secondText">秒的文本</param>
        /// <returns></returns>
        private string GetTimeString(int second, string hourText, string minuText, string secondText)
        {
            string timeStr = string.Empty;
            int hour = second / 3600;
            int minu = second % 3600 / 60;
            int sec = second % 60;
            if (hour > 0)
            {
                timeStr += hour + hourText;
            }
            if (minu > 0)
            {
                timeStr += minu + minuText;
            }
            if (sec > 0)
            {
                timeStr += sec + secondText;
            }
            return timeStr;
        }
        /// <summary>
        /// 添加场景(郭雪城的代码)
        /// </summary>
        private void Add_Scene()
        {
            var scene = new Device.Category.CategoryAddScene();
            UserView.HomePage.Instance.AddChidren(scene);
            UserView.HomePage.Instance.PageIndex += 1;
            scene.Show();
            scene.AddAction = () =>
            {
                //刷新bodyView
                this.RefreshBodyView();
            };
        }
        /// <summary>
        /// 添加自动化(郭雪城的代码)
        /// </summary>
        private void Add_Automation()
        {
            Shared.Phone.Device.Logic.SkipView.SkipAddLogic(0);
            ////new一个新逻辑对象;
            //Common.Logic.CurrentLogic = new Common.Logic();
            //Common.Logic.CurrentLogic.IsEnable = 1;//默认为开
            //Common.Logic.CurrentLogic.LogicName = Language.StringByID(MyInternationalizationString.automation1);
            //var addLogicPage = new Shared.Phone.Device.Logic.AddLogicPage();
            //UserView.HomePage.Instance.AddChidren(addLogicPage);
            //UserView.HomePage.Instance.PageIndex += 1;
            //addLogicPage.Show();
        }
        #endregion
        #region ■ 结构体_____________________________
        /// <summary>
        /// 设备行信息
        /// </summary>
        private class DeviceRowInfo
        {
            /// <summary>
            /// 文本ID,目前用来做主键
            /// </summary>
            public int TextId = 0;
            /// <summary>
            /// 图标
            /// </summary>
            public string IconPath = string.Empty;
            /// <summary>
            /// 图标
            /// </summary>
            public string IconPathSelected = string.Empty;
            /// <summary>
            /// 设备回路主键
            /// </summary>
            public List<string> listDeviceKeys = new List<string>();
        }
        #endregion
        #region ◆ 自动化__________________________
        /// <summary>
        /// 自动化功能代码入口
        /// </summary>
        private void ShowAutotion()
        {
            Shared.Phone.Device.Logic.SkipView.ShowAutotionView(functionSceneAutoBodyView);
        }
            #endregion
        }
}
using System;
using System.Collections.Generic;
using Shared.Common;
using Shared.Phone.UserView;
using Shared.R;
using Shared.Phone.Device.CommonForm;
using Shared.Phone.UserCenter;
using ZigBee.Device;
namespace Shared.Phone.Category
{
    /// <summary>
    /// 分类的主界面
    /// </summary>
    public class CategoryMainForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// The instance.
        /// </summary>
        public static CategoryMainForm instance;
        /// <summary>
        /// 场景功能中部背景bodyView(高度为设备菜单的那个白色背景的上部到屏幕底部)
        /// </summary>
        public FrameLayout functionSceneBodyView;
        /// <summary>
        /// 中部背景bodyView(高度为场景功能切换控件的底部到屏幕底部)
        /// </summary>
        public FrameLayout functionSceneAutoBodyView;
        /// <summary>
        /// 右上角添加按钮
        /// </summary>
        private IconViewControl btnTopRightAdd = null;
        /// <summary>
        /// 楼层控件
        /// </summary>
        private NormalViewControl btnFloorName = null;
        /// <summary>
        /// 楼层图标控件
        /// </summary>
        private IconViewControl btnFloorIcon = null;
        /// <summary>
        /// 设备行控件
        /// </summary>
        private Dictionary<string, Controls.DeviceRowCommon> dicDeviceRowControl = new Dictionary<string, Controls.DeviceRowCommon>();
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 构造方法
        /// </summary>
        public CategoryMainForm()
        {
            this.FormID = "CategoryMainForm";
            BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
            instance = this;
        }
        /// <summary>
        /// 显示界面--默认场景界面
        /// </summary>
        public void ShowForm()
        {
            //先清除吧
            this.RemoveAll();
            //初始化头部控件
            this.InitTopControl();
            //初始化中间控件
            this.InitMidControls();
            //添加设备状态上报事件
            this.AddNormalDeviceReportEvent();
            //添加传感器状态上报事件
            this.AddSensorDeviceReportEvent();
        }
        /// <summary>
        /// 初始化头部控件
        /// </summary>
        private void InitTopControl()
        {
            //头部FrameLayout
            this.topFrameLayout = new FrameLayout();
            topFrameLayout.Y = Application.GetRealHeight(104);
            topFrameLayout.Height = Application.GetRealHeight(127);
            topFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
            this.AddChidren(topFrameLayout);
            //标题分类
            var btnTitle = new NormalViewControl(300, 95, true);
            btnTitle.X = ControlCommonResourse.XXLeft;
            btnTitle.Gravity = Gravity.CenterVertical;
            btnTitle.TextID = R.MyInternationalizationString.Category;
            btnTitle.TextSize = 24;
            btnTitle.TextColor = ZigbeeColor.Current.GXCTextDeepBlackColor;
            btnTitle.IsBold = true;
            topFrameLayout.AddChidren(btnTitle);
            //右上角添加按钮
            this.btnTopRightAdd = new IconViewControl(104);
            btnTopRightAdd.X = Application.GetRealWidth(953);
            btnTopRightAdd.Gravity = Gravity.CenterVertical;
            btnTopRightAdd.UnSelectedImagePath = "Item/Add_Category.png";
            topFrameLayout.AddChidren(btnTopRightAdd);
            btnTopRightAdd.ButtonClickEvent += (sender, e) =>
            {
                if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 0)
                {
                    //添加场景
                    var form = new AddOrEditorSceneForm();
                    form.AddForm(new object[] { null });
                    form.SceneChangedEvent += (scene, roomId) =>
                    {
                        //刷新bodyView
                        this.RefreshBodyView();
                    };
                }
                else if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 2)
                {
                    //添加自动化
                    Device.Logic.SkipView.SkipAddLogic(0);
                }
            };
        }
        /// <summary>
        /// 初始化中间控件
        /// </summary>
        private void InitMidControls()
        {
            //应该是body吧
            this.bodyFrameLayout = new FrameLayout();
            bodyFrameLayout.Y = topFrameLayout.Bottom;
            bodyFrameLayout.Height = this.Height - topFrameLayout.Bottom;
            bodyFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
            this.AddChidren(bodyFrameLayout);
            //场景,功能,自动化的切换控件的容器
            var frameSwitchBack = new FrameLayout();
            frameSwitchBack.Height = Application.GetRealHeight(127);
            bodyFrameLayout.AddChidren(frameSwitchBack);
            //拥有楼层时
            this.btnFloorName = null;
            this.btnFloorIcon = null;
            if (Config.Instance.Home.FloorDics.Count > 0)
            {
                //楼层名
                this.btnFloorName = new NormalViewControl(200, 69, true);
                btnFloorName.X = frameSwitchBack.Width - Application.GetRealWidth(200 + 120);
                btnFloorName.Y = Application.GetRealHeight(17);
                btnFloorName.TextAlignment = TextAlignment.CenterRight;
                btnFloorName.IsBold = true;
                btnFloorName.Text = HdlResidenceLogic.Current.GetFloorNameById(Config.Instance.Home.CurrentFloorId);
                frameSwitchBack.AddChidren(btnFloorName);
                btnFloorName.ButtonClickEvent += (sender, e) =>
                {
                    var floors = new SelectFloorForm();
                    AddChidren(floors);
                    floors.Init(580, 330, Direction.Right);
                    floors.changeFloor = true;
                    floors.FloorAction += (floorId) =>
                    {
                        btnFloorName.Text = HdlResidenceLogic.Current.GetFloorNameById(floorId);
                        HdlRoomLogic.Current.NowCategoryRoom = HdlRoomLogic.Current.GetLoveRoom();
                        //刷新bodyView
                        this.RefreshBodyView();
                    };
                };
                //楼层图标
                this.btnFloorIcon = new IconViewControl(69);
                btnFloorIcon.X = Application.GetRealWidth(950);
                btnFloorIcon.Y = Application.GetRealHeight(17);
                btnFloorIcon.UnSelectedImagePath = "Item/Drop_Down.png";
                frameSwitchBack.AddChidren(btnFloorIcon);
                btnFloorIcon.ButtonClickEvent += (sender, e) =>
                {
                    btnFloorName.ButtonClickEvent?.Invoke(sender, e);
                };
            }
            //目前不知道郭雪城的这个具体放在哪个的位置
            this.functionSceneAutoBodyView = new FrameLayout();
            functionSceneAutoBodyView.Y = frameSwitchBack.Bottom;
            functionSceneAutoBodyView.Height = bodyFrameLayout.Height - frameSwitchBack.Bottom;
            bodyFrameLayout.AddChidren(functionSceneAutoBodyView);
            //场景,功能,自动化的切换控件
            var switchContr = new SceneFunctionSwitchControl();
            switchContr.Width = Application.GetRealWidth(650);
            switchContr.Gravity = Gravity.CenterVertical;
            frameSwitchBack.AddChidren(switchContr);
            var listTitle = new List<string>();
            listTitle.Add(Language.StringByID(R.MyInternationalizationString.uScence));
            listTitle.Add(Language.StringByID(R.MyInternationalizationString.uFunction));
            if (UserCenterResourse.UserInfo.AuthorityNo == 1 || UserCenterResourse.UserInfo.AuthorityNo == 2)
            {
                //成员没有自动化
                listTitle.Add(Language.StringByID(R.MyInternationalizationString.Automation));
            }
            //设置初始值
            switchContr.SetDefultIndex(UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex);
            //选择事件
            switchContr.SelectTabEvent += (selectIndex) =>
            {
                UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex = selectIndex;
                if (selectIndex == 2)
                {
                    Common.Logic.LogicDviceList.Clear();
                    if (Common.Logic.LogicDviceList.Count == 0)
                    {
                        Common.Logic.LogicDviceList.AddRange(LocalDevice.Current.listAllDevice.ToArray());
                    }
                }
                //刷新bodyView
                this.RefreshBodyView();
            };
            //开始初始化
            switchContr.InitControl(listTitle);
        }
        /// <summary>
        /// 刷新bodyView
        /// </summary>
        public void RefreshBodyView()
        {
            this.functionSceneAutoBodyView.RemoveAll();
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                bool floorVisible = true;
                //场景
                if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 0)
                {
                    //初始化房间菜单
                    this.InitRoomMenuControl();
                    //显示右上角的加号
                    this.btnTopRightAdd.Visible = true;
                }
                //功能
                else if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 1)
                {
                    //初始化房间菜单
                    this.InitRoomMenuControl();
                    //隐藏右上角的加号
                    this.btnTopRightAdd.Visible = false;
                }
                //自动化
                else if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 2)
                {
                    //自动化功能代码入口
                    Device.Logic.SkipView.ShowAutotionView(functionSceneAutoBodyView);
                    //隐藏楼层
                    floorVisible = false;
                    //显示右上角的加号
                    this.btnTopRightAdd.Visible = true;
                }
                //修改楼层的显示效果
                if (this.btnFloorName != null)
                {
                    this.btnFloorName.Visible = floorVisible;
                    this.btnFloorIcon.Visible = floorVisible;
                }
            });
        }
        #endregion
        #region ■ 房间菜单控件_______________________
        /// <summary>
        /// 初始化房间菜单控件
        /// </summary>
        private void InitRoomMenuControl()
        {
            //当前楼层的全部房间
            var lisrRoom = HdlRoomLogic.Current.GetRoomsByCurrentFloorIdAppendLoveRoom();
            if (HdlRoomLogic.Current.NowCategoryRoom == null)
            {
                //设置第一个为初始房间
                HdlRoomLogic.Current.NowCategoryRoom = lisrRoom[0];
            }
            //房间菜单控件
            var roomSwitchContr = new RoomDeviceGroupMenuControl(lisrRoom);
            roomSwitchContr.Y = Application.GetRealHeight(-55);
            this.functionSceneAutoBodyView.AddChidren(roomSwitchContr);
            //设置初始值
            roomSwitchContr.SetDefultIndex(HdlRoomLogic.Current.NowCategoryRoom.Id);
            //选择事件
            roomSwitchContr.SelectRoomEvent += (selectRoom) =>
            {
                HdlRoomLogic.Current.NowCategoryRoom = selectRoom;
                //场景
                if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 0)
                {
                    //在外面清空(特效的问题)
                    this.functionSceneBodyView.RemoveAll();
                    HdlThreadLogic.Current.RunMainInThread(() =>
                    {
                        //刷新场景分支控件
                        this.RefreshSceneView(selectRoom);
                    });
                }
                //功能
                else if (UserCenterResourse.ResidenceOption.CategoryPageSwitchIndex == 1)
                {
                    //在外面清空(特效的问题)
                    this.functionSceneBodyView.RemoveAll();
                    HdlThreadLogic.Current.RunMainInThread(() =>
                    {
                        //刷新设备分支控件
                        this.RefreshFunctionView(selectRoom);
                    });
                }
            };
            //功能和场景bodyView
            this.functionSceneBodyView = new FrameLayout();
            functionSceneBodyView.Y = roomSwitchContr.Bottom;
            functionSceneBodyView.Height = functionSceneAutoBodyView.Height - roomSwitchContr.Bottom;
            functionSceneAutoBodyView.AddChidren(functionSceneBodyView);
            //执行初始化
            roomSwitchContr.InitControl();
        }
        #endregion
        #region ■ 功能分支___________________________
        /// <summary>
        /// 刷新功能分支控件
        /// </summary>
        /// <param name="room"></param>
        private void RefreshFunctionView(Common.Room room)
        {
            //获取分组后的设备
            var dicGroupDevice = this.GetAllGroupDevice(room);
            if (dicGroupDevice.Count == 0)
            {
                //没有功能{0}请在个人中心--设备管理处添加
                string[] arryMsg = Language.StringByID(R.MyInternationalizationString.NoFunction).Split(new string[] { "{0}" }, StringSplitOptions.RemoveEmptyEntries);
                this.ShowNotDataImage(functionSceneBodyView, arryMsg);
                return;
            }
            //设备菜单的白色背景
            var functionBack1 = new FrameLayout();
            functionBack1.X = ControlCommonResourse.XXLeft;
            functionBack1.Height = Application.GetRealHeight(160);
            functionBack1.Width = Application.GetRealWidth(1028);
            functionBack1.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
            functionSceneBodyView.AddChidren(functionBack1);
            functionBack1.SetCornerWithSameRadius(Application.GetRealHeight(17), HDLUtils.RectCornerTopLeft);
            var functionBack2 = new FrameLayout();
            functionBack2.X = ControlCommonResourse.XXLeft;
            functionBack2.Y = functionBack1.Bottom - Application.GetRealHeight(50);
            functionBack2.Height = Application.GetRealHeight(279 - 160 + 50);
            functionBack2.Width = Application.GetRealWidth(1028);
            functionBack2.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
            functionSceneBodyView.AddChidren(functionBack2);
            functionBack2.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerBottomLeft);
            //设备菜单的左右滑动的控件
            var HorizontalView = new HorizontalScrolViewLayout();
            HorizontalView.X = Application.GetRealWidth(CommonFormResouce.X_Left);
            HorizontalView.Height = Application.GetRealHeight(279);
            HorizontalView.Width = Application.GetRealWidth(1028);
            functionSceneBodyView.AddChidren(HorizontalView);
            //设备的背景容器
            var frameDeviceBack = new FrameLayout();
            frameDeviceBack.X = ControlCommonResourse.XXLeft;
            frameDeviceBack.Y = HorizontalView.Bottom + Application.GetRealHeight(35);
            frameDeviceBack.BackgroundColor = UserCenterColor.Current.White;
            frameDeviceBack.Width = bodyFrameLayout.Width;
            frameDeviceBack.Height = functionSceneBodyView.Height - HorizontalView.Bottom - Application.GetRealHeight(35);
            frameDeviceBack.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerTopLeft);
            functionSceneBodyView.AddChidren(frameDeviceBack);
            //设备列表控件
            var listDeviceView = new VerticalListControl(35);
            listDeviceView.Y = Application.GetRealHeight(11);
            listDeviceView.Width = Application.GetRealWidth(1022);
            listDeviceView.Height = frameDeviceBack.Height - Application.GetRealHeight(11);
            listDeviceView.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerTopLeft);
            frameDeviceBack.AddChidren(listDeviceView);
            //上一次选择的菜单
            MainPage.Controls.DeviceFunctionUnallocatedControl oldSelectContr = null;
            DeviceRowInfo nowSelectDeviceInfo = null;
            foreach (int Textid in dicGroupDevice.Keys)
            {
                var rowInfo = dicGroupDevice[Textid];
                //设备类型的容器
                var devieFrame = new FrameLayout();
                devieFrame.Width = Application.GetRealWidth(220);
                HorizontalView.AddChidren(devieFrame);
                //菜单图片控件
                var deviceObjContr = new MainPage.Controls.DeviceFunctionUnallocatedControl();
                devieFrame.AddChidren(deviceObjContr);
                deviceObjContr.InitControl(Language.StringByID(Textid), rowInfo.IconPath, rowInfo.IconPathSelected, rowInfo.listDeviceKeys);
                deviceObjContr.ButtonClickEvent += (sender, e) =>
                {
                    //选择的是同一个东西的话,不处理
                    if (nowSelectDeviceInfo.TextId != rowInfo.TextId)
                    {
                        //上一次的菜单取消,本次菜单选择
                        oldSelectContr.SetSelectStatu(false);
                        deviceObjContr.SetSelectStatu(true);
                        oldSelectContr = deviceObjContr;
                        nowSelectDeviceInfo = rowInfo;
                        HdlThreadLogic.Current.RunMainInThread(() =>
                        {
                            //初始化设备列表控件
                            this.InitListDeviceControls(listDeviceView, rowInfo, room);
                        });
                    }
                };
                if (nowSelectDeviceInfo == null)
                {
                    //设置初始选择
                    nowSelectDeviceInfo = rowInfo;
                    //记录初始选择的菜单控件
                    oldSelectContr = deviceObjContr;
                    deviceObjContr.SetSelectStatu(true);
                }
            }
            if (nowSelectDeviceInfo != null)
            {
                //初始化默认的设备列表控件
                this.InitListDeviceControls(listDeviceView, nowSelectDeviceInfo, room);
            }
        }
        #endregion
        #region ■ 初始化设备列表控件_________________
        /// <summary>
        /// 初始化设备列表控件
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="rowInfo"></param>
        private void InitListDeviceControls(VerticalListControl listView, DeviceRowInfo rowInfo, Room i_room)
        {
            //先清空
            listView.RemoveAll();
            var listDevice = new List<CommonDevice>();
            for (int i = 0; i < rowInfo.listDeviceKeys.Count; i++)
            {
                var device = LocalDevice.Current.GetDevice(rowInfo.listDeviceKeys[i]);
                //获取设备类型的
                var deviceEnumInfo = Common.LocalDevice.Current.GetMyDeviceEnumInfo(new List<CommonDevice>() { device });
                //新风面板下的回路不显示,(如新风、温/湿度传感器则不显示)
                if (deviceEnumInfo.ConcreteType == Common.DeviceConcreteType.ButtonPanel_FangyueFreshAir)
                {
                    continue;
                }
                if (device != null)
                {
                    listDevice.Add(device);
                }
                else
                {
                    rowInfo.listDeviceKeys.RemoveAt(i);
                    i--;
                }
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                var listContr = new List<Controls.DeviceRowCommon>();
                foreach (var device in listDevice)
                {
                    Controls.DeviceRowCommon cardContr = null;
                    //窗帘
                    if (device.Type == DeviceType.WindowCoveringDevice)
                    {
                        cardContr = new Controls.DeviceCurtainRowControl();
                    }
                    //继电器
                    else if (device.Type == DeviceType.OnOffOutput)
                    {
                        cardContr = new Controls.DeviceRelayRowControl();
                    }
                    //空气开关
                    else if (device.Type == DeviceType.AirSwitch)
                    {
                        cardContr = new Controls.DeviceAirSwitchRowControl();
                    }
                    //空调
                    else if (device.Type == DeviceType.Thermostat)
                    {
                        cardContr = new Controls.DeviceAcRowControl();
                    }
                    // 新风
                    else if (device.Type == DeviceType.FreshAir)
                    {
                        cardContr = new Controls.DeviceFreshAirRowControl();
                    }
                    // PM2.5
                    else if (device.Type == DeviceType.PMSensor)
                    {
                        cardContr = new Controls.DevicePmSensorRowControl();
                    }
                    //彩灯(调光器)
                    else if (device.Type == DeviceType.DimmableLight)
                    {
                        cardContr = new Controls.DeviceColorLightRowControl();
                    }
                    //传感器
                    else if (device.Type == DeviceType.IASZone)
                    {
                        cardContr = new Controls.DeviceSensorRowControl();
                    }
                    //温湿度
                    else if (device.Type == DeviceType.TemperatureSensor)
                    {
                        cardContr = new Controls.DeviceTemperatureRowControl();
                    }
                    //门锁
                    else if (device.Type == DeviceType.DoorLock)
                    {
                        cardContr = new Controls.DeviceDoorLockRowControl();
                    }
                    //色温灯
                    else if (device.Type == DeviceType.ColorTemperatureLight)
                    {
                        cardContr = new Controls.DeviceColorTemperatureRowControl();
                    }
                    //无法识别
                    else
                    {
                        cardContr = new Controls.DeviceRowCommon();
                        //没有状态功能
                        cardContr.hadStatuFunction = false;
                    }
                    cardContr.chidrenYaxis = listView.rowSpace / 2;
                    //初始化卡片
                    listView.AddChidren(cardContr);
                    cardContr.InitControl(device, HdlRoomLogic.Current.NowCategoryRoom);
                    //控件记录到缓存中
                    this.dicDeviceRowControl[LocalDevice.Current.GetDeviceMainKeys(device)] = cardContr;
                    //加缓存,然后发命令
                    listContr.Add(cardContr);
                    //控件被移除的回调函数
                    cardContr.RowNeedRemoveEvent += () =>
                    {
                        string myKeys = LocalDevice.Current.GetDeviceMainKeys(cardContr.device);
                        rowInfo.listDeviceKeys.Remove(myKeys);
                        this.dicDeviceRowControl.Remove(myKeys);
                        if (rowInfo.listDeviceKeys.Count == 0)
                        {
                            //在外面清空(特效的问题)
                            this.functionSceneBodyView.RemoveAll();
                            HdlThreadLogic.Current.RunMainInThread(() =>
                            {
                                //刷新设备分支控件
                                this.RefreshFunctionView(i_room);
                            });
                        }
                    };
                }
                HdlThreadLogic.Current.RunThread(() =>
                {
                    for (int i = 0; i < listContr.Count; i++)
                    {
                        if (listView.Parent == null)
                        {
                            return;
                        }
                        System.Threading.Thread.Sleep(200);
                        //发送获取状态的命令
                        listContr[i].SendStatuComand();
                    }
                }, ShowErrorMode.NO);
            });
        }
        #endregion
        #region ■ 场景分支___________________________
        /// <summary>
        /// 刷新场景分支控件
        /// </summary>
        /// <param name="room"></param>
        private void RefreshSceneView(Room room)
        {
            //主人,管理员专用刷新控件
            VerticalListRefreshControl listview1 = null;
            //成员专用不能刷新的控件
            VerticalListControl listview2 = null;
            if (UserCenterResourse.UserInfo.AuthorityNo == 1 || UserCenterResourse.UserInfo.AuthorityNo == 2)
            {
                //主人,管理员专用刷新控件
                listview1 = new VerticalListRefreshControl();
                functionSceneBodyView.AddChidren(listview1);
                //下拉刷新
                listview1.BeginHeaderRefreshingAction += () =>
                {
                    //如果当前是虚拟住宅,则只给个特效
                    if (Common.Config.Instance.Home.IsVirtually == true)
                    {
                        HdlThreadLogic.Current.RunThread(() =>
                        {
                            System.Threading.Thread.Sleep(1500);
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                listview1.EndHeaderRefreshing();
                            });
                        });
                        return;
                    }
                    HdlThreadLogic.Current.RunThread(() =>
                    {
                        //从网关刷新场景列表
                        bool result = HdlSceneLogic.Current.RefreshSceneUIList();
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            listview1.EndHeaderRefreshing();
                            if (result == true)
                            {
                                //在外面清空(特效的问题)
                                this.functionSceneBodyView.RemoveAll();
                                HdlThreadLogic.Current.RunMainInThread(() =>
                                {
                                    //刷新场景分支控件
                                    this.RefreshSceneView(room);
                                });
                            }
                        });
                    });
                };
            }
            else
            {
                //成员专用不能刷新的控件
                listview2 = new VerticalListControl();
                functionSceneBodyView.AddChidren(listview2);
            }
            var listScene = new List<SceneUI>();
            foreach (var sceneId in room.ListSceneId)
            {
                var scene = HdlSceneLogic.Current.GetSceneUIBySceneId(sceneId);
                if (scene != null)
                {
                    listScene.Add(scene);
                }
            }
            if (listScene.Count == 0)
            {
                //在滑动控件里面添加一个桌布
                var frameTable = new FrameLayout();
                listview1?.AddChidren(frameTable);
                listview2?.AddChidren(frameTable);
                //没有场景{0}请点击右上角添加
                string[] arryMsg = Language.StringByID(R.MyInternationalizationString.NoScene).Split(new string[] { "{0}" }, StringSplitOptions.RemoveEmptyEntries);
                this.ShowNotDataImage(frameTable, arryMsg);
                return;
            }
            var dicSceneContr = new Dictionary<int, SceneCategoryView>();
            foreach (var scene in listScene)
            {
                //场景卡片控件
                var sceneView = new SceneCategoryView();
                listview1?.AddChidren(sceneView);
                listview2?.AddChidren(sceneView);
                sceneView.InitControl(scene, room);
                dicSceneContr[scene.Id] = sceneView;
            }
            HdlThreadLogic.Current.RunThread(() =>
            {
                if (listview1 != null)
                {
                    //打开全部场景的延时时间
                    this.StartAllDelayScene(dicSceneContr, listview1);
                }
                else
                {
                    //打开全部场景的延时时间
                    this.StartAllDelayScene(dicSceneContr, listview2);
                }
            }, ShowErrorMode.NO);
        }
        /// <summary>
        /// 打开全部场景的延时时间
        /// </summary>
        /// <param name="dicSceneContr"></param>
        private async void StartAllDelayScene(Dictionary<int, SceneCategoryView> dicSceneContr, ViewGroup listview)
        {
            string hourText = Language.StringByID(R.MyInternationalizationString.Hour);
            string minuText = Language.StringByID(R.MyInternationalizationString.Minute);
            string secondText = Language.StringByID(R.MyInternationalizationString.Second);
            //如果当前住宅是虚拟住宅
            if (Config.Instance.Home.IsVirtually == true)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //开启延时特效
                    foreach (var myContr in dicSceneContr.Values)
                    {
                        if (myContr.scene.RemainTime > 0)
                        {
                            //开启场景延时特效
                            this.StartSceneDelayApeal(myContr, listview, myContr.scene.RemainTime, hourText, minuText, secondText);
                        }
                    }
                }, ShowErrorMode.NO);
                return;
            }
            //读取全部的延时时间
            var result = await Scene.CatDelaySceneAsync();
            if (result == null || result.catDelaySceneResponseData == null ||
                result.catDelaySceneResponseData.DelayScenesList.Count == 0)
            {
                //出错不鸟它
                return;
            }
            foreach (var data in result.catDelaySceneResponseData.DelayScenesList)
            {
                if (dicSceneContr.ContainsKey(data.ScenesId) == false)
                {
                    //当前的界面没有显示这个场景
                    continue;
                }
                var seceneContr = dicSceneContr[data.ScenesId];
                //保存缓存时间
                seceneContr.scene.RemainTime = data.RemainTime;
                int remainTime = data.RemainTime;
                if (remainTime <= 0)
                {
                    continue;
                }
                //开启场景延时特效
                this.StartSceneDelayApeal(seceneContr, listview, remainTime, hourText, minuText, secondText);
            }
        }
        /// <summary>
        /// 开启场景延时特效
        /// </summary>
        /// <param name="sceneContr"></param>
        /// <param name="remainTime"></param>
        /// <param name="hourText"></param>
        /// <param name="minuText"></param>
        /// <param name="secondText"></param>
        private void StartSceneDelayApeal(SceneCategoryView sceneContr, ViewGroup listview, int remainTime,
            string hourText, string minuText, string secondText)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                //开启内部延时时间线程(旨在全部地方的同一场景时间同步)
                HdlSceneLogic.Current.StartDelayTimeThread(sceneContr.scene);
                while (remainTime > 0 && this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    Application.RunOnMainThread(() =>
                    {
                        sceneContr.SetTimeText(this.GetTimeString(remainTime, hourText, minuText, secondText));
                    });
                    remainTime--;
                }
                Application.RunOnMainThread(() =>
                {
                    if (listview.Parent != null)
                    {
                        sceneContr.SetTimeImage();
                        //开启Loading特效
                        sceneContr.StartLoadingApreal();
                    }
                });
            });
        }
        #endregion
        #region ■ 一般设备状态上报___________________
        /// <summary>
        /// 添加一般设备状态上报事件
        /// </summary>
        private void AddNormalDeviceReportEvent()
        {
            //设备属性上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("CategoryMainFormDeviceStatus", ReceiveComandDiv.A设备属性上报, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //处理一般设备的上报数据
                    string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                    if (this.dicDeviceRowControl.ContainsKey(mainKeys) == true)
                    {
                        //刷新卡片信息
                        var locadevice = LocalDevice.Current.GetDevice(mainKeys);
                        //已经接收到网关的反馈 2020.05.09:删除Ack主题
                        this.dicDeviceRowControl[mainKeys].SetHadGetResponeResultStatu();
                        //刷新控件
                        this.dicDeviceRowControl[mainKeys].RefreshControlInfo(locadevice);
                    }
                }, ShowErrorMode.NO);
            });
            //设备在线上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("CategoryMainFormDeviceOnline", ReceiveComandDiv.A设备在线上报, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                    if (this.dicDeviceRowControl.ContainsKey(mainKeys) == true)
                    {
                        //刷新卡片状态
                        bool bolOnline = LocalDevice.Current.CheckDeviceIsOnline(report);
                        this.dicDeviceRowControl[mainKeys].SetRowOnlineStatu(bolOnline);
                        return;
                    }
                }, ShowErrorMode.NO);
            });
        }
        #endregion
        #region ■ 传感器状态上报_____________________
        /// <summary>
        /// 添加传感器状态上报事件
        /// </summary>
        private void AddSensorDeviceReportEvent()
        {
            //传感器上报(缓存的修改交由 HdlGatewayReceiveLogic 处理)
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("CategoryMainFormSensor", ReceiveComandDiv.A传感器上报, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //处理传感器上报数据
                    string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
                    if (this.dicDeviceRowControl.ContainsKey(mainKeys) == true)
                    {
                        //本地设备对象
                        var locadevice = LocalDevice.Current.GetDevice(mainKeys);
                        //刷新卡片信息
                        this.dicDeviceRowControl[mainKeys].RefreshControlInfo(locadevice);
                    }
                }, ShowErrorMode.NO);
            });
        }
        #endregion
        #region ■ 整合设备___________________________
        /// <summary>
        /// 获取分组后的设备
        /// </summary>
        /// <returns></returns>
        private Dictionary<int, DeviceRowInfo> GetAllGroupDevice(Common.Room room)
        {
            //全部的设备
            var listDeviceTemp = HdlRoomLogic.Current.GetRoomListDevice(room);
            var listDevice = new List<CommonDevice>();
            foreach (var device in listDeviceTemp)
            {
                //判断该设备能否显示在分类
                if (LocalDevice.Current.CanShowInHomeHomeMainPage(device) == false)
                {
                    continue;
                }
                listDevice.Add(device);
            }
            //根据设备所属类型排序
            listDevice = LocalDevice.Current.SortDeviceByBelongType(listDevice);
            var dic = new Dictionary<int, DeviceRowInfo>();
            foreach (var device in listDevice)
            {
                var typeInfo = LocalDevice.Current.GetDeviceBelongEnumInfo(device);
                //按所属ID分组
                if (dic.ContainsKey(typeInfo.BeloneTextId) == false)
                {
                    dic[typeInfo.BeloneTextId] = new DeviceRowInfo();
                    string path1 = string.Empty;
                    string path2 = string.Empty;
                    //获取图片
                    LocalDevice.Current.GetDeviceFunctionTypeMenuIcon(typeInfo.ConcreteType, ref path1, ref path2);
                    dic[typeInfo.BeloneTextId].IconPath = path1;
                    dic[typeInfo.BeloneTextId].IconPathSelected = path2;
                    dic[typeInfo.BeloneTextId].TextId = typeInfo.BeloneTextId;
                }
                dic[typeInfo.BeloneTextId].listDeviceKeys.Add(LocalDevice.Current.GetDeviceMainKeys(device));
            }
            return dic;
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            //把静态变量的这个东西置空
            instance = null;
            HdlGatewayReceiveLogic.Current.RemoveEvent("CategoryMainFormDeviceStatus");
            HdlGatewayReceiveLogic.Current.RemoveEvent("CategoryMainFormDeviceOnline");
            HdlGatewayReceiveLogic.Current.RemoveEvent("CategoryMainFormSensor");
            base.CloseFormBefore();
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 获取时间的翻译文本
        /// </summary>
        /// <param name="second"></param>
        /// <param name="hourText">小时的文本</param>
        /// <param name="minuText">分的文本</param>
        /// <param name="secondText">秒的文本</param>
        /// <returns></returns>
        private string GetTimeString(int second, string hourText, string minuText, string secondText)
        {
            string timeStr = string.Empty;
            int hour = second / 3600;
            int minu = second % 3600 / 60;
            int sec = second % 60;
            if (hour > 0)
            {
                timeStr += hour + hourText;
            }
            if (minu > 0)
            {
                timeStr += minu + minuText;
            }
            if (sec > 0)
            {
                timeStr += sec + secondText;
            }
            return timeStr;
        }
        #endregion
        #region ■ 结构体_____________________________
        /// <summary>
        /// 设备行信息
        /// </summary>
        private class DeviceRowInfo
        {
            /// <summary>
            /// 文本ID,目前用来做主键
            /// </summary>
            public int TextId = 0;
            /// <summary>
            /// 图标
            /// </summary>
            public string IconPath = string.Empty;
            /// <summary>
            /// 图标
            /// </summary>
            public string IconPathSelected = string.Empty;
            /// <summary>
            /// 设备回路主键
            /// </summary>
            public List<string> listDeviceKeys = new List<string>();
        }
        #endregion
    }
}