StockerInfo.xaml.cs 14.7 KB
using HHWCS.Bll;
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
    {
        private List<DeviceAddressEntity> innerAddresses;
        public event Func<LocationEntity,DeviceEntity, BllResult> DoubleInEvent;
        public event Func<DeviceEntity, BllResult> EmptyOutEvent;
        public DeviceEntity Self { get; set; }
        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)
        {
            innerAddresses = address;
            Self = deviceEntity; 

            #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;
                    case "3":
                        lab_OperationModel.Content = "机载操作";
                        break;
                    case "4":
                        lab_OperationModel.Content = "单机自动";
                        break;
                    case "5":
                        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)
        {
            for (int i = list_Alarm.Items.Count-1; i >= 0; i--)
            {
                var a = (TextBlock)list_Alarm.Items[i];
                if (a.Text.Contains(v))
                {
                    list_Alarm.Items.Remove(list_Alarm.Items[i]);
                }
            }
        }

        /// <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);
        }

        private void Btn_DIn_Click(object sender, RoutedEventArgs e)
        {
            if(innerAddresses != null)
            {
                var doubleIn = innerAddresses.Find(t => t.DevicePropCode == "DoubleIn");
                if(doubleIn != null)
                {
                    if (doubleIn.value == "True")
                    {
                        Frm_LocationInput frm_LocationInput = new Frm_LocationInput
                        {
                            DeviceEntity = Self
                        };
                        if (frm_LocationInput.ShowDialog() == true)
                        {
                            LocationEntity locationEntity = frm_LocationInput.NewLocation;
                            BllResult a = DoubleInEvent?.Invoke(locationEntity,Self);
                            if (a == null)
                            {
                                MessageBox.Show("未处理重入事件");
                            }
                            else
                            {
                                if (a.Success)
                                {
                                    MessageBox.Show("重入处理成功");
                                }
                                else
                                {
                                    MessageBox.Show("重入处理失败:" + a.Msg);
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("当前没有重入报警,无需处理");
                    }
                }
                else
                {
                    MessageBox.Show("未找到堆垛机的重入属性");
                }
            }
            else
            {
                MessageBox.Show("无重入需要处理");
            }
        }

        /// <summary>
        /// 空出处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_EOut_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("是否要进行空出处理?", "注意", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                if (innerAddresses != null)
                {
                    var emptyOut = innerAddresses.Find(t => t.DevicePropCode == "EmptyOut");
                    if (emptyOut != null)
                    {
                        if (emptyOut.value == "True")
                        {
                            BllResult temp = EmptyOutEvent?.Invoke(Self);
                            if (temp == null)
                            {
                                MessageBox.Show("未处理空出事件");
                            }
                            else
                            {
                                if (temp.Success)
                                {
                                    MessageBox.Show("空出处理成功");
                                }
                                else
                                {
                                    MessageBox.Show("空出处理失败:" + temp.Msg);
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("当前没有空出报警,无需处理");
                        }
                    }
                    else
                    {
                        MessageBox.Show("未找到堆垛机的空出属性");
                    }
                }
                else
                {
                    MessageBox.Show("无需空出处理");
                }
            }
        }
    }
}