FeederSystemParam.xaml.cs 12 KB

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