using AutoMapper;
using CapMachine.Core;
using CapMachine.Model;
using CapMachine.Model.CANLIN;
using CapMachine.Wpf.CanDrive;
using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.PrismEvent;
using CapMachine.Wpf.Services;
using Ganss.Excel;
using Microsoft.VisualBasic;
using Microsoft.Win32;
using NPOI.SS.UserModel.Charts;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using Syncfusion.Windows.Tools.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace CapMachine.Wpf.ViewModels
{
///
/// CAN配置ViewModel
///
public class CANConfigViewModel : NavigationViewModel
{
public CANConfigViewModel(IDialogService dialogService, IFreeSql freeSql,
IEventAggregator eventAggregator, IRegionManager regionManager, SysRunService sysRunService,
ConfigService configService, CanDriveService canDriveService,
IMapper mapper, MachineRtDataService machineRtDataService)
{
//LogService = logService;
FreeSql = freeSql;
EventAggregator = eventAggregator;
RegionManager = regionManager;
SysRunService = sysRunService;
ConfigService = configService;
CanDriveService = canDriveService;
Mapper = mapper;
this.MachineRtDataService = machineRtDataService;
//MachineDataService = machineDataService;
DialogService = dialogService;
WriteNameCbxItems = new ObservableCollection()
{
new CbxItems(){ Key="转速",Text="转速"},
new CbxItems(){ Key="功率限制",Text="功率限制"},
new CbxItems(){ Key="使能",Text="使能"},
new CbxItems(){ Key="Anti_Sleep",Text="Anti_Sleep"},
};
ReadNameCbxItems = new ObservableCollection()
{
new CbxItems(){ Key="通讯转速",Text="通讯转速"},
new CbxItems(){ Key="通讯母线电压",Text="通讯母线电压"},
new CbxItems(){ Key="通讯母线电流",Text="通讯母线电流"},
new CbxItems(){ Key="通讯相电流",Text="通讯相电流"},
new CbxItems(){ Key="通讯功率",Text="通讯功率"},
new CbxItems(){ Key="通讯芯片温度",Text="通讯芯片温度"},
};
InitLoadCanConfigPro();
}
///
/// FreeSQL 实例函数
///
public IFreeSql FreeSql { get; }
public IEventAggregator EventAggregator { get; }
public IRegionManager RegionManager { get; }
public SysRunService SysRunService { get; }
public ConfigService ConfigService { get; }
public CanDriveService CanDriveService { get; }
public IMapper Mapper { get; }
private MachineRtDataService MachineRtDataService { get; }
///
/// 弹窗服务
///
public IDialogService DialogService { get; }
#region CanConfigPro
///
/// 初始化j加载执行方法
///
private void InitLoadCanConfigPro()
{
//CAN配置集合数据
canLinConfigPros = FreeSql.Select().Where(a => a.CANLINInfo == CANLIN.CAN)
.Include(a => a.CANConfigExd)
.IncludeMany(a => a.CanLinConfigContents)
.ToList();
ListCanLinConfigPro = new ObservableCollection(canLinConfigPros);
if (SelectCanLinConfigPro != null)
{
SelectCanLinConfigPro = canLinConfigPros.Where(a => a.Id == SelectCanLinConfigPro.Id).FirstOrDefault()!;
SelectedCANConfigExdDto = Mapper.Map(SelectCanLinConfigPro!.CANConfigExd);
var WirteData = SelectCanLinConfigPro.CanLinConfigContents!.Where(a => a.RWInfo == RW.Write).ToList();
if (WirteData != null && WirteData.Count > 0)
{
ListWriteCanLinRWConfigDto = new ObservableCollection(Mapper.Map>(WirteData));
//加载把当前的配置信息给指令
CanDriveService.CmdData.Clear();
foreach (var item in WirteData)
{
CanDriveService.AddCmdData(new CanCmdData()
{
ConfigName = item.Name,
MsgName = item.MsgFrameName,
SignalName = item.SignalName,
SignalCmdValue = double.TryParse(item.DefautValue, out double result) == true ? result : 0,
});
//CanDriveService.CmdData.Add(new CanCmdData()
//{
// ConfigName = item.Name,
// MsgName = item.MsgFrameName,
// SignalName = item.SignalName,
// SignalCmdValue = double.TryParse(item.DefautValue, out double result) == true ? result : 0,
//});
}
}
var ReadData = SelectCanLinConfigPro.CanLinConfigContents!.Where(a => a.RWInfo == RW.Read).ToList();
if (ReadData != null && ReadData.Count > 0)
{
ListReadCanLinRWConfigDto = new ObservableCollection(Mapper.Map>(ReadData));
}
}
}
private bool _IsCanConfigProActive = false;
///
/// CAN 配置是否被激活
///
public bool IsCanConfigProActive
{
get { return _IsCanConfigProActive; }
set { _IsCanConfigProActive = value; RaisePropertyChanged(); }
}
private bool _IsCANConfigDatagridActive = true;
///
/// CANConfigDatagrid是否被触控
/// 关联IsCanConfigProActive 取反
///
public bool IsCANConfigDatagridActive
{
get { return _IsCANConfigDatagridActive; }
set { _IsCANConfigDatagridActive = value; RaisePropertyChanged(); }
}
private string _SelectCanLinConfigProConfigName;
///
/// 选中的 CanLinConfigPro 名称
///
public string SelectCanLinConfigProConfigName
{
get { return _SelectCanLinConfigProConfigName; }
set { _SelectCanLinConfigProConfigName = value; RaisePropertyChanged(); }
}
private DelegateCommand _CanLinConfigPromdCmd;
///
/// CanConfigPro操作的指令
///
public DelegateCommand CanLinConfigPromdCmd
{
set
{
_CanLinConfigPromdCmd = value;
}
get
{
if (_CanLinConfigPromdCmd == null)
{
_CanLinConfigPromdCmd = new DelegateCommand((Par) => CanLinConfigPromdCmdMethod(Par));
}
return _CanLinConfigPromdCmd;
}
}
///
/// CanConfigPro操作的指令方法
///
///
private void CanLinConfigPromdCmdMethod(string Par)
{
switch (Par)
{
case "Add":
//弹窗
DialogService.ShowDialog("DialogCanLinConfigCreateView", new DialogParameters() { { "Name", "" } }, (par) =>
{
if (par.Result == ButtonResult.OK)
{
//程序名称
var ReturnValue = par.Parameters.GetValue("Name");
//加载默认的数据 CANConfigExd
var InsertCANConfigExd = FreeSql.Insert(new CANConfigExd()
{
BaudRate = 250,
DbcPath = "请配置DBC路径",
Cycle = 100,
}).ExecuteInserted();
//返回数据,刷新Chart
var InsertData = FreeSql.Insert(new CanLinConfigPro()
{
ConfigName = ReturnValue,
CANLINInfo = CANLIN.CAN,
CANConfigExdId = InsertCANConfigExd.FirstOrDefault()!.Id
}).ExecuteInserted();
if (InsertData != null)
{
//加载默认的数据 CanLinRWConfig
FreeSql.Insert(new CanLinRWConfig()
{
SignalName = "Speed",
DefautValue = "0",
Name = "转速",
RWInfo = RW.Write,
CanLinConfigProId = InsertData.FirstOrDefault().Id
}).ExecuteInserted();
FreeSql.Insert(new CanLinRWConfig()
{
SignalName = "Vol",
DefautValue = "0",
Name = "电压",
RWInfo = RW.Read,
CanLinConfigProId = InsertData.FirstOrDefault().Id
}).ExecuteInserted();
}
InitLoadCanConfigPro();
SelectCanLinConfigPro = canLinConfigPros!.Find(a => a.Id == InsertData.FirstOrDefault()!.Id);
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
break;
case "Edit":
if (SelectCanLinConfigPro != null)
{
//弹窗
DialogService.ShowDialog("DialogCanLinConfigCreateView", new DialogParameters() { { "Name", SelectCanLinConfigPro.ConfigName } }, (par) =>
{
if (par.Result == ButtonResult.OK)
{
//程序名称
var ReturnValue = par.Parameters.GetValue("Name");
FreeSql.Update()
.Set(a => a.ConfigName, ReturnValue)
.Where(a => a.Id == SelectCanLinConfigPro.Id)
.ExecuteAffrows();
InitLoadCanConfigPro();
SelectCanLinConfigPro = null;
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
else
{
System.Windows.MessageBox.Show("选中后再操作", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
break;
case "Save":
case "Delete":
if (SelectCanLinConfigPro != null)
{
var repo = FreeSql.GetRepository();
repo.DbContextOptions.EnableCascadeSave = true; //关键设置
var DeleteData = repo.Select
.Include(a => a.CANConfigExd)
.IncludeMany(a => a.CanLinConfigContents)
.Where(a => a.Id == SelectCanLinConfigPro.Id)
.ToList();
repo.Delete(DeleteData);
foreach (var item in DeleteData)
{
FreeSql.Delete(item.CANConfigExdId).ExecuteAffrows();
}
InitLoadCanConfigPro();
}
else
{
System.Windows.MessageBox.Show("选中后再操作", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
break;
case "Active":
if (SelectCanLinConfigPro != null)
{
//控件的激活
IsCanConfigProActive = !IsCanConfigProActive;
//控件的激活配置信息
IsCANConfigDatagridActive = !IsCanConfigProActive;
//当前使用的CAN 配置信息
CanDriveService.InitCanConfig(SelectCanLinConfigPro);
InitLoadCanConfigPro();
}
else
{
System.Windows.MessageBox.Show("选中后再操作", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
break;
default:
break;
}
}
///
/// CAN List集合信息
///
private List canLinConfigPros = new List();
private ObservableCollection _ListCanLinConfigPro;
///
/// CAN 配置程序集合 ObservableCollection
///
public ObservableCollection ListCanLinConfigPro
{
get { return _ListCanLinConfigPro; }
set { _ListCanLinConfigPro = value; RaisePropertyChanged(); }
}
///
/// 选中的CanLinConfigPro
///
public CanLinConfigPro SelectCanLinConfigPro { get; set; }
private DelegateCommand