MILSetControl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System.Reflection;
  2. using System.Windows.Forms;
  3. using Uestc.Auto6.Dso.Core;
  4. using Uestc.Auto6.Dso.Core.Decode;
  5. using Uestc.Auto6.Dso.ComModel;
  6. using Uni_Trend.MSO7000X.Language;
  7. using Uni_Trend.MSO7000X.Common.Helper;
  8. using Uni_Trend.MSO7000X.UserControls;
  9. using System;
  10. using System.Linq;
  11. using System.Collections.Generic;
  12. using Uni_Trend.MSO7000X.UserControls.Style;
  13. namespace Uestc.Auto6.Dso.Protocol.MIL
  14. {
  15. [System.ComponentModel.ToolboxItem(false)]
  16. public partial class MILSetControl : UserControl, IProtocolView
  17. {
  18. #region 属性定义
  19. protected new Boolean DesignMode
  20. {
  21. get
  22. {
  23. Boolean rtnflag = false;
  24. #if DEBUG
  25. rtnflag = DesignTimeHelper.InDesignMode(this);
  26. #endif
  27. return rtnflag;
  28. }
  29. }
  30. public MILDecodePrsnt DestinDecodePrsnt
  31. {
  32. get => _DestinDecodePrsnt;
  33. set
  34. {
  35. if (_DestinDecodePrsnt != value)
  36. {
  37. _DestinDecodePrsnt = value;
  38. ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="MILDecodePrsnt"/>属性中
  39. if (DecodePrsnt != null && value != null)
  40. {
  41. typeof(MILDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
  42. {
  43. x.SetValue(DecodePrsnt, x.GetValue(value));
  44. });
  45. }
  46. }
  47. }
  48. }
  49. private MILDecodePrsnt _DestinDecodePrsnt;
  50. private MILDecodePrsnt DecodePrsnt => Presenter as MILDecodePrsnt;
  51. public IProtocolPrsnt Presenter { get; set; }
  52. #endregion 属性定义
  53. public MILSetControl()
  54. {
  55. InitializeComponent();
  56. }
  57. protected override void OnLoad(EventArgs e)
  58. {
  59. base.OnLoad(e);
  60. Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(this, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
  61. Init();
  62. }
  63. private void Init()
  64. {
  65. GetDestinPrsnt();
  66. InitPolarity();
  67. InitRealBiteRate();
  68. InitSignalRate();
  69. InitSource();
  70. InitThreshold();
  71. UpdateView();
  72. }
  73. private void InitSource()
  74. {
  75. this.CbxSource.DataSource = ComModel.ChannelIdExt.GetAnalogs().Select(x => new ComboBoxData<ChannelId>(x.ToString(), x)).ToList();
  76. this.CbxSource.ValueMember = "Value";
  77. this.CbxSource.DisplayMember = "Key";
  78. this.CbxSource.SelectedValueChanged += (_, _) =>
  79. {
  80. DecodePrsnt.Source = (ChannelId)this.CbxSource.SelectedValue;
  81. };
  82. }
  83. private void InitThreshold()
  84. {
  85. this.BtnThreshold.Click += (sender, args) => SetThreshold();
  86. }
  87. private void SetThreshold()
  88. {
  89. Boolean lastflag = true;
  90. if (this.FindForm() is FloatForm form)
  91. {
  92. lastflag = form.CanClose;
  93. form.CanClose = false;
  94. }
  95. FloatForm floatForm = new FloatForm();
  96. floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
  97. floatForm.BackColor = BackColor;
  98. floatForm.ForeColor = ForeColor;
  99. floatForm.Width = 380;
  100. floatForm.Height = 460 + floatForm.HeadHeight;
  101. floatForm.Title = "设置门限";
  102. Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
  103. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
  104. numberKeyboard.Top = floatForm.HeadHeight;
  105. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  106. numberKeyboard.Width = floatForm.Width;
  107. numberKeyboard.ForeColor = ForeColor;
  108. numberKeyboard.BackColor = BackColor;
  109. numberKeyboard.DefaultValue = DecodePrsnt.Threshold;
  110. numberKeyboard.Unit = "V";
  111. numberKeyboard.MinValue = DecodePrsnt.MinThreshold;
  112. numberKeyboard.MaxValue = DecodePrsnt.MaxThreshold;
  113. numberKeyboard.DecimalNumber = 2;
  114. numberKeyboard.OkClickEvent += (sender, args) =>
  115. {
  116. this.DecodePrsnt.Threshold = args.Data;
  117. floatForm?.Close();
  118. };
  119. numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
  120. floatForm.Controls.Add(numberKeyboard);
  121. DefaultStyleManager.Instance.RegisterControlRecursion(floatForm, StyleFlag.FontSize);
  122. floatForm.FormClosing += (_, _) =>
  123. {
  124. if (this.FindForm() is FloatForm form)
  125. form.CanClose = lastflag;
  126. };
  127. floatForm.ShowDialogByPosition();
  128. }
  129. private void InitPolarity()
  130. {
  131. this.RbgPolarity.ButtonItems = Enum.GetValues<ProtocolMIL.Polarity>().Select(x =>
  132. {
  133. String key = String.Empty;
  134. key = Properties.Resources.ResourceManager.GetString(x.GetType().ReflectedType.FullName + "." + x.GetType().Name + "." + x.ToString());
  135. if (String.IsNullOrEmpty(key))
  136. {
  137. key = x.ToString();
  138. }
  139. return new RadioButtonItem()
  140. {
  141. Tag = x,
  142. Text = key
  143. };
  144. }).ToArray();
  145. this.RbgPolarity.IndexChanged += (_, _) =>
  146. {
  147. DecodePrsnt.Polarity = (ProtocolMIL.Polarity)this.RbgPolarity.ButtonItems[this.RbgPolarity.ChoosedButtonIndex].Tag;
  148. };
  149. }
  150. private void InitSignalRate()
  151. {
  152. this.CbxBitRate.DataSource = Enum.GetValues<ProtocolMIL.SignalRate>().Select(x => new KeyValuePair<String, ProtocolMIL.SignalRate>(x switch
  153. {
  154. ProtocolMIL.SignalRate.SignalRate_1M => "1Mbps",
  155. ProtocolMIL.SignalRate.SignalRate_10M => "10Mbps",
  156. ProtocolMIL.SignalRate.Custom => "Custom",
  157. _ => "1Mbps",
  158. }, x)).ToList();
  159. this.CbxBitRate.ValueMember = "Value";
  160. this.CbxBitRate.DisplayMember = "Key";
  161. this.CbxBitRate.SelectedValueChanged += (_, _) =>
  162. {
  163. DecodePrsnt.SignalRate = (ProtocolMIL.SignalRate)this.CbxBitRate.SelectedValue;
  164. };
  165. }
  166. private void InitRealBiteRate()
  167. {
  168. this.BtnRealBitRate.Click += (sender, args) => SetBitRate();
  169. }
  170. private void SetBitRate()
  171. {
  172. Boolean lastflag = true;
  173. if (this.FindForm() is FloatForm form)
  174. {
  175. lastflag = form.CanClose;
  176. form.CanClose = false;
  177. }
  178. FloatForm floatForm = new FloatForm();
  179. floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
  180. floatForm.BackColor = BackColor;
  181. floatForm.ForeColor = ForeColor;
  182. floatForm.Width = 380;
  183. floatForm.Height = 460 + floatForm.HeadHeight;
  184. floatForm.Title = "设置波特率";
  185. Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
  186. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
  187. numberKeyboard.Top = floatForm.HeadHeight;
  188. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  189. numberKeyboard.Width = floatForm.Width;
  190. numberKeyboard.ForeColor = ForeColor;
  191. numberKeyboard.BackColor = BackColor;
  192. numberKeyboard.DefaultValue = DecodePrsnt.RealSignalRate;
  193. numberKeyboard.Unit = "bps";
  194. numberKeyboard.MinValue = DecodePrsnt.MinSignalRate;
  195. numberKeyboard.MaxValue = DecodePrsnt.MaxSignalRate;
  196. numberKeyboard.DecimalNumber = 2;
  197. numberKeyboard.OkClickEvent += (sender, args) =>
  198. {
  199. this.DecodePrsnt.RealSignalRate = (Int32)args.Data;
  200. floatForm?.Close();
  201. };
  202. numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
  203. floatForm.Controls.Add(numberKeyboard);
  204. DefaultStyleManager.Instance.RegisterControlRecursion(floatForm, StyleFlag.FontSize);
  205. floatForm.FormClosing += (_, _) =>
  206. {
  207. if (this.FindForm() is FloatForm form)
  208. form.CanClose = lastflag;
  209. };
  210. floatForm.ShowDialogByPosition();
  211. }
  212. /// <summary>
  213. /// 获取需要同步的Prsnt
  214. /// 当<see cref="DecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
  215. /// </summary>
  216. private void GetDestinPrsnt()
  217. {
  218. if (DecodePrsnt == null)
  219. return;
  220. if (DecodePrsnt.IsTrigger)
  221. DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is MILDecodePrsnt).Cast<MILDecodePrsnt>().Where(x => x.Source == DecodePrsnt.Source).OrderBy(x => x.Source).FirstOrDefault();
  222. else
  223. {
  224. if (Presenter.GetTriggerDecodePrsnt() is MILDecodePrsnt rs232decodePrsnt && rs232decodePrsnt.Source == DecodePrsnt.Source)
  225. DestinDecodePrsnt = rs232decodePrsnt;
  226. else
  227. DestinDecodePrsnt = null;
  228. }
  229. }
  230. /// <summary>
  231. /// 同步两个Prsnt
  232. /// </summary>
  233. /// <param name="propertyName">属性名称</param>
  234. private void DecodeSynchronization(String propertyName)
  235. {
  236. if (DestinDecodePrsnt == null)
  237. return;
  238. ///由于使用了<see cref="IMILDecodePrsnt.SDA"/>进行判断具体同步的目标
  239. ///因此<see cref="IMILDecodePrsnt.SDA"/>参数不进行同步
  240. if (propertyName == nameof(MILDecodePrsnt.Source))
  241. return;
  242. if (String.IsNullOrEmpty(propertyName))
  243. {
  244. typeof(MILDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
  245. {
  246. x.SetValue(DestinDecodePrsnt, x.GetValue(DecodePrsnt));
  247. });
  248. }
  249. else
  250. {
  251. PropertyInfo propertyInfo = typeof(MILDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  252. if (propertyInfo != null && propertyInfo.CanWrite&& propertyInfo.CanRead)
  253. propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(DecodePrsnt));
  254. }
  255. }
  256. public void UpdateView(Object presenter, String propertyName)
  257. {
  258. DecodeSynchronization(propertyName);
  259. if (String.IsNullOrEmpty(propertyName))
  260. {
  261. UpdateView();
  262. return;
  263. }
  264. this.GetType()
  265. .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
  266. .Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null && x.GetCustomAttribute<UpdatePropertyAttribute>().PropertyName == propertyName)
  267. .ToList()
  268. .ForEach(x => x?.Invoke(this, null));
  269. }
  270. private void UpdateView()
  271. {
  272. this.GetType().GetMethods( BindingFlags.Public| BindingFlags.Instance| BindingFlags.NonPublic).Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  273. this.Refresh();
  274. }
  275. [UpdateProperty(nameof(MILDecodePrsnt.SignalRate))]
  276. private void UpdateRate()
  277. {
  278. this.CbxBitRate.SelectedValue = DecodePrsnt.SignalRate;
  279. this.LblRealBitRate.Visible = DecodePrsnt.SignalRate == ProtocolMIL.SignalRate.Custom;
  280. this.BtnRealBitRate.Visible = this.LblRealBitRate.Visible;
  281. }
  282. [UpdateProperty(nameof(MILDecodePrsnt.RealSignalRate))]
  283. private void UpdateRealRate()
  284. {
  285. this.BtnRealBitRate.Text = SIHelper.ValueChangeToSI(DecodePrsnt.RealSignalRate, 2, "bps");
  286. }
  287. [UpdateProperty(nameof(MILDecodePrsnt.Threshold))]
  288. private void UpdateThreshold()
  289. {
  290. this.BtnThreshold.Text = SIHelper.ValueChangeToSI(DecodePrsnt.Threshold, 2, "V");
  291. }
  292. [UpdateProperty(nameof(MILDecodePrsnt.Polarity))]
  293. private void UpdatePolarity()
  294. {
  295. this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues<ProtocolMIL.Polarity>().ToList().FindIndex(x => x == DecodePrsnt.Polarity);
  296. }
  297. [UpdateProperty(nameof(MILDecodePrsnt.Source))]
  298. private void UpdateSource()
  299. {
  300. this.CbxSource.SelectedValue = DecodePrsnt.Source;
  301. GetDestinPrsnt();
  302. }
  303. }
  304. }