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.

729 lines
22 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 DEVExecuteViewModule : ViewModelBase
{
#region base parameters
private int rows = 20;
private int pagenumber = 1;
public DEVExecuteView View { get; set; }
private bool IsConfirm;
private bool isLoading;
private CancellationTokenSource cancellationTokenSource;
private List<HardFormulaDEVEntity> steps;
private System.Timers.Timer timer;
private DateTime dtStart = DateTime.Now;
private DEVPrepareExecuter DEVFlowExecuter;
private DEVFormulaExecuter DEVFormulaExecuter;
#endregion
#region constructor
public DEVExecuteViewModule()
{
View = new DEVExecuteView();
View.DataContext = this;
InitializeCommands();
InitializeParameters();
DEVFlowExecuter = new DEVPrepareExecuter();
DEVFormulaExecuter = new DEVFormulaExecuter();
}
private void InitializeCommands()
{
PrepareCommand = new DelegateCommand(OnPrepareCommand);
StartCommand = new DelegateCommand(OnStartCommand);
StopCommand = new DelegateCommand(OnStopCommand);
}
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();
}
}
~DEVExecuteViewModule()
{
}
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 bool _IsBsr;
public bool IsBsr
{
get
{
return _IsBsr;
}
set
{
_IsBsr = value;
OnPropertyChanged("IsBsr");
}
}
private bool _IsDI;
public bool IsDI
{
get
{
return _IsDI;
}
set
{
_IsDI = value;
OnPropertyChanged("IsDI");
}
}
private bool _IsN2;
public bool IsN2
{
get
{
return _IsN2;
}
set
{
_IsN2 = value;
OnPropertyChanged("IsN2");
}
}
private bool _IsDEV1;
public bool IsDEV1
{
get
{
return _IsDEV1;
}
set
{
_IsDEV1 = value;
OnPropertyChanged("IsDEV1");
}
}
private bool _IsDEV2;
public bool IsDEV2
{
get
{
return _IsDEV2;
}
set
{
_IsDEV2 = value;
OnPropertyChanged("IsDEV2");
}
}
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 _BSRIOStatus;
public ushort BSRIOStatus
{
get
{
return _BSRIOStatus;
}
set
{
_BSRIOStatus = value;
OnPropertyChanged("BSRIOStatus");
}
}
private ushort _N2IOStatus;
public ushort N2IOStatus
{
get
{
return _N2IOStatus;
}
set
{
_N2IOStatus = value;
OnPropertyChanged("N2IOStatus");
}
}
private ushort _DIIOStatus;
public ushort DIIOStatus
{
get
{
return _DIIOStatus;
}
set
{
_DIIOStatus = value;
OnPropertyChanged("DIIOStatus");
}
}
private ushort _DEVIO1Status;
public ushort DEVIO1Status
{
get
{
return _DEVIO1Status;
}
set
{
_DEVIO1Status = value;
OnPropertyChanged("DEVIO1Status");
}
}
private ushort _DEVIO2Status;
public ushort DEVIO2Status
{
get
{
return _DEVIO2Status;
}
set
{
_DEVIO2Status = value;
OnPropertyChanged("DEVIO2Status");
}
}
private ushort _MainAxisIOStatus;
public ushort MainAxisIOStatus
{
get
{
return _MainAxisIOStatus;
}
set
{
_MainAxisIOStatus = value;
OnPropertyChanged("MainAxisIOStatus");
}
}
private ushort _WindowStatus;
public ushort WindowStatus
{
get
{
return _WindowStatus;
}
set
{
_WindowStatus = value;
OnPropertyChanged("WindowStatus");
}
}
private ushort _CupStatus;
public ushort CupStatus
{
get
{
return _CupStatus;
}
set
{
_CupStatus = value;
OnPropertyChanged("CupStatus");
}
}
private ushort _AttractIOStatus;
public ushort AttractIOStatus
{
get
{
return _AttractIOStatus;
}
set
{
_AttractIOStatus = value;
OnPropertyChanged("AttractIOStatus");
}
}
#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");
}
}
#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;
}
DEVFlowExecuter.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);
List<HardFormulaFlowEntity> flows = Global.Flows.FindAll(q => q.StationCode == formula.StationCode && q.IsPrepare);
cancellationTokenSource = new CancellationTokenSource();
Task flowTask = new Task(() =>
{
try
{
DEVFlowExecuter.Execute(cancellationTokenSource, flows, null, true, false);
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;
}
StationLockControl.LockStation(StationCode, null);
DEVFormulaExecuter.CallBack = (HardFormulaDEVEntity step, StepStatus ss) =>
{
StepMessage = ss.Message;
if (!string.IsNullOrWhiteSpace(ss.DetailMessage))
{
DetailMessage = DetailMessage + "\r\n" + ss.DetailMessage;
}
if (ss.Status == ExecuteStatusEnum.Start)
{
dtStart = DateTime.Now;
}
if (step != null)
{
TotalSecond = step.StepTime;
IsBsr = step.IsBsr;
IsDI = step.IsDI;
IsN2 = step.IsN2;
IsDEV1 = step.IsDEV1;
IsDEV2 = step.IsDEV2;
BSRIOStatus = (ushort)(step.IsBsr ? 1 : 0);
DIIOStatus = (ushort)(step.IsDI ? 1 : 0);
N2IOStatus = (ushort)(step.IsN2 ? 1 : 0);
DEVIO1Status = (ushort)(step.IsDEV1 ? 1 : 0);
DEVIO2Status = (ushort)(step.IsDEV2 ? 1 : 0);
}
};
cancellationTokenSource = new CancellationTokenSource();
ProgressEnabled = false;
ProgressTime = 0;
dtStart = DateTime.Now;
timer.Start();
Task formulaTask = new Task(() =>
{
try
{
//<2F>رճ<D8B1>Ƭ̨<C6AC><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
HardFormulaFlowEntity mapFlow = null;
var flow = HardParameters.GetDEVMainAxisIO(StationCode, out mapFlow);
if (flow != null && mapFlow != null)
{
ushort closeBit = 0;
ControlCheck.PMCInvoke(false, "SetPortStatus", null, () => DeviceControl.SetPortStatus(mapFlow.NodeId, mapFlow.SlotId, mapFlow.BitNum, (ushort)(1 - closeBit)));
ControlCheck.PMCInvoke(false, "SetPortStatus", null, () => DeviceControl.SetPortStatus(flow.NodeId, flow.SlotId, flow.BitNum, closeBit));
//<2F>ȴ<EFBFBD>IO<49><4F><EFBFBD>׵<EFBFBD>λ
if ((IOHigh)flow.IsHigh == IOHigh.High || (IOHigh)flow.IsHigh == IOHigh.Low)
{
string refPort = (IOHigh)flow.IsHigh == IOHigh.High ? flow.RefPortHigh : flow.RefPortLow;
bool isTrue = true; // <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>źţ<C5BA><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
if (flow.RefPortHigh == flow.RefPortLow)
{
isTrue = (closeBit == 1);
}
//<2F><><EFBFBD><EFBFBD>false<73><65>ζ<EFBFBD>ų<EFBFBD>ʱ<EFBFBD>˳<EFBFBD>
ControlCheck.WaitDIDone(refPort, isTrue);
}
}
//<2F>رղ<D8B1>׽<EFBFBD><D7BD><EFBFBD><EFBFBD>
var action = HardParameters.GetDEVCUPIO(StationCode);
if (action != null)
{
ushort closeBit = 0;
ControlCheck.PMCInvoke(false, "SetPortStatus", null, () => DeviceControl.SetPortStatus(action.NodeId, action.SlotId, action.BitNo, (ushort)(1 - closeBit)));
//<2F>ȴ<EFBFBD>IO<49><4F><EFBFBD>׵<EFBFBD>λ
if ((IOHigh)action.IsHigh == IOHigh.High || (IOHigh)action.IsHigh == IOHigh.Low)
{
string refPort = (IOHigh)action.IsHigh == IOHigh.High ? action.RefPortHigh : action.RefPortLow;
bool isTrue = true; // <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>źţ<C5BA><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
if (action.RefPortHigh == action.RefPortLow)
{
isTrue = (closeBit == 1);
}
//<2F><><EFBFBD><EFBFBD>false<73><65>ζ<EFBFBD>ų<EFBFBD>ʱ<EFBFBD>˳<EFBFBD>
ControlCheck.WaitDIDone(refPort, isTrue);
}
}
//<2F>ӹ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
List<HardFormulaFlowEntity> flows = Global.Flows.FindAll(q => q.StationCode == StationCode && q.StepCode == string.Format("F{0}52", StationCode));
DEVFlowExecuter.Execute(cancellationTokenSource, flows, null, true, false);
DEVFormulaExecuter.Execute(cancellationTokenSource, steps, null, false);
//<2F>ӹ<EFBFBD><D3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
flows = Global.Flows.FindAll(q => q.StationCode == StationCode && q.IsPrepare);
DEVFlowExecuter.Execute(cancellationTokenSource, flows, null, true, true);
if (cancellationTokenSource.IsCancellationRequested)
{
StepMessage = "<22>ֹ<EFBFBD><D6B9><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>쳣!";
}
else
{
StepMessage = <><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!";
}
IsBsr = false;
IsDI = false;
IsN2 = false;
IsDEV1 = false;
IsDEV2 = false;
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 = "DEVTest";
fs.FlowCode = "DEVTest";
fs.Status = ExecuteStatusEnum.Prepare;
fs.StepStatues = new List<StepStatus>();
var record = HardFormulaDEVDA.Load(new PagerEntity() { PageIndex = 1, Rows = 9999, Conditions = " and FormulaId='" + SelectedFormula.RecId + "' " });
steps = (List<HardFormulaDEVEntity>)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
}
}