xm
2020-05-07 c1de48884fa145a16a0f8bcee93274dcfaa0ff82
ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceDryContactSettionForm.cs
New file
@@ -0,0 +1,639 @@
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Device
{
    /// <summary>
    /// 设备干接点配置
    /// </summary>
    public class DeviceDryContactSettionForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 列表控件
        /// </summary>
        private FrameListControl listview = null;
        /// <summary>
        /// 当前选择的设备
        /// </summary>
        private CommonDevice nowSelectDevice = null;
        /// <summary>
        /// 当前选择的回路控件
        /// </summary>
        private NormalViewControl nowSelectControl = null;
        /// <summary>
        /// 设备备注的控件
        /// </summary>
        private FrameCaptionInputControl btnDeviceName = null;
        /// <summary>
        /// 设备的某一回路
        /// </summary>
        private CommonDevice deviceObj = null;
        /// <summary>
        /// 设备需要保存的设备名字
        /// </summary>
        private Dictionary<int, string> dicDeviceSaveName = new Dictionary<int, string>();
        /// <summary>
        /// 设备具体类型的信息
        /// </summary>
        private DeviceEnumInfo deviceEnumInfo = null;
        /// <summary>
        /// 信息编辑控件
        /// </summary>
        private InformationEditorControl tableContr = null;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_device">设备的某一回路</param>
        public void ShowForm(CommonDevice i_device)
        {
            this.deviceObj = i_device;
            var listDevice = Common.LocalDevice.Current.GetDevicesByMac(deviceObj.DeviceAddr);
            //获取这一堆设备时属于什么类型的
            this.deviceEnumInfo = Common.LocalDevice.Current.GetMyDeviceEnumInfo(listDevice);
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uDryContactSettion));
            //初始化中部信息
            this.InitMiddleFrame();
            //初始化右上角菜单
            this.InitTopRightMenu();
        }
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
            var listBackControl = new VerticalFrameControl();
            listBackControl.Height = bodyFrameLayout.Height;
            bodyFrameLayout.AddChidren(listBackControl);
            //初始化桌布
            this.tableContr = new InformationEditorControl();
            this.listview = tableContr.InitControl(listBackControl.frameTable, Language.StringByID(R.MyInternationalizationString.uDeviceEditor), 1175);
            //初始化设备回路图标
            this.InitDeviceEpointIcon(listBackControl);
            //保存
            var btnFinish = new BottomClickButton();
            btnFinish.TextID = R.MyInternationalizationString.uSave;
            bodyFrameLayout.AddChidren(btnFinish);
            btnFinish.ButtonClickEvent += async (sender, e) =>
            {
                //记录起当前正在操作的回路名字
                dicDeviceSaveName[nowSelectDevice.DeviceEpoint] = btnDeviceName.Text.Trim();
                //开启进度条
                this.ShowProgressBar();
                foreach (var epoint in dicDeviceSaveName.Keys)
                {
                    var device = Common.LocalDevice.Current.GetDevice(deviceObj.DeviceAddr, epoint);
                    if (device == null || dicDeviceSaveName[epoint] == string.Empty)
                    {
                        //不能允许空白名字
                        continue;
                    }
                    string newName = dicDeviceSaveName[epoint];
                    string oldName = Common.LocalDevice.Current.GetDeviceEpointName(device);
                    if (oldName != newName)
                    {
                        //设备名称修改
                        var result = await Common.LocalDevice.Current.ReName(device, newName);
                        if (result == false)
                        {
                            //关闭
                            this.CloseProgressBar();
                            return;
                        }
                    }
                }
                this.CloseProgressBar();
                //关闭自身
                this.CloseForm();
            };
        }
        /// <summary>
        /// 初始化菜单行
        /// </summary>
        private async void InitMenuRow()
        {
            this.listview.RemoveAll();
            //干接点备注
            string caption = Language.StringByID(R.MyInternationalizationString.uDryContactNote);
            string nameValue = Common.LocalDevice.Current.GetDeviceEpointName(nowSelectDevice);
            if (dicDeviceSaveName.ContainsKey(nowSelectDevice.DeviceEpoint) == true)
            {
                nameValue = dicDeviceSaveName[nowSelectDevice.DeviceEpoint];
            }
            this.btnDeviceName = new FrameCaptionInputControl(caption, nameValue, listview.rowSpace / 2);
            listview.AddChidren(btnDeviceName);
            btnDeviceName.InitControl();
            btnDeviceName.AddBottomLine();
            btnDeviceName.txtInput.FinishInputEvent += async () =>
            {
                string oldName = Common.LocalDevice.Current.GetDeviceEpointName(nowSelectDevice);
                if (btnDeviceName.Text.Trim() == string.Empty)
                {
                    //将名字还原
                    btnDeviceName.Text = oldName;
                }
                if (oldName != btnDeviceName.Text.Trim())
                {
                    //设备名称修改
                    var result = await Common.LocalDevice.Current.ReName(nowSelectDevice, btnDeviceName.Text.Trim());
                    if (result == false)
                    {
                        return;
                    }
                    //干接点备注修改成功!
                    string msg = Language.StringByID(R.MyInternationalizationString.uDryContactReNoteSuccess);
                    this.ShowMassage(ShowMsgType.Tip, msg);
                }
            };
            //所属区域
            var rowBeloneArea = new BelongAreaControl(listview.rowSpace / 2);
            listview.AddChidren(rowBeloneArea);
            rowBeloneArea.InitControl(Language.StringByID(R.MyInternationalizationString.uBelongArea), nowSelectDevice);
            //底线
            rowBeloneArea.AddBottomLine();
            rowBeloneArea.SelectRoomEvent += (roomKeys) =>
            {
                //变更房间
                HdlRoomLogic.Current.ChangedRoom(nowSelectDevice, roomKeys);
            };
            //3路继电器或者1路调光器第七回路的配置信息
            List<CommonDevice.AttributeDataObj> listRelayInfo7 = null;
            bool isHdlDevice = Common.LocalDevice.Current.IsHdlDevice(nowSelectDevice);
            //河东设备才会开启此选项
            if (isHdlDevice == true)
            {
                //如果是3路继电器或者1路调光器,并且是第七回路的时候,它好像可以改变类型
                if ((deviceEnumInfo.ConcreteType == DeviceConcreteType.Relay_ThreeLoad && nowSelectDevice.DeviceEpoint == 10)
                    || (deviceEnumInfo.ConcreteType == DeviceConcreteType.DimmableLight_OneLoad && nowSelectDevice.DeviceEpoint == 7))
                {
                    //这里先去获取它的干接点类型
                    this.ShowProgressBar();
                    //获取第七回路的配置信息
                    listRelayInfo7 = await HdlDevicePanelLogic.Current.GetDryContactConfigureInfo(nowSelectDevice);
                    if (listRelayInfo7 == null)
                    {
                        //关闭进度条
                        this.CloseProgressBar(ShowReLoadMode.YES);
                        return;
                    }
                    //关闭进度条
                    this.CloseProgressBar();
                }
                //添加干接点功能行
                this.AddDryContactFunctionRow(listRelayInfo7);
            }
            //添加绑定目标行
            this.AddBindRow(listRelayInfo7);
            //添加背光灯行
            this.AddBackLightRow();
            //初始化桌布完成
            tableContr.FinishInitControl();
        }
        #endregion
        #region ■ 右上角菜单_________________________
        /// <summary>
        /// 初始化右上角菜单
        /// </summary>
        private void InitTopRightMenu()
        {
            //检测此回路是否拥有定位功能
            if (Common.LocalDevice.Current.DeviceIsCanFixedPosition(nowSelectDevice) == false)
            {
                return;
            }
            var btnIcon = new MostRightIconControl(69, 69);
            btnIcon.UnSelectedImagePath = "Item/More.png";
            topFrameLayout.AddChidren(btnIcon);
            btnIcon.InitControl();
            btnIcon.ButtonClickEvent += ((sender, e) =>
            {
                //显示右上角菜单界面
                this.ShowTopRightMenu();
            });
        }
        /// <summary>
        /// 显示右上角菜单界面
        /// </summary>
        private void ShowTopRightMenu()
        {
            int menuCount = 1;
            string deviceMenu = string.Empty;
            //检测此回路是否拥有定位功能
            bool canTest = Common.LocalDevice.Current.DeviceIsCanFixedPosition(nowSelectDevice);
            //if (canTest == true)
            //{
            //    menuCount = 2;
            //}
            var frame = new TopRightMenuControl(menuCount, 1);
            if (canTest == true)
            {
                //定位
                deviceMenu = Language.StringByID(R.MyInternationalizationString.uFixedPosition);
                frame.AddRowMenu(deviceMenu, "Item/FixedPosition.png", "Item/FixedPositionSelected.png", () =>
                {
                    //发送定位功能
                    Common.LocalDevice.Current.SetFixedPositionCommand(nowSelectDevice);
                });
            }
            //删除
            //deviceMenu = Language.StringByID(R.MyInternationalizationString.uDelete);
            //frame.AddRowMenu(deviceMenu, "Item/DeleteIcon2.png", "Item/DeleteIcon2Selected.png", () =>
            //{
            //});
        }
        #endregion
        #region ■ 干接点功能_________________________
        /// <summary>
        /// 添加干接点功能行
        /// </summary>
        /// <param name="listRelayInfo7">3路继电器或者1路调光器第七回路的配置信息</param>
        private void AddDryContactFunctionRow(List<CommonDevice.AttributeDataObj> listRelayInfo7)
        {
            //干接点功能
            string caption = Language.StringByID(R.MyInternationalizationString.uDryContactFunction);
            //默认为:通用干接点
            string strType = Language.StringByID(R.MyInternationalizationString.uCommonDryContact);
            var btnFunction = new FrameCaptionViewControl(caption, strType, listview.rowSpace / 2);
            btnFunction.UseClickStatu = false;
            listview.AddChidren(btnFunction);
            btnFunction.InitControl();
            //底线
            btnFunction.AddBottomLine();
            //如果是3路继电器或者1路调光器,并且是第七回路的时候,它好像可以改变类型
            if (listRelayInfo7 != null)
            {
                //右箭头
                btnFunction.AddRightArrow();
                int nowSelectNo = 1;
                foreach (var data in listRelayInfo7)
                {
                    if (data.AttributeId == (int)AttriButeId.HdlKey && data.AttriButeData == 65535)
                    {
                        //有65535即它是传感器输出
                        nowSelectNo = 0;
                        btnFunction.Text = Language.StringByID(R.MyInternationalizationString.uSensorInput);
                        break;
                    }
                }
                btnFunction.ButtonClickEvent += (sender, e) =>
                {
                    //显示列表
                    var listText = new List<string>();
                    listText.Add(Language.StringByID(R.MyInternationalizationString.uSensorInput));//传感器输入
                    listText.Add(Language.StringByID(R.MyInternationalizationString.uCommonDryContact));//通用干接点
                    //标题:选择干接点功能
                    var title = Language.StringByID(R.MyInternationalizationString.uSelectDryContactFunction);
                    var form = new BottomItemSelectForm();
                    form.AddForm(title, listText, nowSelectNo);
                    form.FinishSelectEvent += async (selectNo) =>
                    {
                        if (selectNo == nowSelectNo)
                        {
                            //相同,不需要处理
                            return;
                        }
                        //默认为通用干接点  102:开关切换
                        int value = 102;
                        if (selectNo == 0)
                        {
                            //选择的是传感器输入
                            value = 65535;
                        }
                        var result = await HdlDevicePanelLogic.Current.EditorDryContactThirdFunction((Panel)nowSelectDevice, value);
                        if (result == true)
                        {
                            //重新初始化菜单行,隐藏或者开放绑定目标菜单
                            this.InitMenuRow();
                        }
                    };
                };
            }
        }
        #endregion
        #region ■ 绑定目标___________________________
        /// <summary>
        /// 添加绑定目标行
        /// </summary>
        /// <param name="listRelayInfo7">3路继电器或者1路调光器第七回路的配置信息</param>
        private void AddBindRow(List<CommonDevice.AttributeDataObj> listRelayInfo7)
        {
            if (nowSelectDevice.Type == DeviceType.OnOffSwitch)
            {
                //如果是3路继电器或者1路调光器,并且是第七回路的时候
                //如果它的类型为:传感器输出的时候,就不存在绑定目标这个概念
                if (listRelayInfo7 != null)
                {
                    foreach (var data in listRelayInfo7)
                    {
                        if (data.AttributeId == (int)AttriButeId.HdlKey && data.AttriButeData == 65535)
                        {
                            //有65535即它是传感器输出
                            return;
                        }
                    }
                }
                string caption = Language.StringByID(R.MyInternationalizationString.uBindTargets);
                var btnBind = new FrameRowControl(listview.rowSpace / 2);
                listview.AddChidren(btnBind);
                btnBind.AddLeftCaption(caption, 600);
                //底线
                btnBind.AddBottomLine();
                //右箭头
                btnBind.AddRightArrow();
                btnBind.ButtonClickEvent += (sender, e) =>
                {
                    var panel = nowSelectDevice as ZigBee.Device.Panel;
                    var bindTargetsPage = new Shared.Phone.UserCenter.DeviceBind.BindTargetsPage(panel);
                    Shared.Phone.UserView.HomePage.Instance.AddChidren(bindTargetsPage);
                    Shared.Phone.UserView.HomePage.Instance.PageIndex += 1;
                    bindTargetsPage.Show();
                };
            }
        }
        #endregion
        #region ■ 背光灯_____________________________
        /// <summary>
        /// 添加背光灯行
        /// </summary>
        private void AddBackLightRow()
        {
            //三路继电器,或者一路调光器 的时候才能开启此选项
            var listDevice = Common.LocalDevice.Current.GetDevicesByMac(deviceObj.DeviceAddr);
            var myType = Common.LocalDevice.Current.GetMyDeviceEnumInfo(listDevice);
            if (myType.ConcreteType == DeviceConcreteType.Relay_ThreeLoad
                || myType.ConcreteType == DeviceConcreteType.DimmableLight_OneLoad)
            {
                //背光灯
                string caption = Language.StringByID(R.MyInternationalizationString.uBackLight);
                var btnLight = new FrameRowControl(listview.rowSpace / 2);
                listview.AddChidren(btnLight);
                btnLight.AddLeftCaption(caption, 600);
                //底线
                btnLight.AddBottomLine();
                //右箭头
                btnLight.AddRightArrow();
                btnLight.ButtonClickEvent += (sender, e) =>
                {
                    var form = new DeviceRelay.RelayThreeLoadBackLightSettionForm();
                    form.AddForm(deviceObj.DeviceAddr);
                };
            }
        }
        #endregion
        #region ■ 初始化设备回路图标_________________
        /// <summary>
        /// 初始化设备回路图标
        /// </summary>
        private void InitDeviceEpointIcon(VerticalFrameControl listBackControl)
        {
            //获取按组分配的设备
            var listIcon = this.GetAllDeviceGroup();
            var frameBorder = new FrameLayout();
            frameBorder.Y = Application.GetRealHeight(81);
            frameBorder.Gravity = Gravity.CenterHorizontal;
            frameBorder.Width = Application.GetRealWidth(887);
            frameBorder.Height = Application.GetRealHeight(423);
            frameBorder.Radius = (uint)Application.GetRealHeight(6);
            listBackControl.frameTable.AddChidren(frameBorder);
            var frameBack = new FrameLayout();
            frameBack.Height = frameBorder.Height;
            if (listIcon.Count > 2)
            {
                //需要滑动控件
                var listControl = new VerticalScrolViewLayout();
                listControl.Height = frameBorder.Height;
                listBackControl.frameTable.AddChidren(listControl);
                listControl.AddChidren(frameBack);
            }
            else
            {
                //不需要滑动控件
                frameBorder.AddChidren(frameBack);
            }
            int index = 0;
            for (int i = 0; i < listIcon.Count; i++)
            {
                var frame = new FrameLayout();
                frame.Width = this.GetPictrueRealSize(887);
                frame.Height = this.GetPictrueRealSize(124);
                frameBack.AddChidren(frame);
                //初始化一行设备图标
                this.InitDeviceIconFrame(frame, listIcon[i], ref index);
                if (listIcon.Count == 1)
                {
                    frame.Gravity = Gravity.CenterVertical;
                }
                else
                {
                    frame.Y = this.GetPictrueRealSize(58) + (frameBack.ChildrenCount - 1) * (frame.Height + this.GetPictrueRealSize(60));
                }
            }
            if (listIcon.Count > 2)
            {
                //计算真实高度
                int realHeight = this.GetPictrueRealSize(58 + 58) + (frameBack.ChildrenCount - 1) * this.GetPictrueRealSize(124 + 60);
                if (realHeight > frameBack.Height)
                {
                    //变更高度
                    frameBack.Height = realHeight;
                }
            }
        }
        /// <summary>
        /// 初始化一行设备图标
        /// </summary>
        /// <param name="frame"></param>
        /// <param name="listDevice"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        private void InitDeviceIconFrame(FrameLayout frame, List<CommonDevice> listDevice, ref int index)
        {
            NormalViewControl btnTemp = null;
            //间距
            int space = this.GetPictrueRealSize(84);
            int defultXX = (frame.Width - frame.Height * listDevice.Count - (listDevice.Count - 1) * space) / 2;
            for (int i = 0; i < listDevice.Count; i++)
            {
                index++;
                var btnIcon = new NormalViewControl(frame.Height, frame.Height, false);
                btnIcon.Name = "btn" + listDevice[i].DeviceEpoint;
                btnIcon.X = defultXX + i * (frame.Height + space);
                btnIcon.Radius = (uint)frame.Height / 2;
                btnIcon.BorderWidth = 2;
                btnIcon.BorderColor = 0xff979797;
                btnIcon.TextAlignment = TextAlignment.Center;
                btnIcon.TextColor = UserCenterColor.Current.TextGrayColor3;
                btnIcon.TextSize = 13;
                btnIcon.Text = "Dry" + index;
                frame.AddChidren(btnIcon);
                //点击事件
                btnIcon.ButtonClickEvent += this.DeviceEpointSelectEvent;
                if (listDevice[i].DeviceEpoint == deviceObj.DeviceEpoint)
                {
                    btnTemp = btnIcon;
                }
            }
            if (btnTemp != null)
            {
                //初始化菜单行
                this.DeviceEpointSelectEvent(btnTemp, null);
            }
        }
        /// <summary>
        /// 获取按组分配的设备
        /// </summary>
        /// <returns></returns>
        private List<List<CommonDevice>> GetAllDeviceGroup()
        {
            bool hadDevice = false;
            var listDevice = Common.LocalDevice.Current.GetDevicesByMac(deviceObj.DeviceAddr);
            var listRelay = new List<CommonDevice>();
            foreach (var device in listDevice)
            {
                if (device.Type == DeviceType.OnOffSwitch)
                {
                    //只要干接点
                    listRelay.Add(device);
                    if (device.DeviceEpoint == deviceObj.DeviceEpoint)
                    {
                        //能够匹配得到这个回路
                        hadDevice = true;
                    }
                }
            }
            //排序
            listRelay.Sort((obj1, obj2) =>
            {
                if (obj1.DeviceEpoint > obj2.DeviceEpoint)
                {
                    return 1;
                }
                return -1;
            });
            if (hadDevice == false)
            {
                //如果匹配不到这个回路,则默认第一个
                this.deviceObj = listRelay[0];
            }
            //从下往上4个为一组
            var listIcon = new List<List<CommonDevice>>();
            var listTemp = new List<CommonDevice>();
            for (int i = listRelay.Count - 1; i >= 0; i--)
            {
                listTemp.Insert(0, listRelay[i]);
                if (listTemp.Count == 4)
                {
                    listIcon.Insert(0, listTemp);
                    listTemp = new List<CommonDevice>();
                }
            }
            if (listTemp.Count > 0)
            {
                listIcon.Insert(0, listTemp);
            }
            return listIcon;
        }
        #endregion
        #region ■ 回路选择___________________________
        /// <summary>
        /// 回路选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeviceEpointSelectEvent(object sender, MouseEventArgs e)
        {
            var nowContr = (NormalViewControl)sender;
            if (nowSelectControl != null)
            {
                //同一个东西,不鸟他
                if (nowSelectControl.Name == nowContr.Name)
                {
                    return;
                }
                //不选择状态
                nowSelectControl.BorderColor = 0xff979797;
                nowSelectControl.TextSize = 13;
                nowSelectControl.TextColor = UserCenterColor.Current.TextGrayColor3;
                //记录它的名字
                dicDeviceSaveName[nowSelectDevice.DeviceEpoint] = btnDeviceName.Text.Trim();
            }
            //选择状态
            nowContr.BorderColor = 0xfffb744a;
            nowContr.TextSize = 14;
            nowContr.TextColor = UserCenterColor.Current.TextColor1;
            nowSelectControl = nowContr;
            nowSelectDevice = Common.LocalDevice.Current.GetDevice(deviceObj.DeviceAddr, Convert.ToInt32(nowContr.Name.Replace("btn", string.Empty)));
            //重新初始化菜单行
            this.InitMenuRow();
        }
        #endregion
    }
}