xm
2020-04-16 6fa9d69da922c8049f5acfcbb9ce9fd26811024c
ZigbeeApp/Shared/Phone/MainPage/Controls/DeviceCard/Base/DeviceCardCommon.cs
New file
@@ -0,0 +1,426 @@
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
using Shared.Phone.UserCenter;
namespace Shared.Phone.MainPage.Controls
{
    /// <summary>
    /// 主页的设备卡片控件底层共通(宽度:458 + 14 * 2 高度:305 + 43)
    /// </summary>
    public class DeviceCardCommon : FrameLayoutControl
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 设备对象
        /// </summary>
        public CommonDevice device = null;
        /// <summary>
        /// 卡片需要被移除的事件
        /// </summary>
        public Action CardNeedRemoveEvent = null;
        /// <summary>
        /// 背景图片控件
        /// </summary>
        private PicViewControl btnBackGroud = null;
        /// <summary>
        /// 设备名称控件
        /// </summary>
        private NormalViewControl btnDeviceName = null;
        /// <summary>
        /// 设备的背景圆圈图控件
        /// </summary>
        private FrameLayout fraDeviceRound = null;
        /// <summary>
        /// 设备图标控件
        /// </summary>
        private IconViewControl btnDeviceIcon = null;
        /// <summary>
        /// 状态文本控件
        /// </summary>
        private NormalViewControl btnStatu = null;
        /// <summary>
        /// 开关控件
        /// </summary>
        private PicViewControl btnSwitch = null;
        /// <summary>
        /// 收集控件
        /// </summary>
        private IconViewControl btnCollect = null;
        /// <summary>
        /// 是否获取网关反馈的结果  0:没有获取得到  1:已经获取得到
        /// </summary>
        private int ResponeResult = 0;
        /// <summary>
        /// 判断是否有开关功能
        /// </summary>
        private bool isHadOpenSwitch = true;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 主页的设备卡片控件底层共通(宽度:458 + 14 * 2 高度:305 + 43)
        /// </summary>
        public DeviceCardCommon()
        {
            //图片真实宽度+图片自身左右空白
            this.Width = HdlControlLogic.Current.GetPictrueRealSize(458 + 14 * 2);
            //图片高度+阴影
            this.Height = HdlControlLogic.Current.GetPictrueRealSize(305 + 43);
            //取消点击特效
            this.UseClickStatu = false;
        }
        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="i_device">指定设备</param>
        public virtual void InitControl(CommonDevice i_device)
        {
            this.device = i_device;
            //背景图片
            this.btnBackGroud = new PicViewControl(this.Width, this.Height, false);
            btnBackGroud.UnSelectedImagePath = "Item/FunctionCardView.png";
            btnBackGroud.SelectedImagePath = "Item/FunctionCardViewSelected.png";
            btnBackGroud.Gravity = Gravity.CenterHorizontal;
            this.AddChidren(btnBackGroud, ChidrenBindMode.BindEventOnly);
            //设备名称控件
            this.btnDeviceName = new NormalViewControl(320, 63, true);
            btnDeviceName.X = HdlControlLogic.Current.GetPictrueRealSize(40);
            btnDeviceName.Y = HdlControlLogic.Current.GetPictrueRealSize(17);
            btnDeviceName.TextSize = 16;
            btnDeviceName.TextColor = UserCenterColor.Current.TextGrayColor1;
            btnDeviceName.SelectedTextColor = UserCenterColor.Current.White;
            btnDeviceName.Text = Common.LocalDevice.Current.GetDeviceEpointName(i_device);
            this.AddChidren(btnDeviceName, ChidrenBindMode.BindEventOnly);
            //收集控件
            this.btnCollect = new IconViewControl(107);
            btnCollect.X = HdlControlLogic.Current.GetPictrueRealSize(350);
            btnCollect.Y = HdlControlLogic.Current.GetPictrueRealSize(12);
            btnCollect.UnSelectedImagePath = "Item/Collection1.png";
            btnCollect.SelectedImagePath = "Item/CollectionSelected1.png";
            this.AddChidren(btnCollect, ChidrenBindMode.NotBind);
            btnCollect.IsSelected = HdlRoomLogic.Current.IsCollectInRoom(i_device);
            btnCollect.ButtonClickEvent += (sender, e) =>
            {
                //状态取反
                btnCollect.IsSelected = !btnCollect.IsSelected;
                if (btnCollect.IsSelected == false)
                {
                    //取消收藏
                    HdlRoomLogic.Current.DeleteLoveDevice(i_device);
                    if (HdlRoomLogic.Current.CurrentRoom.IsLove == true)
                    {
                        //如果当前房间是我的喜爱的话,回调卡片被删除的事件
                        this.CardNeedRemoveEvent?.Invoke();
                    }
                }
                else
                {
                    //添加收藏
                    HdlRoomLogic.Current.AddLoveDevice(i_device);
                }
            };
            //设备的背景圆圈图
            this.fraDeviceRound = new FrameLayout();
            fraDeviceRound.Width = HdlControlLogic.Current.GetPictrueRealSize(124);
            fraDeviceRound.Height = HdlControlLogic.Current.GetPictrueRealSize(124);
            fraDeviceRound.Radius = (uint)HdlControlLogic.Current.GetPictrueRealSize(124) / 2;
            fraDeviceRound.X = HdlControlLogic.Current.GetPictrueRealSize(44);
            fraDeviceRound.Y = HdlControlLogic.Current.GetPictrueRealSize(101);
            fraDeviceRound.BackgroundColor = Common.ZigbeeColor.Current.GXCForFunctionUnSelectedBackgroundColor;
            this.AddChidren(fraDeviceRound, ChidrenBindMode.BindEventOnly);
            //设备图标控件
            this.btnDeviceIcon = new IconViewControl(86);
            btnDeviceIcon.Gravity = Gravity.Center;
            fraDeviceRound.AddChidren(btnDeviceIcon);
            this.ChangedChidrenBindMode(fraDeviceRound, ChidrenBindMode.BindEventOnly);
            //状态文本控件
            this.btnStatu = new NormalViewControl(HdlControlLogic.Current.GetPictrueRealSize(280), HdlControlLogic.Current.GetPictrueRealSize(63), false);
            btnStatu.X = HdlControlLogic.Current.GetPictrueRealSize(46);
            btnStatu.Y = HdlControlLogic.Current.GetPictrueRealSize(236);
            btnStatu.TextColor = UserCenterColor.Current.TextColor1;
            btnStatu.SelectedTextColor = Common.ZigbeeColor.Current.GXCTextSelectedColor;
            btnStatu.IsBold = true;
            this.AddChidren(btnStatu, ChidrenBindMode.BindEventOnly);
            //开关控件(不加入父控件中)
            this.btnSwitch = new PicViewControl(109, 104);
            btnSwitch.X = HdlControlLogic.Current.GetPictrueRealSize(325);
            btnSwitch.Y = HdlControlLogic.Current.GetPictrueRealSize(202);
            btnSwitch.UnSelectedImagePath = "Item/Switch1.png";
            btnSwitch.SelectedImagePath = "Item/SwitchSelected1.png";
            //刷新信息
            this.RefreshControlInfo(i_device);
        }
        #endregion
        #region ■ 添加开关控件_______________________
        /// <summary>
        /// 添加开关控件
        /// </summary>
        /// <returns></returns>
        public PicViewControl AddSwitchControl()
        {
            this.AddChidren(this.btnSwitch, ChidrenBindMode.NotBind);
            return btnSwitch;
        }
        #endregion
        #region ■ 刷新控件状态_______________________
        /// <summary>
        /// 刷新控件状态
        /// </summary>
        /// <param name="i_device">设备对象,不是推送的那个</param>
        public void RefreshControlInfo(CommonDevice i_device)
        {
            bool collect = HdlRoomLogic.Current.IsCollectInRoom(i_device);
            if (btnCollect.IsSelected != collect)
            {
                btnCollect.IsSelected = collect;
            }
            //设备图标也刷新
            Common.LocalDevice.Current.SetDeviceIconToControl2(btnDeviceIcon, i_device);
            //检测设备是否是打开状态
            bool isOpen = this.CheckIsOpenStatu(i_device);
            if (this.isHadOpenSwitch == true && i_device.HadReadDeviceStatu == false)
            {
                //如果这个卡片有开关功能,并且网关还没有回复之前,默认是关闭状态
                //等待网关回复后会重新刷新,不然一直是关闭状态
                isOpen = false;
            }
            this.SetCardOpenStatu(isOpen);
        }
        /// <summary>
        /// 设置卡片状态
        /// </summary>
        /// <param name="isOpen"></param>
        public void SetCardOpenStatu(bool isOpen)
        {
            //设备状态必须刷新
            this.btnStatu.Text = HdlDeviceOtherLogic.Current.GetDeviceStatu(this.device);
            if (this.btnBackGroud.IsSelected == isOpen)
            {
                //状态一样,则不需要处理
                return;
            }
            //背景图片
            this.btnBackGroud.IsSelected = isOpen;
            //设备名称控件
            this.btnDeviceName.IsSelected = isOpen;
            this.btnDeviceName.IsBold = isOpen;
            //设备的背景圆圈图
            if (isOpen == true)
            {
                fraDeviceRound.BackgroundColor = Common.ZigbeeColor.Current.GXCForFunctionBackgroundColor;
            }
            else
            {
                fraDeviceRound.BackgroundColor = Common.ZigbeeColor.Current.GXCForFunctionUnSelectedBackgroundColor;
            }
            //设备图标控件
            this.btnDeviceIcon.IsSelected = isOpen;
            //状态文本控件
            this.btnStatu.IsSelected = isOpen;
            //开关控件
            if (btnSwitch != null)
            {
                this.btnSwitch.IsSelected = isOpen;
            }
        }
        /// <summary>
        /// 设置卡片的在线状态
        /// </summary>
        /// <param name="isOnline">在线</param>
        public void SetCardOnlineStatu(bool isOnline)
        {
            if (this.isHadOpenSwitch == true)
            {
                //如果这个设备有开关功能,则在线状态不会改变状态
                return;
            }
            this.SetCardOpenStatu(isOnline);
        }
        /// <summary>
        /// 设置设备的状态文本
        /// </summary>
        /// <param name="i_Text">状态文本</param>
        public void SetDeviceStatuText(string i_Text)
        {
            this.btnStatu.Text = i_Text;
        }
        #endregion
        #region ■ 发送获取状态命令___________________
        /// <summary>
        /// 发送获取状态命令
        /// </summary>
        public virtual void SendStatuComand()
        {
            return;
        }
        #endregion
        #region ■ 检测设备打开状态___________________
        /// <summary>
        /// 检测设备是否是打开状态
        /// </summary>
        /// <param name="i_device"></param>
        /// <returns></returns>
        public virtual bool CheckIsOpenStatu(CommonDevice i_device)
        {
            //如果这个函数被重写的话,则代表这个设备有开关功能
            this.isHadOpenSwitch = false;
            //默认用在线状态来判断
            return i_device.IsOnline == 1;
        }
        #endregion
        #region ■ 深度卡片信息的返回事件_____________
        /// <summary>
        /// 深度卡片信息的返回事件(底层专用)
        /// </summary>
        /// <param name="device">设备</param>
        /// <param name="room">房间</param>
        public void CardDetailInfoBackEvent(CommonDevice device, Common.Room room)
        {
            if (HdlRoomLogic.Current.CurrentRoom.IsLove == true)
            {
                //如果是主页取消搜藏的话,回调卡片被删除的函数
                if (HdlRoomLogic.Current.IsCollectInRoom(device) == false)
                {
                    this.CardNeedRemoveEvent?.Invoke();
                    return;
                }
            }
            else
            {
                //如果这个设备改变了房间的话,回调卡片被删除的函数
                var nowRoom = HdlRoomLogic.Current.GetRoomByDevice(device);
                if (nowRoom == null || nowRoom.Id != HdlRoomLogic.Current.CurrentRoom.Id)
                {
                    this.CardNeedRemoveEvent?.Invoke();
                    return;
                }
            }
            //如果它还在这个房间的话,刷新卡片信息
            this.RefreshControlInfo(device);
        }
        #endregion
        #region ■ 是否获取网关反馈的结果_____________
        /// <summary>
        /// <para>检测是否获取网关反馈的结果,如果网关没有回复,则会弹出消息</para>
        /// <para>获取到回复结果之后,记得调用SetHadGetResponeResultStatu()函数设置状态</para>
        /// </summary>
        /// <param name="oldOpenStatu">设备目前的开关状态</param>
        public void StartCheckResponeResult(bool oldOpenStatu)
        {
            this.ResponeResult = 0;
            //开关按钮不能再点击
            if (btnSwitch != null) { this.btnSwitch.CanClick = false; }
            HdlThreadLogic.Current.RunThread(() =>
            {
                int waitime = 30;
                while (waitime > 0)
                {
                    System.Threading.Thread.Sleep(100);
                    if (this.ResponeResult == 1)
                    {
                        //已经获取得到数据
                        break;
                    }
                    waitime--;
                }
                if (waitime <= 0)
                {
                    //没有获取得到结果
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        var msgContr = new ShowMsgControl(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.FAIL));
                        msgContr.Show();
                        //变更回原来的状态
                        this.SetCardOpenStatu(oldOpenStatu);
                    });
                }
                //开关按钮不能再点击
                if (btnSwitch != null) { this.btnSwitch.CanClick = true; }
            });
        }
        /// <summary>
        /// 设置已经获取到网关的反馈结果
        /// </summary>
        public void SetHadGetResponeResultStatu()
        {
            this.ResponeResult = 1;
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 检测能否发送获取状态命令
        /// </summary>
        /// <returns></returns>
        public bool CheckCanSendStatuComand()
        {
            if (this.device.HadReadDeviceStatu == false)
            {
                //还没有读取过数据
                return true;
            }
            if ((DateTime.Now - this.device.LastDateTime).TotalMinutes >= 10)
            {
                //10分钟后可以再次获取
                return true;
            }
            return false;
        }
        #endregion
        #region ■ 控件摧毁___________________________
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveFromParent()
        {
            this.CardNeedRemoveEvent = null;
            base.RemoveFromParent();
        }
        #endregion
    }
}