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;
using System.Linq;
using System.Collections.Generic;
using Uni_Trend.MSO7000X.UserControls.Style;
namespace Uestc.Auto6.Dso.Protocol.MIL
{
[System.ComponentModel.ToolboxItem(false)]
public partial class MILSetControl : UserControl, IProtocolView
{
#region 属性定义
protected new Boolean DesignMode
{
get
{
Boolean rtnflag = false;
#if DEBUG
rtnflag = DesignTimeHelper.InDesignMode(this);
#endif
return rtnflag;
}
}
public MILDecodePrsnt DestinDecodePrsnt
{
get => _DestinDecodePrsnt;
set
{
if (_DestinDecodePrsnt != value)
{
_DestinDecodePrsnt = value;
///当有新的同步目标时,应先将目标中参数同步到本地属性中
if (DecodePrsnt != null && value != null)
{
typeof(MILDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
{
x.SetValue(DecodePrsnt, x.GetValue(value));
});
}
}
}
}
private MILDecodePrsnt _DestinDecodePrsnt;
private MILDecodePrsnt DecodePrsnt => Presenter as MILDecodePrsnt;
public IProtocolPrsnt Presenter { get; set; }
#endregion 属性定义
public MILSetControl()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(this, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
Init();
}
private void Init()
{
GetDestinPrsnt();
InitPolarity();
InitRealBiteRate();
InitSignalRate();
InitSource();
InitThreshold();
UpdateView();
}
private void InitSource()
{
this.CbxSource.DataSource = ComModel.ChannelIdExt.GetAnalogs().Select(x => new ComboBoxData(x.ToString(), x)).ToList();
this.CbxSource.ValueMember = "Value";
this.CbxSource.DisplayMember = "Key";
this.CbxSource.SelectedValueChanged += (_, _) =>
{
DecodePrsnt.Source = (ChannelId)this.CbxSource.SelectedValue;
};
}
private void InitThreshold()
{
this.BtnThreshold.Click += (sender, args) => SetThreshold();
}
private void SetThreshold()
{
Boolean lastflag = true;
if (this.FindForm() is FloatForm form)
{
lastflag = form.CanClose;
form.CanClose = false;
}
FloatForm floatForm = new FloatForm();
floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
floatForm.BackColor = BackColor;
floatForm.ForeColor = ForeColor;
floatForm.Width = 380;
floatForm.Height = 460 + floatForm.HeadHeight;
floatForm.Title = "设置门限";
Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
numberKeyboard.Controls.Cast().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
numberKeyboard.Top = floatForm.HeadHeight;
numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
numberKeyboard.Width = floatForm.Width;
numberKeyboard.ForeColor = ForeColor;
numberKeyboard.BackColor = BackColor;
numberKeyboard.DefaultValue = DecodePrsnt.Threshold;
numberKeyboard.Unit = "V";
numberKeyboard.MinValue = DecodePrsnt.MinThreshold;
numberKeyboard.MaxValue = DecodePrsnt.MaxThreshold;
numberKeyboard.DecimalNumber = 2;
numberKeyboard.OkClickEvent += (sender, args) =>
{
this.DecodePrsnt.Threshold = args.Data;
floatForm?.Close();
};
numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
floatForm.Controls.Add(numberKeyboard);
DefaultStyleManager.Instance.RegisterControlRecursion(floatForm, StyleFlag.FontSize);
floatForm.FormClosing += (_, _) =>
{
if (this.FindForm() is FloatForm form)
form.CanClose = lastflag;
};
floatForm.ShowDialogByPosition();
}
private void InitPolarity()
{
this.RbgPolarity.ButtonItems = Enum.GetValues().Select(x =>
{
String key = String.Empty;
key = Properties.Resources.ResourceManager.GetString(x.GetType().ReflectedType.FullName + "." + x.GetType().Name + "." + x.ToString());
if (String.IsNullOrEmpty(key))
{
key = x.ToString();
}
return new RadioButtonItem()
{
Tag = x,
Text = key
};
}).ToArray();
this.RbgPolarity.IndexChanged += (_, _) =>
{
DecodePrsnt.Polarity = (ProtocolMIL.Polarity)this.RbgPolarity.ButtonItems[this.RbgPolarity.ChoosedButtonIndex].Tag;
};
}
private void InitSignalRate()
{
this.CbxBitRate.DataSource = Enum.GetValues().Select(x => new KeyValuePair(x switch
{
ProtocolMIL.SignalRate.SignalRate_1M => "1Mbps",
ProtocolMIL.SignalRate.SignalRate_10M => "10Mbps",
ProtocolMIL.SignalRate.Custom => "Custom",
_ => "1Mbps",
}, x)).ToList();
this.CbxBitRate.ValueMember = "Value";
this.CbxBitRate.DisplayMember = "Key";
this.CbxBitRate.SelectedValueChanged += (_, _) =>
{
DecodePrsnt.SignalRate = (ProtocolMIL.SignalRate)this.CbxBitRate.SelectedValue;
};
}
private void InitRealBiteRate()
{
this.BtnRealBitRate.Click += (sender, args) => SetBitRate();
}
private void SetBitRate()
{
Boolean lastflag = true;
if (this.FindForm() is FloatForm form)
{
lastflag = form.CanClose;
form.CanClose = false;
}
FloatForm floatForm = new FloatForm();
floatForm = new Uni_Trend.MSO7000X.UserControls.FloatForm();
floatForm.BackColor = BackColor;
floatForm.ForeColor = ForeColor;
floatForm.Width = 380;
floatForm.Height = 460 + floatForm.HeadHeight;
floatForm.Title = "设置波特率";
Uni_Trend.MSO7000X.UserControls.NumberKeyboard numberKeyboard = new Uni_Trend.MSO7000X.UserControls.NumberKeyboard();
numberKeyboard.Controls.Cast().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
numberKeyboard.Top = floatForm.HeadHeight;
numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
numberKeyboard.Width = floatForm.Width;
numberKeyboard.ForeColor = ForeColor;
numberKeyboard.BackColor = BackColor;
numberKeyboard.DefaultValue = DecodePrsnt.RealSignalRate;
numberKeyboard.Unit = "bps";
numberKeyboard.MinValue = DecodePrsnt.MinSignalRate;
numberKeyboard.MaxValue = DecodePrsnt.MaxSignalRate;
numberKeyboard.DecimalNumber = 2;
numberKeyboard.OkClickEvent += (sender, args) =>
{
this.DecodePrsnt.RealSignalRate = (Int32)args.Data;
floatForm?.Close();
};
numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
floatForm.Controls.Add(numberKeyboard);
DefaultStyleManager.Instance.RegisterControlRecursion(floatForm, StyleFlag.FontSize);
floatForm.FormClosing += (_, _) =>
{
if (this.FindForm() is FloatForm form)
form.CanClose = lastflag;
};
floatForm.ShowDialogByPosition();
}
///
/// 获取需要同步的Prsnt
/// 当为时表示不需要与其他Prsnt进行同步
///
private void GetDestinPrsnt()
{
if (DecodePrsnt == null)
return;
if (DecodePrsnt.IsTrigger)
DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is MILDecodePrsnt).Cast().Where(x => x.Source == DecodePrsnt.Source).OrderBy(x => x.Source).FirstOrDefault();
else
{
if (Presenter.GetTriggerDecodePrsnt() is MILDecodePrsnt rs232decodePrsnt && rs232decodePrsnt.Source == DecodePrsnt.Source)
DestinDecodePrsnt = rs232decodePrsnt;
else
DestinDecodePrsnt = null;
}
}
///
/// 同步两个Prsnt
///
/// 属性名称
private void DecodeSynchronization(String propertyName)
{
if (DestinDecodePrsnt == null)
return;
///由于使用了进行判断具体同步的目标
///因此参数不进行同步
if (propertyName == nameof(MILDecodePrsnt.Source))
return;
if (String.IsNullOrEmpty(propertyName))
{
typeof(MILDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite).ToList().ForEach(x =>
{
x.SetValue(DestinDecodePrsnt, x.GetValue(DecodePrsnt));
});
}
else
{
PropertyInfo propertyInfo = typeof(MILDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
if (propertyInfo != null && propertyInfo.CanWrite&& propertyInfo.CanRead)
propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(DecodePrsnt));
}
}
public void UpdateView(Object presenter, String propertyName)
{
DecodeSynchronization(propertyName);
if (String.IsNullOrEmpty(propertyName))
{
UpdateView();
return;
}
this.GetType()
.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
.Where(x => x.GetCustomAttribute() != null && x.GetCustomAttribute().PropertyName == propertyName)
.ToList()
.ForEach(x => x?.Invoke(this, null));
}
private void UpdateView()
{
this.GetType().GetMethods( BindingFlags.Public| BindingFlags.Instance| BindingFlags.NonPublic).Where(x => x.GetCustomAttribute() != null).ToList().ForEach(x => x?.Invoke(this, null));
this.Refresh();
}
[UpdateProperty(nameof(MILDecodePrsnt.SignalRate))]
private void UpdateRate()
{
this.CbxBitRate.SelectedValue = DecodePrsnt.SignalRate;
this.LblRealBitRate.Visible = DecodePrsnt.SignalRate == ProtocolMIL.SignalRate.Custom;
this.BtnRealBitRate.Visible = this.LblRealBitRate.Visible;
}
[UpdateProperty(nameof(MILDecodePrsnt.RealSignalRate))]
private void UpdateRealRate()
{
this.BtnRealBitRate.Text = SIHelper.ValueChangeToSI(DecodePrsnt.RealSignalRate, 2, "bps");
}
[UpdateProperty(nameof(MILDecodePrsnt.Threshold))]
private void UpdateThreshold()
{
this.BtnThreshold.Text = SIHelper.ValueChangeToSI(DecodePrsnt.Threshold, 2, "V");
}
[UpdateProperty(nameof(MILDecodePrsnt.Polarity))]
private void UpdatePolarity()
{
this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == DecodePrsnt.Polarity);
}
[UpdateProperty(nameof(MILDecodePrsnt.Source))]
private void UpdateSource()
{
this.CbxSource.SelectedValue = DecodePrsnt.Source;
GetDestinPrsnt();
}
}
}