using Aitex.Core.Common; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Log; using Aitex.Core.UI.Dialog; using Aitex.Core.UI.MVVM; using Aitex.Core.Util; using Aitex.Core.Utilities; using Caliburn.Micro.Core; using MECF.Framework.Common.CommonData.PUF; using MECF.Framework.Common.CommonData.Transporter; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.Layout; using MECF.Framework.Common.RecipeCenter; using MECF.Framework.Common.Utilities; using CyberX8_Core; using CyberX8_MainPages.PMs; using CyberX8_Themes.UserControls; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; namespace CyberX8_MainPages.ViewModels { public class TransporterTeachViewModel : BindableBase { #region 常量 private const string MOTOR_POSITION = "MotorPosition"; private const string IS_SWITCH_ON = "IsSwitchOn"; private const string IS_HOMED = "IsHomed"; private const string IS_MOVING = "IsMoving"; private const string CURRENT_STATION = "CurrentStation"; private const string TRANSPORTER_DATA = "TransporterData"; private const string MOTION_DATA = "MotionData"; #endregion #region 内部变量 private string _module; #region Gantry private string _gantryModuleName; private double _gantryMotionPosition; private string _gantryCurrentStation; private string _gantryCurrentCellName; private bool _gantryIsHomed; private bool _gantryIsMoving; private bool _gantryIsSwitchOn; private bool _gantryTorqueLimited; private CommandMotionData _gantryMotionData; #endregion #region Elevator private string _elevatorModuleName; private double _elevatorMotionPosition; private string _elevatorCurrentStation; private string _elevatorCurrentCellName; private bool _elevatorIsHomed; private bool _elevatorIsMoving; private bool _elevatorIsSwitchOn; private bool _elevatorTorqueLimited; private bool _elevatorIsInUpPlace; private CommandMotionData _elevatorMotionData; #endregion private TransporterData _transporterData; private bool _locked; private bool _unlocked; private bool _clamped; private bool _unclamped; private List _teachPositionList=new List(); private ProcessLayout _processLayout; /// /// 名称与Cell字典(key-Cell Id,value-cell 名称) /// private Dictionary _cellIdCellNameDic = new Dictionary(); /// RT查询key集合 /// private List _rtDataKeys = new List(); /// /// rt查询key数值字典 /// private Dictionary _rtDataValueDic = new Dictionary(); /// /// 定时器 /// private DispatcherTimer _timer; #endregion #region 属性 public string Module { get { return _module; } set { SetProperty(ref _module, value); } } public List TeachPositionList { get { return _teachPositionList; } set { SetProperty(ref _teachPositionList, value); } } public bool Locked { get { return _locked; } set { SetProperty(ref _locked, value); } } public bool Unlocked { get { return _unlocked; } set { SetProperty(ref _unlocked, value); } } public bool Clamped { get { return _clamped; } set { SetProperty(ref _clamped, value); } } public bool Unclamped { get { return _unclamped; } set { SetProperty(ref _unclamped, value); } } #region Gantry /// /// Gantry 模块名称 /// public string GantryModuleName { get { return _gantryModuleName; } set { SetProperty(ref _gantryModuleName, value); } } /// /// Gantry Motion Position /// public double GantryMotionPosition{ get { return _gantryMotionPosition; } set { SetProperty(ref _gantryMotionPosition, value); } } /// /// Gantry当前位置 /// public string GantryCurrentStation{ get { return _gantryCurrentStation; } set { SetProperty(ref _gantryCurrentStation, value); } } /// /// Gantry 当前Cell名称 /// public string GantryCurrentCellName { get { return _gantryCurrentCellName; } set { SetProperty(ref _gantryCurrentCellName, value); } } public bool GantryIsHomed { get { return _gantryIsHomed; } set { SetProperty(ref _gantryIsHomed, value); } } public bool GantryIsSwitchOn { get { return _gantryIsSwitchOn; } set { SetProperty(ref _gantryIsSwitchOn, value); } } public bool GantryIsMoving { get { return _gantryIsMoving; } set { SetProperty(ref _gantryIsMoving, value); } } public bool GantryTorqueLimited { get { return _gantryTorqueLimited; } set { SetProperty(ref _gantryTorqueLimited , value); } } public CommandMotionData GantryMotionData { get { return _gantryMotionData; } set { SetProperty(ref _gantryMotionData, value); } } #endregion #region Elevator public string ElevatorModuleName { get { return _elevatorModuleName; } set { SetProperty(ref _elevatorModuleName, value); } } /// /// Elevator Motion Position /// public double ElevatorMotionPosition { get { return _elevatorMotionPosition; } set { SetProperty(ref _elevatorMotionPosition, value); } } /// /// Elevator当前位置 /// public string ElevatorCurrentStation { get { return _elevatorCurrentStation; } set { SetProperty(ref _elevatorCurrentStation, value); } } /// /// Elevator当前位置 /// public string ElevatorCurrentCellName { get { return _elevatorCurrentCellName; } set { SetProperty(ref _elevatorCurrentCellName, value); } } public bool ElevatorIsHomed { get { return _elevatorIsHomed; } set { SetProperty(ref _elevatorIsHomed, value); } } public bool ElevatorIsSwitchOn { get { return _elevatorIsSwitchOn; } set { SetProperty(ref _elevatorIsSwitchOn, value); } } public bool ElevatorIsMoving { get { return _elevatorIsMoving; } set { SetProperty(ref _elevatorIsMoving, value); } } public bool ElevatorTorqueLimited { get { return _elevatorTorqueLimited; } set { SetProperty(ref _elevatorTorqueLimited , value); } } public bool ElevatorIsInUpPlace { get { return _elevatorIsInUpPlace; } set { SetProperty(ref _elevatorIsInUpPlace, value); } } public CommandMotionData ElevatorMotionData { get { return _elevatorMotionData; } set { SetProperty(ref _elevatorMotionData, value); } } #endregion /// /// Transporter数据 /// public TransporterData TransporterData { get { return _transporterData; } set { SetProperty(ref _transporterData, value); } } #endregion /// /// 构造函数 /// public TransporterTeachViewModel() { } /// /// 加载数据 /// public void LoadData(string systemName) { Module = systemName; LoadLayout(); int startCellId = (int)QueryDataClient.Instance.Service.GetConfig($"Transporter.{Module}.StartCellId"); int endCellId= (int)QueryDataClient.Instance.Service.GetConfig($"Transporter.{Module}.EndCellId"); if(_processLayout!=null&&TeachPositionList.Count==0) { List lst = GetCellItemsByRange(startCellId, endCellId, _processLayout.Items); TeachPositionList.AddRange(lst); } GantryModuleName = $"{Module}.Gantry"; ElevatorModuleName = $"{Module}.Elevator" ; AddDataKeys(); if (_timer == null) { _timer = new DispatcherTimer(); _timer.Interval = TimeSpan.FromMilliseconds(200); _timer.Tick += Timer_Tick; ; } _timer.Start(); } /// /// 定时器执行 /// /// /// /// private void Timer_Tick(object sender, EventArgs e) { if (_rtDataKeys.Count != 0) { _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys); if (_rtDataValueDic != null) { GantryMotionPosition = CommonFunction.GetValue(_rtDataValueDic, $"{GantryModuleName}.{MOTOR_POSITION}"); GantryIsHomed = CommonFunction.GetValue(_rtDataValueDic, $"{GantryModuleName}.{IS_HOMED}"); GantryIsSwitchOn = CommonFunction.GetValue(_rtDataValueDic, $"{GantryModuleName}.{IS_SWITCH_ON}"); GantryIsMoving = CommonFunction.GetValue(_rtDataValueDic, $"{GantryModuleName}.{IS_MOVING}"); GantryCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue(_rtDataValueDic, $"{GantryModuleName}.{CURRENT_STATION}"), GantryModuleName); GantryMotionData = CommonFunction.GetValue(_rtDataValueDic, $"{GantryModuleName}.{MOTION_DATA}"); if (GantryMotionData != null) { GantryTorqueLimited = GantryMotionData.TorqueLimited; } if (_cellIdCellNameDic.ContainsKey(GantryCurrentStation)) { GantryCurrentCellName = _cellIdCellNameDic[GantryCurrentStation]; } else { GantryCurrentCellName = GantryCurrentStation; } ElevatorMotionPosition = CommonFunction.GetValue(_rtDataValueDic, $"{ElevatorModuleName}.{MOTOR_POSITION}"); ElevatorIsHomed = CommonFunction.GetValue(_rtDataValueDic, $"{ElevatorModuleName}.{IS_HOMED}"); ElevatorIsSwitchOn = CommonFunction.GetValue(_rtDataValueDic, $"{ElevatorModuleName}.{IS_SWITCH_ON}"); ElevatorIsMoving = CommonFunction.GetValue(_rtDataValueDic, $"{ElevatorModuleName}.{IS_MOVING}"); ElevatorCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue(_rtDataValueDic, $"{ElevatorModuleName}.{CURRENT_STATION}"), ElevatorModuleName); ElevatorIsInUpPlace = "UP".Equals(ElevatorCurrentStation) ? true : false; if (_cellIdCellNameDic.ContainsKey(ElevatorCurrentStation)) { ElevatorCurrentCellName = _cellIdCellNameDic[ElevatorCurrentStation]; } else { ElevatorCurrentCellName = ElevatorCurrentStation; } ElevatorMotionData = CommonFunction.GetValue(_rtDataValueDic, $"{ElevatorModuleName}.{MOTION_DATA}"); if (ElevatorMotionData != null) { ElevatorTorqueLimited = ElevatorMotionData.TorqueLimited; } TransporterData = CommonFunction.GetValue(_rtDataValueDic, $"{Module}.{TRANSPORTER_DATA}"); if (TransporterData != null) { Locked = TransporterData.Locked1 && TransporterData.Locked2; Unlocked = TransporterData.Unlocked1 && TransporterData.Unlocked2; Clamped = TransporterData.ImmobilizeExtended1 && TransporterData.ImmobilizeExtended2; Unclamped = TransporterData.ImmobilizeRetracted1 && TransporterData.ImmobilizeRetracted2; } } } } /// /// 获取指定范围的单元项集合 /// /// /// /// /// private List GetCellItemsByRange(int startCellId, int endCellId, List items) { List lst = new List(); foreach (ProcessLayoutCellItem item in items) { if (item.CellId >= startCellId && item.CellId <= endCellId) { if (!string.IsNullOrEmpty(item.ModuleName)) { lst.Add(item.ModuleName); _cellIdCellNameDic[$"Cell{item.CellId}"] = item.ModuleName; } } } return lst; } /// /// 隐藏 /// public void Hide() { if (_timer != null) { _timer.Stop(); } } /// /// 加载Layout /// private void LoadLayout() { _rtDataKeys.Clear(); _rtDataKeys.Add("System.Layout"); _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys); if (_rtDataValueDic != null) { _processLayout = CommonFunction.GetValue(_rtDataValueDic, "System.Layout"); } } /// /// 初始化查询数据集合 /// private void AddDataKeys() { _rtDataKeys.Clear(); _rtDataKeys.Add($"{GantryModuleName}.{MOTOR_POSITION}"); _rtDataKeys.Add($"{GantryModuleName}.{IS_SWITCH_ON}"); _rtDataKeys.Add($"{GantryModuleName}.{IS_HOMED}"); _rtDataKeys.Add($"{GantryModuleName}.{IS_MOVING}"); _rtDataKeys.Add($"{GantryModuleName}.{CURRENT_STATION}"); _rtDataKeys.Add($"{GantryModuleName}.{MOTION_DATA}"); _rtDataKeys.Add($"{ElevatorModuleName}.{MOTOR_POSITION}"); _rtDataKeys.Add($"{ElevatorModuleName}.{IS_SWITCH_ON}"); _rtDataKeys.Add($"{ElevatorModuleName}.{IS_HOMED}"); _rtDataKeys.Add($"{ElevatorModuleName}.{IS_MOVING}"); _rtDataKeys.Add($"{ElevatorModuleName}.{CURRENT_STATION}"); _rtDataKeys.Add($"{ElevatorModuleName}.{MOTION_DATA}"); _rtDataKeys.Add($"{Module}.{TRANSPORTER_DATA}"); } } }