using System.Reflection;
using System.Windows.Forms;
using Uestc.Auto6.Dso.Core;
using Uestc.Auto6.Dso.Core.Decode;
using Uestc.Auto6.Dso.ComModel;
using System;
using System.Linq;
using Uni_Trend.MSO7000X.Common.Helper;
using Uni_Trend.MSO7000X.Language;
using Uni_Trend.MSO7000X.UserControls;
using System.Collections.Generic;
namespace Uestc.Auto6.Dso.Protocol.LIN
{
[System.ComponentModel.ToolboxItem(false)]
public partial class LINSetControl : UserControl, IProtocolView
{
#region 属性定义
protected new Boolean DesignMode
{
get
{
Boolean rtnflag = false;
#if DEBUG
rtnflag = DesignTimeHelper.InDesignMode(this);
#endif
return rtnflag;
}
}
public LINDecodePrsnt DestinDecodePrsnt
{
get => _DestinDecodePrsnt;
set
{
if (_DestinDecodePrsnt != value)
{
_DestinDecodePrsnt = value;
///当有新的同步目标时,应先将目标中参数同步到本地属性中
if (LINDecodePrsnt != null && value != null)
{
typeof(LINDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite&& x.CanRead).ToList().ForEach(x =>
{
x.SetValue(LINDecodePrsnt, x.GetValue(value));
});
}
}
}
}
private LINDecodePrsnt _DestinDecodePrsnt;
private LINDecodePrsnt LINDecodePrsnt => Presenter as LINDecodePrsnt;
public IProtocolPrsnt Presenter { get; set; }
#endregion 属性定义
public LINSetControl()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
InitView();
Init();
}
private void InitView()
{
Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(this, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
}
private void Init()
{
GetDestinPrsnt();
InitSource();
InitBPS();
InitCustomBPS();
InitPolarity();
InitSource();
InitThreshold();
InitStandard();
InitDataCount();
}
private void InitSource()
{
CbxSource.DataSource = ChannelIdExt.GetAnalogs().GetEnumList();
CbxSource.ValueMember = "Value";
CbxSource.DisplayMember = "Key";
CbxSource.SelectedValueChanged += (_, _) =>
{
LINDecodePrsnt.Source = (ChannelId)CbxSource.SelectedValue;
};
UpdateSource();
}
private void InitThreshold()
{
BtnThreshold.Click += (_, _) =>
{
SetThreshold();
};
UpdateThreshold();
}
private void InitPolarity()
{
RbgPolarity.ButtonItems = Enum.GetValues().Select(x =>
{
return new RadioButtonItem()
{
Text = x.ToString(),
Tag =x,
};
}).ToArray();
RbgPolarity.IndexChanged += (_, _) =>
{
LINDecodePrsnt.Polarity = (ProtocolLIN.Polarity)RbgPolarity.ButtonItems[RbgPolarity.ChoosedButtonIndex].Tag;
};
}
private void InitBPS()
{
CbxBPS.DataSource = Enum.GetValues().Select(x =>
{
return new KeyValuePair(x switch
{
ProtocolLIN.BPS_ID.BPS_2400 => "2400bps",
ProtocolLIN.BPS_ID.BPS_4800 => "4800bps",
ProtocolLIN.BPS_ID.BPS_9600 => "9600bps",
ProtocolLIN.BPS_ID.BPS_19200 => "19200bps",
ProtocolLIN.BPS_ID.BPS_Special => "Custom",
_ => "Custom",
}, x);
}).ToList();
CbxBPS.DisplayMember = "Key";
CbxBPS.ValueMember = "Value";
CbxBPS.SelectedValueChanged += (_, _) =>
{
LINDecodePrsnt.BPS = (ProtocolLIN.BPS_ID)CbxBPS.SelectedValue;
};
UpdateBPS();
}
private void InitCustomBPS()
{
BtnCustomBPS.Click += (_, _) =>
{
SetCustomBPS();
};
UpdateCustomBPS();
}
private void InitStandard()
{
CbxStandard.DataSource = Enum.GetValues().Select(x =>
{
return new KeyValuePair(x switch
{
ProtocolLIN.Standard.V1=>"LIN1.0",
ProtocolLIN.Standard.V2=>"LIN2.0",
_=>"LIN1.0",
}, x);
}).ToList();
CbxStandard.DisplayMember = "Key";
CbxStandard.ValueMember="Value";
CbxStandard.SelectedValueChanged += (_, _) =>
{
LINDecodePrsnt.Standard = (ProtocolLIN.Standard)CbxStandard.SelectedValue;
};
UpdateStandard();
}
private void InitDataCount()
{
BtnDataCount.Click += (_, _) =>
{
SetDataCount();
};
UpdateDataCount();
}
private void SetDataCount()
{
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.RegisterControl(x));
numberKeyboard.Top = floatForm.HeadHeight;
numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
numberKeyboard.Width = floatForm.Width;
numberKeyboard.ForeColor = ForeColor;
numberKeyboard.BackColor = BackColor;
numberKeyboard.DefaultValue = LINDecodePrsnt.DataCount;
numberKeyboard.Unit = "";
numberKeyboard.MinValue = LINDecodePrsnt.MinDataCount;
numberKeyboard.MaxValue = LINDecodePrsnt.MaxDataCount;
numberKeyboard.DecimalNumber = 2;
numberKeyboard.OkClickEvent += (sender, args) =>
{
this.LINDecodePrsnt.DataCount = (Int32)args.Data;
floatForm?.Close();
};
numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
floatForm.Controls.Add(numberKeyboard);
floatForm.FormClosing += (_, _) =>
{
if (this.FindForm() is FloatForm form)
form.CanClose = lastflag;
};
floatForm.ShowDialogByPosition();
}
private void SetCustomBPS()
{
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.RegisterControl(x));
numberKeyboard.Top = floatForm.HeadHeight;
numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
numberKeyboard.Width = floatForm.Width;
numberKeyboard.ForeColor = ForeColor;
numberKeyboard.BackColor = BackColor;
numberKeyboard.DefaultValue = LINDecodePrsnt.CustomBPS;
numberKeyboard.Unit = "bps";
numberKeyboard.MinValue = LINDecodePrsnt.MinBPS;
numberKeyboard.MaxValue = LINDecodePrsnt.MaxBPS;
numberKeyboard.DecimalNumber = 2;
numberKeyboard.OkClickEvent += (sender, args) =>
{
this.LINDecodePrsnt.CustomBPS = (Int32)args.Data;
floatForm?.Close();
};
numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
floatForm.Controls.Add(numberKeyboard);
floatForm.FormClosing += (_, _) =>
{
if (this.FindForm() is FloatForm form)
form.CanClose = lastflag;
};
floatForm.ShowDialogByPosition();
}
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.RegisterControl(x));
numberKeyboard.Top = floatForm.HeadHeight;
numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
numberKeyboard.Width = floatForm.Width;
numberKeyboard.ForeColor = ForeColor;
numberKeyboard.BackColor = BackColor;
numberKeyboard.DefaultValue = LINDecodePrsnt.Threshold;
numberKeyboard.Unit = "V";
numberKeyboard.MinValue = LINDecodePrsnt.MinThreshold;
numberKeyboard.MaxValue = LINDecodePrsnt.MaxThreshold;
numberKeyboard.DecimalNumber = 2;
numberKeyboard.OkClickEvent += (sender, args) =>
{
this.LINDecodePrsnt.Threshold = args.Data;
this.BtnThreshold.Text = SIHelper.ValueChangeToSI(LINDecodePrsnt.Threshold, 2, "V");
floatForm?.Close();
};
numberKeyboard.CancelEvent += (sender, args) => floatForm?.Close();
floatForm.Controls.Add(numberKeyboard);
floatForm.FormClosing += (_, _) =>
{
if (this.FindForm() is FloatForm form)
form.CanClose = lastflag;
};
floatForm.ShowDialogByPosition();
}
///
/// 获取需要同步的Prsnt
/// 当为时表示不需要与其他Prsnt进行同步
///
private void GetDestinPrsnt()
{
if (LINDecodePrsnt == null)
return;
if (LINDecodePrsnt.IsTrigger)
DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is LINDecodePrsnt).Cast().Where(x => x.Source == LINDecodePrsnt.Source).OrderBy(x => x.Source).FirstOrDefault();
else
{
if (Presenter.GetTriggerDecodePrsnt() is LINDecodePrsnt decodePrsnt && decodePrsnt.Source == LINDecodePrsnt.Source)
DestinDecodePrsnt = decodePrsnt;
else
DestinDecodePrsnt = null;
}
}
///
/// 同步两个Prsnt
///
/// 属性名称
private void DecodeSynchronization(String propertyName)
{
if (DestinDecodePrsnt == null)
return;
///由于使用了进行判断具体同步的目标
///因此参数不进行同步
if (propertyName == nameof(LINDecodePrsnt.Source))
return;
if (String.IsNullOrEmpty(propertyName))
{
typeof(LINDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanWrite&&x.CanRead).ToList().ForEach(x =>
{
x.SetValue(DestinDecodePrsnt, x.GetValue(LINDecodePrsnt));
});
}
else
{
PropertyInfo propertyInfo = typeof(LINDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
if (propertyInfo != null&&propertyInfo.CanWrite&&propertyInfo.CanRead)
propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(LINDecodePrsnt));
}
}
public void UpdateView(Object presenter, String propertyName)
{
DecodeSynchronization(propertyName);
if (String.IsNullOrEmpty(propertyName))
{
UpdateView();
return;
}
this.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(x =>
{
var attr = x.GetCustomAttribute();
return attr != null && attr.PropertyName == propertyName;
}).FirstOrDefault()?.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(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.Source))]
private void UpdateSource()
{
CbxSource.SelectedValue = LINDecodePrsnt.Source;
GetDestinPrsnt();
}
[UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.Threshold))]
private void UpdateThreshold()
{
this.BtnThreshold.Text = SIHelper.ValueChangeToSI(LINDecodePrsnt.Threshold, 2, "V");
}
[UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.Polarity))]
private void UpdatePolarity()
{
this.RbgPolarity.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == LINDecodePrsnt.Polarity);
}
[UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.BPS))]
private void UpdateBPS()
{
this.CbxBPS.SelectedValue = LINDecodePrsnt.BPS;
BtnCustomBPS.Visible = LINDecodePrsnt.BPS == ProtocolLIN.BPS_ID.BPS_Special;
LblCustomBPS.Visible = BtnCustomBPS.Visible;
}
[UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.CustomBPS))]
private void UpdateCustomBPS()
{
this.BtnCustomBPS.Text = LINDecodePrsnt.CustomBPS + "bps";
}
[UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.Standard))]
private void UpdateStandard()
{
this.CbxStandard.SelectedValue = LINDecodePrsnt.Standard;
}
[UpdateProperty(nameof(Uestc.Auto6.Dso.Core.Decode.LINDecodePrsnt.DataCount))]
private void UpdateDataCount()
{
this.BtnDataCount.Text = LINDecodePrsnt.DataCount + "";
}
}
}