xm
2020-07-21 9a4b76398009cf76c508d61f7e48fb6f5cb7ac2d
ZigbeeApp/Shared/Phone/MainPage/ControlForm/DeviceRelayDetailCardForm.cs
New file
@@ -0,0 +1,306 @@
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.MainPage.ControlForm
{
    /// <summary>
    /// 继电器类型的深度卡片界面(含空气开关)
    /// </summary>
    public class DeviceRelayDetailCardForm : DeviceDetailCardCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 界面上可以操作的控件
        /// </summary>
        private List<ButtonBase> listControl = new List<ButtonBase>();
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 底层初始化中部控件完成之后
        /// </summary>
        /// <param name="frameWhiteBack"></param>
        public override void InitMiddleFrameAfter(FrameLayout frameWhiteBack)
        {
            //先清空
            this.listControl = new List<ButtonBase>();
            if (this.device.DfunctionType == DeviceFunctionType.A开关)
            {
                //初始化开关类型控件
                this.InitSwitchControl(frameWhiteBack);
            }
            else if (this.device.DfunctionType == DeviceFunctionType.A插座)
            {
                //初始化插座类型控件
                this.InitPlugControl(frameWhiteBack);
            }
            else
            {
                //初始化灯光类型控件
                this.InitLightControl(frameWhiteBack);
            }
            //设置状态文字
            this.SetStatuText(HdlDeviceOtherLogic.Current.GetDeviceStatu(this.device));
        }
        #endregion
        #region ■ 初始化灯光类型_____________________
        /// <summary>
        /// 初始化灯光类型控件
        /// </summary>
        /// <param name="frameWhiteBack"></param>
        private void InitLightControl(FrameLayout frameWhiteBack)
        {
            //灯光图片
            var picLight = new PicViewControl(377, 435);
            picLight.Y = Application.GetRealHeight(389);
            picLight.UnSelectedImagePath = "Light/DeskLamp.png";
            picLight.SelectedImagePath = "Light/DeskLampSelected.png";
            picLight.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(picLight);
            listControl.Add(picLight);
            //开关
            var btnSwitch = new IconViewControl(81);
            btnSwitch.UnSelectedImagePath = "Item/Switch.png";
            btnSwitch.SelectedImagePath = "Item/SwitchSelected.png";
            btnSwitch.Y = picLight.Bottom + Application.GetRealHeight(173);
            btnSwitch.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(btnSwitch);
            listControl.Add(btnSwitch);
            //设置初始状态
            if (((LightBase)this.device).OnOffStatus == 1)
            {
                picLight.IsSelected = true;
                btnSwitch.IsSelected = true;
            }
            picLight.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SetSwitchCommand(!picLight.IsSelected);
            };
            btnSwitch.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SetSwitchCommand(!btnSwitch.IsSelected);
            };
        }
        #endregion
        #region ■ 初始化开关类型_____________________
        /// <summary>
        /// 初始化开关类型控件
        /// </summary>
        /// <param name="frameWhiteBack"></param>
        private void InitSwitchControl(FrameLayout frameWhiteBack)
        {
            //开关的背景图片
            var picSwitchBack = new FrameLayout();
            picSwitchBack.Height = this.GetPictrueRealSize(579);
            picSwitchBack.Width = this.GetPictrueRealSize(579);
            picSwitchBack.Y = Application.GetRealHeight(340);
            picSwitchBack.BackgroundImagePath = "Light/OnOff.png";
            picSwitchBack.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(picSwitchBack);
            //关闭
            var btnClose = new IconViewControl(81);
            btnClose.UnSelectedImagePath = "Light/OFF.png";
            btnClose.SelectedImagePath = "Light/OFFSelected.png";
            btnClose.X = this.GetPictrueRealSize(101);
            btnClose.Y = this.GetPictrueRealSize(248);
            picSwitchBack.AddChidren(btnClose);
            listControl.Add(btnClose);
            //打开
            var btnOpen = new IconViewControl(81);
            btnOpen.UnSelectedImagePath = "Light/ON.png";
            btnOpen.SelectedImagePath = "Light/ONSelected.png";
            btnOpen.X = this.GetPictrueRealSize(397);
            btnOpen.Y = btnClose.Y;
            picSwitchBack.AddChidren(btnOpen);
            listControl.Add(btnOpen);
            //设置初始状态
            if (((LightBase)this.device).OnOffStatus == 1)
            {
                btnOpen.IsSelected = true;
            }
            else
            {
                btnClose.IsSelected = true;
            }
            btnClose.ButtonClickEvent += (sender, e) =>
            {
                if (btnClose.IsSelected == true) { return; }
                //发送开关命令
                this.SetSwitchCommand(false);
            };
            btnOpen.ButtonClickEvent += (sender, e) =>
            {
                if (btnOpen.IsSelected == true) { return; }
                //发送开关命令
                this.SetSwitchCommand(true);
            };
        }
        #endregion
        #region ■ 初始化插座类型_____________________
        /// <summary>
        /// 初始化插座类型控件
        /// </summary>
        /// <param name="frameWhiteBack"></param>
        private void InitPlugControl(FrameLayout frameWhiteBack)
        {
            //插座图片
            var picPlug = new PicViewControl(567, 567);
            picPlug.Y = Application.GetRealHeight(334);
            picPlug.UnSelectedImagePath = "Light/Plug.png";
            picPlug.SelectedImagePath = "Light/PlugSelected.png";
            picPlug.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(picPlug);
            listControl.Add(picPlug);
            //开关
            var btnSwitch = new IconViewControl(81);
            btnSwitch.UnSelectedImagePath = "Item/Switch.png";
            btnSwitch.SelectedImagePath = "Item/SwitchSelected.png";
            btnSwitch.Y = picPlug.Bottom + Application.GetRealHeight(89);
            btnSwitch.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(btnSwitch);
            listControl.Add(btnSwitch);
            //设置初始状态
            if (((LightBase)this.device).OnOffStatus == 1)
            {
                picPlug.IsSelected = true;
                btnSwitch.IsSelected = true;
            }
            picPlug.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SetSwitchCommand(!picPlug.IsSelected);
            };
            btnSwitch.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SetSwitchCommand(!btnSwitch.IsSelected);
            };
        }
        #endregion
        #region ■ 刷新开关状态_______________________
        /// <summary>
        /// 刷新开关状态
        /// </summary>
        /// <param name="isOpen">打开状态</param>
        private void RefreshSwitchStatu(bool isOpen)
        {
            //变更状态
            if (this.device.DfunctionType == DeviceFunctionType.A开关)
            {
                //开状态
                listControl[1].IsSelected = isOpen;
                //关状态 取反开状态
                listControl[0].IsSelected = !isOpen;
            }
            else
            {
                //开关以外
                listControl[0].IsSelected = isOpen;
                listControl[1].IsSelected = isOpen;
            }
            //设置状态文字
            this.SetStatuText(HdlDeviceOtherLogic.Current.GetDeviceStatu(this.device));
        }
        #endregion
        #region ■ 是否获取网关反馈的结果_____________
        /// <summary>
        /// 检测网关的反馈结果(属性上报的对象:device.DeviceStatusReport)
        /// </summary>
        /// <param name="comandDiv">命令区分</param>
        /// <param name="report">上报数据</param>
        /// <returns></returns>
        public override bool CheckResponeResultStatu(ReceiveComandDiv comandDiv, CommonDevice report)
        {
            if (comandDiv == ReceiveComandDiv.A设备属性上报)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //变更卡片状态
                    this.RefreshSwitchStatu(((LightBase)this.device).OnOffStatus == 1);
                });
                return true;
            }
            return false;
        }
        #endregion
        #region ■ 发送开关命令_______________________
        /// <summary>
        /// 发送开关命令
        /// </summary>
        /// <param name="isOpen"></param>
        private void SetSwitchCommand(bool isOpen)
        {
            //如果住宅为虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                ((LightBase)this.device).OnOffStatus = isOpen == true ? 1 : 0;
                //变更卡片状态
                this.RefreshSwitchStatu(((LightBase)this.device).OnOffStatus == 1);
                return;
            }
            //检测是否获取网关反馈的结果,如果网关没有回复,则会弹出消息
            this.StartCheckResponeResult(this.listControl, (result) =>
            {
                //接收到网关回复
                if (result == true)
                {
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //变更卡片状态
                        bool statu = ((LightBase)this.device).OnOffStatus == 1;
                        this.RefreshSwitchStatu(statu);
                    });
                }
            });
            if (isOpen == true)
            {
                //打开
                this.device.SwitchControl(1);
            }
            else
            {
                //关闭
                this.device.SwitchControl(0);
            }
        }
        #endregion
    }
}