WinOrganization.xaml.cs 13.6 KB
using RCS.Dal;
using RCS.Communication;
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.Model.PLC;
using RCS.WinClient.Common;
using RCS.WinClient.PLC;
using RCS.WinClient.Service;
using System.Windows;
using System.Windows.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;


namespace RCS.WinClient.Views.Pages
{
    /// <summary>
    /// WinOrganization.xaml 的交互逻辑
    /// </summary>
    public partial class WinOrganization : Window
    {
        private static BaseDal<Base_Agv> _agvDb = new BaseDal<Base_Agv>();
        private static BaseDal<Base_Station> _stationDb = new BaseDal<Base_Station>();

        public WinOrganization()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //重写Closing方法
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        /// <summary>
        /// 拖动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DGrid_AGV.ItemsSource = App.AgvList.OrderBy(a => a.AgvNo).ToList();
            DGrid_Charge.ItemsSource = App.StationList.Where(a => a.StationType == EnumMsg.StationType.充电桩).OrderBy(a => a.Name).ToList();
            DGrid_Station.ItemsSource = App.StationList.Where(a => a.StationType > EnumMsg.StationType.充电桩).OrderBy(a => a.Name).ToList();
            DGrid_MatixManager.ItemsSource = App.PointList;
            DGrid_HuahengStaion.ItemsSource = App.HuahengStaionList;
        }

        /// <summary>
        /// 解锁站台事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_Locked_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //if (DGrid_Station.SelectedItem == null) return;
                //Station station = DGrid_Station.SelectedItem as Station;
                //if (station == null) return;
                //station.isLocked = false;
                //bool updateResult = UpdateManage.UpdateStation(station);
                //if (updateResult)
                //{
                //    ComnMethod.Message("解锁成功");
                //}
                //else
                //{
                //    ComnMethod.Message("解锁失败");
                //}
            }
            catch (Exception ex)
            {
                ComnMethod.Message("操作失败:" + ex.ToString());
            }
        }

        private void MItem_AgvOnline_Click(object sender, RoutedEventArgs e)
        {
            Base_Agv agv = DGrid_AGV.SelectedItem as Base_Agv;
            try
            {
                if (agv == null) return;
                if (agv.AgvTask != null)
                {
                    ComnMethod.Message(string.Format("{0}存在任务,不能上下线!", agv.AgvNo));
                    return;
                }
                var updateResult = _agvDb.Update(it => it.AgvNo == agv.AgvNo, it => new Base_Agv()
                {
                    IsEnable = true
                });
                if (updateResult.IsSuccess)
                {
                    agv.IsEnable = true;
                    ComnMethod.Message(string.Format("{0}上线成功!", agv.AgvNo));
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message(ex.ToString());
            }
        }

        private void MItem_AgvOut_Click(object sender, RoutedEventArgs e)
        {
            Base_Agv agv = DGrid_AGV.SelectedItem as Base_Agv;

            try
            {
                if (agv == null) return;
                if (agv.AgvTask != null)
                {
                    ComnMethod.Message(string.Format("{0}存在任务,不能上下线!", agv.AgvNo));
                    return;
                }

                var updateResult = _agvDb.Update(it => it.AgvNo == agv.AgvNo, it => new Base_Agv()
                {
                    Barcode = "0",
                    LockStation = null,
                    IsEnable = false,
                });
                if (updateResult.IsSuccess)
                {
                    agv.AgvState = 0;
                    agv.Barcode = "0";
                    agv.IsEnable = false;
                    agv.IsOnline = false;
                    agv.LockStation = null;
                    List<Base_Point> agvLockedList = App.PointList.FindAll(a => a.LockedAgv == agv);
                    foreach (Base_Point point in agvLockedList)
                    {
                        point.LockedAgv = null;
                    }
                    //更新界面小车位置
                    App.Sharp.AgvChange(agv);
                    ComnMethod.Message(string.Format("{0}下线成功!", agv.AgvNo));
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message(ex.ToString());
            }
        }

        /// <summary>
        /// 禁用充电桩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_Cforbit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Base_Station chargeStation = DGrid_Charge.SelectedItem as Base_Station;
                if (chargeStation == null) return;
                chargeStation.IsEnable = false;
                var isResult = UpdateManage.UpdateStation(chargeStation);
                if (isResult.IsSuccess)
                {
                    ComnMethod.Message(string.Format("{0}充电桩禁用成功!", chargeStation.Name));
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message(ex.ToString());
            }
        }

        /// <summary>
        /// 启用充电桩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_CEnable_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Base_Station chargeStation = DGrid_Charge.SelectedItem as Base_Station;
                if (chargeStation == null) return;
                chargeStation.IsEnable = true;
                var isResult = UpdateManage.UpdateStation(chargeStation);
                if (isResult.IsSuccess)
                {
                    ComnMethod.Message(string.Format("{0}充电桩启用成功!", chargeStation.Name));
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message(ex.ToString());
            }
        }

        /// <summary>
        /// 解锁充电桩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_CUnLock_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DGrid_Charge.SelectedItem is not Base_Station chargeStation) return;
                Base_Agv? agv = App.AgvList.FirstOrDefault(a => a.Barcode == chargeStation.Barcode);
                if (agv != null)
                {
                    ComnMethod.Message(string.Format("{0}占用当前充电桩无法解锁!", agv.AgvNo));
                    return;
                }
                chargeStation.IsLocked = false;
                agv = App.AgvList.Find(x => x.LockStation?.Name == chargeStation.Name);
                if (agv != null)
                {
                    agv.LockStation = null;
                }
                var isResult = UpdateManage.UpdateStation(chargeStation);
                if (isResult.IsSuccess)
                {
                    ComnMethod.Message(string.Format("{0}充电桩解锁成功!", chargeStation.Name));
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message(ex.ToString());
            }
        }

        /// <summary>
        /// 更改为有托盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_OnEnable_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Base_Station station = DGrid_Station.SelectedItem as Base_Station;
                if (station == null) return;
                var updateresult = _stationDb.Update(it => it.Name == station.Name, it => new Base_Station()
                {
                    StationState = EnumMsg.StationState.有托盘
                });
                if (updateresult.IsSuccess)
                {
                    station.StationState = EnumMsg.StationState.有托盘;
                }
                else
                {
                    ComnMethod.Message(string.Format("更改{0}工位失败!", station.Name));
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message(ex.ToString());
            }
        }

        /// <summary>
        /// 更改为无托盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_OffEnable_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Base_Station station = DGrid_Station.SelectedItem as Base_Station;
                if (station == null) return;
                var updateresult = _stationDb.Update(it => it.Name == station.Name, it => new Base_Station()
                {
                    StationState = EnumMsg.StationState.无托盘
                });
                if (updateresult.IsSuccess)
                {
                    station.StationState = EnumMsg.StationState.无托盘;
                }
                else
                {
                    ComnMethod.Message(string.Format("更改{0}工位失败!", station.Name));
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message(ex.ToString());
            }
        }

        /// <summary>
        /// 更改货架信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_ChangeShelf_Click(object sender, RoutedEventArgs e)
        {

        }

        private void MItem_ShelfUnLock_Click(object sender, RoutedEventArgs e)
        {

        }

        private void MItem_SelectShelf_Click(object sender, RoutedEventArgs e)
        {

        }

        private void MItem_Hoister_Click(object sender, RoutedEventArgs e)
        {
            Config_Equipment equipment = DGrid_Hoister.SelectedItem as Config_Equipment;
            if (equipment == null) return;
            ComnMethod.Message("清除成功");
        }

        private async void MItem_ClearHuahengStaion_Click(object sender, RoutedEventArgs e)
        {
            HuahengStaion huahengStaion = DGrid_HuahengStaion.SelectedItem as HuahengStaion;
            if (huahengStaion == null)
            {
                ComnMethod.Message("没有选中数据!");
                return;
            }
            if (string.IsNullOrEmpty(huahengStaion.EquipmentName))
            {
                ComnMethod.Message("选中的数据没有对应的设备名称!");
                return;
            }
            var msg = $"请确认是否清除华恒站台【{huahengStaion.EquipmentName}】的交互信号?可能导致任务出错!";
            if (MessageBox.Show(msg, "警告", MessageBoxButton.YesNo) == MessageBoxResult.No) return;

            await Task.Run(() =>
            {
                //因为PLC操作是异步的,第一次提交的结果是“等待处理”,所以等待2秒再次提交获取结果
                EquipmentExecutor.Instance.RequestOperator(huahengStaion.EquipmentName, EnumMsg.RequestType.清空数据);
                Thread.Sleep(EquipmentExecutor.Instance.ExecuteTime);
                var bllResult = EquipmentExecutor.Instance.RequestOperator(huahengStaion.EquipmentName, EnumMsg.RequestType.清空数据);
                if (!bllResult.IsSuccess)
                {
                    ComnMethod.Message($"清除华恒站台【{huahengStaion.EquipmentName}】的交互信号失败!失败原因:{bllResult.Message}");
                    return;
                }
                ComnMethod.Message($"清除华恒站台【{huahengStaion.EquipmentName}】的交互信号成功");
            });
        }



        private void MItem_EnableChange_Click(object sender, RoutedEventArgs e)
        {
            var point = DGrid_MatixManager.SelectedItem as Base_Point;
            if (point == null) return;
            point.IsEnable = !point.IsEnable;
            ComnMethod.Message("操作成功");
        }
        private void MItem_StopChange_Click(object sender, RoutedEventArgs e)
        {
            var point = DGrid_MatixManager.SelectedItem as Base_Point;
            if (point == null) return;
            point.IsStop = !point.IsStop;
            ComnMethod.Message("操作成功");
        }

        private void DGrid_HuahengStaion_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {

        }
    }
}