RobotCycleViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using Aitex.Core.UI.MVVM;
  2. using Aitex.Sorter.Common;
  3. using MECF.Framework.Common.CommonData;
  4. using MECF.Framework.Common.ControlDataContext;
  5. using MECF.Framework.Common.DataCenter;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.OperationCenter;
  8. using MECF.Framework.Common.Utilities;
  9. using Newtonsoft.Json;
  10. using OpenSEMI.ClientBase.Command;
  11. using Prism.Mvvm;
  12. using PunkHPX8_Core;
  13. using PunkHPX8_MainPages.Roles;
  14. using PunkHPX8_MainPages.Views;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.ComponentModel;
  19. using System.Linq;
  20. using System.Reflection;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Windows.Forms;
  24. using System.Windows.Forms.VisualStyles;
  25. using System.Windows.Input;
  26. using System.Windows.Threading;
  27. namespace PunkHPX8_MainPages.ViewModels
  28. {
  29. public class RobotCycleViewModel : BindableBase
  30. {
  31. #region 内部变量
  32. /// <summary>
  33. /// 输入的Cycle次数
  34. /// </summary>
  35. private int _inPutCycleTimes = 1;
  36. /// <summary>
  37. /// Aligner旋转的角度
  38. /// </summary>
  39. private int _alignerAngle = 0;
  40. /// <summary>
  41. /// 当前正在执行第几次Cycle
  42. /// </summary>
  43. private int _currentCycle;
  44. /// <summary>
  45. /// 是否可以输入参数
  46. /// </summary>
  47. private bool _isInputParameterEnable;
  48. #region 系统数据
  49. /// <summary>
  50. /// 定时器
  51. /// </summary>
  52. DispatcherTimer _timer;
  53. /// <summary>
  54. /// 查询后台数据集合
  55. /// </summary>
  56. private List<string> _rtDataKeys = new List<string>();
  57. /// <summary>
  58. /// rt查询key数值字典
  59. /// </summary>
  60. private Dictionary<string, object> _rtDataValueDic = new Dictionary<string, object>();
  61. #endregion
  62. #endregion
  63. #region 属性
  64. public int InPutCycleTimes
  65. {
  66. get { return _inPutCycleTimes; }
  67. set { SetProperty(ref _inPutCycleTimes, value); }
  68. }
  69. public int AlignerAngle
  70. {
  71. get { return _alignerAngle; }
  72. set { SetProperty(ref _alignerAngle, value); }
  73. }
  74. public int CurrentCycle
  75. {
  76. get { return _currentCycle; }
  77. set { SetProperty(ref _currentCycle, value); }
  78. }
  79. public bool IsInputParameterEnable
  80. {
  81. get { return _isInputParameterEnable; }
  82. set { SetProperty(ref _isInputParameterEnable, value); }
  83. }
  84. #endregion
  85. public class PositionItem : INotifyPropertyChanged
  86. {
  87. private string _destination;
  88. public string ModuleType { get; set; }
  89. public ModuleName ModuleName { get; set; }
  90. public string Parameter { get; set; }
  91. public Hand RobotHand { get; set; }
  92. public Flip PickRobotFlip { get; set; }
  93. public Flip PlaceRobotFlip { get; set; }
  94. public string Destination {
  95. get => _destination;
  96. set
  97. {
  98. _destination = value;
  99. OnPropertyChanged(nameof(Destination)); // 触发通知
  100. }
  101. }
  102. public event PropertyChangedEventHandler PropertyChanged;
  103. protected void OnPropertyChanged(string propertyName)
  104. => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  105. }
  106. private ObservableCollection<PositionItem> _positions = new ObservableCollection<PositionItem>();
  107. public ObservableCollection<PositionItem> Positions
  108. {
  109. get { return _positions; }
  110. set { SetProperty(ref _positions, value); }
  111. }
  112. public ObservableCollection<string> PositionTypes { get; } = new ObservableCollection<string>
  113. {
  114. "LP", "Aligner", "Dummy", "SRD", "VPW","PlatingCell"
  115. };
  116. public ObservableCollection<string> HandsTypes { get; } = new ObservableCollection<string>
  117. {
  118. "Blade1", "Blade2"
  119. };
  120. public ObservableCollection<string> FlipTypes { get; } = new ObservableCollection<string>
  121. {
  122. "Upper", "Down"
  123. };
  124. private string _selectedPositionType;
  125. public string SelectedPositionType
  126. {
  127. get { return _selectedPositionType; }
  128. set
  129. {
  130. SetProperty(ref _selectedPositionType, value);
  131. UpdateAvailableSelections();
  132. }
  133. }
  134. public ObservableCollection<ModuleName> AvailableSelections { get; } = new ObservableCollection<ModuleName>();
  135. private ModuleName _selectedSelection;
  136. public ModuleName SelectedSelection
  137. {
  138. get { return _selectedSelection; }
  139. set { SetProperty(ref _selectedSelection, value); }
  140. }
  141. private string _parameter;
  142. public string Parameter
  143. {
  144. get { return _parameter; }
  145. set { SetProperty(ref _parameter, value); }
  146. }
  147. private Hand _robotHand;
  148. public Hand RobotHand
  149. {
  150. get { return _robotHand; }
  151. set { SetProperty(ref _robotHand, value); }
  152. }
  153. private Flip _pickRobotFlip;
  154. public Flip PickRobotFlip
  155. {
  156. get { return _pickRobotFlip; }
  157. set { SetProperty(ref _pickRobotFlip, value); }
  158. }
  159. private Flip _placeRobotFlip;
  160. public Flip PlaceRobotFlip
  161. {
  162. get { return _placeRobotFlip; }
  163. set { SetProperty(ref _placeRobotFlip, value); }
  164. }
  165. #region 命令
  166. public ICommand RobotCycleConfirmCommand { get; set; }
  167. public ICommand RobotCycleStartCommand { get; set; }
  168. public ICommand RobotCycleAbortCommand { get; set; }
  169. public ICommand AddPositionCommand { get; set; }
  170. public ICommand MoveUpCommand { get; set; }
  171. public ICommand MoveDownCommand { get; set; }
  172. public ICommand RemoveCommand { get; set; }
  173. #endregion
  174. /// <summary>
  175. /// 构造器
  176. /// </summary>
  177. public RobotCycleViewModel()
  178. {
  179. RobotCycleConfirmCommand = new DelegateCommand<object>(RobotCycleConfirmAction);
  180. RobotCycleStartCommand = new DelegateCommand<object>(RobotCycleStartAction);
  181. RobotCycleAbortCommand = new DelegateCommand<object>(RobotCycleAbortAction);
  182. AddPositionCommand = new DelegateCommand<object>(AddPosition);
  183. MoveUpCommand = new DelegateCommand<object>(MovePositionUp);
  184. MoveDownCommand = new DelegateCommand<object>(MovePositionDown);
  185. RemoveCommand = new DelegateCommand<object>(RemovePosition);
  186. SelectedPositionType = PositionTypes.FirstOrDefault();
  187. RobotHand = Hand.Blade1;
  188. PickRobotFlip = Flip.Upper;
  189. PlaceRobotFlip = Flip.Upper;
  190. }
  191. private void UpdateAvailableSelections()
  192. {
  193. AvailableSelections.Clear();
  194. switch (SelectedPositionType)
  195. {
  196. case "LP":
  197. AvailableSelections.Add(ModuleName.LP1);
  198. AvailableSelections.Add(ModuleName.LP2);
  199. break;
  200. case "Aligner":
  201. AvailableSelections.Add(ModuleName.Aligner1);
  202. Parameter = "0"; // 默认角度
  203. break;
  204. case "Dummy":
  205. AvailableSelections.Add(ModuleName.Dummy1);
  206. AvailableSelections.Add(ModuleName.Dummy2);
  207. break;
  208. case "SRD":
  209. AvailableSelections.Add(ModuleName.SRD1);
  210. AvailableSelections.Add(ModuleName.SRD2);
  211. break;
  212. case "VPW":
  213. AvailableSelections.Add(ModuleName.VPW1);
  214. AvailableSelections.Add(ModuleName.VPW2);
  215. break;
  216. case "PlatingCell":
  217. AvailableSelections.Add(ModuleName.PlatingCell1);
  218. AvailableSelections.Add(ModuleName.PlatingCell2);
  219. AvailableSelections.Add(ModuleName.PlatingCell3);
  220. AvailableSelections.Add(ModuleName.PlatingCell4);
  221. break;
  222. }
  223. SelectedSelection = AvailableSelections.FirstOrDefault();
  224. RobotHand = Hand.Blade1;
  225. PickRobotFlip = Flip.Upper;
  226. PlaceRobotFlip = Flip.Upper;
  227. }
  228. private void AddPosition(object obj)
  229. {
  230. if ("Aligner".Equals(SelectedPositionType)) //目前就aligner的条目需要参数
  231. {
  232. Positions.Add(new PositionItem
  233. {
  234. ModuleType = SelectedPositionType,
  235. ModuleName = SelectedSelection,
  236. Parameter = Parameter,
  237. RobotHand = RobotHand,
  238. PickRobotFlip = PickRobotFlip,
  239. PlaceRobotFlip = PlaceRobotFlip
  240. });
  241. }
  242. else
  243. {
  244. Positions.Add(new PositionItem
  245. {
  246. ModuleType = SelectedPositionType,
  247. ModuleName = SelectedSelection,
  248. RobotHand = RobotHand,
  249. PickRobotFlip = PickRobotFlip,
  250. PlaceRobotFlip = PlaceRobotFlip
  251. });
  252. }
  253. }
  254. private void MovePositionUp(object obj)
  255. {
  256. if (obj is PositionItem item)
  257. {
  258. int index = Positions.IndexOf(item);
  259. if (index > 0)
  260. {
  261. Positions.Move(index, index - 1);
  262. }
  263. }
  264. }
  265. private void MovePositionDown(object obj)
  266. {
  267. if (obj is PositionItem item)
  268. {
  269. int index = Positions.IndexOf(item);
  270. if (index < Positions.Count - 1)
  271. {
  272. Positions.Move(index, index + 1);
  273. }
  274. }
  275. }
  276. private void RemovePosition(object obj)
  277. {
  278. if (obj is PositionItem item)
  279. {
  280. Positions.Remove(item);
  281. }
  282. }
  283. private void RobotCycleConfirmAction(object param)
  284. {
  285. if(Positions.Count == 0)
  286. {
  287. return;
  288. }
  289. else if (Positions.Count == 1)
  290. {
  291. Positions[0].Destination = Positions[0].ModuleName.ToString();
  292. }
  293. else
  294. {
  295. for (int i = 0; i < Positions.Count; i++)
  296. {
  297. if(i < Positions.Count - 1)
  298. {
  299. Positions[i].Destination = Positions[i + 1].ModuleName.ToString();
  300. }
  301. else
  302. {
  303. Positions[i].Destination = Positions[0].ModuleName.ToString();
  304. }
  305. }
  306. }
  307. }
  308. private void RobotCycleStartAction(object param)
  309. {
  310. List<RobotCycleParameter> parameters = new List<RobotCycleParameter>();
  311. foreach(var parameter in Positions)
  312. {
  313. RobotCycleParameter robotCycleParameter = new RobotCycleParameter();
  314. robotCycleParameter.ModuleName = parameter.ModuleName;
  315. robotCycleParameter.RobotArm = parameter.RobotHand;
  316. robotCycleParameter.PickRobotFlip = parameter.PickRobotFlip;
  317. robotCycleParameter.PlaceRobotFlip = parameter.PlaceRobotFlip;
  318. robotCycleParameter.Parameter = parameter.Parameter;
  319. parameters.Add(robotCycleParameter);
  320. }
  321. InvokeClient.Instance.Service.DoOperation($"EFEM.{EfemOperation.RobotCycle}", JsonConvert.SerializeObject(parameters), InPutCycleTimes);
  322. }
  323. private void RobotCycleAbortAction(object param)
  324. {
  325. InvokeClient.Instance.Service.DoOperation($"{ModuleName.EfemRobot}.{EfemOperation.Abort}");
  326. }
  327. /// <summary>
  328. /// 加载数据
  329. /// </summary>
  330. public void LoadData(string systemName)
  331. {
  332. _rtDataKeys.Clear();
  333. _rtDataKeys.Add("EFEM.CurrentRobotCycleTime");
  334. if (_timer == null)
  335. {
  336. _timer = new DispatcherTimer();
  337. _timer.Interval = TimeSpan.FromMilliseconds(200);
  338. _timer.Tick += Timer_Tick;
  339. }
  340. _timer.Start();
  341. }
  342. /// <summary>
  343. /// 定时器执行
  344. /// </summary>
  345. /// <param name="sender"></param>
  346. /// <param name="e"></param>
  347. private void Timer_Tick(object sender, EventArgs e)
  348. {
  349. if (_rtDataKeys.Count != 0)
  350. {
  351. IsInputParameterEnable = "Aligner".Equals(SelectedPositionType) ? true : false;
  352. _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  353. if (_rtDataValueDic != null)
  354. {
  355. CurrentCycle = CommonFunction.GetValue<int>(_rtDataValueDic, $"EFEM.CurrentRobotCycleTime");
  356. }
  357. }
  358. }
  359. /// <summary>
  360. /// 隐藏
  361. /// </summary>
  362. public void Hide()
  363. {
  364. }
  365. }
  366. }