gxc
2019-11-07 a4924de3136289d10cabbf2f61a228387d44ded7
ZigbeeApp/Shared/Phone/UserCenter/Safety/GarrisonAreaExistSensorForm.cs
New file
@@ -0,0 +1,280 @@
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Safety
{
    /// <summary>
    /// 已经设置了的传感器的一览画面
    /// </summary>
    public class GarrisonAreaExistSensorForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
        /// <summary>
        /// 防区ID
        /// </summary>
        private int zoonID = 0;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="SectorsName">防区名字</param>
        /// <param name="i_zoonID">防区ID</param>
        public void ShowForm(string SectorsName, int i_zoonID)
        {
            this.zoonID = i_zoonID;
            //设置头部信息
            base.SetTitleText(SectorsName + Language.StringByID(R.MyInternationalizationString.uSettion));
            //右上角的“+”图标
            var btnTopIcon = new MostRightIconControl(69, 69);
            btnTopIcon.UnSelectedImagePath = "Item/Add.png";
            topFrameLayout.AddChidren(btnTopIcon);
            btnTopIcon.InitControl();
            btnTopIcon.ButtonClickEvent += (sender, e) =>
            {
                //添加新的传感器
                this.AddNewSensorDevice();
            };
            //初始化传感器列表信息
            this.InitSensorDevicesListInfo();
        }
        /// <summary>
        /// 初始化传感器列表信息
        /// </summary>
        private void InitSensorDevicesListInfo()
        {
            //清空bodyFrameLayout
            this.ClearBodyFrame();
            //获取指定防区全部的传感器设备的信息
            var listInfo = HdlSafeguardLogic.Current.GetSensorDevicesInfoByZoonID(this.zoonID);
            if (listInfo.Count == 0)
            {
                //还没有设置传感器哦
                this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.uDoNotHadSettionSensorMsg));
                this.listView = null;
                return;
            }
            listView = new VerticalListControl(29);
            listView.Y = Application.GetRealHeight(-6);
            listView.Height = bodyFrameLayout.Height + Application.GetRealHeight(6);
            listView.BackgroundColor = UserCenterColor.Current.White;
            bodyFrameLayout.AddChidren(listView);
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                int count = listInfo.Count - 1;
                for (int i = 0; i < listInfo.Count; i++)
                {
                    this.AddRowLayout(listInfo[i], i != count);
                }
                //调整列表控件的高度
                this.listView.AdjustRealHeight(Application.GetRealHeight(23));
            });
        }
        #endregion
        #region ■ 添加传感器行_______________________
        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="sensorInfo">传感器信息</param>
        /// <param name="addLine">是否添加底线</param>
        private void AddRowLayout(Safeguard.ZoneDeviceListData sensorInfo, bool addLine)
        {
            CommonDevice device = Common.LocalDevice.Current.GetDevice(sensorInfo.MacAddr, sensorInfo.Epoint);
            if (device == null)
            {
                return;
            }
            //自定义控件
            var rowLayout = new DeviceRoomControl(device, listView.rowSpace / 2);
            listView.AddChidren(rowLayout);
            rowLayout.InitControl();
            rowLayout.frameTable.UseClickStatu = false;
            if (addLine == true)
            {
                //底线
                rowLayout.frameTable.AddBottomLine();
            }
            //旁路状态
            var txtStatu = rowLayout.frameTable.AddMostRightView("", 350);
            txtStatu.TextColor = UserCenterColor.Current.TextGrayColor1;
            //旁路设置
            var btnBypass = new NormalViewControl(Application.GetRealWidth(184), rowLayout.Height, false);
            btnBypass.TextID = R.MyInternationalizationString.uBypass;
            btnBypass.BackgroundColor = 0xff4a4a4a;
            btnBypass.TextColor = UserCenterColor.Current.TextColor3;
            btnBypass.TextAlignment = TextAlignment.Center;
            btnBypass.TextSize = 12;
            if (sensorInfo.IsBypass == 1)
            {
                //已设置旁路
                txtStatu.TextID = R.MyInternationalizationString.uHadSetBypass;
                //取消旁路
                btnBypass.TextID = R.MyInternationalizationString.uCancelBypass;
            }
            rowLayout.AddLeftView(btnBypass);
            btnBypass.ButtonClickEvent += (sender, e) =>
            {
                //设置旁路状态
                this.SetBypassStatu(sensorInfo, device);
            };
            //删除
            var btnDelete = new NormalViewControl(Application.GetRealWidth(184), rowLayout.Height, false);
            btnDelete.TextID = R.MyInternationalizationString.uDelete;
            btnDelete.BackgroundColor = 0xfff75858;
            btnDelete.TextColor = UserCenterColor.Current.TextColor3;
            btnDelete.TextAlignment = TextAlignment.Center;
            btnDelete.TextSize = 12;
            rowLayout.AddRightView(btnDelete);
            btnDelete.ButtonClickEvent += (sender, e) =>
            {
                //确认是否要删除?
                string msg = Language.StringByID(R.MyInternationalizationString.uShowDoDeleteMsg);
                this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                {
                    //删除传感器行
                    this.DeleteRow(rowLayout, device);
                });
            };
        }
        #endregion
        #region ■ 删除传感器行_______________________
        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="rowLayout"></param>
        /// <param name="device"></param>
        private async void DeleteRow(DeviceRoomControl rowLayout, CommonDevice device)
        {
            bool result = await HdlSafeguardLogic.Current.DeleteSensorDevice(this.zoonID, new List<CommonDevice>() { device });
            if (result == false)
            {
                return;
            }
            //从画面中移除
            rowLayout?.RemoveFromParent();
            //调整列表控件的高度
            this.listView.AdjustRealHeight(Application.GetRealHeight(23));
            if (this.listView.ChildrenCount == 0)
            {
                //初始化传感器列表信息
                this.InitSensorDevicesListInfo();
            }
        }
        #endregion
        #region ■ 旁路设置___________________________
        /// <summary>
        /// 设置旁路状态
        /// </summary>
        /// <param name="sensorInfo">传感器信息</param>
        /// <param name="device">传感器设备</param>
        private async void SetBypassStatu(Safeguard.ZoneDeviceListData sensorInfo, CommonDevice device)
        {
            //变更状态
            int statu = sensorInfo.IsBypass == 1 ? 0 : 1;
            bool result = await HdlSafeguardLogic.Current.SetByPassStatuToSafety(this.zoonID, device, statu);
            if (result == false)
            {
                return;
            }
            //变更状态
            sensorInfo.IsBypass = statu;
            this.InitSensorDevicesListInfo();
        }
        #endregion
        #region ■ 添加新的传感器_____________________
        /// <summary>
        /// 添加新的传感器
        /// </summary>
        private void AddNewSensorDevice()
        {
            //获取能够添加的传感器
            var list = this.GetCanAddSensorDevice();
            //显示选择界面
            var form = new SelectDeviceForm();
            form.AddForm(list, new List<string>(), true);
            form.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddSensor));
            //设备选择确定
            form.ActionSelectDeviceEx += (async (listDevice) =>
            {
                if (listDevice.Count == 0)
                {
                    //关闭界面
                    form.CloseForm();
                    return;
                }
                //添加设备到安防
                bool success = await HdlSafeguardLogic.Current.AddSensorDevice(this.zoonID, listDevice);
                if (success == true)
                {
                    //关闭界面
                    form.CloseForm();
                    //刷新列表
                    this.InitSensorDevicesListInfo();
                }
            });
        }
        /// <summary>
        /// 获取能够添加的传感器
        /// </summary>
        /// <returns></returns>
        private List<CommonDevice> GetCanAddSensorDevice()
        {
            List<CommonDevice> listNew = new List<CommonDevice>();
            List<CommonDevice> listDevices = Common.LocalDevice.Current.listAllDevice;
            foreach (CommonDevice device in listDevices)
            {
                //只要传感器
                if (device.Type != DeviceType.IASZone)
                {
                    continue;
                }
                //如果那个设备已经添加了,则不再显示
                if (HdlSafeguardLogic.Current.IsSensorDeviceExist(device) == true)
                {
                    continue;
                }
                listNew.Add(device);
            }
            return listNew;
        }
        #endregion
    }
}