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 HPExecuteViewModule : ViewModelBase { #region base parameters private int rows = 20; private int pagenumber = 1; public HPExecuteView View { get; set; } private bool IsConfirm; private bool isLoading; private CancellationTokenSource cancellationTokenSource; private List steps; private System.Timers.Timer timer; private DateTime dtStart; private HPPrepareExecute HPFlowExecuter; private HPFormulaExecuter HPFormulaExecuter; #endregion #region constructor public HPExecuteViewModule() { View = new HPExecuteView(); View.DataContext = this; InitializeCommands(); InitializeParameters(); HPFlowExecuter = new HPPrepareExecute(); HPFormulaExecuter = new HPFormulaExecuter(); } 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(); } } ~HPExecuteViewModule() { } 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 _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 _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(); } 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; } HPFlowExecuter.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); ProgressEnabled = false; List flows = Global.Flows.FindAll(q => q.StationCode == formula.StationCode && q.IsPrepare); cancellationTokenSource = new CancellationTokenSource(); Task flowTask = new Task(() => { try { HPFlowExecuter.Execute(cancellationTokenSource, flows, null, true, false); ProgressEnabled = true; 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; } List flows = Global.Flows.FindAll(q => q.StationCode == StationCode && q.IsPrepare); StationLockControl.LockStation(StationCode, null); HPFormulaExecuter.CallBack = (HardFormulaHPEntity 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; HPFormulaExecuter.Execute(cancellationTokenSource, steps, null); HPFlowExecuter.Execute(cancellationTokenSource, flows, null, true, true); if (cancellationTokenSource.IsCancellationRequested) { StepMessage = "手工终止!"; } else { StepMessage = "执行完毕!"; } 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 = "HPTest"; fs.FlowCode = "HPTest"; fs.Status = ExecuteStatusEnum.Prepare; fs.StepStatues = new List(); var record = HardFormulaHPDA.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 } }