123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- using System;
- using System.Collections;
- using System.Linq;
- using System.Reflection;
- using System.Windows.Forms;
- using Uestc.Auto6.Dso.Core;
- using Uestc.Auto6.Dso.Core.Decode;
- using Uestc.Auto6.Dso.ComModel;
- using Uni_Trend.MSO7000X.Language;
- using Uni_Trend.MSO7000X.Common.Helper;
- using Uni_Trend.MSO7000X.UserControls;
- using System.Diagnostics;
- namespace Uestc.Auto6.Dso.Protocol.CAN
- {
- [DebuggerDisplay("{" + nameof(GetDebuggerDisplay) + "(),nq}")]
- public partial class CANSetControl : UserControl, IProtocolView
- {
- #region 属性定义
- protected new Boolean DesignMode
- {
- get
- {
- Boolean rtnflag = false;
- #if DEBUG
- rtnflag = DesignTimeHelper.InDesignMode(this);
- #endif
- return rtnflag;
- }
- }
- public CANDecodePrsnt DestinDecodePrsnt
- {
- get => _DestinDecodePrsnt;
- set
- {
- if (_DestinDecodePrsnt != value)
- {
- _DestinDecodePrsnt = value;
- ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="_CANDecodePsrnt"/>属性中
- if (_CANDecodePsrnt != null && value != null)
- {
- typeof(CANDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
- {
- x.SetValue(_CANDecodePsrnt, x.GetValue(value));
- });
- }
- }
- }
- }
- private CANDecodePrsnt _DestinDecodePrsnt;
- private CANDecodePrsnt _CANDecodePsrnt => Presenter as CANDecodePrsnt;
- public IProtocolPrsnt Presenter { get; set; }
- private FloatForm _FloatForm;
- private Int32 _RowHeight = 66;
- private Int32 _ColnumCount = 2;
- #endregion 属性定义
- public CANSetControl()
- {
- InitializeComponent();
-
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- Init();
- InitView();
- }
- private void Init()
- {
- GetDestinPrsnt();
- InitSignalType();
- InitSignalRate();
- InitSignalInput1();
- SetSamplePoint();
- }
- /// <summary>
- /// 获取需要同步的Prsnt
- /// 当<see cref="_CANDecodePsrnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
- /// </summary>
- private void GetDestinPrsnt()
- {
- if (_CANDecodePsrnt == null)
- return;
- if (_CANDecodePsrnt.IsTrigger)
- DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is CANDecodePrsnt decodePrsnt && decodePrsnt.SignalInput1 == _CANDecodePsrnt.SignalInput1).Cast<CANDecodePrsnt>().OrderBy(x => x.SignalRate).FirstOrDefault();
- else
- {
- if (Presenter.GetTriggerDecodePrsnt() is CANDecodePrsnt CANdecodePsrnt && CANdecodePsrnt.SignalInput1 == _CANDecodePsrnt.SignalInput1)
- DestinDecodePrsnt = CANdecodePsrnt;
- else
- DestinDecodePrsnt = null;
- }
- }
- /// <summary>
- /// 同步两个Prsnt
- /// </summary>
- /// <param name="propertyName">属性名称</param>
- private void DecodeSynchronization(string propertyName)
- {
- if (DestinDecodePrsnt == null)
- return;
- if (propertyName == nameof(CANDecodePrsnt.SignalInput1))
- return;
- if (string.IsNullOrEmpty(propertyName))
- {
- typeof(CANDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
- {
- x.SetValue(DestinDecodePrsnt, x.GetValue(_CANDecodePsrnt));
- });
- }
- else
- {
- PropertyInfo propertyInfo = typeof(CANDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- if (propertyInfo != null)
- propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(_CANDecodePsrnt));
- }
- }
- private void InitSignalType()
- {
- this.CbxSignalType.DataSource = _CANDecodePsrnt.SignalType.GetEnumList();
- this.CbxSignalType.DisplayMember = "Key";
- this.CbxSignalType.ValueMember = "Value";
- this.CbxSignalType.SelectedValueChanged += (_, _) =>
- {
- _CANDecodePsrnt.SignalType = (ProtocolCAN.SignalType)this.CbxSignalType.SelectedValue;
- };
- this.CbxSignalType.SelectedValue = _CANDecodePsrnt.SignalRate;
- }
- private void InitSignalRate()
- {
- this.CbxSignalRate.DataSource = _CANDecodePsrnt.SignalRate.GetEnumList();
- this.CbxSignalRate.DisplayMember = "Key";
- this.CbxSignalRate.ValueMember = "Value";
- this.CbxSignalRate.SelectedValueChanged += (_, _) =>
- {
- _CANDecodePsrnt.SignalRate = (ProtocolCAN.SignalRate)this.CbxSignalRate.SelectedValue;
- };
- this.CbxSignalRate.SelectedValue = _CANDecodePsrnt.SignalRate;
- }
- private void InitSignalInput1()
- {
- this.CbxSignalInput1.DataSource = ChannelIdExt.GetAnalogs().GetEnumList();
- this.CbxSignalInput1.DisplayMember = "Key";
- this.CbxSignalInput1.ValueMember = "Value";
- this.CbxSignalInput1.SelectedValueChanged += (_, _) =>
- {
- _CANDecodePsrnt.SignalInput1 = (ChannelId)this.CbxSignalInput1.SelectedValue;
- SourceChanged();
- };
- this.CbxSignalInput1.SelectedValue = _CANDecodePsrnt.SignalInput1;
- }
- private void SourceChanged()
- {
- //通道发生改变后重新获取需要同步的Prsnt
- GetDestinPrsnt();
- }
- private void SetSamplePoint()
- {
- }
- private void InitView()
- {
- Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
- this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<RowColAttribute>() != null && x.GetValue(this) is Control).Select(x =>
- {
- RowColAttribute attribute = x.GetCustomAttribute<RowColAttribute>();
- Control control = x.GetValue(this) as Control;
- return (attribute, control);
- }).GroupBy(x => x.attribute, (x => x.control)).ToList().ForEach(x =>
- {
- Control lable = x.First(x => x is UestcLabel);
- using Control control = x.First(x => !(x is UestcLabel));
- lable.Left = Width / _ColnumCount * x.Key.Colnum;
- lable.Width = control.Width;
- lable.Height = lable.Font.Height;
- lable.Top = 10 + _RowHeight * x.Key.Row;
- control.Left = lable.Left;
- control.Top = lable.Bottom + 2;
- lable.Width = 120;
- control.Width = 120;
- });
- }
- private class ComboboxData<T>
- {
- public string Key { get; set; }
- public T Value { get; set; }
- public bool Visible { get; set; }
- public ComboboxData(string key, T value, bool visible = true)
- {
- Key = key;
- Value = value;
- Visible = visible;
- }
- }
- #region UpdateFunction
- [UpdateProperty(nameof(CANDecodePrsnt.SignalType))]
- private void UpdateSignalType()
- {
- this.CbxSignalType.Text = SIHelper.ValueChangeToSI((double)_CANDecodePsrnt.SignalType, 2);
- }
- [UpdateProperty(nameof(CANDecodePrsnt.SamplePoint))]
- private void UpdateSamplePoint()
- {
- this.BtnSamplePoint.Text = SIHelper.ValueChangeToSI(_CANDecodePsrnt.SamplePoint, 2);
- }
- [UpdateProperty(nameof(CANDecodePrsnt.SignalRate))]
- private void UpdateSignalRate()
- {
- this.CbxSignalRate.SelectedValue = _CANDecodePsrnt.SignalRate;
- }
- [UpdateProperty(nameof(CANDecodePrsnt.SignalInput1))]
- private void UpdateSignalInput1()
- {
- ChannelId signalInput1 = _CANDecodePsrnt.SignalInput1;
- this.CbxSignalInput1.SelectedValue = signalInput1;
- GetDestinPrsnt();
- }
- #endregion UpdateFunction
- public void UpdateView(string propertyName)
- {
- DecodeSynchronization(propertyName);
- if (string.IsNullOrEmpty(propertyName))
- {
- UpdateView();
- return;
- }
- this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
- {
- UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
- return attribute != null && attribute.PropertyName == propertyName;
- }).FirstOrDefault()?.Invoke(this, null);
- }
- private void UpdateView()
- {
- this.GetType().GetMethods().Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
- this.Refresh();
- }
- private string GetDebuggerDisplay()
- {
- return ToString();
- }
- }
- class RowColAttribute : Attribute
- {
- public RowColAttribute(Int32 row = 0, Int32 col = 0)
- {
- Row = row;
- Colnum = col;
- }
- public Int32 Row { get; set; }
- public Int32 Colnum { get; set; }
- public override bool Equals(object obj)
- {
- if (obj == null)
- return false;
- if (!(obj is RowColAttribute attribute))
- return false;
- return attribute.Colnum == Colnum && attribute.Row == Row;
- }
- public override int GetHashCode()
- {
- int hashCode = Row.GetHashCode();
- if (Row.GetHashCode() != Colnum.GetHashCode())
- hashCode ^= Colnum.GetHashCode();
- return hashCode;
- }
- }
- [AttributeUsage(AttributeTargets.Method)]
- class UpdatePropertyAttribute : Attribute
- {
- public UpdatePropertyAttribute(string propertyName) => this.PropertyName = propertyName;
- public string PropertyName { get; set; }
- }
- }
-
|