using AutoMapper;
using CapMachine.Core;
using CapMachine.Model.CANLIN;
using CapMachine.Wpf.CanDrive;
using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.LinDrive;
using CapMachine.Wpf.Services;
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.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Microsoft.Win32;
using static CapMachine.Wpf.Models.ComEnum;
using ImTools;
namespace CapMachine.Wpf.ViewModels
{
public class LinConfigViewModel : NavigationViewModel
{
///
/// 实例化函数
///
///
///
///
///
///
///
///
///
///
public LinConfigViewModel(IDialogService dialogService, IFreeSql freeSql,
IEventAggregator eventAggregator, IRegionManager regionManager, SysRunService sysRunService,
ConfigService configService, LinDriveService linDriveService, ComActionService comActionService, LogicRuleService logicRuleService,
IMapper mapper, MachineRtDataService machineRtDataService)
{
//LogService = logService;
FreeSql = freeSql;
EventAggregator = eventAggregator;
RegionManager = regionManager;
SysRunService = sysRunService;
ConfigService = configService;
LinDriveService = linDriveService;
ComActionService = comActionService;
LogicRuleService = logicRuleService;
Mapper = mapper;
this.MachineRtDataService = machineRtDataService;
//MachineDataService = machineDataService;
DialogService = dialogService;
EventAggregator.GetEvent().Subscribe(LogicRuleChangeEventCall);
EventAggregator.GetEvent().Subscribe(CanLinConfigChangedEventCall);
//数据波特率
DataBaudRateCbxItems = new ObservableCollection()
{
new CbxItems(){ Key="38400",Text="38400"},
new CbxItems(){ Key="19200",Text="19200"},
new CbxItems(){ Key="14400",Text="14400"},
new CbxItems(){ Key="9600",Text="9600"},
new CbxItems(){ Key="4800",Text="4800"},
new CbxItems(){ Key="2400",Text="2400"},
};
WriteNameCbxItems = new ObservableCollection()
{
new CbxItems(){ Key="转速",Text="转速"},
new CbxItems(){ Key="功率限制",Text="功率限制"},
new CbxItems(){ Key="使能",Text="使能"},
new CbxItems(){ Key="Anti_Sleep",Text="Anti_Sleep"},
new CbxItems(){ Key="PTC使能",Text="PTC使能"},
new CbxItems(){ Key="PTC功率",Text="PTC功率"},
new CbxItems(){ Key="PTC水流量",Text="PTC水流量"},
new CbxItems(){ Key="PTC水温",Text="PTC水温"},
};
ReadNameCbxItems = new ObservableCollection()
{
new CbxItems(){ Key="通讯Cmp转速",Text="通讯Cmp转速"},
new CbxItems(){ Key="通讯Cmp母线电压",Text="通讯Cmp母线电压"},
new CbxItems(){ Key="通讯Cmp母线电流",Text="通讯Cmp母线电流"},
new CbxItems(){ Key="通讯Cmp相电流",Text="通讯Cmp相电流"},
new CbxItems(){ Key="通讯Cmp功率",Text="通讯Cmp功率"},
new CbxItems(){ Key="通讯Cmp芯片温度",Text="通讯Cmp芯片温度"},
new CbxItems(){ Key="通讯PTC入水温度",Text="通讯PTC入水温度"},
new CbxItems(){ Key="通讯PTC出水温度",Text="通讯PTC出水温度"},
new CbxItems(){ Key="通讯PTC峰值电流",Text="通讯PTC峰值电流"},
new CbxItems(){ Key="通讯PTC母线电流",Text="通讯PTC母线电流"},
new CbxItems(){ Key="通讯PTC膜温",Text="通讯PTC膜温"},
new CbxItems(){ Key="通讯PTC模块温度",Text="通讯PTC模块温度"},
};
InitLoadLinConfigPro();
//初始化写规则下拉框
InitWriteRuleCbx();
}
///
/// FreeSQL 实例函数
///
public IFreeSql FreeSql { get; }
public IEventAggregator EventAggregator { get; }
public IRegionManager RegionManager { get; }
public SysRunService SysRunService { get; }
public ConfigService ConfigService { get; }
public LinDriveService LinDriveService { get; }
public ComActionService ComActionService { get; }
public LogicRuleService LogicRuleService { get; }
public IMapper Mapper { get; }
private MachineRtDataService MachineRtDataService { get; }
///
/// 弹窗服务
///
public IDialogService DialogService { get; }
private ObservableCollection _WriteRuleCbxItems;
///
/// 写入的规格集合
///
public ObservableCollection WriteRuleCbxItems
{
get { return _WriteRuleCbxItems; }
set { _WriteRuleCbxItems = value; RaisePropertyChanged(); }
}
#region 规则
///
/// 逻辑更改事件
///
///
///
private void LogicRuleChangeEventCall(string msg)
{
//InitWriteRuleCbx();
}
///
/// CAN/LIN配置变更事件处理
///
///
private void CanLinConfigChangedEventCall(string msg)
{
RefreshConfigList();
}
///
/// 初始化写规则下拉框
///
private void InitWriteRuleCbx()
{
WriteRuleCbxItems = new ObservableCollection();
//选择的读写规则
foreach (var itemRule in LogicRuleService.LogicRuleDtos)
{
WriteRuleCbxItems.Add(new CbxItems()
{
Key = itemRule.Id.ToString(),
Text = itemRule.Name
});
}
}
#endregion
#region LinConfigPro
///
/// 初始化j加载执行方法
///
private void InitLoadLinConfigPro()
{
//LIN配置集合数据
canLinConfigPros = FreeSql.Select().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(canLinConfigPros);
if (SelectCanLinConfigPro != null)
{
SelectCanLinConfigPro = canLinConfigPros.Where(a => a.Id == SelectCanLinConfigPro.Id).FirstOrDefault()!;
//无数据就返回
if (SelectCanLinConfigPro == null) return;
SelectedLINConfigExdDto = Mapper.Map(SelectCanLinConfigPro!.LINConfigExd);
var WirteData = SelectCanLinConfigPro.CanLinConfigContents!.Where(a => a.RWInfo == RW.Write).ToList();
if (WirteData != null && WirteData.Count > 0)
{
ListWriteCanLinRWConfigDto = new ObservableCollection(Mapper.Map>(WirteData));
//加载把当前的配置信息给指令
LinDriveService.CmdData.Clear();
foreach (var item in WirteData)
{
LinDriveService.AddCmdData(new LinCmdData()
{
ConfigName = item.Name,
MsgName = item.MsgFrameName,
SignalName = item.SignalName,
SignalCmdValue = double.TryParse(item.DefautValue, out double result) == true ? result : 0,
LogicRuleDto = Mapper.Map(item.LogicRule),
});
//LinDriveService.CmdData.Add(new LinCmdData()
//{
// ConfigName = item.Name,
// MsgName = item.MsgFrameName,
// SignalName = item.SignalName,
// SignalCmdValue = double.TryParse(item.DefautValue, out double result) == true ? result : 0,
//});
}
}
else
{
ListWriteCanLinRWConfigDto = new ObservableCollection();
}
var ReadData = SelectCanLinConfigPro.CanLinConfigContents!.Where(a => a.RWInfo == RW.Read).ToList();
if (ReadData != null && ReadData.Count > 0)
{
ListReadCanLinRWConfigDto = new ObservableCollection(Mapper.Map>(ReadData));
}
else
{
ListReadCanLinRWConfigDto = new ObservableCollection();
}
//调度表配置信息
if (SelectCanLinConfigPro.LinScheduleConfigs != null && SelectCanLinConfigPro.LinScheduleConfigs.Count() > 0)
{
ListLINScheduleConfigDto = new ObservableCollection(Mapper.Map>(SelectCanLinConfigPro.LinScheduleConfigs));
}
else
{
ListLINScheduleConfigDto = new ObservableCollection();
}
//匹配选中的SelectCanLinConfigPro.CanLinConfigContents和ListLinLdfModel
MatchSeletedAndLinLdfModel();
}
}
///
/// 匹配选中的SelectCanLinConfigPro.CanLinConfigContents和LinLdfModel
/// 为了标注和着色使用
///
private void MatchSeletedAndLinLdfModel()
{
//通过CanLinConfigContents标注ListLinLdfModel的选中状态属性,方便着色
if (ListLinLdfModel != null && ListLinLdfModel!.Count() > 0 && SelectCanLinConfigPro.CanLinConfigContents != null && SelectCanLinConfigPro.CanLinConfigContents.Count() > 0)
{
foreach (var itemLinLdfModel in ListLinLdfModel)
{
var FindData = SelectCanLinConfigPro.CanLinConfigContents.FindFirst(a => a.SignalName == itemLinLdfModel.SignalName);
if (FindData != null)//找到了就标注
{
switch (FindData.RWInfo)
{
case RW.Write:
itemLinLdfModel.IsSeletedInfo = 1;
break;
case RW.Read:
itemLinLdfModel.IsSeletedInfo = 2;
break;
default:
break;
}
}
else
{
//没有找到了就标注为0
itemLinLdfModel.IsSeletedInfo = 0;
}
}
}
}
private bool _IsLinConfigProActive = false;
///
/// LIN 配置是否被激活
///
public bool IsLinConfigProActive
{
get { return _IsLinConfigProActive; }
set { _IsLinConfigProActive = value; RaisePropertyChanged(); }
}
private bool _IsLINConfigDatagridActive = true;
///
/// LINConfigDatagrid是否被触控
/// 关联IsLinConfigProActive 取反
///
public bool IsLINConfigDatagridActive
{
get { return _IsLINConfigDatagridActive; }
set { _IsLINConfigDatagridActive = value; RaisePropertyChanged(); }
}
private string _SelectCanLinConfigProConfigName;
///
/// 选中的 CanLinConfigPro 名称
///
public string SelectCanLinConfigProConfigName
{
get { return _SelectCanLinConfigProConfigName; }
set { _SelectCanLinConfigProConfigName = value; RaisePropertyChanged(); }
}
private DelegateCommand _CanLinConfigPromdCmd;
///
/// LINConfigPro操作的指令
///
public DelegateCommand CanLinConfigPromdCmd
{
set
{
_CanLinConfigPromdCmd = value;
}
get
{
if (_CanLinConfigPromdCmd == null)
{
_CanLinConfigPromdCmd = new DelegateCommand((Par) => CanLinConfigPromdCmdMethod(Par));
}
return _CanLinConfigPromdCmd;
}
}
///
/// LinConfigPro操作的指令方法
///
///
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");
//加载默认的数据 LINConfigExd
var InsertLINConfigExd = FreeSql.Insert(new LINConfigExd()
{
BaudRate = 250,
LdfPath = "请配置LDF路径",
Cycle = 100,
}).ExecuteInserted();
//返回数据,刷新Chart
var InsertData = FreeSql.Insert(new CanLinConfigPro()
{
ConfigName = ReturnValue,
CANLINInfo = CANLIN.LIN,
LINConfigExdId = InsertLINConfigExd.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();
}
InitLoadLinConfigPro();
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();
InitLoadLinConfigPro();
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.LINConfigExd)
.IncludeMany(a => a.CanLinConfigContents)
.Where(a => a.Id == SelectCanLinConfigPro.Id)
.ToList();
repo.Delete(DeleteData);
foreach (var item in DeleteData)
{
FreeSql.Delete(item.LINConfigExdId).ExecuteAffrows();
}
InitLoadLinConfigPro();
}
else
{
System.Windows.MessageBox.Show("选中后再操作", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
break;
case "Active":
//激活到取消的状态的判断
if (IsLinConfigProActive == true)
{
//控件的激活
IsLinConfigProActive = !IsLinConfigProActive;
//控件的激活配置信息
IsLINConfigDatagridActive = !IsLinConfigProActive;
return;
}
if (LinDriveService.ToomossLinDrive.OpenState == true && LinDriveService.ToomossLinDrive.LdfParserState == true)
{
if (SelectCanLinConfigPro != null)
{
//控件的激活
IsLinConfigProActive = !IsLinConfigProActive;
//控件的激活配置信息
IsLINConfigDatagridActive = !IsLinConfigProActive;
//当前使用的LIN配置信息
LinDriveService.InitLinConfig(SelectCanLinConfigPro);
InitLoadLinConfigPro();
// 订阅 CmdData 的数据变化事件,用于基于数据变化刷新调度表
LinDriveService.ToomossLinDrive.LoadCmdDataToDrive(LinDriveService.CmdData);
}
else
{
System.Windows.MessageBox.Show("选中后再操作", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
}
else
{
System.Windows.MessageBox.Show("请确保LIN连接打开和LDF解析成功后再激活", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
break;
default:
break;
}
}
///
/// LIN List集合信息
///
private List canLinConfigPros = new List();
private ObservableCollection _ListCanLinConfigPro;
///
/// LIN 配置程序集合 ObservableCollection
///
public ObservableCollection ListCanLinConfigPro
{
get { return _ListCanLinConfigPro; }
set { _ListCanLinConfigPro = value; RaisePropertyChanged(); }
}
///
/// 选中的CanLinConfigPro
///
public CanLinConfigPro SelectCanLinConfigPro { get; set; }
private DelegateCommand