TransporterTeachViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using Aitex.Core.Common;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.UI.Dialog;
  5. using Aitex.Core.UI.MVVM;
  6. using Aitex.Core.Util;
  7. using Aitex.Core.Utilities;
  8. using Caliburn.Micro.Core;
  9. using MECF.Framework.Common.CommonData.PUF;
  10. using MECF.Framework.Common.CommonData.Transporter;
  11. using MECF.Framework.Common.DataCenter;
  12. using MECF.Framework.Common.Equipment;
  13. using MECF.Framework.Common.Layout;
  14. using MECF.Framework.Common.RecipeCenter;
  15. using MECF.Framework.Common.Utilities;
  16. using CyberX8_Core;
  17. using CyberX8_MainPages.PMs;
  18. using CyberX8_Themes.UserControls;
  19. using Prism.Mvvm;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Collections.ObjectModel;
  23. using System.ComponentModel;
  24. using System.IO;
  25. using System.Linq;
  26. using System.Reflection;
  27. using System.Runtime.InteropServices;
  28. using System.Text;
  29. using System.Threading.Tasks;
  30. using System.Timers;
  31. using System.Windows;
  32. using System.Windows.Controls;
  33. using System.Windows.Input;
  34. using System.Windows.Threading;
  35. namespace CyberX8_MainPages.ViewModels
  36. {
  37. public class TransporterTeachViewModel : BindableBase
  38. {
  39. #region 常量
  40. private const string MOTOR_POSITION = "MotorPosition";
  41. private const string IS_SWITCH_ON = "IsSwitchOn";
  42. private const string IS_HOMED = "IsHomed";
  43. private const string IS_MOVING = "IsMoving";
  44. private const string CURRENT_STATION = "CurrentStation";
  45. private const string TRANSPORTER_DATA = "TransporterData";
  46. private const string MOTION_DATA = "MotionData";
  47. #endregion
  48. #region 内部变量
  49. private string _module;
  50. #region Gantry
  51. private string _gantryModuleName;
  52. private double _gantryMotionPosition;
  53. private string _gantryCurrentStation;
  54. private string _gantryCurrentCellName;
  55. private bool _gantryIsHomed;
  56. private bool _gantryIsMoving;
  57. private bool _gantryIsSwitchOn;
  58. private bool _gantryTorqueLimited;
  59. private CommandMotionData _gantryMotionData;
  60. #endregion
  61. #region Elevator
  62. private string _elevatorModuleName;
  63. private double _elevatorMotionPosition;
  64. private string _elevatorCurrentStation;
  65. private string _elevatorCurrentCellName;
  66. private bool _elevatorIsHomed;
  67. private bool _elevatorIsMoving;
  68. private bool _elevatorIsSwitchOn;
  69. private bool _elevatorTorqueLimited;
  70. private bool _elevatorIsInUpPlace;
  71. private CommandMotionData _elevatorMotionData;
  72. #endregion
  73. private TransporterData _transporterData;
  74. private bool _locked;
  75. private bool _unlocked;
  76. private bool _clamped;
  77. private bool _unclamped;
  78. private List<string> _teachPositionList=new List<string>();
  79. private ProcessLayout _processLayout;
  80. /// <summary>
  81. /// 名称与Cell字典(key-Cell Id,value-cell 名称)
  82. /// </summary>
  83. private Dictionary<string, string> _cellIdCellNameDic = new Dictionary<string, string>();
  84. /// RT查询key集合
  85. /// </summary>
  86. private List<string> _rtDataKeys = new List<string>();
  87. /// <summary>
  88. /// rt查询key数值字典
  89. /// </summary>
  90. private Dictionary<string, object> _rtDataValueDic = new Dictionary<string, object>();
  91. /// <summary>
  92. /// 定时器
  93. /// </summary>
  94. private DispatcherTimer _timer;
  95. #endregion
  96. #region 属性
  97. public string Module { get { return _module; } set { SetProperty(ref _module, value); } }
  98. public List<string> TeachPositionList { get { return _teachPositionList; } set { SetProperty(ref _teachPositionList, value); } }
  99. public bool Locked { get { return _locked; } set { SetProperty(ref _locked, value); } }
  100. public bool Unlocked { get { return _unlocked; } set { SetProperty(ref _unlocked, value); } }
  101. public bool Clamped { get { return _clamped; } set { SetProperty(ref _clamped, value); } }
  102. public bool Unclamped { get { return _unclamped; } set { SetProperty(ref _unclamped, value); } }
  103. #region Gantry
  104. /// <summary>
  105. /// Gantry 模块名称
  106. /// </summary>
  107. public string GantryModuleName { get { return _gantryModuleName; } set { SetProperty(ref _gantryModuleName, value); } }
  108. /// <summary>
  109. /// Gantry Motion Position
  110. /// </summary>
  111. public double GantryMotionPosition{ get { return _gantryMotionPosition; } set { SetProperty(ref _gantryMotionPosition, value); } }
  112. /// <summary>
  113. /// Gantry当前位置
  114. /// </summary>
  115. public string GantryCurrentStation{ get { return _gantryCurrentStation; } set { SetProperty(ref _gantryCurrentStation, value); } }
  116. /// <summary>
  117. /// Gantry 当前Cell名称
  118. /// </summary>
  119. public string GantryCurrentCellName { get { return _gantryCurrentCellName; } set { SetProperty(ref _gantryCurrentCellName, value); } }
  120. public bool GantryIsHomed { get { return _gantryIsHomed; } set { SetProperty(ref _gantryIsHomed, value); } }
  121. public bool GantryIsSwitchOn { get { return _gantryIsSwitchOn; } set { SetProperty(ref _gantryIsSwitchOn, value); } }
  122. public bool GantryIsMoving { get { return _gantryIsMoving; } set { SetProperty(ref _gantryIsMoving, value); } }
  123. public bool GantryTorqueLimited { get { return _gantryTorqueLimited; } set { SetProperty(ref _gantryTorqueLimited , value); } }
  124. public CommandMotionData GantryMotionData { get { return _gantryMotionData; } set { SetProperty(ref _gantryMotionData, value); } }
  125. #endregion
  126. #region Elevator
  127. public string ElevatorModuleName { get { return _elevatorModuleName; } set { SetProperty(ref _elevatorModuleName, value); } }
  128. /// <summary>
  129. /// Elevator Motion Position
  130. /// </summary>
  131. public double ElevatorMotionPosition { get { return _elevatorMotionPosition; } set { SetProperty(ref _elevatorMotionPosition, value); } }
  132. /// <summary>
  133. /// Elevator当前位置
  134. /// </summary>
  135. public string ElevatorCurrentStation { get { return _elevatorCurrentStation; } set { SetProperty(ref _elevatorCurrentStation, value); } }
  136. /// <summary>
  137. /// Elevator当前位置
  138. /// </summary>
  139. public string ElevatorCurrentCellName { get { return _elevatorCurrentCellName; } set { SetProperty(ref _elevatorCurrentCellName, value); } }
  140. public bool ElevatorIsHomed { get { return _elevatorIsHomed; } set { SetProperty(ref _elevatorIsHomed, value); } }
  141. public bool ElevatorIsSwitchOn { get { return _elevatorIsSwitchOn; } set { SetProperty(ref _elevatorIsSwitchOn, value); } }
  142. public bool ElevatorIsMoving { get { return _elevatorIsMoving; } set { SetProperty(ref _elevatorIsMoving, value); } }
  143. public bool ElevatorTorqueLimited { get { return _elevatorTorqueLimited; } set { SetProperty(ref _elevatorTorqueLimited , value); } }
  144. public bool ElevatorIsInUpPlace { get { return _elevatorIsInUpPlace; } set { SetProperty(ref _elevatorIsInUpPlace, value); } }
  145. public CommandMotionData ElevatorMotionData { get { return _elevatorMotionData; } set { SetProperty(ref _elevatorMotionData, value); } }
  146. #endregion
  147. /// <summary>
  148. /// Transporter数据
  149. /// </summary>
  150. public TransporterData TransporterData { get { return _transporterData; } set { SetProperty(ref _transporterData, value); } }
  151. #endregion
  152. /// <summary>
  153. /// 构造函数
  154. /// </summary>
  155. public TransporterTeachViewModel()
  156. {
  157. }
  158. /// <summary>
  159. /// 加载数据
  160. /// </summary>
  161. public void LoadData(string systemName)
  162. {
  163. Module = systemName;
  164. LoadLayout();
  165. int startCellId = (int)QueryDataClient.Instance.Service.GetConfig($"Transporter.{Module}.StartCellId");
  166. int endCellId= (int)QueryDataClient.Instance.Service.GetConfig($"Transporter.{Module}.EndCellId");
  167. if(_processLayout!=null&&TeachPositionList.Count==0)
  168. {
  169. List<string> lst = GetCellItemsByRange(startCellId, endCellId, _processLayout.Items);
  170. TeachPositionList.AddRange(lst);
  171. }
  172. GantryModuleName = $"{Module}.Gantry";
  173. ElevatorModuleName = $"{Module}.Elevator" ;
  174. AddDataKeys();
  175. if (_timer == null)
  176. {
  177. _timer = new DispatcherTimer();
  178. _timer.Interval = TimeSpan.FromMilliseconds(200);
  179. _timer.Tick += Timer_Tick; ;
  180. }
  181. _timer.Start();
  182. }
  183. /// <summary>
  184. /// 定时器执行
  185. /// </summary>
  186. /// <param name="sender"></param>
  187. /// <param name="e"></param>
  188. /// <exception cref="NotImplementedException"></exception>
  189. private void Timer_Tick(object sender, EventArgs e)
  190. {
  191. if (_rtDataKeys.Count != 0)
  192. {
  193. _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  194. if (_rtDataValueDic != null)
  195. {
  196. GantryMotionPosition = CommonFunction.GetValue<double>(_rtDataValueDic, $"{GantryModuleName}.{MOTOR_POSITION}");
  197. GantryIsHomed = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{GantryModuleName}.{IS_HOMED}");
  198. GantryIsSwitchOn = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{GantryModuleName}.{IS_SWITCH_ON}");
  199. GantryIsMoving = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{GantryModuleName}.{IS_MOVING}");
  200. GantryCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue<string>(_rtDataValueDic, $"{GantryModuleName}.{CURRENT_STATION}"), GantryModuleName);
  201. GantryMotionData = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{GantryModuleName}.{MOTION_DATA}");
  202. if (GantryMotionData != null)
  203. {
  204. GantryTorqueLimited = GantryMotionData.TorqueLimited;
  205. }
  206. if (_cellIdCellNameDic.ContainsKey(GantryCurrentStation))
  207. {
  208. GantryCurrentCellName = _cellIdCellNameDic[GantryCurrentStation];
  209. }
  210. else
  211. {
  212. GantryCurrentCellName = GantryCurrentStation;
  213. }
  214. ElevatorMotionPosition = CommonFunction.GetValue<double>(_rtDataValueDic, $"{ElevatorModuleName}.{MOTOR_POSITION}");
  215. ElevatorIsHomed = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ElevatorModuleName}.{IS_HOMED}");
  216. ElevatorIsSwitchOn = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ElevatorModuleName}.{IS_SWITCH_ON}");
  217. ElevatorIsMoving = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ElevatorModuleName}.{IS_MOVING}");
  218. ElevatorCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue<string>(_rtDataValueDic, $"{ElevatorModuleName}.{CURRENT_STATION}"), ElevatorModuleName);
  219. ElevatorIsInUpPlace = "UP".Equals(ElevatorCurrentStation) ? true : false;
  220. if (_cellIdCellNameDic.ContainsKey(ElevatorCurrentStation))
  221. {
  222. ElevatorCurrentCellName = _cellIdCellNameDic[ElevatorCurrentStation];
  223. }
  224. else
  225. {
  226. ElevatorCurrentCellName = ElevatorCurrentStation;
  227. }
  228. ElevatorMotionData = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{ElevatorModuleName}.{MOTION_DATA}");
  229. if (ElevatorMotionData != null)
  230. {
  231. ElevatorTorqueLimited = ElevatorMotionData.TorqueLimited;
  232. }
  233. TransporterData = CommonFunction.GetValue<TransporterData>(_rtDataValueDic, $"{Module}.{TRANSPORTER_DATA}");
  234. if (TransporterData != null)
  235. {
  236. Locked = TransporterData.Locked1 && TransporterData.Locked2;
  237. Unlocked = TransporterData.Unlocked1 && TransporterData.Unlocked2;
  238. Clamped = TransporterData.ImmobilizeExtended1 && TransporterData.ImmobilizeExtended2;
  239. Unclamped = TransporterData.ImmobilizeRetracted1 && TransporterData.ImmobilizeRetracted2;
  240. }
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// 获取指定范围的单元项集合
  246. /// </summary>
  247. /// <param name="startCellId"></param>
  248. /// <param name="endCellId"></param>
  249. /// <param name="items"></param>
  250. /// <returns></returns>
  251. private List<string> GetCellItemsByRange(int startCellId, int endCellId, List<ProcessLayoutCellItem> items)
  252. {
  253. List<string> lst = new List<string>();
  254. foreach (ProcessLayoutCellItem item in items)
  255. {
  256. if (item.CellId >= startCellId && item.CellId <= endCellId)
  257. {
  258. if (!string.IsNullOrEmpty(item.ModuleName))
  259. {
  260. lst.Add(item.ModuleName);
  261. _cellIdCellNameDic[$"Cell{item.CellId}"] = item.ModuleName;
  262. }
  263. }
  264. }
  265. return lst;
  266. }
  267. /// <summary>
  268. /// 隐藏
  269. /// </summary>
  270. public void Hide()
  271. {
  272. if (_timer != null)
  273. {
  274. _timer.Stop();
  275. }
  276. }
  277. /// <summary>
  278. /// 加载Layout
  279. /// </summary>
  280. private void LoadLayout()
  281. {
  282. _rtDataKeys.Clear();
  283. _rtDataKeys.Add("System.Layout");
  284. _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  285. if (_rtDataValueDic != null)
  286. {
  287. _processLayout = CommonFunction.GetValue<ProcessLayout>(_rtDataValueDic, "System.Layout");
  288. }
  289. }
  290. /// <summary>
  291. /// 初始化查询数据集合
  292. /// </summary>
  293. private void AddDataKeys()
  294. {
  295. _rtDataKeys.Clear();
  296. _rtDataKeys.Add($"{GantryModuleName}.{MOTOR_POSITION}");
  297. _rtDataKeys.Add($"{GantryModuleName}.{IS_SWITCH_ON}");
  298. _rtDataKeys.Add($"{GantryModuleName}.{IS_HOMED}");
  299. _rtDataKeys.Add($"{GantryModuleName}.{IS_MOVING}");
  300. _rtDataKeys.Add($"{GantryModuleName}.{CURRENT_STATION}");
  301. _rtDataKeys.Add($"{GantryModuleName}.{MOTION_DATA}");
  302. _rtDataKeys.Add($"{ElevatorModuleName}.{MOTOR_POSITION}");
  303. _rtDataKeys.Add($"{ElevatorModuleName}.{IS_SWITCH_ON}");
  304. _rtDataKeys.Add($"{ElevatorModuleName}.{IS_HOMED}");
  305. _rtDataKeys.Add($"{ElevatorModuleName}.{IS_MOVING}");
  306. _rtDataKeys.Add($"{ElevatorModuleName}.{CURRENT_STATION}");
  307. _rtDataKeys.Add($"{ElevatorModuleName}.{MOTION_DATA}");
  308. _rtDataKeys.Add($"{Module}.{TRANSPORTER_DATA}");
  309. }
  310. }
  311. }