123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Uestc.Auto6.Dso.Core;
- using Uestc.Auto6.Dso.Core.Decode;
- using Uni_Trend.MSO7000X.Common.Helper;
- using Uni_Trend.MSO7000X.UserControls;
- namespace Uestc.Auto6.Dso.Protocol.SPI
- {
- public partial class SPISetControl : UserControl, IProtocolView
- {
- #region 属性定义
- protected new Boolean DesignMode
- {
- get
- {
- Boolean rtnflag = false;
- #if DEBUG
- rtnflag = DesignTimeHelper.InDesignMode(this);
- #endif
- return rtnflag;
- }
- }
- FloatForm _FloatForm;
- private SPIDecodePrsnt DestinDecodePrsnt
- {
- get => _DestinDecodePrsnt;
- set
- {
- if (_DestinDecodePrsnt != value)
- {
- _DestinDecodePrsnt = value;
- ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="SPIDecodePrsnt"/>属性中
- if (SPIDecodePrsnt != null && value != null)
- {
- typeof(SPIDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
- {
- x.SetValue(SPIDecodePrsnt, x.GetValue(value));
- });
- }
- }
- }
- }
- private SPIDecodePrsnt _DestinDecodePrsnt;
- private SPIDecodePrsnt SPIDecodePrsnt => Presenter as SPIDecodePrsnt;
- public IProtocolPrsnt Presenter { get; set; }
- #endregion 属性定义
- public SPISetControl()
- {
- InitializeComponent();
- this.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
- Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(this);
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- Init();
- }
- private void Init()
- {
- GetDestinPrsnt();
- 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 =>
- {
- RowAttribute row = x.GetCustomAttribute<RowAttribute>();
- IProtocolView view = (IProtocolView)Activator.CreateInstance(x);
- view.Presenter = this.Presenter;
- Presenter.TryAddView(view);
- (view as IPage).ShowKeyboard += (sender) =>
- {
- Boolean lastflag = true;
- if (this.FindForm() is FloatForm form)
- {
- lastflag = form.CanClose;
- form.CanClose = false;
- }
- if (_FloatForm != null && !_FloatForm.IsDisposed)
- _FloatForm?.Close();
- _FloatForm = sender as FloatForm;
- _FloatForm.FormClosing += (_, _) =>
- {
- if (this.FindForm() is FloatForm form)
- form.CanClose = lastflag;
- };
- EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
- {
- Current = _FloatForm,
- Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
- });
- };
- TablePage.Controls.Add(view as Control, 0, row.Row);
- });
- SetDecodeChannel();
- SetFrameModel();
- }
- 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();
- }
- /// <summary>
- /// 同步两个Prsnt
- /// </summary>
- /// <param name="propertyName">属性名称</param>
- private void DecodeSynchronization(String propertyName)
- {
- if (DestinDecodePrsnt == null)
- return;
- ///由于使用了<see cref="IRS232DecodePrsnt.ChannelId"/>进行判断具体同步的目标
- ///因此<see cref="IRS232DecodePrsnt.ChannelId"/>参数不进行同步
- if (propertyName == nameof(Core.Decode.SPIDecodePrsnt.MISO) || propertyName == nameof(Core.Decode.SPIDecodePrsnt.MOSI) || propertyName == nameof(SPIDecodePrsnt.DecodeChannel))
- return;
- if (String.IsNullOrEmpty(propertyName))
- {
- typeof(SPIDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
- {
- x.SetValue(DestinDecodePrsnt, x.GetValue(this.SPIDecodePrsnt));
- });
- }
- else
- {
- PropertyInfo propertyInfo = typeof(SPIDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- if (propertyInfo != null)
- propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(this.SPIDecodePrsnt));
- }
- }
- /// <summary>
- /// 获取需要同步的Prsnt
- /// 当<see cref="DestinDecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
- /// 当<see cref="SPIDecodePrsnt.DecodeChannel"/> <see cref="SPIDecodePrsnt.MISO"/> <see cref="SPIDecodePrsnt.MOSI"/> 相同时进行同步
- /// </summary>
- private void GetDestinPrsnt()
- {
- if (this.SPIDecodePrsnt == null)
- return;
- if (SPIDecodePrsnt.IsTrigger)
- DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is SPIDecodePrsnt).Cast<SPIDecodePrsnt>().Where(x =>CheckRules(x,SPIDecodePrsnt)).OrderBy(x => x.MISO).FirstOrDefault();
- else
- {
- if (Presenter.GetTriggerDecodePrsnt() is SPIDecodePrsnt decodePrsnt && CheckRules(decodePrsnt,SPIDecodePrsnt))
- DestinDecodePrsnt = decodePrsnt;
- else
- DestinDecodePrsnt = null;
- }
- }
- /// <summary>
- /// 判断两个Prsnt是否同步的规则
- /// </summary>
- /// <param name="fisrt"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- private Boolean CheckRules(SPIDecodePrsnt fisrt,SPIDecodePrsnt second)
- {
- if (fisrt == null || second == null)
- return false;
- if (fisrt.DecodeChannel != second.DecodeChannel)
- return false;
- if(fisrt.DecodeChannel == ComModel.ProtocolSPI.DecodeChannel.MISO)
- return fisrt.MISO == second.MISO;
- if (fisrt.DecodeChannel == ComModel.ProtocolSPI.DecodeChannel.MOSI)
- return fisrt.MOSI == second.MOSI;
- if (fisrt.DecodeChannel == ComModel.ProtocolSPI.DecodeChannel.MOMI)
- return fisrt.MOSI == second.MOSI && fisrt.MISO == fisrt.MISO;
- return false;
- }
- [UpdateProperty(nameof(Core.Decode.SPIDecodePrsnt.DecodeChannel))]
- private void SetDecodeChannel()
- {
- switch(SPIDecodePrsnt.DecodeChannel)
- {
- case ComModel.ProtocolSPI.DecodeChannel.MISO:
- TablePage.Controls.Cast<Control>().First(x => x is MISOPage).Visible = true;
- TablePage.Controls.Cast<Control>().First(x => x is MOSIPage).Visible = false;
- break;
- case ComModel.ProtocolSPI.DecodeChannel.MOSI:
- TablePage.Controls.Cast<Control>().First(x => x is MISOPage).Visible = false;
- TablePage.Controls.Cast<Control>().First(x => x is MOSIPage).Visible = true;
- break;
- case ComModel.ProtocolSPI.DecodeChannel.MOMI:
- TablePage.Controls.Cast<Control>().First(x => x is MISOPage).Visible = true;
- TablePage.Controls.Cast<Control>().First(x => x is MOSIPage).Visible = true;
- break;
- }
- }
- [UpdateProperty(nameof(Core.Decode.SPIDecodePrsnt.FramingMode))]
- private void SetFrameModel()
- {
- TablePage.Controls.Cast<Control>().First(x => x is CSPage).Visible = SPIDecodePrsnt.FramingMode == ComModel.ProtocolSPI.FramingMode.Slave;
- }
- [UpdateProperty(nameof(Core.Decode.SPIDecodePrsnt.MISO))]
- private void SetMISO()
- {
- GetDestinPrsnt();
- }
- [UpdateProperty(nameof(Core.Decode.SPIDecodePrsnt.MOSI))]
- private void SetMOSI()
- {
- GetDestinPrsnt();
- }
- }
- }
|