RobotCycleViewModel.cs 12 KB

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