You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
13 KiB
C#

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;
using SlnMesnac.Repository.service;
using SlnMesnac.TouchSocket;
using SlnMesnac.WPF.Page.ControlPage;
using SlnMesnac.WPF.Page.Generate;
using SlnMesnac.WPF.Page.IndexPage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace SlnMesnac.WPF.ViewModel
{
public class MainWindowViewModel : ViewModelBase
{
private readonly ILogger<MainWindowViewModel> _logger;
private readonly ILogger<BaseTaskInfoBusiness> _BaseTaskInfoBusinesslogger;
private readonly ILogger<BaseAGVBusiness> _baseAGVBusinessLogger;
private readonly ILogger<VisionBusiness> _VisionBusinessLogger;
private readonly ILogger<BaseStateRefreshBusiness> _StateRefreshBusinessLogger;
private IAirportTaskService _Taskservice;
private IAGVStateService _AGVStateService;
private IAGVMapPointService _AGVMapPointService;
private IAGVSettingService _AGVSettingService;
private IAGVJobService _agvJobService;
private IVisionSettingService _visionSettingService;
private TcpServer _tcpServer;
//代码生成
private readonly GenerateControl generateControl = new GenerateControl();
private IndexContent indexContent = new IndexContent();
private UserTaskControl taskControl = new UserTaskControl();
private AppConfig _appConfig;
private AirPorthttpClient _airPorthttpClient;
private BaseTaskInfoBusiness _baseTaskInfoBusiness;
private BaseStateRefreshBusiness _baseStateRefreshBusiness;
private BaseAGVBusiness _baseAGVBusiness;
private VisionBusiness _visionBusiness;
#region 参数定义
/// <summary>
/// PLC设备状态
/// </summary>
private int _PlcStatus = 0;
public int PlcStatus
{
get { return _PlcStatus; }
set { _PlcStatus = value; RaisePropertyChanged(nameof(PlcStatus)); }
}
/// <summary>
/// 箱壳扫码器状态
/// </summary>
private int _ShellScannerStatus = 0;
public int ShellScannerStatus
{
get { return _ShellScannerStatus; }
set { _ShellScannerStatus = value; RaisePropertyChanged(nameof(ShellScannerStatus)); }
}
/// <summary>
/// 内胆扫码器状态
/// </summary>
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 事件定义
/// <summary>
/// 界面跳转按钮事件
/// </summary>
public RelayCommand<object> ControlOnClickCommand { get; set; }
/// <summary>
/// 打开系统键盘
/// </summary>
public RelayCommand OpenSystemKeyboardCommand { get; set; }
/// <summary>
/// 窗体控制
/// </summary>
public RelayCommand<object> FormControlCommand { get; set; }
#endregion
public MainWindowViewModel()
{
_appConfig = App.ServiceProvider.GetService<AppConfig>();
_logger = App.ServiceProvider.GetService<ILogger<MainWindowViewModel>>();
_BaseTaskInfoBusinesslogger = App.ServiceProvider.GetService<ILogger<BaseTaskInfoBusiness>>();
_baseAGVBusinessLogger = App.ServiceProvider.GetService<ILogger<BaseAGVBusiness>>();
_VisionBusinessLogger = App.ServiceProvider.GetService<ILogger<VisionBusiness>>();
_Taskservice = App.ServiceProvider.GetService<IAirportTaskService>();
_AGVStateService = App.ServiceProvider.GetService<IAGVStateService>();
_AGVMapPointService = App.ServiceProvider.GetService<IAGVMapPointService>();
_AGVSettingService = App.ServiceProvider.GetService<IAGVSettingService>();
_agvJobService = App.ServiceProvider.GetService<IAGVJobService>();
_visionSettingService = App.ServiceProvider.GetService<IVisionSettingService>();
_tcpServer = App.ServiceProvider.GetService<TcpServer>();
_airPorthttpClient = App.ServiceProvider.GetService<AirPorthttpClient>();
_baseTaskInfoBusiness = BaseTaskInfoBusiness.GetInstance(_BaseTaskInfoBusinesslogger,
_Taskservice, _AGVStateService, _AGVMapPointService, _agvJobService,
_tcpServer, _airPorthttpClient, _baseAGVBusiness, _visionBusiness);
_baseAGVBusiness = BaseAGVBusiness.GetInstance(_baseAGVBusinessLogger, _airPorthttpClient, _AGVStateService, _AGVMapPointService, _appConfig);
_visionBusiness = VisionBusiness.GetInstance(_VisionBusinessLogger, _tcpServer, _Taskservice, _visionSettingService, _AGVStateService, _agvJobService);
_baseStateRefreshBusiness = BaseStateRefreshBusiness.GetInstance(_StateRefreshBusinessLogger, _AGVStateService, _airPorthttpClient, _AGVSettingService);
ControlOnClickCommand = new RelayCommand<object>(obj => ControlOnClick(obj));
FormControlCommand = new RelayCommand<object>(x => FormControl(x));
StopAllTaskActionInvoke();
_tcpServer.RefreshStateAction += (_clientIP, _state) =>
{
if (_clientIP == _appConfig.AMRIP)
{
ShellScannerStatus = _state ? 1 : 2;
}
};
//
UserContent = indexContent;
}
/// <summary>
/// 窗体控制
/// </summary>
/// <param name="obj"></param>
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 "DeliverNeedPosition":
//AMR就绪请求视觉初步开始工作
_visionBusiness.TotalJudge = true;
_visionBusiness.RequestVisionStartWork(StackState.NXAGVNeedPositioning, 0, _tcpServer.VID);
break;
case "DeilverNoPosition":
//AMR就绪请求无定向AGV开始工作
_visionBusiness.TotalJudge = true;
_visionBusiness.RequestVisionStartWork(StackState.NXAGVNoPositioning, 0, _tcpServer.VID);
break;
case "AMRNoPosition":
//AMR就绪请求无定向AMR开始工作
_visionBusiness.TotalJudge = true;
_visionBusiness.RequestVisionStartWork(StackState.NXAMRNoPositioning, 0, _tcpServer.VID);
break;
case "VisionEnd":
//码垛结束,请求视觉系统复位
_visionBusiness.RequestVisionReplace(_tcpServer.VID);
break;
case "Get800":
_baseAGVBusiness.EndTaskAndClearErrorAndDownloadTask(_appConfig.AGVConfig.Where(x => x.AGVName == "S800-B").First().AGVGUID, "800小车进入位置任务pause");
break;
case "Out800":
_baseAGVBusiness.EndTaskAndClearErrorAndDownloadTask(_appConfig.AGVConfig.Where(x => x.AGVName == "S800-B").First().AGVGUID, "800小车入库任务pause");
break;
case "Get1000":
_baseAGVBusiness.EndTaskAndClearErrorAndDownloadTask(_appConfig.AGVConfig.Where(x => x.AGVName == "S1000-A").First().AGVGUID, "1000小车进入位置任务pause");
break;
case "Out1000":
_baseAGVBusiness.EndTaskAndClearErrorAndDownloadTask(_appConfig.AGVConfig.Where(x => x.AGVName == "S1000-A").First().AGVGUID, "1000小车入库任务pause");
break;
case "Reset1000":
_baseAGVBusiness.EndTaskAndClearErrorAndDownloadTask(_appConfig.AGVConfig.Where(x => x.AGVName == "S1000-A").First().AGVGUID, "1000校正模板");
break;
case "ManualStop800Task":
//手动停止任务
_baseAGVBusiness.EndTaskAndClearError("S800-B");
break;
case "ManualClear800Error":
//手动清除报警
_baseAGVBusiness.ClaerError("S800-B");
break;
case "ManualStop1000Task":
_baseAGVBusiness.EndTaskAndClearError("S1000-A");
break;
case "StartAGVStateRefresh":
_baseStateRefreshBusiness.AGVStateUpdateTimerOperation(TimerControl.Start);
break;
case "StopAGVStateRefresh":
_baseStateRefreshBusiness.AGVStateUpdateTimerOperation(TimerControl.Stop);
break;
case "StartTaskRefresh":
_baseTaskInfoBusiness.TaskStateUpdateTimerOperation(TimerControl.Start);
break;
case "StopTaskRefresh":
_baseTaskInfoBusiness.TaskStateUpdateTimerOperation(TimerControl.Stop);
break;
default:
break;
}
}
catch (Exception ex)
{
_logger.LogError("窗体控制逻辑异常" + ex.Message);
}
}
public void StopAllTaskActionInvoke()
{
taskControl.StopTaskAction = () =>
{
List<AirportTask> taskList = _Taskservice.GetTaskInfos(x => x.taskstate == "抓取中" && x.taskstate == "派车中").ToList();
//任务状态设置为已终止并且机械臂复位
foreach(var taskInfo in taskList)
{
_visionBusiness.RequestVisionReplace(_visionSettingService.GetVisionSettingByAMRGUID(taskInfo.amragvno).VisionID);
taskInfo.visiontaskno = null;
taskInfo.amragvisarrive = null;
taskInfo.amragvno = null;
taskInfo.deliveragvno = null;
taskInfo.deliveragvisarrive = null;
taskInfo.finishtime = DateTime.Now;
taskInfo.taskstate = "已终止";
}
//agv全部停止
_logger.LogInformation("已成功全部停止");
};
}
/// <summary>
/// 界面跳转
/// </summary>
private void ControlOnClick(object obj)
{
try
{
string info = obj as string;
switch (info)
{
case "Index":
UserContent = indexContent;
break;
case "Control":
UserContent = taskControl;
break;
default: break;
}
}
catch (Exception ex)
{
_logger.LogError("界面跳转逻辑异常", ex);
}
}
}
}