using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using HslCommunication.Enthernet; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SlnMesnac.Business.@base; using SlnMesnac.Config; using SlnMesnac.Model.AirportApiEntity; using SlnMesnac.Repository.service; using SlnMesnac.TouchSocket; using SlnMesnac.WPF.Page.Generate; using SlnMesnac.WPF.Page.IndexPage; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Windows; namespace SlnMesnac.WPF.ViewModel { public class MainWindowViewModel : ViewModelBase { private readonly ILogger _logger; private readonly ILogger _BaseTaskInfoBusinesslogger; private readonly ILogger _baseAGVBusinessLogger; private readonly ILogger _VisionBusinessLogger; private IAirportTaskService _Taskservice; private IAGVStateService _AGVStateService; private IAGVMapPointService _AGVMapPointService; private TcpServer _tcpServer; //代码生成 private readonly GenerateControl generateControl = new GenerateControl(); private IndexContent indexContent = new IndexContent(); private AppConfig _appConfig; private AirPorthttpClient _airPorthttpClient; private BaseTaskInfoBusiness _baseTaskInfoBusiness; private BaseAGVBusiness _baseAGVBusiness; private VisionBusiness _visionBusiness; #region 参数定义 /// /// PLC设备状态 /// private int _PlcStatus = 0; public int PlcStatus { get { return _PlcStatus; } set { _PlcStatus = value; RaisePropertyChanged(nameof(PlcStatus)); } } /// /// 箱壳扫码器状态 /// private int _ShellScannerStatus = 0; public int ShellScannerStatus { get { return _ShellScannerStatus; } set { _ShellScannerStatus = value; RaisePropertyChanged(nameof(ShellScannerStatus)); } } /// /// 内胆扫码器状态 /// private int _BoldScannerStatus = 0; public int BoldScannerStatus { get { return _BoldScannerStatus; } set { _BoldScannerStatus = value; RaisePropertyChanged(nameof(BoldScannerStatus)); } } public System.Windows.Controls.UserControl _content; public System.Windows.Controls.UserControl UserContent { get { return _content; } set { _content = value; RaisePropertyChanged(nameof(UserContent)); } } #endregion #region 事件定义 /// /// 界面跳转按钮事件 /// public RelayCommand ControlOnClickCommand { get; set; } /// /// 打开系统键盘 /// public RelayCommand OpenSystemKeyboardCommand { get; set; } /// /// 窗体控制 /// public RelayCommand FormControlCommand { get; set; } #endregion public MainWindowViewModel() { _appConfig = App.ServiceProvider.GetService(); _logger = App.ServiceProvider.GetService>(); _BaseTaskInfoBusinesslogger = App.ServiceProvider.GetService>(); _VisionBusinessLogger = App.ServiceProvider.GetService>(); _Taskservice = App.ServiceProvider.GetService(); _AGVStateService = App.ServiceProvider.GetService(); _AGVMapPointService = App.ServiceProvider.GetService(); _tcpServer = App.ServiceProvider.GetService(); _airPorthttpClient = App.ServiceProvider.GetService(); _baseTaskInfoBusiness = BaseTaskInfoBusiness.GetInstance(_BaseTaskInfoBusinesslogger, _Taskservice, _AGVStateService, _tcpServer, _airPorthttpClient); _baseAGVBusiness = BaseAGVBusiness.GetInstance(_baseAGVBusinessLogger, _airPorthttpClient, _AGVStateService, _AGVMapPointService); _visionBusiness = VisionBusiness.GetInstance(_VisionBusinessLogger, _tcpServer); ControlOnClickCommand = new RelayCommand(obj => ControlOnClick(obj)); FormControlCommand = new RelayCommand(x => FormControl(x)); _tcpServer.RefreshStateAction += (_clientIP, _state) => { if (_clientIP == _appConfig.AMRIP) { ShellScannerStatus = _state ? 1 : 2; } }; UserContent = indexContent; } /// /// 窗体控制 /// /// private void FormControl(object obj) { try { string controlType = obj as string; switch (controlType) { // 关闭当前窗口 case "Exit": //Environment.Exit(0); Application.Current.Shutdown(); break; case "Generate": UserContent = generateControl; break; // 还原 或者 最大化当前窗口 case "Normal": if (Application.Current.MainWindow.WindowState == WindowState.Normal) { Application.Current.MainWindow.WindowState = WindowState.Maximized; break; } if (Application.Current.MainWindow.WindowState == WindowState.Maximized) { Application.Current.MainWindow.WindowState = WindowState.Normal; break; } break; // 最小化当前窗口 case "Minimized": Application.Current.MainWindow.WindowState = WindowState.Minimized; break; case "TestButton1": //AMR就绪请求视觉初步开始工作 _visionBusiness.RequestVisionStartWork(StackState.AGVNeedPositioning, _tcpServer.VID); break; case "TestButton2": //码垛结束,请求视觉系统复位 _visionBusiness.RequestVisionReplace(_tcpServer.VID); break; case "TestButton3": //终止小车当前任务 _baseAGVBusiness.EndAGVTask(_appConfig.AGVConfig.Where(x => x.AGVName == "S800-B").First().AGVGUID); Thread.Sleep(500); //调用小车到抓取位置 _baseAGVBusiness.DownloadTask( _appConfig.AGVConfig.Where(x => x.AGVName == "S800-B").First().AGVGUID, _appConfig.JobConfig.Where(x => x.JobName == "800小车进入位置任务pause").First().JobGUID, _appConfig.PositionConfig.Where(x => x.PointName == "800装货点").First().PointGUID, _appConfig.PositionConfig.Where(x => x.PointName == "800装货点").First().PointParamName ); break; case "TestButton4": //终止小车当前任务 _baseAGVBusiness.EndAGVTask(_appConfig.AGVConfig.Where(x => x.AGVName == "S800-B").First().AGVGUID); Thread.Sleep(500); //调用小车到库位 _baseAGVBusiness.DownloadTask( _appConfig.AGVConfig.Where(x => x.AGVName == "S800-B").First().AGVGUID, _appConfig.JobConfig.Where(x => x.JobName == "800小车入库任务pause").First().JobGUID, _appConfig.PositionConfig.Where(x => x.PointName == "800装货点").First().PointGUID, _appConfig.PositionConfig.Where(x => x.PointName == "800装货点").First().PointParamName ); break; default: break; } } catch (Exception ex) { _logger.LogError("窗体控制逻辑异常", ex); } } /// /// 界面跳转 /// private void ControlOnClick(object obj) { try { string info = obj as string; switch (info) { case "Index": UserContent = indexContent; break; default: break; } } catch (Exception ex) { _logger.LogError("界面跳转逻辑异常", ex); } } } }