using HMI.Core.Util;
|
using HMI.Util;
|
using NationalInstruments.TestStand.Interop.API;
|
using Plugin.TsExecution.Entity;
|
using System;
|
using System.Collections.ObjectModel;
|
using System.ComponentModel;
|
using System.IO;
|
using System.Runtime.CompilerServices;
|
using System.Timers;
|
using System.Windows;
|
using System.Windows.Controls;
|
|
namespace Plugin.TsExecution
|
{
|
/// <summary>
|
/// SingleControl.xaml 的交互逻辑
|
/// </summary>
|
public partial class SingleControl : UserControl, INotifyPropertyChanged
|
{
|
#region ...
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
protected void Set([CallerMemberName] string name = null)
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
}
|
|
protected void Set<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
|
{
|
old = @new;
|
if (propertyName != null)
|
{
|
Set(propertyName);
|
}
|
}
|
#endregion
|
private bool isLoadOK = false;
|
private int index;
|
private string CountIniPath;
|
|
private string status = "Ready";
|
|
public string Status
|
{
|
get { return status; }
|
set { Set(ref status, value); }
|
}
|
|
private double totalNum;
|
public double TotalNum
|
{
|
get => totalNum;
|
private set
|
{
|
Set(ref totalNum, value);
|
}
|
}
|
|
private double passNum;
|
public double PassNum
|
{
|
get => passNum;
|
private set { Set(ref passNum, value); }
|
}
|
|
private double failNum;
|
public double FailNum
|
{
|
get => failNum;
|
private set { Set(ref failNum, value); }
|
}
|
|
private double passRate;
|
public double PassRate
|
{
|
get => passRate;
|
private set
|
{
|
Set(ref passRate, value);
|
}
|
}
|
|
public string NumInfo
|
{
|
get => this.lang("Ts.NumInfo", TotalNum, PassNum, FailNum, string.Format("{0:0.00}", PassRate));
|
}
|
|
public string TestInfo
|
{
|
get => this.lang("Ts.TestInfo", Barcode, ElapseTime);
|
}
|
|
private string barcode = string.Empty;
|
public string Barcode
|
{
|
get { return barcode; }
|
set { Set(ref barcode, value); }
|
}
|
|
private string elapseTime = "00:00:00";
|
public string ElapseTime
|
{
|
get { return elapseTime; }
|
set
|
{
|
if (value == "Start Timing")
|
{
|
this.refreshTimer.Enabled = true;
|
TimeNow = DateTime.Now;
|
}
|
else
|
{
|
this.refreshTimer.Enabled = false;
|
if (value != "Stop Timing")
|
{
|
Set(ref elapseTime, value + "s");
|
Set("TestInfo");
|
}
|
}
|
}
|
}
|
|
private ObservableCollection<TestStep> _datasource = new ObservableCollection<TestStep>();
|
public ObservableCollection<TestStep> Datasource
|
{
|
get => _datasource;
|
set
|
{
|
Set(ref _datasource, value);
|
}
|
}
|
|
private DateTime TimeNow = new DateTime();
|
private TimeSpan TimeCount = new TimeSpan();
|
|
private Timer refreshTimer;
|
|
private string currentStep = "等待扫码";
|
public string CurrentStep
|
{
|
get => currentStep;
|
set
|
{
|
Set(ref currentStep, value);
|
}
|
}
|
|
private int _probeCount;
|
public int ProbeCount
|
{
|
get => _probeCount;
|
set
|
{
|
Set(ref _probeCount, value);
|
}
|
}
|
|
/// <summary>
|
/// 进度值
|
/// </summary>
|
private int _processValue;
|
public int ProcessValue
|
{
|
get => _processValue;
|
set
|
{
|
Set(ref _processValue, value);
|
}
|
}
|
|
public string ProcessValueLabel
|
{
|
get
|
{
|
if (ProcessValue > 0) return $"{ProcessValue}%";
|
return "";
|
}
|
set
|
{
|
}
|
}
|
|
public void SetSimpleUI(bool flag)
|
{
|
if (flag)
|
{
|
simplePanel.Visibility = Visibility.Visible;
|
detailPanel.Visibility = Visibility.Collapsed;
|
}
|
else
|
{
|
simplePanel.Visibility = Visibility.Collapsed;
|
detailPanel.Visibility = Visibility.Visible;
|
}
|
}
|
|
public int ProbeWarn { get; private set; }
|
|
public SingleControl(int Index, string CountIniPath, int probeWarn)
|
{
|
this.CountIniPath = CountIniPath;
|
DataContext = this;
|
this.index = Index;
|
ProbeWarn = probeWarn;
|
|
refreshTimer = new Timer()
|
{
|
Interval = 100
|
};
|
refreshTimer.Elapsed += RefreshTimer_Elapsed;
|
ReadProbeCount();
|
ReadCounter();
|
InitializeComponent();
|
//if (ProbeWarn > 0)
|
//{
|
// probeWarnPanel.Visibility = Visibility.Visible;
|
//}
|
//else
|
//{
|
// probeWarnPanel.Visibility = Visibility.Collapsed;
|
//}
|
}
|
|
private void RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
|
{
|
//Dispatcher.Invoke(() =>
|
//{
|
// var data = PluginLoader.Instance.Plugins.FirstOrDefault(p => p.Name == "TS");
|
// if (data?.Content is TsExecution ts)
|
// {
|
// if (Status == "Testing")
|
// {
|
// ts.UpdatePositionStatus(index, "Passed");
|
// }
|
// else
|
// {
|
// ts.UpdatePositionStatus(index, "Testing");
|
// }
|
// }
|
//});
|
|
//AddTestData("1", "1", "1", "1", "1", "1", "1");
|
|
TimeCount = DateTime.Now - TimeNow;
|
elapseTime = string.Format("{0:00}:{1:00}:{2:00}", TimeCount.Hours, TimeCount.Minutes, TimeCount.Seconds);
|
Set("ElapseTime");
|
Set("TestInfo");
|
}
|
|
public SequenceContext SequenceFileContext
|
{
|
get;
|
set;
|
}
|
|
/// <summary>
|
/// 获取TS的每步测试的数据添加到表格
|
/// 项目名称,测试参数,测试值,上限,下限,单位,结果
|
/// </summary>
|
/// <param name="TS_Data"></param>
|
public void AddTestData(string name, string step_name, string value, string HightLimit, string LowLimit, string unit, string Status)
|
{
|
try
|
{
|
var data = new TestStep
|
{
|
Name = name,
|
Step = step_name,
|
Value = value,
|
LimitUp = HightLimit,
|
LimitDown = LowLimit,
|
Unit = unit,
|
Result = Status
|
};
|
Dispatcher.BeginInvoke(new Action(() =>
|
{
|
Datasource.Add(data);
|
table.ScrollIntoView(data);
|
}));
|
}
|
catch (Exception ex)
|
{
|
//屏蔽错误
|
}
|
}
|
|
public void UpdateStatus(string status)
|
{
|
if (status == "Passed")
|
{
|
UpdateTestCounter(true);
|
}
|
else if (status == "Error" || status == "Failed")
|
{
|
UpdateTestCounter(false);
|
}
|
else if (Status != "Testing" && status == "Testing")
|
{
|
UpdateProbeCount(ProbeCount + 1);
|
}
|
Status = status;
|
}
|
|
public void IniTestData()
|
{
|
Datasource.Clear();
|
}
|
|
/// <summary>
|
/// 设置测试进度条
|
/// </summary>
|
/// <param name="ProcessVal"></param>
|
public void SetTestProcess(int ProcessVal)
|
{
|
ProcessValue = ProcessVal;
|
Set("ProcessValueLabel");
|
}
|
|
private void UserControl_Loaded(object sender, RoutedEventArgs e)
|
{
|
this.UpdateTestCounter(null);
|
}
|
|
private void UpdateTestCounter(bool? result)
|
{
|
if (result == true)
|
{
|
PassNum += 1;
|
}
|
else if (result == false)
|
{
|
FailNum += 1;
|
}
|
|
TotalNum = PassNum + FailNum;
|
if (TotalNum != 0)
|
{
|
PassRate = (PassNum / TotalNum);
|
}
|
else
|
{
|
PassRate = 0.0;
|
}
|
Set("NumInfo");
|
this.WriteCounter();
|
}
|
|
/// <summary>
|
/// 从ini读取探针使用次数
|
/// </summary>
|
private void ReadProbeCount()
|
{
|
try
|
{
|
var num = IniFileHelper.ReadIniData("Socket" + this.index.ToString(), "ProbeCount", "0", this.CountIniPath);
|
this.ProbeCount = int.Parse(num);
|
}
|
catch (Exception ex)
|
{
|
this.ProbeCount = 0;
|
}
|
}
|
|
/// <summary>
|
/// 更新探针使用次数
|
/// </summary>
|
/// <param name="count"></param>
|
private void UpdateProbeCount(int count)
|
{
|
/// 写到配置文件
|
IniFileHelper.WriteIniData("Socket" + this.index.ToString(), "ProbeCount", count.ToString(), this.CountIniPath);
|
ProbeCount = count;
|
|
//if (ProbeWarn <= 0) probeCountAlarm.Visibility = Visibility.Collapsed;
|
//else if (count >= ProbeWarn) probeCountAlarm.Visibility = Visibility.Visible;
|
//else probeCountAlarm.Visibility = Visibility.Collapsed;
|
}
|
|
private void WriteCounter()
|
{
|
IniFileHelper.WriteIniData("Socket" + this.index.ToString(), "Pass",
|
this.passNum.ToString(), this.CountIniPath);
|
IniFileHelper.WriteIniData("Socket" + this.index.ToString(), "Fail", this.failNum.ToString(),
|
this.CountIniPath);
|
}
|
|
private void ReadCounter()
|
{
|
try
|
{
|
this.PassNum = Convert.ToInt32(IniFileHelper.ReadIniData("Socket" + this.index.ToString(), "Pass", "", CountIniPath));
|
this.FailNum = Convert.ToInt32(IniFileHelper.ReadIniData("Socket" + this.index.ToString(), "Fail", "", CountIniPath));
|
}
|
catch (Exception ex)
|
{
|
//do nothing
|
}
|
}
|
|
public void ResetCounter()
|
{
|
this.PassNum = 0;
|
this.FailNum = 0;
|
this.UpdateTestCounter(null);
|
}
|
|
public void WriteToCSV(string path)
|
{
|
try
|
{
|
|
FileInfo fi = new FileInfo(path);
|
if (!fi.Directory.Exists)
|
{
|
fi.Directory.Create(); //文件不存在,创建文件
|
}
|
|
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
|
using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
|
{
|
string title = "项目名称,测试参数,测试值,上限,下限,单位,结果";
|
sw.WriteLine(title);
|
|
if (Datasource != null && Datasource.Count > 0)
|
{
|
foreach (var data in Datasource)
|
{
|
sw.WriteLine($"{data.Name},{data.Step},{data.Value},{data.LimitUp},{data.LimitDown},{data.Unit},{data.Result}");
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
TsExecution.logger.Error("Write CSV File Failed.", ex);
|
}
|
}
|
|
public void ShowPausePanel()
|
{
|
pausePanel.Visibility = Visibility.Visible;
|
}
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
{
|
Button btn = sender as Button;
|
switch (btn.Name)
|
{
|
case "clear":
|
UpdateProbeCount(0);
|
break;
|
case "cancelPauseBtn":
|
SequenceFileContext?.StationGlobals.SetValBoolean($"StationState.IsContinue_{index}", 1, false);
|
pausePanel.Visibility = Visibility.Collapsed;
|
break;
|
case "continuePauseBtn":
|
SequenceFileContext?.StationGlobals.SetValBoolean($"StationState.IsContinue_{index}", 1, true);
|
pausePanel.Visibility = Visibility.Collapsed;
|
break;
|
default:
|
break;
|
}
|
}
|
|
private void pausePanel_Loaded(object sender, RoutedEventArgs e)
|
{
|
var can = pausePanel;
|
var modal = visualModal;
|
Canvas.SetLeft(modal, (pausePanel.ActualWidth - visualModal.ActualWidth) / 2);
|
Canvas.SetTop(modal, (pausePanel.ActualHeight - visualModal.ActualHeight) / 2);
|
}
|
}
|
}
|