陈嘉乐
2020-05-19 0e0584b87851c56745bf73b1a961b80613dd0721
ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceListMainForm.cs
@@ -30,10 +30,6 @@
        /// </summary>
        private Dictionary<string, DeviceObjRowInfo> dicRowInfo = new Dictionary<string, DeviceObjRowInfo>();
        /// <summary>
        /// 重新获取设备的在线状态
        /// </summary>
        private bool reGetDeviceOnlineStatu = false;
        /// <summary>
        /// 当前正在操作的设备对象Mac地址(设备信息编辑界面用)
        /// </summary>
        private string nowActionDeviceMac = null;
@@ -65,15 +61,28 @@
            //初始化中部控件
            this.InitMiddleFrame();
            //开启传感器报警监视
            this.StartCheckDeviceAlarm();
            //添加接收设备在线上报的监听
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceListFormReceivePushOnline", ReceiveComandDiv.A设备在线上报, (report) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //接受设备状态
                    this.ReceiveDeviceStatu(report);
                }, ShowErrorMode.NO);
            });
            //开启检测网关在线状态的线程
            HdlGatewayLogic.Current.StartCheckGatewayOnlineThread(this);
        }
        /// <summary>
        /// 初始化中部控件(外部可以调用)
        /// </summary>
        /// <param name="i_reGetDeviceOnlineStatu">重新获取设备的在线状态</param>
        public void InitMiddleFrame(bool i_reGetDeviceOnlineStatu = true)
        public void InitMiddleFrame()
        {
            this.reGetDeviceOnlineStatu = i_reGetDeviceOnlineStatu;
            //清空bodyFrame
            this.ClearBodyFrame();
@@ -125,6 +134,9 @@
            frame.AddChidren(listView);
            listView.BeginHeaderRefreshingAction += () =>
            {
                //刷新设备的话,主页需要重新刷新
                UserView.UserPage.Instance.RefreshForm = true;
                HdlThreadLogic.Current.RunThread(() =>
                {
                    //下拉刷新
@@ -348,8 +360,7 @@
            //右箭头
            rowDevice.frameTable.AddRightArrow();
            //在线状态
            rowDevice.isOnline = Common.LocalDevice.Current.CheckDeviceIsOnline(device);
            rowDevice.IsOnline = rowInfo.MenuRow.IsOnline;
            //保存控件
            string maikey = Common.LocalDevice.Current.GetDeviceMainKeys(device);
            rowInfo.dicDetailRow[maikey] = rowDevice;
@@ -470,7 +481,7 @@
                {
                    //方悦面板的功能配置
                    var form = new DevicePanel.PanelFangyueFunctionSettionForm();
                    form.AddForm(device);
                    form.AddForm(device, deviceEnumInfo);
                }
                else
                {
@@ -646,7 +657,7 @@
                if (result != -1)
                {
                    //重新刷新界面
                    this.InitMiddleFrame(false);
                    this.InitMiddleFrame();
                }
            });
        }
@@ -676,9 +687,10 @@
                    if (this.gatewayViewRow != null && this.gatewayViewRow.zbGatewayId == selectGwId)
                    {
                        bool online = HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(zbGateway);
                        this.gatewayViewRow.RefreshControl();
                        //开启设备在线线程
                        this.StartDeviceListControlThread(online);
                        //刷新控件在线状态
                        this.gatewayViewRow?.RefreshOnlineStatu();
                        //根据网关在线状态刷新设备在线状态
                        this.RefreshDeviceOnlineStatuByGatewayOnline(online);
                    }
                });
            });
@@ -699,7 +711,14 @@
            {
                Application.RunOnMainThread(() =>
                {
                    this.gatewayViewRow?.RefreshControl();
                    //相同的状态,不需要再次刷新
                    if (this.gatewayViewRow.isOnline != online)
                    {
                        //根据网关在线状态刷新设备在线状态
                        this.RefreshDeviceOnlineStatuByGatewayOnline(online);
                    }
                    //刷新控件在线状态
                    this.gatewayViewRow?.RefreshOnlineStatu();
                });
            }
        }
@@ -709,52 +728,43 @@
        #region ■ 设备在线检测_______________________
        /// <summary>
        /// 开启设备在线线程
        /// 根据网关在线状态刷新设备在线状态
        /// </summary>
        /// <param name="gatewayOnline">网关的在线状态</param>
        private void StartDeviceListControlThread(bool gatewayOnline)
        private void RefreshDeviceOnlineStatuByGatewayOnline(bool gatewayOnline)
        {
            if (gatewayOnline == false)
            {
                //设置全部设备离线
                this.SetAllDeviceOffLine();
                return;
            }
            //开启传感器报警监视
            this.StartCheckDeviceAlarm();
            //开启设备在线监测
            this.StartCheckDeviceOnline();
            else
            {
                //刷新设备在线状态
                this.RefreshDeviceOnlineStatu();
            }
        }
        /// <summary>
        /// 开启设备在线监测
        /// 刷新设备在线状态
        /// </summary>
        private void StartCheckDeviceOnline()
        private void RefreshDeviceOnlineStatu()
        {
            //外部调用的话,不再重新获取设备状态
            if (this.reGetDeviceOnlineStatu == false)
            {
                return;
            }
            //添加接收设备在线上报的监听
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceListFormReceivePushOnline", ReceiveComandDiv.A设备在线上报, this.ReceiveDeviceStatu);
            string gwId = GatewayResourse.NowSelectGatewayId;
            HdlThreadLogic.Current.RunThread(() =>
            {
                System.Threading.Thread.Sleep(2000);
                //这里主要只是获取在线状态
                var zbway = HdlGatewayLogic.Current.GetLocalGateway(gwId);
                int statu = 0;
                var list = LocalDevice.Current.GetDeviceListFromGateway(zbway, ref statu, false, ShowErrorMode.NO);
                if (statu != -1)
                var list = LocalDevice.Current.GetDeviceOnlineList(gwId);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        //设置设备在线状态
                        this.ReceiveDeviceStatu(list[i]);
                    }
                }
                }, ShowErrorMode.NO);
            });
        }
@@ -779,29 +789,22 @@
            if (localDevice != null)
            {
                //在线状态一样的话,不需要刷新
                if (localDevice.IsOnline == device.IsOnline)
                if (localDevice.IsOnline != device.IsOnline)
                {
                    return;
                    //保存状态
                    localDevice.IsOnline = device.IsOnline;
                    localDevice.ReSave();
                }
                //保存状态
                localDevice.IsOnline = device.IsOnline;
                localDevice.ReSave();
            }
            HdlThreadLogic.Current.RunMain(() =>
            rowInfo.MenuRow.IsOnline = Common.LocalDevice.Current.CheckDeviceIsOnline(device);
            if (rowInfo.dicDetailRow != null)
            {
                if (rowInfo != null)
                foreach (var detailRow in rowInfo.dicDetailRow.Values)
                {
                    rowInfo.MenuRow.isOnline = Common.LocalDevice.Current.CheckDeviceIsOnline(device);
                    if (rowInfo.dicDetailRow != null)
                    {
                        foreach (var detailRow in rowInfo.dicDetailRow.Values)
                        {
                            detailRow.isOnline = rowInfo.MenuRow.isOnline;
                        }
                    }
                    detailRow.IsOnline = rowInfo.MenuRow.IsOnline;
                }
            });
            }
        }
        /// <summary>
@@ -809,17 +812,20 @@
        /// </summary>
        private void SetAllDeviceOffLine()
        {
            lock (dicRowInfo)
            foreach (var rowInfo in this.dicRowInfo.Values)
            {
                foreach (var rowInfo in this.dicRowInfo.Values)
                //菜单
                if (rowInfo.MenuRow != null)
                {
                    Application.RunOnMainThread(() =>
                    rowInfo.MenuRow.IsOnline = false;
                }
                //明细
                if (rowInfo.dicDetailRow != null)
                {
                    foreach (var detailRow in rowInfo.dicDetailRow.Values)
                    {
                        if (rowInfo.MenuRow != null)
                        {
                            rowInfo.MenuRow.isOnline = false;
                        }
                    });
                        detailRow.IsOnline = false;
                    }
                }
            }
        }
@@ -905,7 +911,7 @@
        #region ■ 实现外部调用_______________________
        /// <summary>
        /// 添加新的设备到界面桌布中
        /// 添加新的设备到界面桌布中(外部调用)
        /// </summary>
        /// <param name="deviceAddr">设备Mac地址</param>
        public void AddDeviceToFormTable(string deviceAddr)
@@ -933,6 +939,30 @@
                //创建新的行
                this.AddDeviceMenuRow(deviceAddr);
                //记录当前的正在操作的设备的Mac地址
                this.nowActionDeviceMac = deviceAddr;
            }
        }
        /// <summary>
        /// 刷新指定设备行的信息(外部调用)
        /// </summary>
        /// <param name="deviceAddr">设备Mac地址</param>
        public void RefreshDeviceRow(string deviceAddr)
        {
            if (this.dicRowInfo.ContainsKey(deviceAddr) == true)
            {
                var row = this.dicRowInfo[deviceAddr];
                row.MenuRow?.RefreshControlInfo(true);
                //明细
                if (row.dicDetailRow != null)
                {
                    foreach (var detailRow in row.dicDetailRow.Values)
                    {
                        detailRow.RefreshControlInfo();
                    }
                }
            }
        }
@@ -1082,6 +1112,10 @@
            /// 网关名字控件
            /// </summary>
            private NormalViewControl btnName = null;
            /// <summary>
            /// 在线状态
            /// </summary>
            public bool isOnline = false;
            /// <summary>
            /// 自定义网关控件
@@ -1124,7 +1158,9 @@
                if (zbway != null)
                {
                    btnName.Text = HdlGatewayLogic.Current.GetGatewayName(zbway);
                    if (HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(zbway) == false)
                    //在线状态
                    this.isOnline = HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(zbway);
                    if (this.isOnline == false)
                    {
                        btnName.TextColor = UserCenterColor.Current.TextGrayColor1;
                    }
@@ -1176,13 +1212,34 @@
                    return;
                }
                btnName.Text = HdlGatewayLogic.Current.GetGatewayName(zbway);
                if (HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(zbway) == false)
                //刷新控件在线状态
                this.RefreshOnlineStatu();
            }
            /// <summary>
            /// 刷新控件在线状态
            /// </summary>
            public void RefreshOnlineStatu()
            {
                //获取本地网关对象
                ZbGateway zbway = HdlGatewayLogic.Current.GetLocalGateway(zbGatewayId);
                if (zbway == null)
                {
                    btnName.TextColor = UserCenterColor.Current.TextGrayColor1;
                    return;
                }
                else
                //在线状态
                bool statu = HdlGatewayLogic.Current.CheckGatewayOnlineByFlag(zbway);
                if (this.isOnline != statu)
                {
                    btnName.TextColor = UserCenterColor.Current.TextColor1;
                    this.isOnline = statu;
                    if (this.isOnline == false)
                    {
                        btnName.TextColor = UserCenterColor.Current.TextGrayColor1;
                    }
                    else
                    {
                        btnName.TextColor = UserCenterColor.Current.TextColor1;
                    }
                }
            }
        }