SPISetControl.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Uestc.Auto6.Dso.Core;
  12. using Uestc.Auto6.Dso.Core.Decode;
  13. using Uni_Trend.MSO7000X.Common.Helper;
  14. using Uni_Trend.MSO7000X.UserControls;
  15. namespace Uestc.Auto6.Dso.Protocol.SPI
  16. {
  17. public partial class SPISetControl : UserControl, IProtocolView
  18. {
  19. #region 属性定义
  20. protected new Boolean DesignMode
  21. {
  22. get
  23. {
  24. Boolean rtnflag = false;
  25. #if DEBUG
  26. rtnflag = DesignTimeHelper.InDesignMode(this);
  27. #endif
  28. return rtnflag;
  29. }
  30. }
  31. FloatForm _FloatForm;
  32. private SPIDecodePrsnt DestinDecodePrsnt
  33. {
  34. get => _DestinDecodePrsnt;
  35. set
  36. {
  37. if (_DestinDecodePrsnt != value)
  38. {
  39. _DestinDecodePrsnt = value;
  40. ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="SPIDecodePrsnt"/>属性中
  41. if (SPIDecodePrsnt != null && value != null)
  42. {
  43. typeof(SPIDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
  44. {
  45. x.SetValue(SPIDecodePrsnt, x.GetValue(value));
  46. });
  47. }
  48. }
  49. }
  50. }
  51. private SPIDecodePrsnt _DestinDecodePrsnt;
  52. private SPIDecodePrsnt SPIDecodePrsnt => Presenter as SPIDecodePrsnt;
  53. public IProtocolPrsnt Presenter { get; set; }
  54. #endregion 属性定义
  55. public SPISetControl()
  56. {
  57. InitializeComponent();
  58. this.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  59. Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(this);
  60. }
  61. protected override void OnLoad(EventArgs e)
  62. {
  63. base.OnLoad(e);
  64. Init();
  65. }
  66. private void Init()
  67. {
  68. GetDestinPrsnt();
  69. this.GetType().Assembly.GetTypes().Where(x => x.GetCustomAttribute<RowAttribute>() != null && x.GetInterface(typeof(IPage).FullName) != null && x.GetInterface(typeof(IProtocolView).FullName) != null).ToList().ForEach(x =>
  70. {
  71. RowAttribute row = x.GetCustomAttribute<RowAttribute>();
  72. IProtocolView view = (IProtocolView)Activator.CreateInstance(x);
  73. view.Presenter = this.Presenter;
  74. Presenter.TryAddView(view);
  75. (view as IPage).ShowKeyboard += (sender) =>
  76. {
  77. Boolean lastflag = true;
  78. if (this.FindForm() is FloatForm form)
  79. {
  80. lastflag = form.CanClose;
  81. form.CanClose = false;
  82. }
  83. if (_FloatForm != null && !_FloatForm.IsDisposed)
  84. _FloatForm?.Close();
  85. _FloatForm = sender as FloatForm;
  86. _FloatForm.FormClosing += (_, _) =>
  87. {
  88. if (this.FindForm() is FloatForm form)
  89. form.CanClose = lastflag;
  90. };
  91. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  92. {
  93. Current = _FloatForm,
  94. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  95. });
  96. };
  97. TablePage.Controls.Add(view as Control, 0, row.Row);
  98. });
  99. SetDecodeChannel();
  100. SetFrameModel();
  101. }
  102. public void UpdateView(String propertyName)
  103. {
  104. DecodeSynchronization(propertyName);
  105. if (String.IsNullOrEmpty(propertyName))
  106. {
  107. UpdateView();
  108. return;
  109. }
  110. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
  111. {
  112. UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
  113. return attribute != null && attribute.PropertyName == propertyName;
  114. }).FirstOrDefault()?.Invoke(this, null);
  115. }
  116. private void UpdateView()
  117. {
  118. this.GetType().GetMethods().Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  119. this.Refresh();
  120. }
  121. /// <summary>
  122. /// 同步两个Prsnt
  123. /// </summary>
  124. /// <param name="propertyName">属性名称</param>
  125. private void DecodeSynchronization(String propertyName)
  126. {
  127. if (DestinDecodePrsnt == null)
  128. return;
  129. ///由于使用了<see cref="IRS232DecodePrsnt.ChannelId"/>进行判断具体同步的目标
  130. ///因此<see cref="IRS232DecodePrsnt.ChannelId"/>参数不进行同步
  131. if (propertyName == nameof(Core.Decode.SPIDecodePrsnt.MISO) || propertyName == nameof(Core.Decode.SPIDecodePrsnt.MOSI) || propertyName == nameof(SPIDecodePrsnt.DecodeChannel))
  132. return;
  133. if (String.IsNullOrEmpty(propertyName))
  134. {
  135. typeof(SPIDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
  136. {
  137. x.SetValue(DestinDecodePrsnt, x.GetValue(this.SPIDecodePrsnt));
  138. });
  139. }
  140. else
  141. {
  142. PropertyInfo propertyInfo = typeof(SPIDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  143. if (propertyInfo != null)
  144. propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(this.SPIDecodePrsnt));
  145. }
  146. }
  147. /// <summary>
  148. /// 获取需要同步的Prsnt
  149. /// 当<see cref="DestinDecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
  150. /// 当<see cref="SPIDecodePrsnt.DecodeChannel"/> <see cref="SPIDecodePrsnt.MISO"/> <see cref="SPIDecodePrsnt.MOSI"/> 相同时进行同步
  151. /// </summary>
  152. private void GetDestinPrsnt()
  153. {
  154. if (this.SPIDecodePrsnt == null)
  155. return;
  156. if (SPIDecodePrsnt.IsTrigger)
  157. DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is SPIDecodePrsnt).Cast<SPIDecodePrsnt>().Where(x =>CheckRules(x,SPIDecodePrsnt)).OrderBy(x => x.MISO).FirstOrDefault();
  158. else
  159. {
  160. if (Presenter.GetTriggerDecodePrsnt() is SPIDecodePrsnt decodePrsnt && CheckRules(decodePrsnt,SPIDecodePrsnt))
  161. DestinDecodePrsnt = decodePrsnt;
  162. else
  163. DestinDecodePrsnt = null;
  164. }
  165. }
  166. /// <summary>
  167. /// 判断两个Prsnt是否同步的规则
  168. /// </summary>
  169. /// <param name="fisrt"></param>
  170. /// <param name="second"></param>
  171. /// <returns></returns>
  172. private Boolean CheckRules(SPIDecodePrsnt fisrt,SPIDecodePrsnt second)
  173. {
  174. if (fisrt == null || second == null)
  175. return false;
  176. if (fisrt.DecodeChannel != second.DecodeChannel)
  177. return false;
  178. if(fisrt.DecodeChannel == ComModel.ProtocolSPI.DecodeChannel.MISO)
  179. return fisrt.MISO == second.MISO;
  180. if (fisrt.DecodeChannel == ComModel.ProtocolSPI.DecodeChannel.MOSI)
  181. return fisrt.MOSI == second.MOSI;
  182. if (fisrt.DecodeChannel == ComModel.ProtocolSPI.DecodeChannel.MOMI)
  183. return fisrt.MOSI == second.MOSI && fisrt.MISO == fisrt.MISO;
  184. return false;
  185. }
  186. [UpdateProperty(nameof(Core.Decode.SPIDecodePrsnt.DecodeChannel))]
  187. private void SetDecodeChannel()
  188. {
  189. switch(SPIDecodePrsnt.DecodeChannel)
  190. {
  191. case ComModel.ProtocolSPI.DecodeChannel.MISO:
  192. TablePage.Controls.Cast<Control>().First(x => x is MISOPage).Visible = true;
  193. TablePage.Controls.Cast<Control>().First(x => x is MOSIPage).Visible = false;
  194. break;
  195. case ComModel.ProtocolSPI.DecodeChannel.MOSI:
  196. TablePage.Controls.Cast<Control>().First(x => x is MISOPage).Visible = false;
  197. TablePage.Controls.Cast<Control>().First(x => x is MOSIPage).Visible = true;
  198. break;
  199. case ComModel.ProtocolSPI.DecodeChannel.MOMI:
  200. TablePage.Controls.Cast<Control>().First(x => x is MISOPage).Visible = true;
  201. TablePage.Controls.Cast<Control>().First(x => x is MOSIPage).Visible = true;
  202. break;
  203. }
  204. }
  205. [UpdateProperty(nameof(Core.Decode.SPIDecodePrsnt.FramingMode))]
  206. private void SetFrameModel()
  207. {
  208. TablePage.Controls.Cast<Control>().First(x => x is CSPage).Visible = SPIDecodePrsnt.FramingMode == ComModel.ProtocolSPI.FramingMode.Slave;
  209. }
  210. [UpdateProperty(nameof(Core.Decode.SPIDecodePrsnt.MISO))]
  211. private void SetMISO()
  212. {
  213. GetDestinPrsnt();
  214. }
  215. [UpdateProperty(nameof(Core.Decode.SPIDecodePrsnt.MOSI))]
  216. private void SetMOSI()
  217. {
  218. GetDestinPrsnt();
  219. }
  220. }
  221. }