AddRobotViewModel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Prism.Commands;
  2. using Prism.Ioc;
  3. using Prism.Mvvm;
  4. using Prism.Services.Dialogs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Windows;
  10. using TeamAAS_VP.Core;
  11. using TeamAAS_VP.Interfaces;
  12. using TeamAAS_VP.Models;
  13. using TeamAAS_VP.Models.PLC;
  14. using TeamAAS_VP.Resources.Languages;
  15. using TouchSocket.Core;
  16. namespace TeamAAS_VP.ViewModels.Setting
  17. {
  18. public class AddRobotViewModel : BindableBase, IDialogAware
  19. {
  20. private readonly IConfigService _configService;
  21. private RobotInfo _Robot;
  22. /// <summary>
  23. /// 机器人
  24. /// </summary>
  25. public RobotInfo Robot
  26. {
  27. get { return _Robot; }
  28. set
  29. {
  30. SetProperty(ref _Robot, value);
  31. }
  32. }
  33. private ObservableCollection<PlcInfo> _AllPlc;
  34. public ObservableCollection<PlcInfo> AllPlc
  35. {
  36. get { return _AllPlc; }
  37. set { SetProperty(ref _AllPlc, value); }
  38. }
  39. private PlcInfo _SelectedPlc;
  40. public PlcInfo SelectedPlc
  41. {
  42. get { return _SelectedPlc; }
  43. set
  44. {
  45. SetProperty(ref _SelectedPlc, value);
  46. if (value != null)
  47. {
  48. Robot.PLC = value.Id;
  49. if (Robot.PlcRobotParameter == null)
  50. {
  51. Robot.PlcRobotParameter = new PlcRobotParameter();
  52. }
  53. if (Robot.PlcRobotParameter.AxixList == null)
  54. {
  55. Robot.PlcRobotParameter.AxixList = new List<Core.Robots.Axis>();
  56. if (Robot.RobotBrand == Enums.RobotBrand.XYZ_Platform)
  57. {
  58. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("X", 1));
  59. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("Y", 2));
  60. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("Z", 3));
  61. }
  62. else
  63. {
  64. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("X", 1));
  65. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("Y", 2));
  66. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("Z", 3));
  67. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("U", 4));
  68. }
  69. }
  70. }
  71. else
  72. {
  73. Robot.PLC = Guid.Empty;
  74. Robot.PlcRobotParameter = null;
  75. }
  76. }
  77. }
  78. private DelegateCommand _ConfirmCommand;
  79. public DelegateCommand ConfirmCommand =>
  80. _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand, CanExecuteConfirmCommand).ObservesProperty(() => Robot.RobotName));
  81. private DelegateCommand _CancelCommand;
  82. public DelegateCommand CancelCommand =>
  83. _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand));
  84. public AddRobotViewModel(IConfigService configService)
  85. {
  86. _configService = configService;
  87. }
  88. /// <summary>
  89. /// 确认按钮
  90. /// </summary>
  91. /// <returns></returns>
  92. bool CanExecuteConfirmCommand()
  93. {
  94. return Robot != null && Robot.RobotName != null && !string.IsNullOrEmpty(Robot.RobotName.Trim());
  95. }
  96. void ExecuteConfirmCommand()
  97. {
  98. if (string.IsNullOrEmpty(Robot.RobotName))
  99. {
  100. return;
  101. }
  102. if (!FileHelper.CheckFileName(Robot.RobotName))
  103. {
  104. MessageBox.Show(Lang.AddCameraVM_message1, "AAS", MessageBoxButton.OK, MessageBoxImage.Error);
  105. return;
  106. }
  107. if (Robot.RobotBrand == Enums.RobotBrand.XYZ_Platform)
  108. {
  109. if (Robot.PlcRobotParameter == null)
  110. {
  111. Robot.PlcRobotParameter = new PlcRobotParameter();
  112. }
  113. if (Robot.PlcRobotParameter.AxixList == null)
  114. {
  115. Robot.PlcRobotParameter.AxixList = new List<Core.Robots.Axis>();
  116. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("X", 1));
  117. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("Y", 2));
  118. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("Z", 3));
  119. }
  120. }
  121. else if (Robot.RobotBrand == Enums.RobotBrand.XYZU_Platform)
  122. {
  123. if (Robot.PlcRobotParameter == null)
  124. {
  125. Robot.PlcRobotParameter = new PlcRobotParameter();
  126. }
  127. if (Robot.PlcRobotParameter.AxixList == null)
  128. {
  129. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("X", 1));
  130. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("Y", 2));
  131. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("Z", 3));
  132. Robot.PlcRobotParameter.AxixList.Add(new Core.Robots.Axis("U", 4));
  133. }
  134. }
  135. else
  136. {
  137. Robot.PlcRobotParameter = null;
  138. }
  139. IDialogParameters parameters = new DialogParameters();
  140. parameters.Add("Robot", Robot);
  141. Tuple<bool, RobotInfo> rest = new Tuple<bool, RobotInfo>(false, Robot);
  142. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
  143. }
  144. /// <summary>
  145. /// 取消按钮
  146. /// </summary>
  147. void ExecuteCancelCommand()
  148. {
  149. IDialogParameters parameters = new DialogParameters();
  150. parameters.Add("Robot", Robot);
  151. Tuple<bool, RobotInfo> rest = new Tuple<bool, RobotInfo>(false, Robot);
  152. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters));
  153. }
  154. public string Title { get; set; } = Lang.添加机器人;
  155. public event Action<IDialogResult> RequestClose;
  156. public bool CanCloseDialog()
  157. {
  158. return true;
  159. }
  160. public void OnDialogClosed()
  161. {
  162. }
  163. public void OnDialogOpened(IDialogParameters parameters)
  164. {
  165. Title = Lang.增加机器人;
  166. Robot = new RobotInfo() { Id = Guid.NewGuid(), RobotBrand = Enums.RobotBrand.Default, ConnectType = Enums.TCPConnectType.Client, Terminator = Enums.Terminator.CRLF, DataEncoding = Enums.DataEncoding.Default };
  167. AllPlc = new ObservableCollection<PlcInfo>(_configService.GetAllPlcs());
  168. }
  169. }
  170. }