ZigbeeApp/Shared/Phone/Category/AdjustTargetAddDeviceForm.cs
New file
@@ -0,0 +1,664 @@
using Shared.Common;
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.Category
{
    /// <summary>
    /// 场景执行目标添加设备的界面
    /// </summary>
    public class AdjustTargetAddDeviceForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 完成选择的事件(Key:设备主键)
        /// </summary>
        public Action<Dictionary<string, List<Safeguard.TaskListInfo>>> FinishSelectEvent = null;
        /// <summary>
        /// 当前已经添加的执行目标
        /// </summary>
        private List<Scene.DeviceListData> listAdjustTarget = null;
        /// <summary>
        /// 当前已经添加的执行目标(存在检测用)
        /// </summary>
        private Dictionary<string, List<Safeguard.TaskListInfo>> dicOldListTask = null;
        /// <summary>
        /// 当前界面上显示的执行目标(存在检测用)
        /// </summary>
        private Dictionary<string, List<Safeguard.TaskListInfo>> dicNewListTask = null;
        /// <summary>
        /// 完成按钮
        /// </summary>
        private BottomClickButton btnFinishControl = null;
        /// <summary>
        /// 设备功能的菜单控件
        /// </summary>
        private HorizontalScrolViewLayout deviceFunctionMenuContr = null;
        /// <summary>
        /// 设备列表控件
        /// </summary>
        private VerticalListControl listDeviceView = null;
        /// <summary>
        /// 当前选择的楼层
        /// </summary>
        private string nowSelectFloorId = string.Empty;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_listAdjustTarget">当前执行目标列表</param>
        public void ShowForm(List<Scene.DeviceListData> i_listAdjustTarget)
        {
            this.listAdjustTarget = i_listAdjustTarget;
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.AddFunction));
            //初始化右上角的控件
            this.InitTopRightMenuControl();
            //初始化中部信息
            this.InitMiddleFrame();
        }
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空body
            this.ClearBodyFrame();
            //初始化房间菜单控件
            this.InitRoomMenuControl();
        }
        /// <summary>
        /// 初始化右上角的控件
        /// </summary>
        private void InitTopRightMenuControl()
        {
            //获取楼层
            var dicFloor = HdlRoomLogic.Current.GetFloorSortList();
            if (dicFloor.Count == 0)
            {
                return;
            }
            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 TopRightFloorMenuControl(dicFloor.Count, 2, this.nowSelectFloorId, Language.StringByID(R.MyInternationalizationString.SelectFloor));
                foreach (var floorId in dicFloor.Keys)
                {
                    contr.AddRowMenu(floorId, () =>
                    {
                        //记录起选择的ID
                        this.nowSelectFloorId = floorId;
                        btnFloor.Text = dicFloor[this.nowSelectFloorId];
                        //初始化中部信息
                        this.InitMiddleFrame();
                    });
                }
            };
        }
        #endregion
        #region ■ 房间菜单控件_______________________
        /// <summary>
        /// 初始化房间菜单控件
        /// </summary>
        private void InitRoomMenuControl()
        {
            //获取能够显示的房间列表
            var listRoom = this.GetCanShowRoomList();
            if (listRoom.Count == 0)
            {
                //没有可以添加的目标
                this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.uNotHadAddTarget));
                return;
            }
            //这个控件的高度为:房间菜单的底部到屏幕底部
            var functionBodyView = new FrameLayout();
            //房间菜单控件
            var roomSwitchContr = new RoomDeviceGroupMenuControl(listRoom);
            this.bodyFrameLayout.AddChidren(roomSwitchContr);
            //选择事件
            roomSwitchContr.SelectRoomEvent += (selectRoom) =>
            {
                //在外面清空(特效的问题)
                if (this.deviceFunctionMenuContr != null && this.deviceFunctionMenuContr.Parent != null)
                {
                    this.deviceFunctionMenuContr.RemoveAll();
                }
                this.listDeviceView?.RemoveAll();
                HdlThreadLogic.Current.RunMainInThread(() =>
                {
                    //刷新设备分支控件
                    this.RefreshFunctionView(selectRoom, functionBodyView);
                });
            };
            functionBodyView.Y = roomSwitchContr.Bottom;
            functionBodyView.Height = bodyFrameLayout.Height - roomSwitchContr.Bottom;
            bodyFrameLayout.AddChidren(functionBodyView);
            //完成按钮
            this.btnFinishControl = new BottomClickButton();
            btnFinishControl.TextID = R.MyInternationalizationString.uFinish;
            bodyFrameLayout.AddChidren(btnFinishControl);
            if (this.dicNewListTask == null || this.dicNewListTask.Count == 0)
            {
                btnFinishControl.Visible = false;
            }
            btnFinishControl.ButtonClickEvent += (sender, e) =>
            {
                //回调函数
                this.FinishSelectEvent?.Invoke(this.dicNewListTask);
                this.CloseForm();
            };
            //执行初始化(会自动触发SelectRoomEvent事件)
            roomSwitchContr.InitControl();
        }
        /// <summary>
        /// 获取能够显示的房间列表
        /// </summary>
        /// <param name="listRomm"></param>
        /// <returns></returns>
        private List<Room> GetCanShowRoomList()
        {
            if (this.dicNewListTask == null)
            {
                //先将列表Dictionary化
                this.dicNewListTask = new Dictionary<string, List<Safeguard.TaskListInfo>>();
                this.dicOldListTask = new Dictionary<string, List<Safeguard.TaskListInfo>>();
                foreach (var data in this.listAdjustTarget)
                {
                    if (data.Type == 0)
                    {
                        //只要设备
                        string mainkey = LocalDevice.Current.GetDeviceMainKeys(data.DeviceAddr, data.Epoint);
                        dicOldListTask[mainkey] = data.TaskList;
                    }
                }
            }
            //当前楼层的全部房间
            var lisrRoom = HdlRoomLogic.Current.GetRoomsByFloorIdAppendLoveRoom(this.nowSelectFloorId);
            var listShowRoom = new List<Room>();
            foreach (var room in lisrRoom)
            {
                foreach (var mainkey in room.ListDevice)
                {
                    var device = LocalDevice.Current.GetDevice(mainkey);
                    if (this.CheckDeviceCanShow(device) == true)
                    {
                        //这个设备本地存在,并且它还没有加入到执行目标列表,则这个房间可以显示
                        listShowRoom.Add(room);
                        break;
                    }
                }
            }
            return listShowRoom;
        }
        #endregion
        #region ■ 功能分支___________________________
        /// <summary>
        /// 刷新功能分支控件
        /// </summary>
        /// <param name="room"></param>
        private void RefreshFunctionView(Common.Room room, FrameLayout functionBodyView)
        {
            //获取分组后的设备
            var dicGroupDevice = this.GetAllGroupDevice(room);
            //只初始化一次
            if (this.deviceFunctionMenuContr == null || this.deviceFunctionMenuContr.Parent == null)
            {
                //设备菜单的白色背景
                var functionBack1 = new FrameLayout();
                functionBack1.X = ControlCommonResourse.XXLeft;
                functionBack1.Height = Application.GetRealHeight(160);
                functionBack1.Width = Application.GetRealWidth(1028);
                functionBack1.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
                functionBodyView.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;
                functionBodyView.AddChidren(functionBack2);
                functionBack2.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerBottomLeft);
                //设备菜单的左右滑动的控件
                this.deviceFunctionMenuContr = new HorizontalScrolViewLayout();
                deviceFunctionMenuContr.X = ControlCommonResourse.XXLeft;
                deviceFunctionMenuContr.Height = Application.GetRealHeight(279);
                deviceFunctionMenuContr.Width = Application.GetRealWidth(1028);
                functionBodyView.AddChidren(deviceFunctionMenuContr);
                //设备的背景容器
                var frameDeviceBack = new FrameLayout();
                frameDeviceBack.X = ControlCommonResourse.XXLeft;
                frameDeviceBack.Y = deviceFunctionMenuContr.Bottom + Application.GetRealHeight(35);
                frameDeviceBack.BackgroundColor = UserCenterColor.Current.White;
                frameDeviceBack.Width = bodyFrameLayout.Width;
                frameDeviceBack.Height = functionBodyView.Height - deviceFunctionMenuContr.Bottom - Application.GetRealHeight(35);
                frameDeviceBack.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerTopLeft);
                functionBodyView.AddChidren(frameDeviceBack);
                //设备列表控件
                this.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);
            }
            //上一次选择的菜单和数据
            DeviceRowInfo nowSelectDeviceInfo = null;
            MainPage.Controls.DeviceFunctionUnallocatedControl oldSelectContr = null;
            foreach (string strText in dicGroupDevice.Keys)
            {
                var rowInfo = dicGroupDevice[strText];
                //设备类型的容器
                var devieFrame = new FrameLayout();
                devieFrame.Width = Application.GetRealWidth(220);
                deviceFunctionMenuContr.AddChidren(devieFrame);
                //菜单图片控件
                var deviceObjContr = new MainPage.Controls.DeviceFunctionUnallocatedControl();
                devieFrame.AddChidren(deviceObjContr);
                deviceObjContr.InitControl(strText, 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);
            }
        }
        #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>();
            for (int i = 0; i < rowInfo.listDeviceKeys.Count; i++)
            {
                var device = LocalDevice.Current.GetDevice(rowInfo.listDeviceKeys[i]);
                if (device != null)
                {
                    listDevice.Add(device);
                }
                else
                {
                    rowInfo.listDeviceKeys.RemoveAt(i);
                    i--;
                }
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                foreach (var device in listDevice)
                {
                    //添加设备行
                    this.AddDeviceRow(listView, device);
                }
                //调整桌布,促使它能够滑动超过完成按钮
                listView.AdjustRealHeightByBottomButton(Application.GetRealHeight(23), Application.GetRealHeight(529));
            });
        }
        /// <summary>
        /// 添加设备行
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="device"></param>
        private void AddDeviceRow(VerticalListControl listView, CommonDevice device)
        {
            string mainkey = LocalDevice.Current.GetDeviceMainKeys(device);
            //设备控件
            var rowDevice = new FrameRowControl(listView.rowSpace / 2);
            rowDevice.RightOffset = ControlCommonResourse.XXLeft - Application.GetRealWidth(109);
            listView.AddChidren(rowDevice);
            //设备图标背景控件
            var frameIconBackGroud = new FrameLayout();
            frameIconBackGroud.Height = this.GetPictrueRealSize(112);
            frameIconBackGroud.Width = this.GetPictrueRealSize(112);
            frameIconBackGroud.Gravity = Gravity.CenterVertical;
            frameIconBackGroud.X = Application.GetRealWidth(46);
            frameIconBackGroud.Radius = (uint)this.GetPictrueRealSize(112 / 2);
            frameIconBackGroud.BackgroundColor = Common.ZigbeeColor.Current.GXCGrayBackgroundColor;
            rowDevice.AddChidren(frameIconBackGroud, ChidrenBindMode.NotBind);
            frameIconBackGroud.Y += rowDevice.chidrenYaxis;
            //设备图标控件
            var btnDeviceIcon = new IconViewControl(78);
            btnDeviceIcon.Gravity = Gravity.Center;
            Common.LocalDevice.Current.SetDeviceIconToControl(btnDeviceIcon, device);
            frameIconBackGroud.AddChidren(btnDeviceIcon);
            //重新绑定事件
            rowDevice.ChangedChidrenBindMode(frameIconBackGroud, ChidrenBindMode.BindEvent);
            //设备名字
            var btnDeviceName = new NormalViewControl(600, 60, true);
            btnDeviceName.X = Application.GetRealWidth(181);
            btnDeviceName.Gravity = Gravity.CenterVertical;
            btnDeviceName.Text = Common.LocalDevice.Current.GetDeviceEpointName(device);
            rowDevice.AddChidren(btnDeviceName, ChidrenBindMode.BindEvent);
            btnDeviceName.Y += rowDevice.chidrenYaxis;
            //底线
            var btnBottomLine = new NormalViewControl(Application.GetRealWidth(841), ControlCommonResourse.BottomLineHeight, false);
            btnBottomLine.X = Application.GetRealWidth(181);
            btnBottomLine.Y = rowDevice.Height - ControlCommonResourse.BottomLineHeight;
            btnBottomLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
            rowDevice.AddChidren(btnBottomLine, ChidrenBindMode.NotBind);
            //右箭头
            rowDevice.AddRightArrow();
            //状态
            var btnStatu = rowDevice.AddMostRightView("", 400);
            if (this.dicNewListTask.ContainsKey(mainkey) == true)
            {
                //显示配置状态
                btnStatu.Text = HdlSafeguardLogic.Current.GetAdjustTargetStatuText(dicNewListTask[mainkey]);
            }
            rowDevice.ButtonClickEvent += (sender, e) =>
            {
                //功能的详细配置
                this.ShowDeviceDetailSettion(device, btnStatu);
            };
        }
        #endregion
        #region ■ 功能的详细配置_____________________
        /// <summary>
        /// 功能的详细配置
        /// </summary>
        /// <param name="device"></param>
        /// <param name="btnStatu"></param>
        private void ShowDeviceDetailSettion(CommonDevice device, NormalViewControl btnStatu)
        {
            string mainKeys = LocalDevice.Current.GetDeviceMainKeys(device);
            List<Safeguard.TaskListInfo> listTaskinfo = null;
            if (dicNewListTask.ContainsKey(mainKeys) == true)
            {
                //取缓存中还未保存的数据
                listTaskinfo = dicNewListTask[mainKeys];
            }
            if (device.Type == DeviceType.DimmableLight//调光器
                || device.Type == DeviceType.ColorDimmableLight)//彩灯
            {
                var form = new UserCenter.Safety.AlarmTargetStatuSelectLightForm();
                form.AddForm(device, listTaskinfo);
                form.FinishSelectEvent += (statuText, listInfo) =>
                {
                    if (listInfo.Count == 0) { statuText = string.Empty; }
                    btnStatu.Text = statuText;
                    //将新的执行目标添加入缓存
                    this.AddSettionDataToMemory(device, listInfo);
                };
            }
            else if (device.Type == DeviceType.Thermostat)//空调
            {
                var form = new UserCenter.Safety.AlarmTargetStatuSelectAcForm();
                form.AddForm(device, listTaskinfo);
                form.FinishSelectEvent += (statuText, listInfo) =>
                {
                    if (listInfo.Count == 0) { statuText = string.Empty; }
                    btnStatu.Text = statuText;
                    //将新的执行目标添加入缓存
                    this.AddSettionDataToMemory(device, listInfo);
                };
            }
            else if (device.Type == DeviceType.WindowCoveringDevice)//窗帘
            {
                var form = new UserCenter.Safety.AlarmTargetStatuSelectCurtainForm();
                form.AddForm(device, listTaskinfo);
                form.FinishSelectEvent += (statuText, listInfo) =>
                {
                    if (listInfo.Count == 0) { statuText = string.Empty; }
                    btnStatu.Text = statuText;
                    //将新的执行目标添加入缓存
                    this.AddSettionDataToMemory(device, listInfo);
                };
            }
            else
            {
                //其他直接归为开关类
                var form = new UserCenter.Safety.AlarmTargetStatuSelectSwitchForm();
                form.AddForm(device, listTaskinfo);
                form.FinishSelectEvent += (statuText, listInfo) =>
                {
                    if (listInfo.Count == 0) { statuText = string.Empty; }
                    btnStatu.Text = statuText;
                    //将新的执行目标添加入缓存
                    this.AddSettionDataToMemory(device, listInfo);
                };
            }
        }
        /// <summary>
        /// 将新的执行目标添加入缓存
        /// </summary>
        /// <param name="deviceInfo"></param>
        /// <param name="listInfo"></param>
        private void AddSettionDataToMemory(CommonDevice device, List<Safeguard.TaskListInfo> listInfo)
        {
            string mainKeys = LocalDevice.Current.GetDeviceMainKeys(device);
            if (listInfo == null || listInfo.Count == 0)
            {
                //指定为无动作模式
                if (this.dicNewListTask.ContainsKey(mainKeys) == true)
                {
                    this.dicNewListTask.Remove(mainKeys);
                    if (dicNewListTask.Count == 0)
                    {
                        this.btnFinishControl.Visible = false;
                    }
                }
            }
            else
            {
                //确认添加动作
                this.dicNewListTask[mainKeys] = listInfo;
                if (this.btnFinishControl.Visible == false)
                {
                    this.btnFinishControl.Visible = true;
                }
            }
        }
        #endregion
        #region ■ 整合设备___________________________
        /// <summary>
        /// 获取分组后的设备
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, DeviceRowInfo> GetAllGroupDevice(Common.Room room)
        {
            //全部的设备
            var listDeviceTemp = HdlRoomLogic.Current.GetRoomListDevice(room);
            var listDevice = new List<CommonDevice>();
            foreach (var device in listDeviceTemp)
            {
                //判断该设备能否显示
                if (this.CheckDeviceCanShow(device) == true)
                {
                    listDevice.Add(device);
                }
            }
            //根据设备所属类型排序
            listDevice = LocalDevice.Current.SortDeviceByBelongType(listDevice);
            var dic = new Dictionary<string, DeviceRowInfo>();
            foreach (var device in listDevice)
            {
                var typeInfo = LocalDevice.Current.GetDeviceBelongEnumInfo(device);
                //按所属ID分组
                if (dic.ContainsKey(typeInfo.BeloneText) == false)
                {
                    dic[typeInfo.BeloneText] = new DeviceRowInfo();
                    string path1 = string.Empty;
                    string path2 = string.Empty;
                    //获取图片
                    LocalDevice.Current.GetDeviceFunctionTypeMenuIcon(typeInfo.ConcreteType, ref path1, ref path2);
                    dic[typeInfo.BeloneText].IconPath = path1;
                    dic[typeInfo.BeloneText].IconPathSelected = path2;
                    dic[typeInfo.BeloneText].TextId = typeInfo.BeloneText;
                }
                dic[typeInfo.BeloneText].listDeviceKeys.Add(LocalDevice.Current.GetDeviceMainKeys(device));
            }
            return dic;
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            this.FinishSelectEvent = null;
            base.CloseFormBefore();
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 检测设备能否显示
        /// </summary>
        /// <param name="device"></param>
        private bool CheckDeviceCanShow(CommonDevice device)
        {
            if (device == null) { return false; }
            if (device.Type == DeviceType.AirSwitch ||//空气开关
                device.Type == DeviceType.ColorDimmableLight ||//调光灯
                device.Type == DeviceType.DimmableLight ||//彩灯
                device.Type == DeviceType.OnOffOutput ||//继电器
                device.Type == DeviceType.Thermostat ||//空调
                device.Type == DeviceType.ColorTemperatureLight ||//色温灯
                device.Type == DeviceType.WindowCoveringDevice)//窗帘
            {
                string mainkey = LocalDevice.Current.GetDeviceMainKeys(device);
                return this.dicOldListTask.ContainsKey(mainkey) == false;
            }
            return false;
        }
        #endregion
        #region ■ 结构体_____________________________
        /// <summary>
        /// 设备行信息
        /// </summary>
        private class DeviceRowInfo
        {
            /// <summary>
            /// 文本,目前用来做主键
            /// </summary>
            public string TextId = string.Empty;
            /// <summary>
            /// 图标
            /// </summary>
            public string IconPath = string.Empty;
            /// <summary>
            /// 图标
            /// </summary>
            public string IconPathSelected = string.Empty;
            /// <summary>
            /// 设备回路主键
            /// </summary>
            public List<string> listDeviceKeys = new List<string>();
        }
        #endregion
    }
}