Пример #1
0
        private Action <DragThumb, FmsAssetTagSetting> _showInfo; //显示界面信息

        private void ThreadShowWorkState(object obj)
        {
            //状态颜色 0:其他状态=脱机的颜色;1:工作状态颜色;2:故障状态颜色;3:待机状态颜色;4:急停状态颜色
            Brush[] StateColors = new Brush[5]
            {
                Brushes.Gray, Brushes.Green, Brushes.OrangeRed, Brushes.DarkGoldenrod, Brushes.DarkRed
            };

            #region 创建显示信息的函数,提高效率

            _showInfo = (thumb, tag) =>
            {
                if ((thumb == null) || (tag == null))
                {
                    return;
                }

                if (tag.STATE_MARK_TYPE == 0) //普通内容
                {
                    thumb.Text = tag.TAG_VALUE_NAME + " " + tag.CUR_VALUE + " " + tag.VALUE_UNIT;
                }
                else if (tag.STATE_MARK_TYPE == 1) //联机状态
                {
                    int state = SafeConverter.SafeToInt(tag.CUR_VALUE, 0);
                    if (state <= 4) //状态
                    {
                        thumb.Background  = StateColors[state];
                        thumb.BorderBrush = StateColors[state];
                    }
                }
                else if (tag.STATE_MARK_TYPE == 2) //待机状态
                {
                    int state = SafeConverter.SafeToInt(tag.CUR_VALUE, 0);
                    if (state == 2)
                    {
                        state = 3;
                    }

                    if (state <= 4) //状态
                    {
                        thumb.Background  = StateColors[state];
                        thumb.BorderBrush = StateColors[state];
                    }
                }
                else if (tag.STATE_MARK_TYPE == 4) //故障状态
                {
                    int state = SafeConverter.SafeToInt(tag.CUR_VALUE, -1);
                    if (state == 1) //故障,整个产线报警
                    {
                        thumb.Background  = StateColors[2];
                        thumb.BorderBrush = StateColors[2];
                    }
                    else if (state == 0) //正常
                    {
                        thumb.Background  = StateColors[1];
                        thumb.BorderBrush = StateColors[1];
                    }
                    else //离线状态
                    {
                        thumb.Background  = StateColors[0];
                        thumb.BorderBrush = StateColors[0];
                    }
                }
                else if (tag.STATE_MARK_TYPE == 10) //状态
                {
                    int state = SafeConverter.SafeToInt(tag.CUR_VALUE, 0);
                    if ((state < 0) || (state >= StateColors.Length))
                    {
                        state = 0;
                    }

                    thumb.Background  = StateColors[state];
                    thumb.BorderBrush = StateColors[state];
                }
            };

            #endregion

            #region 后台执行

            while (!CBaseData.AppClosing)
            {
                if (!bShowInfo)
                {
                    Thread.Sleep(500);
                    continue;
                }

                //显示状态
                try
                {
                    foreach (DragThumb thumb in cvMain.DragThumbs)
                    {
                        if (!bShowInfo)
                        {
                            break;
                        }

                        FmsAssetTagSetting tag = DeviceMonitor.GetTagSettingById(thumb.CtrlName); //查找TagSetting
                        if (tag == null)
                        {
                            continue;
                        }

                        Dispatcher.BeginInvoke(_showInfo, thumb, tag);
                    }
                }
                catch (Exception ex)
                {
                    EventLogger.Log($"!!!!!!实时监控错误,原因:{ex.Message}!!!!!!");
                }

                Thread.Sleep(500);
            }

            #endregion
        }