RobotCycleViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 PickRobotFlip { get; set; }
  91. public Flip PlaceRobotFlip { get; set; }
  92. }
  93. private ObservableCollection<PositionItem> _positions = new ObservableCollection<PositionItem>();
  94. public ObservableCollection<PositionItem> Positions
  95. {
  96. get { return _positions; }
  97. set { SetProperty(ref _positions, value); }
  98. }
  99. public ObservableCollection<string> PositionTypes { get; } = new ObservableCollection<string>
  100. {
  101. "LP", "Aligner", "Dummy", "SRD", "VPW","PlatingCell"
  102. };
  103. public ObservableCollection<string> HandsTypes { get; } = new ObservableCollection<string>
  104. {
  105. "Blade1", "Blade2"
  106. };
  107. public ObservableCollection<string> FlipTypes { get; } = new ObservableCollection<string>
  108. {
  109. "Upper", "Down"
  110. };
  111. private string _selectedPositionType;
  112. public string SelectedPositionType
  113. {
  114. get { return _selectedPositionType; }
  115. set
  116. {
  117. SetProperty(ref _selectedPositionType, value);
  118. UpdateAvailableSelections();
  119. }
  120. }
  121. public ObservableCollection<ModuleName> AvailableSelections { get; } = new ObservableCollection<ModuleName>();
  122. private ModuleName _selectedSelection;
  123. public ModuleName SelectedSelection
  124. {
  125. get { return _selectedSelection; }
  126. set { SetProperty(ref _selectedSelection, value); }
  127. }
  128. private string _parameter;
  129. public string Parameter
  130. {
  131. get { return _parameter; }
  132. set { SetProperty(ref _parameter, value); }
  133. }
  134. private Hand _robotHand;
  135. public Hand RobotHand
  136. {
  137. get { return _robotHand; }
  138. set { SetProperty(ref _robotHand, value); }
  139. }
  140. private Flip _pickRobotFlip;
  141. public Flip PickRobotFlip
  142. {
  143. get { return _pickRobotFlip; }
  144. set { SetProperty(ref _pickRobotFlip, value); }
  145. }
  146. private Flip _placeRobotFlip;
  147. public Flip PlaceRobotFlip
  148. {
  149. get { return _placeRobotFlip; }
  150. set { SetProperty(ref _placeRobotFlip, value); }
  151. }
  152. #region 命令
  153. public ICommand RobotCycleStartCommand { get; set; }
  154. public ICommand RobotCycleAbortCommand { get; set; }
  155. public ICommand AddPositionCommand { get; set; }
  156. public ICommand MoveUpCommand { get; set; }
  157. public ICommand MoveDownCommand { get; set; }
  158. public ICommand RemoveCommand { get; set; }
  159. #endregion
  160. /// <summary>
  161. /// 构造器
  162. /// </summary>
  163. public RobotCycleViewModel()
  164. {
  165. RobotCycleStartCommand = new DelegateCommand<object>(RobotCycleStartAction);
  166. RobotCycleAbortCommand = new DelegateCommand<object>(RobotCycleAbortAction);
  167. AddPositionCommand = new DelegateCommand<object>(AddPosition);
  168. MoveUpCommand = new DelegateCommand<object>(MovePositionUp);
  169. MoveDownCommand = new DelegateCommand<object>(MovePositionDown);
  170. RemoveCommand = new DelegateCommand<object>(RemovePosition);
  171. SelectedPositionType = PositionTypes.FirstOrDefault();
  172. RobotHand = Hand.Blade1;
  173. PickRobotFlip = Flip.Upper;
  174. PlaceRobotFlip = Flip.Upper;
  175. }
  176. private void UpdateAvailableSelections()
  177. {
  178. AvailableSelections.Clear();
  179. switch (SelectedPositionType)
  180. {
  181. case "LP":
  182. AvailableSelections.Add(ModuleName.LP1);
  183. AvailableSelections.Add(ModuleName.LP2);
  184. break;
  185. case "Aligner":
  186. AvailableSelections.Add(ModuleName.Aligner1);
  187. Parameter = "0"; // 默认角度
  188. break;
  189. case "Dummy":
  190. AvailableSelections.Add(ModuleName.Dummy1);
  191. AvailableSelections.Add(ModuleName.Dummy2);
  192. break;
  193. case "SRD":
  194. AvailableSelections.Add(ModuleName.SRD1);
  195. AvailableSelections.Add(ModuleName.SRD2);
  196. break;
  197. case "VPW":
  198. AvailableSelections.Add(ModuleName.VPW1);
  199. AvailableSelections.Add(ModuleName.VPW2);
  200. break;
  201. case "PlatingCell":
  202. AvailableSelections.Add(ModuleName.PlatingCell1);
  203. AvailableSelections.Add(ModuleName.PlatingCell2);
  204. AvailableSelections.Add(ModuleName.PlatingCell3);
  205. AvailableSelections.Add(ModuleName.PlatingCell4);
  206. break;
  207. }
  208. SelectedSelection = AvailableSelections.FirstOrDefault();
  209. RobotHand = Hand.Blade1;
  210. PickRobotFlip = Flip.Upper;
  211. PlaceRobotFlip = Flip.Upper;
  212. }
  213. private void AddPosition(object obj)
  214. {
  215. if ("Aligner".Equals(SelectedPositionType)) //目前就aligner的条目需要参数
  216. {
  217. Positions.Add(new PositionItem
  218. {
  219. ModuleType = SelectedPositionType,
  220. ModuleName = SelectedSelection,
  221. Parameter = Parameter,
  222. RobotHand = RobotHand,
  223. PickRobotFlip = PickRobotFlip,
  224. PlaceRobotFlip = PlaceRobotFlip
  225. });
  226. }
  227. else
  228. {
  229. Positions.Add(new PositionItem
  230. {
  231. ModuleType = SelectedPositionType,
  232. ModuleName = SelectedSelection,
  233. RobotHand = RobotHand,
  234. PickRobotFlip = PickRobotFlip,
  235. PlaceRobotFlip = PlaceRobotFlip
  236. });
  237. }
  238. }
  239. private void MovePositionUp(object obj)
  240. {
  241. if (obj is PositionItem item)
  242. {
  243. int index = Positions.IndexOf(item);
  244. if (index > 0)
  245. {
  246. Positions.Move(index, index - 1);
  247. }
  248. }
  249. }
  250. private void MovePositionDown(object obj)
  251. {
  252. if (obj is PositionItem item)
  253. {
  254. int index = Positions.IndexOf(item);
  255. if (index < Positions.Count - 1)
  256. {
  257. Positions.Move(index, index + 1);
  258. }
  259. }
  260. }
  261. private void RemovePosition(object obj)
  262. {
  263. if (obj is PositionItem item)
  264. {
  265. Positions.Remove(item);
  266. }
  267. }
  268. private void RobotCycleStartAction(object param)
  269. {
  270. List<RobotCycleParameter> parameters = new List<RobotCycleParameter>();
  271. foreach(var parameter in Positions)
  272. {
  273. RobotCycleParameter robotCycleParameter = new RobotCycleParameter();
  274. robotCycleParameter.ModuleName = parameter.ModuleName;
  275. robotCycleParameter.RobotArm = parameter.RobotHand;
  276. robotCycleParameter.PickRobotFlip = parameter.PickRobotFlip;
  277. robotCycleParameter.PlaceRobotFlip = parameter.PlaceRobotFlip;
  278. robotCycleParameter.Parameter = parameter.Parameter;
  279. parameters.Add(robotCycleParameter);
  280. }
  281. InvokeClient.Instance.Service.DoOperation($"EFEM.{EfemOperation.RobotCycle}", JsonConvert.SerializeObject(parameters), InPutCycleTimes);
  282. }
  283. private void RobotCycleAbortAction(object param)
  284. {
  285. InvokeClient.Instance.Service.DoOperation($"{ModuleName.EfemRobot}.{EfemOperation.Abort}");
  286. }
  287. /// <summary>
  288. /// 加载数据
  289. /// </summary>
  290. public void LoadData(string systemName)
  291. {
  292. _rtDataKeys.Clear();
  293. _rtDataKeys.Add("EFEM.CurrentRobotCycleTime");
  294. if (_timer == null)
  295. {
  296. _timer = new DispatcherTimer();
  297. _timer.Interval = TimeSpan.FromMilliseconds(200);
  298. _timer.Tick += Timer_Tick;
  299. }
  300. _timer.Start();
  301. }
  302. /// <summary>
  303. /// 定时器执行
  304. /// </summary>
  305. /// <param name="sender"></param>
  306. /// <param name="e"></param>
  307. private void Timer_Tick(object sender, EventArgs e)
  308. {
  309. if (_rtDataKeys.Count != 0)
  310. {
  311. IsInputParameterEnable = "Aligner".Equals(SelectedPositionType) ? true : false;
  312. _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  313. if (_rtDataValueDic != null)
  314. {
  315. CurrentCycle = CommonFunction.GetValue<int>(_rtDataValueDic, $"EFEM.CurrentRobotCycleTime");
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// 隐藏
  321. /// </summary>
  322. public void Hide()
  323. {
  324. }
  325. }
  326. }