340 lines
12 KiB
C#
340 lines
12 KiB
C#
using AutoMapper;
|
||
using CapMachine.Core;
|
||
using CapMachine.Model.CANLIN;
|
||
using CapMachine.Wpf.Dtos;
|
||
using CapMachine.Wpf.LinDrive;
|
||
using CapMachine.Wpf.Services;
|
||
using ImTools;
|
||
using Microsoft.Win32;
|
||
using Prism.Commands;
|
||
using Prism.Events;
|
||
using Prism.Regions;
|
||
using Prism.Services.Dialogs;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Linq;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using static CapMachine.Wpf.Models.ComEnum;
|
||
|
||
namespace CapMachine.Wpf.ViewModels
|
||
{
|
||
/// <summary>
|
||
/// ZLG LIN 配置 ViewModel。
|
||
/// </summary>
|
||
public class ZlgLinDriveConfigViewModel : NavigationViewModel
|
||
{
|
||
/// <summary>
|
||
/// 构造函数。
|
||
/// </summary>
|
||
public ZlgLinDriveConfigViewModel(IDialogService dialogService, IFreeSql freeSql,
|
||
IEventAggregator eventAggregator, IRegionManager regionManager, SysRunService sysRunService,
|
||
ConfigService configService, ZlgLinDriveService zlgLinDriveService, ZlgCanDriveService zlgCanDriveService,
|
||
ComActionService comActionService, LogicRuleService logicRuleService,
|
||
IMapper mapper)
|
||
{
|
||
DialogService = dialogService;
|
||
FreeSql = freeSql;
|
||
EventAggregator = eventAggregator;
|
||
RegionManager = regionManager;
|
||
SysRunService = sysRunService;
|
||
ConfigService = configService;
|
||
ZlgLinDriveService = zlgLinDriveService;
|
||
ZlgCanDriveService = zlgCanDriveService;
|
||
ComActionService = comActionService;
|
||
LogicRuleService = logicRuleService;
|
||
Mapper = mapper;
|
||
|
||
InitLoadLinConfigPro();
|
||
}
|
||
|
||
public IDialogService DialogService { get; }
|
||
public IFreeSql FreeSql { get; }
|
||
public IEventAggregator EventAggregator { get; }
|
||
public IRegionManager RegionManager { get; }
|
||
public SysRunService SysRunService { get; }
|
||
public ConfigService ConfigService { get; }
|
||
public ZlgLinDriveService ZlgLinDriveService { get; }
|
||
public ZlgCanDriveService ZlgCanDriveService { get; }
|
||
public ComActionService ComActionService { get; }
|
||
public LogicRuleService LogicRuleService { get; }
|
||
public IMapper Mapper { get; }
|
||
|
||
private List<CanLinConfigPro> linConfigPros = new List<CanLinConfigPro>();
|
||
|
||
private ObservableCollection<CanLinConfigPro>? _ListCanLinConfigPro;
|
||
/// <summary>
|
||
/// LIN 配置程序集合。
|
||
/// </summary>
|
||
public ObservableCollection<CanLinConfigPro>? ListCanLinConfigPro
|
||
{
|
||
get { return _ListCanLinConfigPro; }
|
||
set { _ListCanLinConfigPro = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 选中的配置程序。
|
||
/// </summary>
|
||
public CanLinConfigPro? SelectCanLinConfigPro { get; set; }
|
||
|
||
private LINConfigExdDto? _SelectedLINConfigExdDto;
|
||
/// <summary>
|
||
/// 选中的 LIN 配置 DTO。
|
||
/// </summary>
|
||
public LINConfigExdDto? SelectedLINConfigExdDto
|
||
{
|
||
get { return _SelectedLINConfigExdDto; }
|
||
set { _SelectedLINConfigExdDto = value; RaisePropertyChanged(); RaisePropertyChanged(nameof(CurrentLdfPath)); RaisePropertyChanged(nameof(CurrentSchEnable)); RaisePropertyChanged(nameof(LinBaudRate)); }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 当前 LDF 路径。
|
||
/// </summary>
|
||
public string? CurrentLdfPath
|
||
{
|
||
get { return SelectedLINConfigExdDto?.LdfPath; }
|
||
set
|
||
{
|
||
if (SelectedLINConfigExdDto == null) return;
|
||
SelectedLINConfigExdDto.LdfPath = value;
|
||
RaisePropertyChanged();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// LIN 波特率。
|
||
/// </summary>
|
||
public int LinBaudRate
|
||
{
|
||
get { return SelectedLINConfigExdDto?.BaudRate ?? 0; }
|
||
set
|
||
{
|
||
if (SelectedLINConfigExdDto == null) return;
|
||
SelectedLINConfigExdDto.BaudRate = value;
|
||
RaisePropertyChanged();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调度表使能。
|
||
/// </summary>
|
||
public bool CurrentSchEnable
|
||
{
|
||
get { return SelectedLINConfigExdDto?.SchEnable ?? false; }
|
||
set
|
||
{
|
||
if (SelectedLINConfigExdDto == null) return;
|
||
SelectedLINConfigExdDto.SchEnable = value;
|
||
RaisePropertyChanged();
|
||
}
|
||
}
|
||
|
||
private void InitLoadLinConfigPro()
|
||
{
|
||
linConfigPros = FreeSql.Select<CanLinConfigPro>()
|
||
.Where(a => a.CANLINInfo == CANLIN.LIN)
|
||
.Include(a => a.LINConfigExd)
|
||
.IncludeMany(a => a.CanLinConfigContents, then => then.Include(b => b.LogicRule))
|
||
.IncludeMany(a => a.LinScheduleConfigs)
|
||
.ToList();
|
||
|
||
ListCanLinConfigPro = new ObservableCollection<CanLinConfigPro>(linConfigPros);
|
||
}
|
||
|
||
private void SyncSelectedConfig()
|
||
{
|
||
if (SelectCanLinConfigPro == null) return;
|
||
SelectedLINConfigExdDto = Mapper.Map<LINConfigExdDto>(SelectCanLinConfigPro.LINConfigExd);
|
||
}
|
||
|
||
private DelegateCommand<object>? _LinConfigProGridSelectionChangedCmd;
|
||
/// <summary>
|
||
/// LIN 配置程序选中变化。
|
||
/// </summary>
|
||
public DelegateCommand<object> LinConfigProGridSelectionChangedCmd
|
||
{
|
||
get
|
||
{
|
||
if (_LinConfigProGridSelectionChangedCmd == null)
|
||
{
|
||
_LinConfigProGridSelectionChangedCmd = new DelegateCommand<object>(LinConfigProGridSelectionChangedCmdMethod);
|
||
}
|
||
return _LinConfigProGridSelectionChangedCmd;
|
||
}
|
||
}
|
||
|
||
private void LinConfigProGridSelectionChangedCmdMethod(object par)
|
||
{
|
||
if (par == null) return;
|
||
if (par is SelectionChangedEventArgs) return;
|
||
|
||
if (par is CanLinConfigPro)
|
||
{
|
||
SelectCanLinConfigPro = par as CanLinConfigPro;
|
||
SyncSelectedConfig();
|
||
return;
|
||
}
|
||
|
||
var args = par as SelectionChangedEventArgs;
|
||
if (args == null || args.AddedItems == null || args.AddedItems.Count == 0) return;
|
||
var selected = args.AddedItems[0] as CanLinConfigPro;
|
||
if (selected == null) return;
|
||
|
||
SelectCanLinConfigPro = selected;
|
||
SyncSelectedConfig();
|
||
}
|
||
|
||
private DelegateCommand? _LoadLdfCmd;
|
||
/// <summary>
|
||
/// 选择 LDF 文件。
|
||
/// </summary>
|
||
public DelegateCommand LoadLdfCmd
|
||
{
|
||
get
|
||
{
|
||
if (_LoadLdfCmd == null)
|
||
{
|
||
_LoadLdfCmd = new DelegateCommand(LoadLdfCmdMethod);
|
||
}
|
||
return _LoadLdfCmd;
|
||
}
|
||
}
|
||
|
||
private void LoadLdfCmdMethod()
|
||
{
|
||
try
|
||
{
|
||
if (SelectCanLinConfigPro == null || SelectedLINConfigExdDto == null)
|
||
{
|
||
MessageBox.Show("选中LIN配置名称后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
|
||
return;
|
||
}
|
||
|
||
OpenFileDialog openFileDialogInfo = new OpenFileDialog();
|
||
openFileDialogInfo.Filter = "(*.ldf;*.ldf)|*.ldf;*.ldf|all|*.*";
|
||
openFileDialogInfo.CheckFileExists = true;
|
||
openFileDialogInfo.CheckPathExists = true;
|
||
openFileDialogInfo.ShowDialog();
|
||
|
||
CurrentLdfPath = openFileDialogInfo.FileName;
|
||
}
|
||
catch
|
||
{
|
||
MessageBox.Show("可能未选择信息", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Hand);
|
||
}
|
||
}
|
||
|
||
private DelegateCommand<string>? _LinOpCmd;
|
||
/// <summary>
|
||
/// LIN 操作命令。
|
||
/// </summary>
|
||
public DelegateCommand<string> LinOpCmd
|
||
{
|
||
get
|
||
{
|
||
if (_LinOpCmd == null)
|
||
{
|
||
_LinOpCmd = new DelegateCommand<string>(LinOpCmdMethod);
|
||
}
|
||
return _LinOpCmd;
|
||
}
|
||
}
|
||
|
||
private void LinOpCmdMethod(string par)
|
||
{
|
||
switch (par)
|
||
{
|
||
case "Open":
|
||
if (ComActionService.IsLINToDoWork() == false)
|
||
{
|
||
MessageBox.Show("请关闭CAN连接后才能开启LIN,同一个时刻只能有一个通信驱动压缩机", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
|
||
return;
|
||
}
|
||
|
||
if (ZlgCanDriveService.OpenState)
|
||
{
|
||
MessageBox.Show("请先关闭 ZLG CAN 连接后再开启 ZLG LIN", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
|
||
return;
|
||
}
|
||
|
||
if (SelectCanLinConfigPro == null || SelectedLINConfigExdDto == null)
|
||
{
|
||
MessageBox.Show("选中LIN配置名称后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
|
||
return;
|
||
}
|
||
|
||
// 打开 LIN(不依赖 LDF)
|
||
ZlgLinDriveService.StartLinDrive(0, (uint)SelectedLINConfigExdDto.BaudRate, isMaster: true);
|
||
if (ZlgLinDriveService.OpenState)
|
||
{
|
||
ConfigService.CanLinRunStateModel.CurSysSelectedCanLin = CanLinEnum.Lin;
|
||
}
|
||
break;
|
||
|
||
case "Close":
|
||
ZlgLinDriveService.CloseDevice();
|
||
ConfigService.CanLinRunStateModel.CurSysSelectedCanLin = CanLinEnum.No;
|
||
break;
|
||
|
||
case "Save":
|
||
if (SelectCanLinConfigPro == null || SelectedLINConfigExdDto == null)
|
||
{
|
||
MessageBox.Show("选中LIN配置名称后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
|
||
return;
|
||
}
|
||
|
||
FreeSql.Update<LINConfigExd>()
|
||
.Set(a => a.LdfPath, SelectedLINConfigExdDto.LdfPath)
|
||
.Set(a => a.Cycle, SelectedLINConfigExdDto.Cycle)
|
||
.Set(a => a.BaudRate, SelectedLINConfigExdDto.BaudRate)
|
||
.Set(a => a.SchEnable, SelectedLINConfigExdDto.SchEnable)
|
||
.Where(a => a.Id == SelectedLINConfigExdDto.Id)
|
||
.ExecuteUpdated();
|
||
|
||
InitLoadLinConfigPro();
|
||
break;
|
||
|
||
case "Parse":
|
||
// 明确提示:当前 ZLG LIN 暂不支持 LDF
|
||
if (SelectCanLinConfigPro == null || SelectedLINConfigExdDto == null)
|
||
{
|
||
MessageBox.Show("选中LIN配置名称后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
ZlgLinDriveService.StartLdf(SelectedLINConfigExdDto.LdfPath ?? string.Empty);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
private DelegateCommand<object>? _SchEnableCmd;
|
||
/// <summary>
|
||
/// 调度表使能写入驱动。
|
||
/// </summary>
|
||
public DelegateCommand<object> SchEnableCmd
|
||
{
|
||
get
|
||
{
|
||
if (_SchEnableCmd == null)
|
||
{
|
||
_SchEnableCmd = new DelegateCommand<object>(SchEnableCmdCall);
|
||
}
|
||
return _SchEnableCmd;
|
||
}
|
||
}
|
||
|
||
private void SchEnableCmdCall(object par)
|
||
{
|
||
ZlgLinDriveService.SchEnable = CurrentSchEnable;
|
||
}
|
||
}
|
||
}
|