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 RobotStatusViewModule : ViewModelBase { #region base parameters private int rows = 20; private int pagenumber = 1; public RobotStatusView View { get; set; } private bool IsConfirm; private bool isLoading; #endregion #region constructor public RobotStatusViewModule() { View = new RobotStatusView(); View.DataContext = this; Status = new int[5]; InitializeCommands(); InitializeParameters(); LoadTimer(); RobotAxises = new ObservableCollection(); RobotAxises.Add(new ComboEntity() { RecId = "T", ComboCode = "T", ComboName = "T" }); RobotAxises.Add(new ComboEntity() { RecId = "Z", ComboCode = "Z", ComboName = "Z" }); RobotAxises.Add(new ComboEntity() { RecId = "R", ComboCode = "R", ComboName = "R" }); RobotAxises.Add(new ComboEntity() { RecId = "W", ComboCode = "W", ComboName = "W" }); SelectedRobotAxis = RobotAxises[0]; } private void InitializeCommands() { AddCommand = new DelegateCommand(OnAddCommand); MintusCommand = new DelegateCommand(OnMintusCommand); SpeedCommand = new DelegateCommand(OnSpeedCommand); } public override void InitializeParameters(object content = null) { IsConfirm = false; WindowTitle = ""; } ~RobotStatusViewModule() { } public void LoadDefaultValue() { } public void OnMoveSpeedTextChangedCommand(string value) { MoveSpeed = Common.ToInt(value); } private void LoadTimer() { Thread tr = new Thread(new ThreadStart((Action)(() => { string message; HardRobotCommandEntity command; while (!Global.IsTermination) { try { if (this.IsActiveChildPage) { View.Dispatcher.Invoke((Action)(() => { ErrorCode = Global.RobotErrorCode; ErrorInfo = Global.RobotErrorMessage; ErrorCause = Global.RobotErrorCause; ErrorWay = Global.RobotErrorWay; })); command = Global.RobotCommands.FirstOrDefault(q => q.CommandCode == Constant.STAT); message = Robot.Send(command.CommandCode); View.Dispatcher.Invoke((Action)(() => { CurrentStatus = message; })); command = Global.RobotCommands.FirstOrDefault(q => q.CommandCode == Constant.RCP); message = Robot.Send(string.Format("{0} {1}", command.CommandCode, command.CommandParam)); string[] messages = message.Split(','); View.Dispatcher.Invoke((Action)(() => { CoordTRead = messages.Length > 0 ? Common.ToDecimal(messages[0]) : 0; CoordRRead = messages.Length > 2 ? Common.ToDecimal(messages[1]) : 0; CoordZRead = messages.Length > 1 ? Common.ToDecimal(messages[2]) : 0; CoordLRead = messages.Length > 3 ? Common.ToDecimal(messages[3]) : 0; })); command = Global.RobotCommands.FirstOrDefault(q => q.CommandCode == Constant.RSPP); message = Robot.Send(string.Format("{0} {1}", command.CommandCode, command.CommandParam)); string[] speeds = message.Split(','); View.Dispatcher.Invoke((Action)(() => { MoveSpeedRead = (speeds.Length > 0 && speeds[0] != ">" ? Common.ToInt(speeds[0]) : 0); })); } } catch (Exception ex) { Global.WriteErrorLog(ex); } Thread.Sleep(5000); } }))); tr.IsBackground = true; tr.Start(); } #endregion #region Binding Properties public string WindowTitle { get; private set; } private int[] _Status; public int[] Status { get { return _Status; } set { _Status = value; OnPropertyChanged("Status"); } } private string _ControlName; public string ControlName { get { return _ControlName; } set { _ControlName = value; OnPropertyChanged("ControlName"); } } private string _ErrorCode; public string ErrorCode { get { return _ErrorCode; } set { _ErrorCode = value; OnPropertyChanged("ErrorCode"); } } private string _CurrentStatus; public string CurrentStatus { get { return _CurrentStatus; } set { _CurrentStatus = value; OnPropertyChanged("CurrentStatus"); } } private decimal _CoordTRead; public decimal CoordTRead { get { return _CoordTRead; } set { _CoordTRead = value; OnPropertyChanged("CoordTRead"); } } private decimal _CoordZRead; public decimal CoordZRead { get { return _CoordZRead; } set { _CoordZRead = value; OnPropertyChanged("CoordZRead"); } } private string _ErrorCause; public string ErrorCause { get { return _ErrorCause; } set { _ErrorCause = value; OnPropertyChanged("ErrorCause"); } } private string _ErrorWay; public string ErrorWay { get { return _ErrorWay; } set { _ErrorWay = value; OnPropertyChanged("ErrorWay"); } } private string _ErrorInfo; public string ErrorInfo { get { return _ErrorInfo; } set { _ErrorInfo = value; OnPropertyChanged("ErrorInfo"); } } private bool _ProgressEnabled; public bool ProgressEnabled { get { return _ProgressEnabled; } set { _ProgressEnabled = value; OnPropertyChanged("ProgressEnabled"); } } private decimal _CoordRRead; public decimal CoordRRead { get { return _CoordRRead; } set { _CoordRRead = value; OnPropertyChanged("CoordRRead"); } } private decimal _CoordLRead; public decimal CoordLRead { get { return _CoordLRead; } set { _CoordLRead = value; OnPropertyChanged("CoordLRead"); } } private decimal _CoordTWrite; public decimal CoordTWrite { get { return _CoordTWrite; } set { _CoordTWrite = value; OnPropertyChanged("CoordTWrite"); } } private decimal _CoordZWrite; public decimal CoordZWrite { get { return _CoordZWrite; } set { _CoordZWrite = value; OnPropertyChanged("CoordZWrite"); } } private decimal _CoordRWrite; public decimal CoordRWrite { get { return _CoordRWrite; } set { _CoordRWrite = value; OnPropertyChanged("CoordRWrite"); } } private decimal _CoordLWrite; public decimal CoordLWrite { get { return _CoordLWrite; } set { _CoordLWrite = value; OnPropertyChanged("CoordLWrite"); } } private int _MoveSpeed; public int MoveSpeed { get { return _MoveSpeed; } set { _MoveSpeed = value; OnPropertyChanged("MoveSpeed"); } } private int _MoveSpeedRead; public int MoveSpeedRead { get { return _MoveSpeedRead; } set { _MoveSpeedRead = value; OnPropertyChanged("MoveSpeedRead"); } } private ObservableCollection _RobotAxises; public ObservableCollection RobotAxises { get { return _RobotAxises; } set { _RobotAxises = value; OnPropertyChanged("RobotAxises"); } } private ComboEntity _SelectedRobotAxis; public ComboEntity SelectedRobotAxis { get { return _SelectedRobotAxis; } set { _SelectedRobotAxis = value; OnPropertyChanged("SelectedRobotAxis"); } } private double _MovePosition; public double MovePosition { get { return _MovePosition; } set { _MovePosition = value; OnPropertyChanged("MovePosition"); } } #endregion #region Binding Commands private ICommand _MintusCommand; public ICommand MintusCommand { get { return _MintusCommand; } set { _MintusCommand = value; OnPropertyChanged("MintusCommand"); } } private ICommand _AddCommand; public ICommand AddCommand { get { return _AddCommand; } set { _AddCommand = value; OnPropertyChanged("AddCommand"); } } private ICommand _SpeedCommand; public ICommand SpeedCommand { get { return _SpeedCommand; } set { _SpeedCommand = value; OnPropertyChanged("SpeedCommand"); } } #endregion #region Private Methods private void OnAddCommand(Object sender) { if (Helpering.IsRunning) { ShowErrorBox(string.Format("配方流程正在运行,不能手动操作机器人")); return; } if (SelectedRobotAxis == null) { ShowMessageBox(string.Format("请选择要移动的轴")); return; } int statusIndex = 0; if (Status[statusIndex] == 1) { return; } int CanNext = 0; Status[statusIndex] = 1; OnPropertyChanged("Status"); Thread tr = new Thread(new ThreadStart((Action)(() => { try { var command = Global.RobotCommands.FirstOrDefault(q => q.CommandCode == Constant.MOVR); var message = Robot.Send(string.Format("{0} {1},{2}", command.CommandCode, SelectedRobotAxis.ComboCode, MovePosition)); CanNext = 2; } catch { CanNext = 1; } }))); tr.IsBackground = true; tr.Start(); //等待信息返回 while (CanNext == 0) { Helpering.Sleep(100); } Status[statusIndex] = 0; OnPropertyChanged("Status"); } private void OnMintusCommand(Object sender) { if (Helpering.IsRunning) { ShowErrorBox(string.Format("配方流程正在运行,不能手动操作机器人")); return; } if (SelectedRobotAxis == null) { ShowMessageBox(string.Format("请选择要移动的轴")); return; } int statusIndex = 1; if (Status[statusIndex] == 1) { return; } int CanNext = 0; Status[statusIndex] = 1; OnPropertyChanged("Status"); Thread tr = new Thread(new ThreadStart((Action)(() => { try { var command = Global.RobotCommands.FirstOrDefault(q => q.CommandCode == Constant.MOVR); var message = Robot.Send(string.Format("{0} {1},-{2}", command.CommandCode, SelectedRobotAxis.ComboCode, MovePosition)); CanNext = 2; } catch { CanNext = 1; } }))); tr.IsBackground = true; tr.Start(); //等待信息返回 while (CanNext == 0) { Helpering.Sleep(100); } Status[statusIndex] = 0; OnPropertyChanged("Status"); } public void OnSpeedCommand(Object sender) { if (Helpering.IsRunning) { ShowErrorBox(string.Format("配方流程正在运行,不能手动操作机器人")); return; } int statusIndex = 3; if (Status[statusIndex] == 1) { return; } int CanNext = 0; Status[statusIndex] = 1; OnPropertyChanged("Status"); Thread tr = new Thread(new ThreadStart((Action)(() => { try { var command = Global.RobotCommands.FirstOrDefault(q => q.CommandCode == Constant.SSPP); var message = Robot.Send(string.Format("{0} {1},{2},{2},{2},{2}", command.CommandCode, command.CommandParam, MoveSpeed)); CanNext = 2; } catch { CanNext = 1; } }))); tr.IsBackground = true; tr.Start(); //等待信息返回 while (CanNext == 0) { Helpering.Sleep(100); } Status[statusIndex] = 0; OnPropertyChanged("Status"); } #endregion } }