StockerInfo.xaml.cs 10.1 KB
using HHWCS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HHWCSHost.Controls
{
    /// <summary>
    /// StockerInfo.xaml 的交互逻辑
    /// </summary>
    public partial class StockerInfo : UserControl
    {
        public string ControlName
        {
            get { return (string)GetValue(ControlNameProperty); }
            set { SetValue(ControlNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ControlName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ControlNameProperty =
            DependencyProperty.Register("ControlName", typeof(string), typeof(StockerInfo), new PropertyMetadata(""));


        public StockerInfo(int maxW, int maxH)
        {
            InitializeComponent();
            this.Width = maxW;
            this.Height = maxH;
            lab_Name.SetBinding(Label.ContentProperty, new Binding("ControlName") { Source = this });
        }

        /// <summary>
        /// 赋值属性
        /// </summary>
        /// <param name="deviceEntity"></param>
        /// <param name="props"></param>
        /// <param name="address"></param>
        public void SetProps(DeviceEntity deviceEntity, List<DevicePropEntity> props, List<DeviceAddressEntity> address)
        {
            #region 固定属性赋值
            var taskValidate = address.Find(t => t.DevicePropCode == "TaskValidate");
            if (taskValidate != null)
            {
                lab_TaskValidate.Foreground = Brushes.Blue;
                switch (taskValidate.value)
                {
                    case "1":
                        lab_TaskValidate.Content = "正确";
                        break;
                    case "2":
                        lab_TaskValidate.Content = "错误";
                        lab_TaskValidate.Foreground = Brushes.Red;
                        break;
                    default:
                        lab_TaskValidate.Content = "未获取或不识别";
                        lab_TaskValidate.Foreground = Brushes.Red;
                        break;
                }
            }

            var workModel = address.Find(t => t.DevicePropCode == "WorkModel");
            if (workModel != null)
            {
                lab_WorkModel.Foreground = Brushes.Blue;
                switch (workModel.value)
                {
                    case "1":
                        lab_WorkModel.Content = "脱机";
                        break;
                    case "2":
                        lab_WorkModel.Content = "联机";
                        break;
                    default:
                        lab_WorkModel.Content = "未获取或不识别";
                        lab_WorkModel.Foreground = Brushes.Red;
                        break;
                }
            }

            var operationModel = address.Find(t => t.DevicePropCode == "OperationModel");
            if (operationModel != null)
            {
                lab_OperationModel.Foreground = Brushes.Blue;
                switch (operationModel.value)
                {
                    case "1":
                        lab_OperationModel.Content = "脱机";
                        break;
                    case "2":
                        lab_OperationModel.Content = "联机";
                        break;
                    default:
                        lab_OperationModel.Content = "未获取或不识别";
                        lab_OperationModel.Foreground = Brushes.Red;
                        break;
                }
            }

            var workStatus = address.Find(t => t.DevicePropCode == "WorkStatus");
            if (workStatus != null)
            {
                lab_WorkStatus.Foreground = Brushes.Blue;
                switch (workStatus.value)
                {
                    case "0":
                        lab_WorkStatus.Content = "待机";
                        break;
                    case "1":
                        lab_WorkStatus.Content = "入库";
                        break;
                    case "2":
                        lab_WorkStatus.Content = "出库";
                        break;
                    case "3":
                        lab_WorkStatus.Content = "拣选";
                        break;
                    case "4":
                        lab_WorkStatus.Content = "转库";
                        break;
                    case "5":
                        lab_WorkStatus.Content = "双重入库";
                        break;
                    case "6":
                        lab_WorkStatus.Content = "回库台位(入库准备)";
                        break;
                    case "7":
                        lab_WorkStatus.Content = "召回";
                        break;
                    case "8":
                        lab_WorkStatus.Content = "急停";
                        lab_WorkStatus.Foreground = Brushes.Red;
                        break;
                    default:
                        lab_WorkStatus.Content = "未获取或不识别";
                        lab_WorkStatus.Foreground = Brushes.Red;
                        break;
                }
            }

            var taskExcuteStatus = address.Find(t => t.DevicePropCode == "TaskExcuteStatus");
            if (taskExcuteStatus != null)
            {
                lab_TaskExcuteStatus.Foreground = Brushes.Blue;
                switch (taskExcuteStatus.value)
                {
                    case "1":
                        lab_TaskExcuteStatus.Content = "待机";
                        break;
                    case "2":
                        lab_TaskExcuteStatus.Content = "任务执行中";
                        break;
                    case "3":
                        lab_TaskExcuteStatus.Content = "任务完成";
                        break;
                    case "4":
                        lab_TaskExcuteStatus.Content = "任务中断(出错)";
                        lab_TaskExcuteStatus.Foreground = Brushes.Red;
                        break;
                    default:
                        lab_TaskExcuteStatus.Content = "未获取或不识别";
                        lab_TaskExcuteStatus.Foreground = Brushes.Red;
                        break;
                }
            }

            var taskNo1 = address.Find(t => t.DevicePropCode == "TaskNo1");
            if (taskNo1 != null)
            {
                lab_TaskNo1.Content = taskNo1.value;              
            }

            var taskNo2 = address.Find(t => t.DevicePropCode == "TaskNo2");
            if (taskNo2 != null)
            {
                lab_TaskNo2.Content = taskNo2.value;
            }

            var currentColumn = address.Find(t => t.DevicePropCode == "CurrentColumn");
            if (currentColumn != null)
            {
                lab_CurrentColumn.Content = currentColumn.value;
            }

            var currentLayer = address.Find(t => t.DevicePropCode == "CurrentLayer");
            if (currentLayer != null)
            {
                lab_CurrentLayer.Content = currentLayer.value;
            }

            var horizontalDistance = address.Find(t => t.DevicePropCode == "HorizontalDistance");
            if (horizontalDistance != null)
            {
                lab_HorizontalDistance.Content = horizontalDistance.value;
            }

            var verticalDistance = address.Find(t => t.DevicePropCode == "VerticalDistance");
            if (verticalDistance != null)
            {
                lab_VerticalDistance.Content = verticalDistance.value;
            }


            #endregion

            //监控属性赋值
            var tempProps = props.FindAll(t => t.DeviceTypeId == deviceEntity.DeviceTypeId && t.IsMonitor == 1);
            foreach (var item in tempProps)
            {
                var temp = address.Find(t => t.DevicePropCode == item.Code);
                if (temp.value != item.MonitorCompareValue)
                {
                    AddAlarm("报警:" + item.Name + " 信息:" + item.MonitorFailure, 2);
                }
                else
                {
                    RemoveAlarm("报警:" + item.Name + " 信息:" + item.MonitorFailure);
                }
            }
        }

        private void RemoveAlarm(string v)
        {
            foreach (var item in list_Alarm.Items)
            {
                var a = (TextBlock)item;
                if (a.Text.Contains(v))
                {
                    list_Alarm.Items.Remove(item);
                }
            }
        }

        /// <summary>
        /// 添加报警
        /// </summary>
        /// <param name="log"></param>
        /// <param name="level">1显示绿色,2显示红色</param>
        public void AddAlarm(string log, int level)
        {
            //先找存不存在
            foreach (var item in list_Alarm.Items)
            {
                var a = (TextBlock)item;
                if (a.Text.Contains(log))
                {
                    //存在就不再次添加
                    return;
                }
            }
            TextBlock textBlock = new TextBlock
            {
                Text = DateTime.Now.ToLongTimeString()+":" + log
            };
            switch (level)
            {
                case 1:
                    textBlock.Background = Brushes.Green;
                    break;
                case 2:
                    textBlock.Background = Brushes.Red;
                    break;
            }
            textBlock.MaxWidth = this.Width;
            textBlock.TextWrapping = TextWrapping.Wrap;
            this.list_Alarm.Items.Add(textBlock);
            this.list_Alarm.SelectedIndex = this.list_Alarm.Items.Count - 1;
            this.list_Alarm.ScrollIntoView(this.list_Alarm.SelectedItem);
        }
    }
}