FeederSystemParam.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using Prism.Commands;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using Team.FFFeederService.Interfaces;
  19. using TeamAAS_VP;
  20. using static NPOI.HSSF.Util.HSSFColor;
  21. namespace TeamAAS_VP.Controls
  22. {
  23. /// <summary>
  24. /// FeederSystemParam.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class FeederSystemParam : UserControl, INotifyPropertyChanged
  27. {
  28. private bool IsLoading = false;
  29. public FeederSystemParam()
  30. {
  31. InitializeComponent();
  32. this.DataContext = this;
  33. this.Loaded += FeederSystemParam_Loaded;
  34. }
  35. #region 属性
  36. public IVoiceCoilMotorFeeder Feeder { get; set; }
  37. private ushort _LightTimeOutValue;
  38. /// <summary>
  39. /// 背光超时时长
  40. /// </summary>
  41. public ushort LightTimeOutValue
  42. {
  43. get { return _LightTimeOutValue; }
  44. set { SetProperty(ref _LightTimeOutValue,value); }
  45. }
  46. private ushort _LightValue;
  47. /// <summary>
  48. /// 背光亮度
  49. /// </summary>
  50. public ushort LightValue
  51. {
  52. get { return _LightValue; }
  53. set { SetProperty(ref _LightValue, value); }
  54. }
  55. private int _BackLightFlashTime;
  56. /// <summary>
  57. /// 背光闪烁时间
  58. /// </summary>
  59. public int BackLightFlashTime
  60. {
  61. get { return _BackLightFlashTime; }
  62. set { SetProperty(ref _BackLightFlashTime, value); }
  63. }
  64. private int _PlatShakeTimeout;
  65. /// <summary>
  66. /// 平台振动超时
  67. /// </summary>
  68. public int PlatShakeTimeout
  69. {
  70. get { return _PlatShakeTimeout; }
  71. set { SetProperty(ref _PlatShakeTimeout, value); }
  72. }
  73. private int _UpperFeederShakeTimeout;
  74. /// <summary>
  75. /// 料斗振动超时
  76. /// </summary>
  77. public int UpperFeederShakeTimeout
  78. {
  79. get { return _UpperFeederShakeTimeout; }
  80. set { SetProperty(ref _UpperFeederShakeTimeout, value); }
  81. }
  82. #endregion
  83. #region 命令
  84. private DelegateCommand<double?> _LightTimeOutValueChangedCommand;
  85. public DelegateCommand<double?> LightTimeOutValueChangedCommand =>
  86. _LightTimeOutValueChangedCommand ?? (_LightTimeOutValueChangedCommand = new DelegateCommand<double?>(ExecuteLightTimeOutValueChangedCommand));
  87. private DelegateCommand<double?> _LightValueChangedCommand;
  88. public DelegateCommand<double?> LightValueChangedCommand =>
  89. _LightValueChangedCommand ?? (_LightValueChangedCommand = new DelegateCommand<double?>(ExecuteLightValueChangedCommand));
  90. private DelegateCommand<double?> _LightFlashTimeChangedCommand;
  91. public DelegateCommand<double?> LightFlashTimeChangedCommand =>
  92. _LightFlashTimeChangedCommand ?? (_LightFlashTimeChangedCommand = new DelegateCommand<double?>(ExecuteLightFlashTimeChangedCommand));
  93. private DelegateCommand<double?> _PlatShakeTimeoutChangedCommand;
  94. public DelegateCommand<double?> PlatShakeTimeoutChangedCommand =>
  95. _PlatShakeTimeoutChangedCommand ?? (_PlatShakeTimeoutChangedCommand = new DelegateCommand<double?>(ExecutePlatShakeTimeoutChangedCommand));
  96. private DelegateCommand<double?> _UpperFeederShakeTimeoutChangedCommand;
  97. public DelegateCommand<double?> UpperFeederShakeTimeoutChangedCommand =>
  98. _UpperFeederShakeTimeoutChangedCommand ?? (_UpperFeederShakeTimeoutChangedCommand = new DelegateCommand<double?>(ExecuteUpperFeederShakeTimeoutChangedCommand));
  99. private DelegateCommand _WriteCommand;
  100. public DelegateCommand WriteCommand =>
  101. _WriteCommand ?? (_WriteCommand = new DelegateCommand(ExecuteWriteCommand));
  102. #endregion
  103. #region 方法
  104. private async void FeederSystemParam_Loaded(object sender, RoutedEventArgs e)
  105. {
  106. if (Feeder == null || !Feeder.IsConnected) return;
  107. IsLoading = true;
  108. try
  109. {
  110. LightTimeOutValue = await Feeder.GetLightTimeoutAsync();
  111. LightValue = await Feeder.GetLightBrightnessAsync();
  112. BackLightFlashTime = await Feeder.GetBackLightFlashTimeAsync();
  113. UpperFeederShakeTimeout = await Feeder.GetUpperFeederShakeTimeoutAsync();
  114. PlatShakeTimeout = await Feeder.GetPlatShakeTimeoutAsync();
  115. }
  116. catch (Exception ex)
  117. {
  118. LogHelper.WriteLogError("Feeder系统参数加载时出错!", ex);
  119. }
  120. IsLoading = false;
  121. }
  122. /// <summary>
  123. /// 背光超时时长修改时
  124. /// </summary>
  125. async void ExecuteLightTimeOutValueChangedCommand(double? lightvalue)
  126. {
  127. if (Feeder == null || !Feeder.IsConnected || IsLoading) return;
  128. if (lightvalue == null) return;
  129. try
  130. {
  131. await Feeder.SetLightTimeOutAsync((ushort)lightvalue);
  132. }
  133. catch (Exception ex)
  134. {
  135. LogHelper.WriteLogError("Feeder背光超时时长修改时出错!", ex);
  136. }
  137. }
  138. /// <summary>
  139. /// 背光亮度改变时
  140. /// </summary>
  141. async void ExecuteLightValueChangedCommand(double? lightvalue)
  142. {
  143. if (Feeder == null || !Feeder.IsConnected || IsLoading) return;
  144. if (lightvalue==null) return;
  145. try
  146. {
  147. await Feeder.SetLightValueToBufferAsync((ushort)lightvalue);
  148. }
  149. catch (Exception ex)
  150. {
  151. LogHelper.WriteLogError("Feeder背光亮度改变时出错!", ex);
  152. }
  153. }
  154. /// <summary>
  155. /// 背光闪烁时间改变时
  156. /// </summary>
  157. /// <param name="lightvalue"></param>
  158. async void ExecuteLightFlashTimeChangedCommand(double? lightvalue)
  159. {
  160. if (Feeder == null || !Feeder.IsConnected || IsLoading) return;
  161. if (lightvalue == null) return;
  162. try
  163. {
  164. await Feeder.SetBackLightFlashTimeAsync((ushort)lightvalue);
  165. }
  166. catch (Exception ex)
  167. {
  168. LogHelper.WriteLogError("Feeder背光闪烁时间改变时出错!", ex);
  169. }
  170. }
  171. /// <summary>
  172. /// 平台振动超时改变时
  173. /// </summary>
  174. async void ExecutePlatShakeTimeoutChangedCommand(double? time)
  175. {
  176. if (Feeder == null || !Feeder.IsConnected || IsLoading) return;
  177. if (time == null) return;
  178. try
  179. {
  180. await Feeder.SetPlatShakeTimeoutAsync((ushort)time);
  181. }
  182. catch (Exception ex)
  183. {
  184. LogHelper.WriteLogError("Feeder平台振动超时改变时出错!", ex);
  185. }
  186. }
  187. /// <summary>
  188. /// 料斗振动超时改变时
  189. /// </summary>
  190. /// <param name="time"></param>
  191. async void ExecuteUpperFeederShakeTimeoutChangedCommand(double? time)
  192. {
  193. if (Feeder == null || !Feeder.IsConnected || IsLoading) return;
  194. if (time == null) return;
  195. try
  196. {
  197. await Feeder.SetUpperFeederShakeTimeoutAsync((ushort)time);
  198. }
  199. catch (Exception ex)
  200. {
  201. LogHelper.WriteLogError("Feeder料斗振动超时时出错!", ex);
  202. }
  203. }
  204. /// <summary>
  205. /// 保存
  206. /// </summary>
  207. async void ExecuteWriteCommand()
  208. {
  209. if (Feeder == null || !Feeder.IsConnected || IsLoading) return;
  210. try
  211. {
  212. await Feeder.SaveGlobalSetAsync();
  213. }
  214. catch (Exception ex)
  215. {
  216. LogHelper.WriteLogError("Feeder保存系统参数时出错!", ex);
  217. }
  218. }
  219. #endregion
  220. #region 属性更改通知
  221. /// <summary>
  222. /// Occurs when a property value changes.
  223. /// </summary>
  224. public event PropertyChangedEventHandler PropertyChanged;
  225. /// <summary>
  226. /// Checks if a property already matches a desired value. Sets the property and
  227. /// notifies listeners only when necessary.
  228. /// </summary>
  229. /// <typeparam name="T">Type of the property.</typeparam>
  230. /// <param name="storage">Reference to a property with both getter and setter.</param>
  231. /// <param name="value">Desired value for the property.</param>
  232. /// <param name="propertyName">Name of the property used to notify listeners. This
  233. /// value is optional and can be provided automatically when invoked from compilers that
  234. /// support CallerMemberName.</param>
  235. /// <returns>True if the value was changed, false if the existing value matched the
  236. /// desired value.</returns>
  237. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  238. {
  239. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  240. storage = value;
  241. RaisePropertyChanged(propertyName);
  242. return true;
  243. }
  244. /// <summary>
  245. /// Checks if a property already matches a desired value. Sets the property and
  246. /// notifies listeners only when necessary.
  247. /// </summary>
  248. /// <typeparam name="T">Type of the property.</typeparam>
  249. /// <param name="storage">Reference to a property with both getter and setter.</param>
  250. /// <param name="value">Desired value for the property.</param>
  251. /// <param name="propertyName">Name of the property used to notify listeners. This
  252. /// value is optional and can be provided automatically when invoked from compilers that
  253. /// support CallerMemberName.</param>
  254. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  255. /// <returns>True if the value was changed, false if the existing value matched the
  256. /// desired value.</returns>
  257. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  258. {
  259. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  260. storage = value;
  261. onChanged?.Invoke();
  262. RaisePropertyChanged(propertyName);
  263. return true;
  264. }
  265. /// <summary>
  266. /// Raises this object's PropertyChanged event.
  267. /// </summary>
  268. /// <param name="propertyName">Name of the property used to notify listeners. This
  269. /// value is optional and can be provided automatically when invoked from compilers
  270. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  271. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  272. {
  273. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  274. }
  275. /// <summary>
  276. /// Raises this object's PropertyChanged event.
  277. /// </summary>
  278. /// <param name="args">The PropertyChangedEventArgs</param>
  279. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  280. {
  281. PropertyChanged?.Invoke(this, args);
  282. }
  283. #endregion
  284. }
  285. }