CANSetControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Windows.Forms;
  6. using Uestc.Auto6.Dso.Core;
  7. using Uestc.Auto6.Dso.Core.Decode;
  8. using Uestc.Auto6.Dso.ComModel;
  9. using Uni_Trend.MSO7000X.Language;
  10. using Uni_Trend.MSO7000X.Common.Helper;
  11. using Uni_Trend.MSO7000X.UserControls;
  12. using System.Diagnostics;
  13. namespace Uestc.Auto6.Dso.Protocol.CAN
  14. {
  15. [DebuggerDisplay("{" + nameof(GetDebuggerDisplay) + "(),nq}")]
  16. public partial class CANSetControl : 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 CANDecodePrsnt DestinDecodePrsnt
  31. {
  32. get => _DestinDecodePrsnt;
  33. set
  34. {
  35. if (_DestinDecodePrsnt != value)
  36. {
  37. _DestinDecodePrsnt = value;
  38. ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="_CANDecodePsrnt"/>属性中
  39. if (_CANDecodePsrnt != null && value != null)
  40. {
  41. typeof(CANDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
  42. {
  43. x.SetValue(_CANDecodePsrnt, x.GetValue(value));
  44. });
  45. }
  46. }
  47. }
  48. }
  49. private CANDecodePrsnt _DestinDecodePrsnt;
  50. private CANDecodePrsnt _CANDecodePsrnt => Presenter as CANDecodePrsnt;
  51. public IProtocolPrsnt Presenter { get; set; }
  52. private FloatForm _FloatForm;
  53. private Int32 _RowHeight = 66;
  54. private Int32 _ColnumCount = 2;
  55. #endregion 属性定义
  56. public CANSetControl()
  57. {
  58. InitializeComponent();
  59. }
  60. protected override void OnLoad(EventArgs e)
  61. {
  62. base.OnLoad(e);
  63. Init();
  64. InitView();
  65. }
  66. private void Init()
  67. {
  68. GetDestinPrsnt();
  69. InitSignalType();
  70. InitSignalRate();
  71. InitSignalInput1();
  72. SetSamplePoint();
  73. }
  74. /// <summary>
  75. /// 获取需要同步的Prsnt
  76. /// 当<see cref="_CANDecodePsrnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
  77. /// </summary>
  78. private void GetDestinPrsnt()
  79. {
  80. if (_CANDecodePsrnt == null)
  81. return;
  82. if (_CANDecodePsrnt.IsTrigger)
  83. DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is CANDecodePrsnt decodePrsnt && decodePrsnt.SignalInput1 == _CANDecodePsrnt.SignalInput1).Cast<CANDecodePrsnt>().OrderBy(x => x.SignalRate).FirstOrDefault();
  84. else
  85. {
  86. if (Presenter.GetTriggerDecodePrsnt() is CANDecodePrsnt CANdecodePsrnt && CANdecodePsrnt.SignalInput1 == _CANDecodePsrnt.SignalInput1)
  87. DestinDecodePrsnt = CANdecodePsrnt;
  88. else
  89. DestinDecodePrsnt = null;
  90. }
  91. }
  92. /// <summary>
  93. /// 同步两个Prsnt
  94. /// </summary>
  95. /// <param name="propertyName">属性名称</param>
  96. private void DecodeSynchronization(string propertyName)
  97. {
  98. if (DestinDecodePrsnt == null)
  99. return;
  100. if (propertyName == nameof(CANDecodePrsnt.SignalInput1))
  101. return;
  102. if (string.IsNullOrEmpty(propertyName))
  103. {
  104. typeof(CANDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
  105. {
  106. x.SetValue(DestinDecodePrsnt, x.GetValue(_CANDecodePsrnt));
  107. });
  108. }
  109. else
  110. {
  111. PropertyInfo propertyInfo = typeof(CANDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  112. if (propertyInfo != null)
  113. propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(_CANDecodePsrnt));
  114. }
  115. }
  116. private void InitSignalType()
  117. {
  118. this.CbxSignalType.DataSource = _CANDecodePsrnt.SignalType.GetEnumList();
  119. this.CbxSignalType.DisplayMember = "Key";
  120. this.CbxSignalType.ValueMember = "Value";
  121. this.CbxSignalType.SelectedValueChanged += (_, _) =>
  122. {
  123. _CANDecodePsrnt.SignalType = (ProtocolCAN.SignalType)this.CbxSignalType.SelectedValue;
  124. };
  125. this.CbxSignalType.SelectedValue = _CANDecodePsrnt.SignalRate;
  126. }
  127. private void InitSignalRate()
  128. {
  129. this.CbxSignalRate.DataSource = _CANDecodePsrnt.SignalRate.GetEnumList();
  130. this.CbxSignalRate.DisplayMember = "Key";
  131. this.CbxSignalRate.ValueMember = "Value";
  132. this.CbxSignalRate.SelectedValueChanged += (_, _) =>
  133. {
  134. _CANDecodePsrnt.SignalRate = (ProtocolCAN.SignalRate)this.CbxSignalRate.SelectedValue;
  135. };
  136. this.CbxSignalRate.SelectedValue = _CANDecodePsrnt.SignalRate;
  137. }
  138. private void InitSignalInput1()
  139. {
  140. this.CbxSignalInput1.DataSource = ChannelIdExt.GetAnalogs().GetEnumList();
  141. this.CbxSignalInput1.DisplayMember = "Key";
  142. this.CbxSignalInput1.ValueMember = "Value";
  143. this.CbxSignalInput1.SelectedValueChanged += (_, _) =>
  144. {
  145. _CANDecodePsrnt.SignalInput1 = (ChannelId)this.CbxSignalInput1.SelectedValue;
  146. SourceChanged();
  147. };
  148. this.CbxSignalInput1.SelectedValue = _CANDecodePsrnt.SignalInput1;
  149. }
  150. private void SourceChanged()
  151. {
  152. //通道发生改变后重新获取需要同步的Prsnt
  153. GetDestinPrsnt();
  154. }
  155. private void SetSamplePoint()
  156. {
  157. }
  158. private void InitView()
  159. {
  160. Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  161. this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<RowColAttribute>() != null && x.GetValue(this) is Control).Select(x =>
  162. {
  163. RowColAttribute attribute = x.GetCustomAttribute<RowColAttribute>();
  164. Control control = x.GetValue(this) as Control;
  165. return (attribute, control);
  166. }).GroupBy(x => x.attribute, (x => x.control)).ToList().ForEach(x =>
  167. {
  168. Control lable = x.First(x => x is UestcLabel);
  169. using Control control = x.First(x => !(x is UestcLabel));
  170. lable.Left = Width / _ColnumCount * x.Key.Colnum;
  171. lable.Width = control.Width;
  172. lable.Height = lable.Font.Height;
  173. lable.Top = 10 + _RowHeight * x.Key.Row;
  174. control.Left = lable.Left;
  175. control.Top = lable.Bottom + 2;
  176. lable.Width = 120;
  177. control.Width = 120;
  178. });
  179. }
  180. private class ComboboxData<T>
  181. {
  182. public string Key { get; set; }
  183. public T Value { get; set; }
  184. public bool Visible { get; set; }
  185. public ComboboxData(string key, T value, bool visible = true)
  186. {
  187. Key = key;
  188. Value = value;
  189. Visible = visible;
  190. }
  191. }
  192. #region UpdateFunction
  193. [UpdateProperty(nameof(CANDecodePrsnt.SignalType))]
  194. private void UpdateSignalType()
  195. {
  196. this.CbxSignalType.Text = SIHelper.ValueChangeToSI((double)_CANDecodePsrnt.SignalType, 2);
  197. }
  198. [UpdateProperty(nameof(CANDecodePrsnt.SamplePoint))]
  199. private void UpdateSamplePoint()
  200. {
  201. this.BtnSamplePoint.Text = SIHelper.ValueChangeToSI(_CANDecodePsrnt.SamplePoint, 2);
  202. }
  203. [UpdateProperty(nameof(CANDecodePrsnt.SignalRate))]
  204. private void UpdateSignalRate()
  205. {
  206. this.CbxSignalRate.SelectedValue = _CANDecodePsrnt.SignalRate;
  207. }
  208. [UpdateProperty(nameof(CANDecodePrsnt.SignalInput1))]
  209. private void UpdateSignalInput1()
  210. {
  211. ChannelId signalInput1 = _CANDecodePsrnt.SignalInput1;
  212. this.CbxSignalInput1.SelectedValue = signalInput1;
  213. GetDestinPrsnt();
  214. }
  215. #endregion UpdateFunction
  216. public void UpdateView(string propertyName)
  217. {
  218. DecodeSynchronization(propertyName);
  219. if (string.IsNullOrEmpty(propertyName))
  220. {
  221. UpdateView();
  222. return;
  223. }
  224. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
  225. {
  226. UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
  227. return attribute != null && attribute.PropertyName == propertyName;
  228. }).FirstOrDefault()?.Invoke(this, null);
  229. }
  230. private void UpdateView()
  231. {
  232. this.GetType().GetMethods().Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  233. this.Refresh();
  234. }
  235. private string GetDebuggerDisplay()
  236. {
  237. return ToString();
  238. }
  239. }
  240. class RowColAttribute : Attribute
  241. {
  242. public RowColAttribute(Int32 row = 0, Int32 col = 0)
  243. {
  244. Row = row;
  245. Colnum = col;
  246. }
  247. public Int32 Row { get; set; }
  248. public Int32 Colnum { get; set; }
  249. public override bool Equals(object obj)
  250. {
  251. if (obj == null)
  252. return false;
  253. if (!(obj is RowColAttribute attribute))
  254. return false;
  255. return attribute.Colnum == Colnum && attribute.Row == Row;
  256. }
  257. public override int GetHashCode()
  258. {
  259. int hashCode = Row.GetHashCode();
  260. if (Row.GetHashCode() != Colnum.GetHashCode())
  261. hashCode ^= Colnum.GetHashCode();
  262. return hashCode;
  263. }
  264. }
  265. [AttributeUsage(AttributeTargets.Method)]
  266. class UpdatePropertyAttribute : Attribute
  267. {
  268. public UpdatePropertyAttribute(string propertyName) => this.PropertyName = propertyName;
  269. public string PropertyName { get; set; }
  270. }
  271. }