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 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 _HardFormulas; public ObservableCollection 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(); } HardFormulas.Clear(); var record = HardFormulaDA.Load(new PagerEntity() { PageIndex = pagenumber, Rows = rows, Conditions = " and StationCode='" + StationCode + "' " }); List logs = (List)record.rows; View.Dispatcher.Invoke((Action)(() => { foreach (var s in logs) { HardFormulas.Add(s); } OnPropertyChanged("HartFormula"); if (HardFormulas.Count > 0)//默认加载第一个 { 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("系统初始化未完成,请等待"); // return; //} if (SelectedFormula == null) { ShowMessageBox("请选择一个配方再进行加工!"); 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("请选择一个有效配方再进行加工!"); return; } if (StationLockControl.StationIsLock(StationCode)) { ShowMessageBox(HintMessage.StationLocked); return; } StationLockControl.LockStation(StationCode, null); List 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(" 执行异常:{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("系统初始化未完成,请等待"); // return; //} if (SelectedFormula == null) { ShowMessageBox("请选择一个配方再进行加工!"); return; } var status = DeviceControl.GetCardStatus(); if (status != CardStatus.Prepared) { Helpering.ShowErrorBox(string.Format("初始化轴卡失败,请确保板卡连接正常!")); return; } if (steps == null || steps.Count <= 0) { Helpering.ShowErrorBox(string.Format("请选择一个有效配方再进行加工!")); 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 { //关闭承片台,避免撞车 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)); //等待IO气缸到位 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; // 相同检测信号,则对照输出信号 if (flow.RefPortHigh == flow.RefPortLow) { isTrue = (closeBit == 1); } //返回false意味着超时退出 ControlCheck.WaitDIDone(refPort, isTrue); } } //关闭捕捉杯子 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))); //等待IO气缸到位 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; // 相同检测信号,则对照输出信号 if (action.RefPortHigh == action.RefPortLow) { isTrue = (closeBit == 1); } //返回false意味着超时退出 ControlCheck.WaitDIDone(refPort, isTrue); } } //加工前步骤 List 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); //加工结束后复位 flows = Global.Flows.FindAll(q => q.StationCode == StationCode && q.IsPrepare); DEVFlowExecuter.Execute(cancellationTokenSource, flows, null, true, true); if (cancellationTokenSource.IsCancellationRequested) { StepMessage = "手工终止或异常!"; } else { StepMessage = "执行完毕!"; } 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(" 执行异常:{0}!", ex.Message); ProgressEnabled = true; StationLockControl.UnLockStation(StationCode); } }, cancellationTokenSource.Token, TaskCreationOptions.LongRunning); formulaTask.Start(); } private void OnStopCommand(Object sender) { if (cancellationTokenSource != null) { StepMessage = "正在取消,请稍后!"; cancellationTokenSource.Cancel(); } } private void OnSelectedFormulaChangedCommand(Object sender) { LoadFormulaSteps(); } private void LoadFormulaSteps() { if (SelectedFormula != null) { HardFormulaEntity formula = HardFormulaDA.Load(SelectedFormula.RecId); if (formula == null) { ShowMessageBox("请选择一个有效配方再进行加工!"); return; } FlowStatus fs = new FlowStatus(); fs.FlowId = "DEVTest"; fs.FlowCode = "DEVTest"; fs.Status = ExecuteStatusEnum.Prepare; fs.StepStatues = new List(); var record = HardFormulaDEVDA.Load(new PagerEntity() { PageIndex = 1, Rows = 9999, Conditions = " and FormulaId='" + SelectedFormula.RecId + "' " }); steps = (List)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 } }