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

509 lines
16 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using GummingCommon;
using GummingEntity;
using GummingSupport;
using GummingControl;
using System.Text;
using GummingBusiness;
using Newtonsoft.Json;
using GummingLine;
using System.Threading.Tasks;
namespace Gumming
{
public class CPExecuteViewModule : ViewModelBase
{
#region base parameters
private int rows = 20;
private int pagenumber = 1;
public CPExecuteView View { get; set; }
private bool IsConfirm;
private bool isLoading;
private CancellationTokenSource cancellationTokenSource;
private List<HardFormulaCPEntity> steps;
private System.Timers.Timer timer;
private DateTime dtStart;
private CPFlowExecuter CPFlowExecuter;
private CPFormulaExecuter CPFormulaExecuter;
#endregion
#region constructor
public CPExecuteViewModule()
{
View = new CPExecuteView();
View.DataContext = this;
InitializeCommands();
InitializeParameters();
CPFlowExecuter = new CPFlowExecuter();
CPFormulaExecuter = new CPFormulaExecuter();
}
private void InitializeCommands()
{
PrepareCommand = new DelegateCommand(OnPrepareCommand);
StartCommand = new DelegateCommand(OnStartCommand);
StopCommand = new DelegateCommand(OnStopCommand);
SelectedFormulaChangedCommand = new DelegateCommand(OnSelectedFormulaChangedCommand);
}
public override void InitializeParameters(object content = null)
{
IsConfirm = false;
WindowTitle = "";
ProgressEnabled = true;
if (timer == null)
{
timer = new System.Timers.Timer();
timer.Interval = 300;
timer.Elapsed += timer_Elapsed;
timer.Stop();
}
}
~CPExecuteViewModule()
{
}
public void LoadDefaultValue()
{
StartLoadTestItem();
}
#endregion
#region Binding Properties
public string WindowTitle { get; private set; }
private string _ControlName;
public string ControlName
{
get
{
return _ControlName;
}
set
{
_ControlName = value;
OnPropertyChanged("ControlName");
}
}
private string _StationCode;
public string StationCode
{
get
{
return _StationCode;
}
set
{
_StationCode = value;
OnPropertyChanged("StationCode");
}
}
private string _StepMessage;
public string StepMessage
{
get
{
return _StepMessage;
}
set
{
_StepMessage = value;
OnPropertyChanged("StepMessage");
}
}
private string _DetailMessage;
public string DetailMessage
{
get
{
return _DetailMessage;
}
set
{
_DetailMessage = value;
OnPropertyChanged("DetailMessage");
}
}
private int _ProgressTime;
public int ProgressTime
{
get
{
return _ProgressTime;
}
set
{
_ProgressTime = value;
OnPropertyChanged("ProgressTime");
}
}
private decimal _TotalSecond;
public decimal TotalSecond
{
get
{
return _TotalSecond;
}
set
{
_TotalSecond = value;
OnPropertyChanged("TotalSecond");
}
}
private ObservableCollection<HardFormulaEntity> _HardFormulas;
public ObservableCollection<HardFormulaEntity> HardFormulas
{
get
{
return _HardFormulas;
}
set
{
_HardFormulas = value;
OnPropertyChanged("HardFormulas");
}
}
private HardFormulaEntity _SelectedFormula;
public HardFormulaEntity SelectedFormula
{
get
{
return _SelectedFormula;
}
set
{
_SelectedFormula = value;
OnPropertyChanged("SelectedFormula");
}
}
private bool _ProgressEnabled;
public bool ProgressEnabled
{
get
{
return _ProgressEnabled;
}
set
{
_ProgressEnabled = value;
OnPropertyChanged("ProgressEnabled");
}
}
private ushort _DingZhenStatus;
public ushort DingZhenStatus
{
get
{
return _DingZhenStatus;
}
set
{
_DingZhenStatus = value;
OnPropertyChanged("DingZhenStatus");
}
}
#endregion
#region Binding Commands
private ICommand _PrepareCommand;
public ICommand PrepareCommand
{
get
{
return _PrepareCommand;
}
set
{
_PrepareCommand = value;
OnPropertyChanged("PrepareCommand");
}
}
private ICommand _StartCommand;
public ICommand StartCommand
{
get
{
return _StartCommand;
}
set
{
_StartCommand = value;
OnPropertyChanged("StartCommand");
}
}
private ICommand _StopCommand;
public ICommand StopCommand
{
get
{
return _StopCommand;
}
set
{
_StopCommand = value;
OnPropertyChanged("StopCommand");
}
}
private ICommand _SelectedFormulaChangedCommand;
public ICommand SelectedFormulaChangedCommand
{
get
{
return _SelectedFormulaChangedCommand;
}
set
{
_SelectedFormulaChangedCommand = value;
OnPropertyChanged("SelectedFormulaChangedCommand");
}
}
#endregion
#region Private Methods
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
TimeSpan ts = DateTime.Now - dtStart;
ProgressTime = (int)ts.TotalSeconds;
}
private void StartLoadTestItem()
{
try
{
isLoading = true;
if (HardFormulas == null)
{
HardFormulas = new ObservableCollection<HardFormulaEntity>();
}
HardFormulas.Clear();
var record = HardFormulaDA.Load(new PagerEntity() { PageIndex = pagenumber, Rows = rows, Conditions = " and StationCode='" + StationCode + "' " });
List<HardFormulaEntity> logs = (List<HardFormulaEntity>)record.rows;
View.Dispatcher.Invoke((Action)(() =>
{
foreach (var s in logs)
{
HardFormulas.Add(s);
}
OnPropertyChanged("HartFormula");
if (HardFormulas.Count > 0)//Ĭ<>ϼ<EFBFBD><CFBC>ص<EFBFBD>һ<EFBFBD><D2BB>
{
SelectedFormula = HardFormulas[0];
LoadFormulaSteps();
}
isLoading = false;
}));
}
catch (Exception ex)
{
LogService.WriteErrorLog(ex);
ShowErrorBox("Error:" + ex.Message);
return;
}
finally
{
CloseProgressView();
}
}
private void OnPrepareCommand(Object sender)
{
//if (!Global.InitializeSystemDone)
//{
// ProgressEnabled = true;
// ShowMessageBox("ϵͳ<CFB5><CDB3>ʼ<EFBFBD><CABC>δ<EFBFBD><CEB4><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD>ȴ<EFBFBD>");
// return;
//}
if (SelectedFormula == null)
{
ShowMessageBox("<22><>ѡ<EFBFBD><D1A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD>мӹ<D0BC><D3B9><EFBFBD>");
return;
}
CPFlowExecuter.CallBack = (HardFormulaFlowEntity flow, FlowStatus fs, StationStatus stationStatus) =>
{
StepMessage = fs.Message;
if (!string.IsNullOrWhiteSpace(fs.DetailMessage))
{
DetailMessage = DetailMessage + "\r\n" + fs.DetailMessage;
}
};
HardFormulaEntity formula = HardFormulaDA.Load(SelectedFormula.RecId);
if (formula == null)
{
ShowMessageBox("<22><>ѡ<EFBFBD><D1A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD>мӹ<D0BC><D3B9><EFBFBD>");
return;
}
if (StationLockControl.StationIsLock(StationCode))
{
ShowMessageBox(HintMessage.StationLocked);
return;
}
StationLockControl.LockStation(StationCode, null);
ProgressEnabled = false;
List<HardFormulaFlowEntity> flows = Global.Flows.FindAll(q => q.StationCode == formula.StationCode && q.IsPrepare);
cancellationTokenSource = new CancellationTokenSource();
Task flowTask = new Task(() =>
{
try
{
CPFlowExecuter.Execute(cancellationTokenSource, flows, null, true, false);
ProgressEnabled = true;
StationLockControl.UnLockStation(StationCode);
}
catch (Exception ex)
{
StepMessage = string.Format(" ִ<><D6B4><EFBFBD>쳣:{0}!", ex.Message);
ProgressEnabled = true;
StationLockControl.UnLockStation(StationCode);
}
}, cancellationTokenSource.Token, TaskCreationOptions.LongRunning);
flowTask.Start();
}
private void OnStartCommand(Object sender)
{
//if (!Global.InitializeSystemDone)
//{
// ProgressEnabled = true;
// ShowMessageBox("ϵͳ<CFB5><CDB3>ʼ<EFBFBD><CABC>δ<EFBFBD><CEB4><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD>ȴ<EFBFBD>");
// return;
//}
if (SelectedFormula == null)
{
ShowMessageBox("<22><>ѡ<EFBFBD><D1A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD>мӹ<D0BC><D3B9><EFBFBD>");
return;
}
var status = DeviceControl.GetCardStatus();
if (status != CardStatus.Prepared)
{
Helpering.ShowErrorBox(string.Format("<22><>ʼ<EFBFBD><CABC><EFBFBD>Ῠʧ<E1BFA8>ܣ<EFBFBD><DCA3><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><E5BFA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
return;
}
if (steps == null || steps.Count <= 0)
{
Helpering.ShowErrorBox(string.Format("<22><>ѡ<EFBFBD><D1A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD>мӹ<D0BC><D3B9><EFBFBD>"));
return;
}
if (StationLockControl.StationIsLock(StationCode))
{
ShowMessageBox(HintMessage.StationLocked);
return;
}
List<HardFormulaFlowEntity> flows = Global.Flows.FindAll(q => q.StationCode == StationCode && q.IsPrepare);
StationLockControl.LockStation(StationCode, null);
CPFormulaExecuter.CallBack = (HardFormulaCPEntity step, StepStatus ss) =>
{
StepMessage = ss.Message;
if (!string.IsNullOrWhiteSpace(ss.DetailMessage))
{
DetailMessage = DetailMessage + "\r\n" + ss.DetailMessage;
}
//DingZhenStatus = (ushort)(step.IsDingZhen ? 1 : 0);
if (ss.Status == ExecuteStatusEnum.Start)
{
dtStart = DateTime.Now;
}
if (step != null)
{
TotalSecond = step.StepTime;
}
};
cancellationTokenSource = new CancellationTokenSource();
ProgressEnabled = false;
ProgressTime = 0;
dtStart = DateTime.Now;
timer.Start();
Task formulaTask = new Task(() =>
{
try
{
ProgressTime = 0;
CPFormulaExecuter.Execute(cancellationTokenSource, steps, null);
CPFlowExecuter.Execute(cancellationTokenSource, flows, null, true, true);
if (cancellationTokenSource.IsCancellationRequested)
{
StepMessage = "<22>ֹ<EFBFBD><D6B9><EFBFBD>ֹ!";
}
else
{
StepMessage = <><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!";
}
ProgressEnabled = true;
StationLockControl.UnLockStation(StationCode);
timer.Stop();
TotalSecond = 0;
ProgressTime = 0;
}
catch (Exception ex)
{
StepMessage = string.Format(" ִ<><D6B4><EFBFBD>쳣:{0}!", ex.Message);
ProgressEnabled = true;
StationLockControl.UnLockStation(StationCode);
}
}, cancellationTokenSource.Token, TaskCreationOptions.LongRunning);
formulaTask.Start();
}
private void OnStopCommand(Object sender)
{
if (cancellationTokenSource != null)
{
StepMessage = "<22><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD>!";
cancellationTokenSource.Cancel();
}
}
private void OnSelectedFormulaChangedCommand(Object sender)
{
LoadFormulaSteps();
}
private void LoadFormulaSteps()
{
if (SelectedFormula != null)
{
HardFormulaEntity formula = HardFormulaDA.Load(SelectedFormula.RecId);
if (formula == null)
{
ShowMessageBox("<22><>ѡ<EFBFBD><D1A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD>мӹ<D0BC><D3B9><EFBFBD>");
return;
}
FlowStatus fs = new FlowStatus();
fs.FlowId = "CPTest";
fs.FlowCode = "CPTest";
fs.Status = ExecuteStatusEnum.Prepare;
fs.StepStatues = new List<StepStatus>();
var record = HardFormulaCPDA.Load(new PagerEntity() { PageIndex = 1, Rows = 9999, Conditions = " and FormulaId='" + SelectedFormula.RecId + "' " });
steps = (List<HardFormulaCPEntity>)record.rows;
foreach (var step in steps)
{
StepStatus sb = new StepStatus();
sb.StepId = step.RecId;
sb.StepCode = step.StepCode;
sb.Status = ExecuteStatusEnum.Prepare;
fs.StepStatues.Add(sb);
step.StationCode = formula.StationCode;
}
//TotalSecond = steps.Sum(q => q.StepTime);
}
}
#endregion
}
}