using GummingBusiness;
using GummingCommon;
using GummingEntity;
using GummingLine;
using Module.Plc.ModbusTcp.Tool;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
//using GummingSupport;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
namespace Gumming
{
public class DispenseViewModule : ViewModelBase
{
#region 字段
private int rows = 20;
private int pagenumber = 1;
public DispenseView View { get; set; }
private bool IsConfirm;
private bool isLoading;
private List<ComboEntity> modes;
private List<HardFormulaDispenseEntity> CopyDispenses;
#endregion
#region 结构体
public DispenseViewModule()
{
View = new DispenseView();
View.DataContext = this;
InitializeCommands();
InitializeParameters();
}
private void InitializeCommands()
{
NewCommand = new DelegateCommand(OnNewCommand);
AddStepCommand = new DelegateCommand(OnAddStepCommand);
InsertStepCommand = new DelegateCommand(OnInsertStepCommand);
DeleteStepCommand = new DelegateCommand(OnDeleteStepCommand);
SaveStepCommand = new DelegateCommand(OnSaveStepCommand);//保存
AllowCommand = new DelegateCommand(OnAllowCommand);//编辑
SelectStepCommand = new DelegateCommand(OnSelectStepCommand);
CopyStepCommand = new DelegateCommand(OnCopyStepCommand);
DisabledCommand = new DelegateCommand(OnDisabledCommand);//取消编辑
UnitClickCommand = new DelegateCommand(OnUnitClickCommand);
SelectFlowCommand = new DelegateCommand(OnSelectFlowCommand);
SendDataCommand = new DelegateCommand(OnSendDataCommand);//下发配方数据
}
public override void InitializeParameters(object content = null)
{
IsConfirm = false;
WindowTitle = "";
AllowEdit = false;
SendStatus = true;
CreateBy = Global.CurrentUserCode;
//CreateTime=new DateTime();
CopyDispenses = JsonConvert.DeserializeObject<List<HardFormulaDispenseEntity>>(JsonConvert.SerializeObject(Global.Dispenses));
Dispenses = new ObservableCollection<HardFormulaDispenseEntity>();
Dispenses02 = new ObservableCollection<HardFormulaDispenseEntity>();
modes = new List<ComboEntity>();
modes.Clear();
//0:None、1:LOT Head、2:Interval、3:Both
modes.Add(new ComboEntity() { ComboCode = "None", ComboName = "None", RecId = "0", Tag = 0 });
modes.Add(new ComboEntity() { ComboCode = "LOT Head", ComboName = "LOT Head", RecId = "1", Tag = 1 });
modes.Add(new ComboEntity() { ComboCode = "Interval", ComboName = "Interval", RecId = "2", Tag = 2 });
modes.Add(new ComboEntity() { ComboCode = "Both", ComboName = "Both", RecId = "3", Tag = 3 });
StartLoadFlow();
}
~DispenseViewModule()
{
}
public void LoadDefaultValue()
{
LoadFlowUnit(false);
}
#endregion
#region 属性
public string WindowTitle { get; private set; }
private ObservableCollection<ComboEntity> _StationCodes;
public ObservableCollection<ComboEntity> StationCodes
{
get
{
return _StationCodes;
}
set
{
_StationCodes = value;
OnPropertyChanged("StationCodes");
}
}
private ComboEntity _SelectedStationCode;
public ComboEntity SelectedStationCode
{
get
{
return _SelectedStationCode;
}
set
{
_SelectedStationCode = value;
OnPropertyChanged("SelectedStationCode");
}
}
private ObservableCollection<HardFormulaDispenseEntity> _Dispenses;
public ObservableCollection<HardFormulaDispenseEntity> Dispenses
{
get
{
return _Dispenses;
}
set
{
_Dispenses = value;
OnPropertyChanged("Dispenses");
}
}
private ObservableCollection<HardFormulaDispenseEntity> _Dispenses02;
/// <summary>
/// 存胶头数据
/// </summary>
public ObservableCollection<HardFormulaDispenseEntity> Dispenses02
{
get
{
return _Dispenses02;
}
set
{
_Dispenses02 = value;
OnPropertyChanged("Dispenses02");
}
}
private HardFormulaDispenseEntity _SelectedDispense;
public HardFormulaDispenseEntity SelectedDispense
{
get
{
return _SelectedDispense;
}
set
{
_SelectedDispense = value;
OnPropertyChanged("SelectedDispense");
}
}
private string _StationCode;
public string StationCode
{
get
{
return _StationCode;
}
set
{
_StationCode = value;
OnPropertyChanged("StationCode");
}
}
private string _CreateBy;
public string CreateBy
{
get
{
return _CreateBy;
}
set
{
_CreateBy = value;
OnPropertyChanged("CreateBy");
}
}
private DateTime? _CreateTime;
public DateTime? CreateTime
{
get
{
return _CreateTime;
}
set
{
_CreateTime = value;
OnPropertyChanged("CreateTime");
}
}
private HardStationType _SelectedFormulaType;
public HardStationType SelectedFormulaType
{
get
{
return _SelectedFormulaType;
}
set
{
_SelectedFormulaType = value;
OnPropertyChanged("SelectedFormulaType");
}
}
private HardFormulaEntity _SelectedFormula;
public HardFormulaEntity SelectedFormula
{
get
{
return _SelectedFormula;
}
set
{
_SelectedFormula = value;
OnPropertyChanged("SelectedFormula");
}
}
private ComboEntity _SelecedMode;
public ComboEntity SelecedMode
{
get { return _SelecedMode; }
set
{
_SelecedMode = value;
OnPropertyChanged("SelecedMode");
}
}
private List<ComboEntity> _Modes;
public List<ComboEntity> Modes
{
get { return _Modes; }
set
{
_Modes = value;
OnPropertyChanged("Modes");
}
}
private bool _AllowEdit;
public bool AllowEdit
{
get
{
return _AllowEdit;
}
set
{
_AllowEdit = value;
OnPropertyChanged("AllowEdit");
}
}
private bool _SendStatus;
/// <summary>
/// 下发数据状态
/// </summary>
public bool SendStatus
{
get { return _SendStatus; }
set
{
_SendStatus = value;
OnPropertyChanged("SendStatus");
}
}
#endregion
#region 指令属性
private ICommand _NewCommand;
public ICommand NewCommand
{
get
{
return _NewCommand;
}
set
{
_NewCommand = value;
OnPropertyChanged("NewCommand");
}
}
private ICommand _AddStepCommand;
public ICommand AddStepCommand
{
get
{
return _AddStepCommand;
}
set
{
_AddStepCommand = value;
OnPropertyChanged("AddStepCommand");
}
}
private ICommand _DeleteStepCommand;
public ICommand DeleteStepCommand
{
get
{
return _DeleteStepCommand;
}
set
{
_DeleteStepCommand = value;
OnPropertyChanged("DeleteStepCommand");
}
}
private ICommand _SaveStepCommand;
public ICommand SaveStepCommand
{
get
{
return _SaveStepCommand;
}
set
{
_SaveStepCommand = value;
OnPropertyChanged("SaveStepCommand");
}
}
private ICommand _AllowCommand;
public ICommand AllowCommand
{
get
{
return _AllowCommand;
}
set
{
_AllowCommand = value;
OnPropertyChanged("AllowCommand");
}
}
private ICommand _InsertStepCommand;
public ICommand InsertStepCommand
{
get
{
return _InsertStepCommand;
}
set
{
_InsertStepCommand = value;
OnPropertyChanged("InsertStepCommand");
}
}
private ICommand _SelectStepCommand;
public ICommand SelectStepCommand
{
get
{
return _SelectStepCommand;
}
set
{
_SelectStepCommand = value;
OnPropertyChanged("SelectStepCommand");
}
}
private ICommand _CopyStepCommand;
public ICommand CopyStepCommand
{
get
{
return _CopyStepCommand;
}
set
{
_CopyStepCommand = value;
OnPropertyChanged("CopyStepCommand");
}
}
private ICommand _DisabledCommand;
public ICommand DisabledCommand
{
get
{
return _DisabledCommand;
}
set
{
_DisabledCommand = value;
OnPropertyChanged("DisabledCommand");
}
}
private ICommand _UnitClickCommand;
public ICommand UnitClickCommand
{
get
{
return _UnitClickCommand;
}
set
{
_UnitClickCommand = value;
OnPropertyChanged("UnitClickCommand");
}
}
private ICommand _SelectFlowCommand;
public ICommand SelectFlowCommand
{
get
{
return _SelectFlowCommand;
}
set
{
_SelectFlowCommand = value;
OnPropertyChanged("SelectFlowCommand");
}
}
private ICommand _SendDataCommand;
/// <summary>
/// 发送数据
/// </summary>
public ICommand SendDataCommand
{
get { return _SendDataCommand; }
set
{
_SendDataCommand = value;
OnPropertyChanged("SendDataCommand");
}
}
#endregion
#region 私有属性
public void StartLoadFlow()
{
try
{
isLoading = true;
StationCodes = new ObservableCollection<ComboEntity>();
StationCodes.Add(new ComboEntity() { RecId = Constant.COT1, ComboCode = Constant.COT1, ComboName = Constant.COT1 });
StationCodes.Add(new ComboEntity() { RecId = Constant.COT2, ComboCode = Constant.COT2, ComboName = Constant.COT2 });
StationCodes.Add(new ComboEntity() { RecId = Constant.COT3, ComboCode = Constant.COT3, ComboName = Constant.COT3 });
StationCodes.Add(new ComboEntity() { RecId = Constant.COT4, ComboCode = Constant.COT4, ComboName = Constant.COT4 });
View.Dispatcher.Invoke((Action)(() =>
{
OnPropertyChanged("StationCodes");
isLoading = false;
}));
if (StationCodes != null && StationCodes.Count > 0)
{
SelectedStationCode = StationCodes[0];
LoadFlowUnit(false);
}
else
{
OnNewCommand(null);
}
}
catch (Exception ex)
{
LogService.WriteErrorLog(ex);
ShowErrorBox("Error:" + ex.Message);
return;
}
finally
{
CloseProgressView();
}
}
private void LoadFlowUnit(bool force)
{
if (SelectedStationCode != null)
{
string flowId = SelectedStationCode.RecId;
AllowEdit = false;
StationCode = SelectedStationCode.ComboCode;
Dispenses.Clear();
Dispenses02.Clear();
if (force)
{
var stations = Global.Dispenses.FindAll(q=>q.StationCode == StationCode);
CopyDispenses.RemoveAll(q=>q.StationCode == StationCode);
for (int i = 0; i < stations.Count(); i++)
{
CopyDispenses.Add((HardFormulaDispenseEntity)stations[i].Clone());
}
}
var logs = CopyDispenses.Where(q => q.StationCode == StationCode).OrderBy(q => q.RecId);
View.Dispatcher.Invoke((Action)(() =>
{
foreach (var s in logs)
{
CreateTime = s.ModifyTime;
s.Modes = modes;
s.SelecedMode = s.Modes.FirstOrDefault(q => q.Tag == s.Mode);
if (s.Copy == null || force)
{
s.Copy = (HardFormulaDispenseEntity)s.Clone();
}
if (s.StepCode != "NZ")
{
Dispenses.Add(s);
}
else
{
Dispenses02.Add(s);
}
}
OnPropertyChanged("Dispenses");
OnPropertyChanged("Dispenses02");
isLoading = false;
}));
}
}
private void OnAddStepCommand(Object sender)
{
}
private void OnInsertStepCommand(Object sender)
{
}
private void OnDeleteStepCommand(Object sender)
{
}
private void OnSaveStepCommand(Object sender)
{
foreach (var s in Dispenses.Where(q => q.StationCode == StationCode))
{
s.Mode = s.SelecedMode?.Tag;
s.Copy = (HardFormulaDispenseEntity)s.Clone();
s.ModifyTime=DateTime.Now;
HardFormulaDispenseDA.Update(s);
var dispenseSource = Global.Dispenses.FirstOrDefault(q=>q.RecId == s.RecId);
if (dispenseSource != null)
{
dispenseSource = s;
}
var dispenseSource2 = CopyDispenses.FirstOrDefault(q => q.RecId == s.RecId);
if (dispenseSource2 != null)
{
dispenseSource2 = s;
}
}
foreach (var s in Dispenses02.Where(q => q.StationCode == StationCode))
{
s.Mode = s.SelecedMode?.Tag;
s.Copy = (HardFormulaDispenseEntity)s.Clone();
s.ModifyTime = DateTime.Now;
HardFormulaDispenseDA.Update(s);
var dispenseSource = Global.Dispenses.FirstOrDefault(q => q.RecId == s.RecId);
if (dispenseSource != null)
{
dispenseSource = s;
}
var dispenseSource2 = CopyDispenses.FirstOrDefault(q => q.RecId == s.RecId);
if (dispenseSource2 != null)
{
dispenseSource2 = s;
}
}
//刷新数据
var dispense = HardFormulaDispenseDA.Load(new PagerEntity() { PageIndex = 1, Rows = 999, Conditions = " and 1=1 " });
Global.Dispenses = (List<HardFormulaDispenseEntity>)dispense.rows;
CreateTime = DateTime.Now;
AllowEdit = false;
SendStatus = true;
}
private void OnAllowCommand(Object sender)
{
AllowEdit = true;
SendStatus = false;
}
private void OnDisabledCommand(Object sender)
{
AllowEdit = false;
SendStatus = true;
LoadFlowUnit(true);
}
/// <summary>
/// 下发数据 命令
/// </summary>
/// <param name="sender"></param>
private async void OnSendDataCommand(Object sender)
{
// 显示等待窗口
var loadingWindow = new CustomLoading();
loadingWindow.Owner = System.Windows.Application.Current.MainWindow;
loadingWindow.Show();
try
{
await Task.Run(() => SendData());
}
catch (Exception ex)
{
}
finally
{
//关闭等待窗口
if (loadingWindow.IsLoaded)
{
loadingWindow.Close();
}
}
LoadFlowUnit(true);
}
/// <summary>
/// 下发数据 方法
/// </summary>
public void SendData()
{
var datas = Global.Dispenses;
datas = datas.Where(q => q.StationCode == StationCode).ToList();
//数据不连续,单个写
foreach ( var dataItem in datas)
{
switch (dataItem.StepCode)
{
case "PR1":
CmnParam.PlcTool.WriteInt16General(StationCode, 176, (short)(dataItem.Mode));
CmnParam.PlcTool.WriteInt16General(StationCode, 179, (short)(dataItem.DispenseCount));
CmnParam.PlcTool.WriteInt32General(StationCode, 182, (int)(dataItem.IntervalTime * 60000));
CmnParam.PlcTool.WriteInt32General(StationCode, 188, (int)(dataItem.DispenseInterval * 1000));
break;
case "PR2":
CmnParam.PlcTool.WriteInt16General(StationCode, 177, (short)(dataItem.Mode));
CmnParam.PlcTool.WriteInt16General(StationCode, 180, (short)(dataItem.DispenseCount));
CmnParam.PlcTool.WriteInt32General(StationCode, 184, (int)(dataItem.IntervalTime * 60000));
CmnParam.PlcTool.WriteInt32General(StationCode, 190, (int)(dataItem.DispenseInterval * 1000));
break;
case "PR3":
CmnParam.PlcTool.WriteInt16General(StationCode, 178, (short)(dataItem.Mode));
CmnParam.PlcTool.WriteInt16General(StationCode, 181, (short)(dataItem.DispenseCount));
CmnParam.PlcTool.WriteInt32General(StationCode, 186, (int)(dataItem.IntervalTime * 60000));
CmnParam.PlcTool.WriteInt32General(StationCode, 192, (int)(dataItem.DispenseInterval * 1000));
break;
case "CCR":
CmnParam.PlcTool.WriteInt16General(StationCode, 202, (short)(dataItem.Mode));
CmnParam.PlcTool.WriteInt16General(StationCode, 203, (short)(dataItem.DispenseCount));
CmnParam.PlcTool.WriteInt32General(StationCode, 204, (int)(dataItem.IntervalTime * 60000));
CmnParam.PlcTool.WriteInt32General(StationCode, 206, (int)(dataItem.DispenseInterval * 1000));
CmnParam.PlcTool.WriteInt32General(StationCode, 208, (int)(dataItem.DispenseTime * 1000));
CmnParam.PlcTool.WriteInt32General(StationCode, 212, (int)(dataItem.ProcessCount));
break;
case "NZ":
CmnParam.PlcTool.WriteInt32General(StationCode, 232, (int)(dataItem.DispenseTime * 1000));
CmnParam.PlcTool.WriteInt32General(StationCode, 234, (int)(dataItem.BlowTime * 1000));
CmnParam.PlcTool.WriteInt32General(StationCode, 236, (int)(dataItem.BlowInterval * 1000));
break;
default:
break;
}
}
}
private void OnSelectStepCommand(Object sender)
{
}
private void OnCopyStepCommand(Object sender)
{
}
private void OnUnitClickCommand(Object sender)
{
}
private void OnSelectFlowCommand(Object sender)
{
LoadFlowUnit(false);
}
private void OnNewCommand(Object sender)
{
}
#endregion
}
}
我的后台是这样的帮忙看看改哪里
最新发布