xm
2020-12-14 d6fb0646531172f23648441c224cdcccd721b894
ZigbeeApp/Shared/Phone/UserCenter/Device/PirSensor/PirSensorTargetSelectForm.cs
New file
@@ -0,0 +1,386 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.UserCenter.DevicePirSensor
{
    /// <summary>
    /// PIR传感器的触发目标选择界面
    /// </summary>
    public class PirSensorTargetSelectForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 结束选择的事件(设备主键)
        /// </summary>
        public Action<List<string>> FinishSelectEvent = null;
        /// <summary>
        /// 当前选择的楼层ID
        /// </summary>
        private string nowSelectFloorId = string.Empty;
        /// <summary>
        /// 桌布控件
        /// </summary>
        private NormalFrameLayout frameTable = null;
        /// <summary>
        /// 房间对象
        /// </summary>
        private Dictionary<string, List<Common.Room>> dicRoom = new Dictionary<string, List<Common.Room>>();
        /// <summary>
        /// 已经存在的绑定设备
        /// </summary>
        private List<string> listEsixtDevice = new List<string>();
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_listEsixtDevice">存在的设备</param>
        public void ShowForm(List<string> i_listEsixtDevice)
        {
            this.listEsixtDevice.AddRange(i_listEsixtDevice);
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uTriggerTarget));
            //初始化右上角的控件
            this.InitTopRightMenuControl();
            //初始化中部信息
            this.InitMiddleFrame();
        }
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
            //房间的容器控件
            var frameBack = new FrameLayout();
            frameBack.Height = Application.GetRealHeight(204);
            bodyFrameLayout.AddChidren(frameBack);
            //桌布控件
            this.frameTable = new NormalFrameLayout();
            frameTable.Y = frameBack.Bottom;
            frameTable.Height = bodyFrameLayout.Height - frameBack.Height;
            bodyFrameLayout.AddChidren(frameTable);
            //其他
            var tempRoom = new Common.Room();
            tempRoom.Name = Language.StringByID(R.MyInternationalizationString.uOther);
            foreach (var mainKeys in this.listEsixtDevice)
            {
                var device = Common.LocalDevice.Current.GetDevice(mainKeys);
                if (device == null)
                {
                    continue;
                }
                //这个手机弄弄,那个手机弄弄的情况下,
                //采用后备操作 -> 新建一个临时房间对象出来
                if (HdlRoomLogic.Current.GetRoomByDevice(device) == null)
                {
                    tempRoom.ListDevice.Add(Common.LocalDevice.Current.GetDeviceMainKeys(device));
                }
            }
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                if (dicRoom.ContainsKey(nowSelectFloorId) == true && dicRoom[nowSelectFloorId].Count > 0)
                {
                    //初始化房间控件
                    var listRoom = new List<Common.Room>();
                    listRoom.AddRange(dicRoom[nowSelectFloorId]);
                    if (tempRoom.ListDevice.Count > 0)
                    {
                        //显示新建的临时房间对象
                        listRoom.Add(tempRoom);
                    }
                    var roomContr = new RoomDeviceGroupMenuControl(listRoom);
                    frameBack.AddChidren(roomContr);
                    roomContr.SelectRoomEvent += (myRoom) =>
                    {
                        //初始化设备行
                        this.InitDeviceControl(myRoom);
                    };
                    roomContr.InitControl();
                    //确定按钮
                    var btnOk = new BottomClickButton();
                    btnOk.TextID = R.MyInternationalizationString.uConfirm1;
                    bodyFrameLayout.AddChidren(btnOk);
                    btnOk.ButtonClickEvent += (sender, e) =>
                    {
                        //调用回调函数
                        this.FinishSelectEvent?.Invoke(listEsixtDevice);
                        //界面关闭
                        this.CloseForm();
                    };
                    //查看模板时,不允许编辑
                    if (Common.Config.Instance.Home.IsShowTemplate == true)
                    {
                        btnOk.CanClick = false;
                    }
                }
                else
                {
                    //没有可以添加的目标
                    this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.uNotHadAddTarget));
                }
            });
        }
        #endregion
        #region ■ 初始化右上角的控件_________________
        /// <summary>
        /// 初始化右上角的控件
        /// </summary>
        private void InitTopRightMenuControl()
        {
            //房间分组
            var listRoom = HdlRoomLogic.Current.GetAllListRooms();
            foreach (var room in listRoom)
            {
                //检测该房间能否显示
                if (this.CheckCanShowRow(room) == false)
                {
                    continue;
                }
                if (dicRoom.ContainsKey(room.FloorId) == false)
                {
                    dicRoom[room.FloorId] = new List<Common.Room>();
                }
                dicRoom[room.FloorId].Add(room);
            }
            if (dicRoom.Count == 0)
            {
                //默认一个空的东西
                dicRoom[string.Empty] = new List<Common.Room>();
            }
            //获取楼层
            var dicFloor = HdlRoomLogic.Current.GetFloorSortList();
            if (dicFloor.Count == 0)
            {
                return;
            }
            int indexTemp = 1;
            foreach (var floorId in dicRoom.Keys)
            {
                if (dicFloor.ContainsKey(floorId) == false)
                {
                    //这是多台手机瞎搞之后出现的后备补救措施
                    dicFloor[floorId] = Language.StringByID(R.MyInternationalizationString.uOther) + indexTemp;
                    indexTemp++;
                }
            }
            var btnIconContr = new MostRightIconControl(69, 69);
            btnIconContr.UnSelectedImagePath = "Item/Drop_Down.png";
            topFrameLayout.AddChidren(btnIconContr);
            btnIconContr.InitControl();
            var btnFloor = new NormalViewControl(300, 69, true);
            btnFloor.Gravity = Gravity.CenterVertical;
            btnFloor.X = btnIconContr.X + btnIconContr.btnIcon.X - Application.GetRealWidth(300);
            btnFloor.TextAlignment = TextAlignment.CenterRight;
            topFrameLayout.AddChidren(btnFloor);
            foreach (var floorId in dicFloor.Keys)
            {
                //第一个楼层
                this.nowSelectFloorId = floorId;
                btnFloor.Text = dicFloor[floorId];
                break;
            }
            btnIconContr.ButtonClickEvent += (sender, e) =>
            {
                //楼层菜单
                var contr = new TopRightMenuControl(dicFloor.Count, 2, Language.StringByID(R.MyInternationalizationString.SelectFloor));
                foreach (var floorId in dicFloor.Keys)
                {
                    contr.AddRowMenu(dicFloor[floorId], "Floor/Floor.png", "Floor/FloorSelected.png", () =>
                    {
                        //记录起选择的ID
                        this.nowSelectFloorId = floorId;
                        btnFloor.Text = dicFloor[this.nowSelectFloorId];
                        //初始化中部信息
                        this.InitMiddleFrame();
                    });
                }
            };
        }
        #endregion
        #region ■ 初始化设备控件_____________________
        /// <summary>
        /// 初始化设备控件
        /// </summary>
        /// <param name="room"></param>
        private void InitDeviceControl(Common.Room room)
        {
            //先清空
            this.frameTable.RemoveAll();
            var frameBack = new FrameLayout();
            frameBack.Height = Application.GetRealHeight(11);
            frameBack.BackgroundColor = UserCenterColor.Current.White;
            frameTable.AddChidren(frameBack);
            var listView = new VerticalListControl(12);
            listView.Y = frameBack.Bottom;
            listView.Height = frameTable.Height - frameBack.Height;
            listView.BackgroundColor = UserCenterColor.Current.White;
            frameTable.AddChidren(listView);
            var listDevice = new List<ZigBee.Device.CommonDevice>();
            foreach (var deviceKeys in room.ListDevice)
            {
                var device = Common.LocalDevice.Current.GetDevice(deviceKeys);
                //检测设备
                if (this.CheckCanShowDevice(device) == true)
                {
                    listDevice.Add(device);
                }
            }
            for (int i = 0; i < listDevice.Count; i++)
            {
                var device = listDevice[i];
                string mainKey = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                var rowDevice = new FrameRowControl(listView.rowSpace / 2);
                listView.AddChidren(rowDevice);
                //图标
                var btnIcon = rowDevice.AddLeftIcon(81);
                Common.LocalDevice.Current.SetDeviceIconToControl(btnIcon, device);
                //设备名称
                var btnName = rowDevice.AddLeftCaption(Common.LocalDevice.Current.GetDeviceEpointName(device), 700);
                btnName.TextSize = 15;
                //选择
                var btnSelect = rowDevice.AddMostRightEmptyIcon(58, 58);
                if (listEsixtDevice.Contains(mainKey) == false)
                {
                    btnSelect.Visible = false;
                }
                btnSelect.UnSelectedImagePath = "Item/ItemSelected.png";
                if (i != listDevice.Count - 1)
                {
                    //底线
                    rowDevice.AddBottomLine();
                }
                rowDevice.ButtonClickEvent += (sender, e) =>
                {
                    btnSelect.Visible = !btnSelect.Visible;
                    if (btnSelect.Visible == true)
                    {
                        listEsixtDevice.Add(mainKey);
                    }
                    else
                    {
                        listEsixtDevice.Remove(mainKey);
                    }
                };
                //查看模板时,不允许编辑
                if (Common.Config.Instance.Home.IsShowTemplate == true)
                {
                    rowDevice.CanClick = false;
                }
            }
            listDevice = null;
            //调整控件真实高度
            listView.AdjustRealHeight(Application.GetRealHeight(23));
            //借用这个东西进行检测
            var btnTemp = new BottomClickButton();
            //如果真实高度已经超过了确定按键
            if (listView.Bottom + frameTable.Y > btnTemp.Yaxis)
            {
                listView.Height = frameTable.Height - frameBack.Height;
                var frameTemp = new FrameLayout();
                frameTemp.Height = bodyFrameLayout.Height - btnTemp.Yaxis;
                listView.AddChidren(frameTemp);
            }
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 检测该房间能否显示
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        private bool CheckCanShowRow(Common.Room room)
        {
            if (room.ListDevice.Count == 0)
            {
                return false;
            }
            if (room.IsLove == true)
            {
                return false;
            }
            foreach (var deviceKeys in room.ListDevice)
            {
                //检测该设备能否显示
                var device = Common.LocalDevice.Current.GetDevice(deviceKeys);
                if (this.CheckCanShowDevice(device) == false)
                {
                    continue;
                }
                //存在设备的话,此房间可以显示
                return true;
            }
            return false;
        }
        /// <summary>
        /// 检测该设备能否显示
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        private bool CheckCanShowDevice(ZigBee.Device.CommonDevice device)
        {
            if (device == null)
            {
                return false;
            }
            //如果是传感器,或者是没有开关簇的话(这里判断的是输入簇)
            if ((device.Type == ZigBee.Device.DeviceType.IASZone) || Common.LocalDevice.Current.InDeviceIsCanOnOff(device) == false)
            {
                return false;
            }
            return true;
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            this.FinishSelectEvent = null;
            base.CloseFormBefore();
        }
        #endregion
    }
}