TransporterViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using MECF.Framework.Common.CommonData.Transporter;
  2. using MECF.Framework.Common.DataCenter;
  3. using MECF.Framework.Common.Equipment;
  4. using MECF.Framework.Common.Layout;
  5. using MECF.Framework.Common.ToolLayout;
  6. using MECF.Framework.Common.Utilities;
  7. using Prism.Mvvm;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.ComponentModel;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Timers;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Input;
  21. using System.Windows.Threading;
  22. namespace CyberX8_MainPages.ViewModels
  23. {
  24. public class TransporterViewModel : BindableBase
  25. {
  26. #region 常量
  27. private const string TRANSPORTER_DATA = "TransporterData";
  28. private const string IS_SWITCH_ON = "IsSwitchOn";
  29. private const string IS_HOMED = "IsHomed";
  30. private const string IS_MOVING = "IsMoving";
  31. private const string FORWARD_LIMIT = "ForwardLimit";
  32. #endregion
  33. #region 内部变量
  34. private ProcessLayout _processLayout;
  35. /// RT查询key集合
  36. /// </summary>
  37. private List<string> _rtDataKeys = new List<string>();
  38. /// <summary>
  39. /// rt查询key数值字典
  40. /// </summary>
  41. private Dictionary<string, object> _rtDataValueDic = new Dictionary<string, object>();
  42. /// <summary>
  43. /// 定时器
  44. /// </summary>
  45. private DispatcherTimer _timer;
  46. #region Loader Transporter
  47. private TransporterData _loaderTransporterData;
  48. private string _loaderbarcodeReaderDeviceName;
  49. private bool _loaderGantryMotorOn;
  50. private bool _loaderElevatorMotorOn;
  51. private bool _loaderGantryHomed;
  52. private bool _loaderElevatorHomed;
  53. private bool _loaderGantryMoving;
  54. private bool _loaderElevatorMoving;
  55. private bool _loaderElevatorInUpPlace;
  56. private bool _loaderElevatorJam;
  57. private string _loaderBarcode;
  58. private List<string> _loaderPositionList;
  59. private string _loaderFromSelectedItem;
  60. private string _loaderToSelectedItem;
  61. /// <summary>
  62. /// 模块Homed状态
  63. /// </summary>
  64. private bool _loaderTransporterIsHomed;
  65. #endregion
  66. #region Process Transporter
  67. private TransporterData _processTransporterData;
  68. private string _processbarcodeReaderDeviceName;
  69. private bool _processGantryMotorOn;
  70. private bool _processElevatorMotorOn;
  71. private bool _processGantryHomed;
  72. private bool _processElevatorHomed;
  73. private bool _processGantryMoving;
  74. private bool _processElevatorMoving;
  75. private bool _processElevatorInUpPlace;
  76. private bool _processElevatorJam;
  77. private string _processBarcode;
  78. private List<string> _processPositionList;
  79. private string _processFromSelectedItem;
  80. private string _processToSelectedItem;
  81. private bool _processTransporterIsHomed;
  82. #endregion
  83. #endregion
  84. #region 属性
  85. #region Loader Transporter
  86. public string LoaderBarcodeReaderDeviceName { get { return _loaderbarcodeReaderDeviceName; } set { SetProperty(ref _loaderbarcodeReaderDeviceName, value); } }
  87. public TransporterData LoaderTransporterData { get { return _loaderTransporterData; } set { SetProperty(ref _loaderTransporterData, value); } }
  88. public bool LoaderGantryMotorOn { get { return _loaderGantryMotorOn; } set { SetProperty(ref _loaderGantryMotorOn,value); } }
  89. public bool LoaderElevatorMotorOn { get { return _loaderElevatorMotorOn; } set { SetProperty(ref _loaderElevatorMotorOn,value); } }
  90. public bool LoaderGantryHomed { get { return _loaderGantryHomed; } set { SetProperty(ref _loaderGantryHomed,value); } }
  91. public bool LoaderElevatorHomed { get { return _loaderElevatorHomed; } set { SetProperty(ref _loaderElevatorHomed,value); } }
  92. public bool LoaderGantryMoving { get { return _loaderGantryMoving; } set { SetProperty(ref _loaderGantryMoving, value); } }
  93. public bool LoaderElevatorMoving { get { return _loaderElevatorMoving; } set { SetProperty(ref _loaderElevatorMoving,value); } }
  94. public bool LoaderElevatorIsInUpPlace { get { return _loaderElevatorInUpPlace; } set { SetProperty(ref _loaderElevatorInUpPlace, value); } }
  95. public string LoaderBarcode { get { return _loaderBarcode; } set { SetProperty(ref _loaderBarcode, value); } }
  96. public List<string> LoaderPositionList { get { return _loaderPositionList; } set { SetProperty(ref _loaderPositionList, value); } }
  97. public string LoaderFromSelectedItem { get { return _loaderFromSelectedItem; } set { SetProperty(ref _loaderFromSelectedItem, value); } }
  98. public string LoaderToSelectedItem { get { return _loaderToSelectedItem; } set { SetProperty(ref _loaderToSelectedItem, value); } }
  99. /// <summary>
  100. /// Loader Homed状态
  101. /// </summary>
  102. public bool LoaderTransporterIsHomed { get { return _loaderTransporterIsHomed; } set { SetProperty(ref _loaderTransporterIsHomed, value); } }
  103. // <summary>
  104. /// ForwarLimit(钢带信号)
  105. /// </summary>
  106. public bool LoaderElevatorJam { get { return _loaderElevatorJam; } set { SetProperty(ref _loaderElevatorJam, value); } }
  107. #endregion
  108. #region Process Transporter
  109. public string ProcessBarcodeReaderDeviceName { get { return _processbarcodeReaderDeviceName; } set { SetProperty(ref _processbarcodeReaderDeviceName, value); } }
  110. public TransporterData ProcessTransporterData { get { return _processTransporterData; } set { SetProperty(ref _processTransporterData, value); } }
  111. public bool ProcessGantryMotorOn { get { return _processGantryMotorOn; } set { SetProperty(ref _processGantryMotorOn, value); } }
  112. public bool ProcessElevatorMotorOn { get { return _processElevatorMotorOn; } set { SetProperty(ref _processElevatorMotorOn, value); } }
  113. public bool ProcessGantryHomed { get { return _processGantryHomed; } set { SetProperty(ref _processGantryHomed, value); } }
  114. public bool ProcessElevatorHomed { get { return _processElevatorHomed; } set { SetProperty(ref _processElevatorHomed, value); } }
  115. public bool ProcessGantryMoving { get { return _processGantryMoving; } set { SetProperty(ref _processGantryMoving, value); } }
  116. public bool ProcessElevatorIsInUpPlace { get { return _processElevatorInUpPlace; } set { SetProperty(ref _processElevatorInUpPlace, value); } }
  117. public bool ProcessElevatorMoving { get { return _processElevatorMoving; } set { SetProperty(ref _processElevatorMoving, value); } }
  118. public string ProcessBarcode { get { return _processBarcode; } set { SetProperty(ref _processBarcode, value); } }
  119. public List<string> ProcessPositionList { get { return _processPositionList; } set { SetProperty(ref _processPositionList, value); } }
  120. public string ProcessFromSelectedItem { get { return _processFromSelectedItem; } set { SetProperty(ref _processFromSelectedItem, value); } }
  121. public string ProcessToSelectedItem { get { return _processToSelectedItem; } set { SetProperty(ref _processToSelectedItem, value); } }
  122. /// <summary>
  123. /// Process Homed状态
  124. /// </summary>
  125. public bool ProcessTransporterIsHomed { get { return _processTransporterIsHomed; } set { SetProperty(ref _processTransporterIsHomed, value); } }
  126. /// <summary>
  127. /// ForwarLimit(钢带信号)
  128. /// </summary>
  129. public bool ProcessElevatorJam { get { return _processElevatorJam; } set { SetProperty(ref _processElevatorJam, value); } }
  130. #endregion
  131. #endregion
  132. #region 指令
  133. #endregion
  134. /// <summary>
  135. /// 构造函数
  136. /// </summary>
  137. public TransporterViewModel()
  138. {
  139. LoaderPositionList = new List<string>();
  140. ProcessPositionList = new List<string>();
  141. }
  142. /// <summary>
  143. /// 加载数据
  144. /// </summary>
  145. public void LoadData(string system)
  146. {
  147. LoadLayout();
  148. LoadLoaderTransferPositionList();
  149. LoadProcessTransferPositionList();
  150. if (_timer == null)
  151. {
  152. _timer = new DispatcherTimer();
  153. _timer.Interval = TimeSpan.FromMilliseconds(200);
  154. _timer.Tick += Timer_Tick; ;
  155. }
  156. InitialKeys();
  157. _timer.Start();
  158. }
  159. private void Timer_Tick(object sender, EventArgs e)
  160. {
  161. if (_rtDataKeys.Count != 0)
  162. {
  163. _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  164. if (_rtDataValueDic != null)
  165. {
  166. LoaderTransporterData = CommonFunction.GetValue<TransporterData>(_rtDataValueDic, $"{ModuleName.Transporter2}.{TRANSPORTER_DATA}");
  167. LoaderGantryMotorOn = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter2}.Gantry.{IS_SWITCH_ON}");
  168. LoaderGantryHomed = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter2}.Gantry.{IS_HOMED}");
  169. LoaderElevatorMotorOn = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter2}.Elevator.{IS_SWITCH_ON}");
  170. LoaderElevatorHomed = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter2}.Elevator.{IS_HOMED}");
  171. LoaderGantryMoving = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter2}.Gantry.{IS_MOVING}");
  172. LoaderElevatorMoving = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter2}.Elevator.{IS_MOVING}");
  173. LoaderTransporterIsHomed = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter2}.{IS_HOMED}");
  174. LoaderBarcode = CommonFunction.GetValue<string>(_rtDataValueDic, $"{LoaderBarcodeReaderDeviceName}.BarcodeValue");
  175. LoaderElevatorJam = !CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter2}.Elevator.{FORWARD_LIMIT}");
  176. ProcessTransporterData = CommonFunction.GetValue<TransporterData>(_rtDataValueDic, $"{ModuleName.Transporter1}.{TRANSPORTER_DATA}");
  177. ProcessGantryMotorOn = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter1}.Gantry.{IS_SWITCH_ON}");
  178. ProcessGantryHomed = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter1}.Gantry.{IS_HOMED}");
  179. ProcessElevatorMotorOn = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter1}.Elevator.{IS_SWITCH_ON}");
  180. ProcessElevatorHomed = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter1}.Elevator.{IS_HOMED}");
  181. ProcessGantryMoving = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter1}.Gantry.{IS_MOVING}");
  182. ProcessElevatorMoving = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter1}.Elevator.{IS_MOVING}");
  183. ProcessTransporterIsHomed = CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter1}.{IS_HOMED}");
  184. ProcessBarcode = CommonFunction.GetValue<string>(_rtDataValueDic, $"{ProcessBarcodeReaderDeviceName}.BarcodeValue");
  185. ProcessElevatorJam = !CommonFunction.GetValue<bool>(_rtDataValueDic, $"{ModuleName.Transporter1}.Elevator.{FORWARD_LIMIT}");
  186. string loaderElevatorStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue<string>(_rtDataValueDic, $"{ModuleName.Transporter2}.Elevator.CurrentStation"), $"{ModuleName.Transporter2}.Elevator");
  187. LoaderElevatorIsInUpPlace = "UP".Equals(loaderElevatorStation) ? true : false;
  188. string processElevatorStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue<string>(_rtDataValueDic, $"{ModuleName.Transporter1}.Elevator.CurrentStation"), $"{ModuleName.Transporter1}.Elevator");
  189. ProcessElevatorIsInUpPlace = "UP".Equals(processElevatorStation) ? true : false;
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// 隐藏
  195. /// </summary>
  196. public void Hide()
  197. {
  198. if (_timer != null)
  199. {
  200. _timer.Stop();
  201. }
  202. }
  203. /// <summary>
  204. /// 加载Layout
  205. /// </summary>
  206. private void LoadLayout()
  207. {
  208. _rtDataKeys.Clear();
  209. _rtDataKeys.Add("System.Layout");
  210. _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  211. if (_rtDataValueDic != null)
  212. {
  213. _processLayout = CommonFunction.GetValue<ProcessLayout>(_rtDataValueDic, "System.Layout");
  214. }
  215. }
  216. /// <summary>
  217. /// 加载Loader Transfer位置集合
  218. /// </summary>
  219. private void LoadLoaderTransferPositionList()
  220. {
  221. int startCellId = (int)QueryDataClient.Instance.Service.GetConfig($"Transporter.{ModuleName.Transporter2}.StartCellId");
  222. int endCellId = (int)QueryDataClient.Instance.Service.GetConfig($"Transporter.{ModuleName.Transporter2}.EndCellId");
  223. if (_processLayout != null && LoaderPositionList.Count == 0)
  224. {
  225. List<string> lst=GetCellItemsByRange(startCellId, endCellId,_processLayout.Items);
  226. LoaderPositionList.AddRange(lst);
  227. }
  228. }
  229. /// <summary>
  230. /// 加载Process Transfer位置集合
  231. /// </summary>
  232. private void LoadProcessTransferPositionList()
  233. {
  234. int startCellId = (int)QueryDataClient.Instance.Service.GetConfig($"Transporter.{ModuleName.Transporter1}.StartCellId");
  235. int endCellId = (int)QueryDataClient.Instance.Service.GetConfig($"Transporter.{ModuleName.Transporter1}.EndCellId");
  236. if (_processLayout != null && ProcessPositionList.Count == 0)
  237. {
  238. List<string> lst = GetCellItemsByRange(startCellId, endCellId, _processLayout.Items);
  239. ProcessPositionList.AddRange(lst);
  240. }
  241. }
  242. /// <summary>
  243. /// 获取指定范围的单元项集合
  244. /// </summary>
  245. /// <param name="startCellId"></param>
  246. /// <param name="endCellId"></param>
  247. /// <param name="items"></param>
  248. /// <returns></returns>
  249. private List<string> GetCellItemsByRange(int startCellId,int endCellId,List<ProcessLayoutCellItem> items)
  250. {
  251. List<string> lst = new List<string>();
  252. foreach(ProcessLayoutCellItem item in items)
  253. {
  254. if(item.CellId>=startCellId&&item.CellId<=endCellId)
  255. {
  256. if (!string.IsNullOrEmpty(item.ModuleName))
  257. {
  258. lst.Add(item.ModuleName);
  259. }
  260. }
  261. }
  262. return lst;
  263. }
  264. /// <summary>
  265. /// 初始化Keys
  266. /// </summary>
  267. private void InitialKeys()
  268. {
  269. _rtDataKeys.Clear();
  270. List<string> lst = new List<string>();
  271. lst.Add($"{ModuleName.Transporter1}.BarcodeReader");
  272. lst.Add($"{ModuleName.Transporter2}.BarcodeReader");
  273. _rtDataValueDic = QueryDataClient.Instance.Service.PollData(lst);
  274. if (_rtDataValueDic != null)
  275. {
  276. LoaderBarcodeReaderDeviceName = CommonFunction.GetValue<string>(_rtDataValueDic, $"{ModuleName.Transporter2}.BarcodeReader");
  277. ProcessBarcodeReaderDeviceName = CommonFunction.GetValue<string>(_rtDataValueDic, $"{ModuleName.Transporter1}.BarcodeReader");
  278. if (!string.IsNullOrEmpty(LoaderBarcodeReaderDeviceName))
  279. {
  280. _rtDataKeys.Add($"{LoaderBarcodeReaderDeviceName}.BarcodeValue");
  281. }
  282. if (!string.IsNullOrEmpty(ProcessBarcodeReaderDeviceName))
  283. {
  284. _rtDataKeys.Add($"{ProcessBarcodeReaderDeviceName}.BarcodeValue");
  285. }
  286. }
  287. _rtDataKeys.Add($"{ModuleName.Transporter1}.{TRANSPORTER_DATA}");
  288. _rtDataKeys.Add($"{ModuleName.Transporter1}.{IS_HOMED}");
  289. _rtDataKeys.Add($"{ModuleName.Transporter1}.Gantry.{IS_HOMED}");
  290. _rtDataKeys.Add($"{ModuleName.Transporter1}.Elevator.{IS_HOMED}");
  291. _rtDataKeys.Add($"{ModuleName.Transporter1}.Elevator.CurrentStation");
  292. _rtDataKeys.Add($"{ModuleName.Transporter1}.Gantry.{IS_SWITCH_ON}");
  293. _rtDataKeys.Add($"{ModuleName.Transporter1}.Elevator.{IS_SWITCH_ON}");
  294. _rtDataKeys.Add($"{ModuleName.Transporter1}.Gantry.{IS_MOVING}");
  295. _rtDataKeys.Add($"{ModuleName.Transporter1}.Elevator.{IS_MOVING}");
  296. _rtDataKeys.Add($"{ModuleName.Transporter1}.Elevator.{FORWARD_LIMIT}");
  297. _rtDataKeys.Add($"{ModuleName.Transporter2}.{TRANSPORTER_DATA}");
  298. _rtDataKeys.Add($"{ModuleName.Transporter2}.{IS_HOMED}");
  299. _rtDataKeys.Add($"{ModuleName.Transporter2}.Gantry.{IS_HOMED}");
  300. _rtDataKeys.Add($"{ModuleName.Transporter2}.Elevator.{IS_HOMED}");
  301. _rtDataKeys.Add($"{ModuleName.Transporter2}.Gantry.{IS_SWITCH_ON}");
  302. _rtDataKeys.Add($"{ModuleName.Transporter2}.Elevator.{IS_SWITCH_ON}");
  303. _rtDataKeys.Add($"{ModuleName.Transporter2}.Elevator.CurrentStation");
  304. _rtDataKeys.Add($"{ModuleName.Transporter2}.Gantry.{IS_MOVING}");
  305. _rtDataKeys.Add($"{ModuleName.Transporter2}.Elevator.{IS_MOVING}");
  306. _rtDataKeys.Add($"{ModuleName.Transporter2}.Elevator.{FORWARD_LIMIT}");
  307. }
  308. }
  309. }