RobotCycleViewModel.cs 14 KB

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