CarInfo.xaml.cs 19.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
using HHECS.Application.Enums;
using HHECS.BLL.EquipmentExcute.RGV;
using HHECS.BllModel;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.Model.Enums.Car;
using HHHECS.Model.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace HHECS.WinCommon.Controls
{
    /// <summary>
    /// Car.xaml 的交互逻辑
    /// </summary>
    public partial class CarInfo : UserControl
    {
        /// <summary>
        ///  复位、开启、删除、切换、设置间距
        /// </summary>
        public event Func<Equipment, BllResult> CommandReset;
        public event Func<Equipment, BllResult> CommandDelete;
        public event Func<Equipment, BllResult> SwitchMode;
        public event Func<Equipment, BllResult> CommandSetGrap;
        public event Func<Equipment, BllResult> CommandFabricSpeed;
        public event Func<Equipment, BllResult> CommandFabricDistance;
        public event Func<Equipment, BllResult> CommandUpdateLocation;
        public event Func<Equipment, BllResult> CommandCharge;
        public event Func<Equipment, BllResult> CommandReSend;
        public event Func<Equipment, BllResult> CommandSetPosition;

        /// <summary>
        /// 小车名称
        /// </summary>
        private string carName = string.Empty;
        /// <summary>
        /// RGV任务类型
        /// </summary>
        private readonly Dictionary<string, string> carActionTypeList;
        /// <summary>
        /// RGV故障
        /// </summary>
        private readonly Dictionary<string, string> carErrorList;
        /// <summary>
        /// RGV载货状态
        /// </summary>
        private readonly Dictionary<string, string> hasPalletList;


        ///// <summary>
        ///// RGV位置
        ///// </summary>
        //private Dictionary<string, string> carPositionList;
        /// <summary>
        /// RGV状态
        /// </summary>
        private readonly Dictionary<string, string> carStatusList;
        /// <summary>
        /// 控制模式
        /// </summary>
        private readonly Dictionary<string, string> controlModeList;

        /// <summary>
        /// 小车PLC数据
        /// </summary>
        public Equipment Self { get; set; }

        //倒料机PLC数据
        //Equipment feeder = null;

        /// <summary>
        /// 错误提示文本
        /// </summary>
        readonly string alarmText = "";

        /// <summary>
        /// 控件显示名
        /// </summary>
        public string ControlName
        {
            get
            {
                return (string)GetValue(ControlNameProperty);
            }
            set
            {
                SetValue(ControlNameProperty, value);
                this.carName = 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(CarInfo), new PropertyMetadata(""));

        public CarInfo()
        {
            InitializeComponent();
            this.lab_Name.SetBinding(Label.ContentProperty, new Binding("ControlName") { Source = this });

            this.carActionTypeList = CommonHelper.EnumListDic<CarActionType>();
            //this.carPositionList = CommonHelper.EnumListDic<CarPosition>();
            this.carStatusList = CommonHelper.EnumListDic<EquipmentStatus>();
            this.controlModeList = CommonHelper.EnumListDic<CarControlMode>();
            this.carErrorList = CommonHelper.EnumListDic<CarError>();
            this.hasPalletList = CommonHelper.EnumListDic<HasPallet>();

            //this.cb_position.ItemsSource = carPositionList.Where(t=> t.Key == "2" || t.Key == "7" || t.Key == "8").ToList();
            //this.cb_position.SelectedValuePath = "Key";
            //this.cb_position.DisplayMemberPath = "Value";
            //this.cb_position.SelectedIndex = 1;

        }

        /// <summary>
        /// 赋值属性
        /// </summary>
        /// <param name="deviceEntity"></param>
        /// <param name="props"></param>
        /// <param name="address"></param>
        public void SetProps(Equipment equipment)
        {
            Self = equipment;
            try
            {
                #region 监控文本赋值

                var hasPallet = Self.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == RGVProps.HasPallet.ToString()).Value;
                if (hasPallet != null)
                {
                    lab_hasPallet.Foreground = Brushes.Blue;
                    lab_hasPallet.Content = this.hasPalletList[hasPallet];
                }
                //小车编码
                var carNo = Self.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == RGVProps.CarNo.ToString()).Value;
                if (!string.IsNullOrEmpty(carNo))
                {
                    lab_carNo.Foreground = Brushes.Blue;
                    lab_carNo.Content = carNo;
                }
                //故障状态
                var carError = Self.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == RGVProps.CarError.ToString()).Value;
                if (!string.IsNullOrEmpty(carNo))
                {
                    lab_carError.Foreground = Brushes.Blue;
                    lab_carError.Content = this.carErrorList[carError];
                }
                //当前行
                var row = Self.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == RGVProps.Row.ToString()).Value;
                if (row != null)
                {
                    lab_Row.Foreground = Brushes.Blue;
                    lab_Row.Content = row;
                }
                // 任务号
                var TaskHeaderID = Self.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == RGVProps.TaskBarcode.ToString()).Value;
                if (TaskHeaderID != null)
                {
                    lab_taskHeaderId.Foreground = Brushes.Blue;
                    lab_taskHeaderId.Content = TaskHeaderID;
                }

                //显示任务类型
                var actionType = Self.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == RGVProps.ActionType.ToString()).Value;
                if (this.carActionTypeList.ContainsKey(actionType))
                {
                    lab_actionType.Content = this.carActionTypeList[actionType];
                }
                //显示设备状态
                var carStatus = Self.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == RGVProps.Status.ToString()).Value;
                if (this.carStatusList.ContainsKey(carStatus))
                {
                    lab_carStatus.Content = this.carStatusList[carStatus];
                }
                //显示控制模式
                var controlMode = Self.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == RGVProps.ControlMode.ToString()).Value;
                if (this.controlModeList.ContainsKey(controlMode))
                {
                    lab_controlMode.Content = this.controlModeList[controlMode];
                }


                var tempProps = Self.EquipmentProps.FindAll(t => t.EquipmentTypePropTemplate.PropType == EquipmentPropType.PLCAlarmAddress.GetIndexString());
                foreach (var item in tempProps)
                {
                    if (item.Value != item.EquipmentTypePropTemplate.MonitorCompareValue)
                    {
                        AddAlarm("报警:" + item.EquipmentTypePropTemplate.Name + " 信息:" + item.EquipmentTypePropTemplate.MonitorFailure, 2);
                    }
                    else
                    {
                        RemoveAlarm("报警:" + item.EquipmentTypePropTemplate.Name + " 信息:" + item.EquipmentTypePropTemplate.MonitorFailure);
                    }
                }

                #endregion

                #region 清除WCS写入的DB

                ////清除【行列层】设置

                //var wcsActionType = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.wcsActionType.ToString());
                //if (wcsActionType.Value == CarActionType.ResetLocation.GetIndexString())
                //{
                //    var wcsRow = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.row.ToString());
                //    if (wcsRow.Value != "0" )
                //    {
                //        var wcsSwitch = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.wcsSwitch.ToString());
                //        wcsSwitch.Value = "0";
                //        wcsActionType.Value = CarActionType.Init.GetIndexString();
                //        wcsRow.Value = "0";
                //        CommandUpdateLocation?.Invoke(Self);
                //    }
                //}
                ////清除【位置】设置
                //if (wcsActionType.Value == CarActionType.ResetPosition.GetIndexString())
                //{
                //    var wcsPosition = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "wcsPosition");
                //    if (wcsPosition.Value != "0")
                //    {
                //        var wcsSwitch = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.wcsSwitch.ToString());
                //        wcsSwitch.Value = "0";
                //        wcsActionType.Value = CarActionType.Init.GetIndexString();
                //        wcsPosition.Value = "0";
                //        CommandSetPosition?.Invoke(Self);
                //    }
                //}

                //清除【复位】设置
                var wcsResetCommand = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.WCSResetCommand.ToString());
                if (wcsResetCommand.Value != "0")
                {
                    wcsResetCommand.Value = CarResetCommand.初始.GetIndexString();
                    CommandReset?.Invoke(Self);
                }
                //清除【切换模式】设置
                var wcsSwitchMode = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.WCSSwitchMode.ToString());
                if (wcsSwitchMode.Value != "0")
                {
                    wcsSwitchMode.Value = CarSwitchMode.初始.GetIndexString();
                    SwitchMode?.Invoke(Self);
                }
                #endregion

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                //Logger.Log($"小车监控界面异常:{ex.Message}", LogLevel.Exception, ex);
            }
        }


        /// <summary>
        /// 删除报警
        /// </summary>
        /// <param name="v"></param>
        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);
        }



        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_CommandDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show($"是否确认删除任务?", "注意", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Button button = (Button)sender;
                if (Self != null)
                {
                    //var carStatus = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "carStatus");
                    //if (carStatus.Value != CarStatus.idle.GetIndexString())
                    //{
                    //    MessageBox.Show("小车状态不是空闲,不能设置!");
                    //    return;
                    //} 
                    var props = Self.EquipmentProps;
                    var wcsDeleteCommand = props.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.WCSActionType.ToString());
                    wcsDeleteCommand.Value = CarActionType.TaskDelete.GetIndexString();
                    var temp = CommandDelete?.Invoke(Self);
                    if (temp == null)
                    {
                        MessageBox.Show($"未处理{button.Content}事件");
                    }
                    else
                    {
                        if (temp.Success)
                        {
                            MessageBox.Show($"{button.Content}操作成功:{temp.Msg}");
                        }
                        else
                        {
                            MessageBox.Show($"{button.Content}操作失败:{temp.Msg}");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("操作失败,通信中断,请重试!");
                }
            }
        }



        /// <summary>
        /// 控制模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_controlMode_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            if (Self != null)
            {
                //var carStatus = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.carStatus.ToString());
                //if (carStatus.Value != CarStatus.idle.GetIndexString())
                //{
                //    MessageBox.Show("小车状态不是空闲,不能设置!");
                //    return;
                //}
                var wcsSwitchMode = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.WCSSwitchMode.ToString());
                if (wcsSwitchMode != null)
                {
                    wcsSwitchMode.Value = CarSwitchMode.切换模式.GetIndexString();
                    var temp = SwitchMode?.Invoke(Self);
                    if (temp == null)
                    {
                        MessageBox.Show($"未处理{button.Content}事件");
                    }
                    else
                    {
                        if (temp.Success)
                        {
                            MessageBox.Show($"{button.Content}操作成功:{temp.Msg}");
                        }
                        else
                        {
                            MessageBox.Show($"{button.Content}操作失败:{temp.Msg}");
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("操作失败,通信中断,请重试!");
            }
        }

        /// <summary>
        /// 复位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Reset_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            if (Self != null)
            {
                //var carStatus = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.carStatus.ToString());
                //if (carStatus.Value != CarStatus.idle.GetIndexString())
                //{
                //    MessageBox.Show("小车状态不是空闲,不能设置!");
                //    return;
                //}
                var wcsResetCommand = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.WCSResetCommand.ToString());
                if (wcsResetCommand != null)
                {
                    wcsResetCommand.Value = CarResetCommand.故障复位.GetIndexString();
                    var temp = CommandReset?.Invoke(Self);
                    if (temp == null)
                    {
                        MessageBox.Show($"未处理{button.Content}事件");
                    }
                    else
                    {
                        if (temp.Success)
                        {
                            MessageBox.Show($"{button.Content}操作成功:{temp.Msg}");
                        }
                        else
                        {
                            MessageBox.Show($"{button.Content}操作失败:{temp.Msg}");
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("操作失败,通信中断,请重试!");
            }
        }

        /// <summary>
        /// 任务重发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_ReSend_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            if (Self != null)
            {
                //var carStatus = Self.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == RGVProps.carStatus.ToString());
                //if (carStatus.Value != CarStatus.idle.GetIndexString())
                //{
                //    MessageBox.Show("小车状态不是空闲,不能设置!");
                //    return;
                //}
                var temp = CommandReSend?.Invoke(Self);
                if (temp == null)
                {
                    MessageBox.Show($"未处理{button.Content}事件");
                }
                else
                {
                    if (temp.Success)
                    {
                        MessageBox.Show($"{button.Content}操作成功:{temp.Msg}");
                    }
                    else
                    {
                        MessageBox.Show($"{button.Content}操作失败:{temp.Msg}");
                    }
                }
            }
            else
            {
                MessageBox.Show("操作失败,通信中断,请重试!");
            }
        }








    }
}