using AddInPlugin.Util;
using AddInPlugin;
using Microsoft.Win32;
using OpenTap.Addin.Listener;
using OpenTap.Addin.Reader;
using OpenTap.Addin.Util;
using OpenTap.Addin;
using OpenTap;
using System.Collections.ObjectModel;
using System.Threading;
using System;
using System.Windows;
namespace TestSeq
{
public class Net47Getter : IRegGetter
{
public string GetAppPath(string keyPath)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyPath))
{
if (key == null)
{
throw new Exception("注册表不存在,请先运行一次编辑器");
}
string value = key.GetValue("Location")?.ToString();
if (string.IsNullOrEmpty(value))
{
throw new Exception("注册表不存在,请先运行一次编辑器");
}
return value;
}
}
}
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public const int MaxLogCount = 1000;
private readonly ObservableCollection _logItems = new ObservableCollection();
public MainWindow()
{
RegUtil.Register(new Net47Getter());
StationGlobalsManager.Register(new DefaultStationGlobalsManager());
VariableResolverFactory.Register(new DefaultVariableResolverFactory());
InitializeComponent();
LogListView.ItemsSource = _logItems;
}
private void self_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
tapThread?.Abort();
tapThread = null;
AppendLog("已停止");
}
///
/// 打开按钮:选择 *.TestPlan 文件,并将路径显示在 PathTextBlock 中。
///
private void OpenButton_Click(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog
{
Title = "选择 TapPlan 文件",
Filter = "TapPlan 文件 (*.TapPlan)|*.TapPlan|所有文件 (*.*)|*.*",
CheckFileExists = true,
Multiselect = false
};
if (dlg.ShowDialog(this) == true)
{
PathTextBlock.Text = dlg.FileName;
}
}
private TapThread tapThread;
// TODO: 执行按钮逻辑,由你自己编写
private void RunButton_Click(object sender, RoutedEventArgs e)
{
_logItems.Clear();
var uiMessageHandler = PostUIMessageHandler.Get();
uiMessageHandler.OnPostUIMsg += UiMessageHandler_OnPostUIMsg;
TestPlan plan = TestPlanReader.ReadTestPlan(PathTextBlock.Text);
tapThread = TapThread.Start(() =>
{
ResultSettings.Current.Add(uiMessageHandler);
var run = plan.Execute(ResultSettings.Current, null, null, null);
});
AppendLog($"执行{PathTextBlock.Text}...");
}
// TODO: 停止按钮逻辑,由你自己编写
private void StopButton_Click(object sender, RoutedEventArgs e)
{
tapThread?.Abort();
tapThread = null;
AppendLog("已停止");
}
///
/// 追加一行日志到日志区域,可在任意线程调用。
///
public void AppendLog(string message)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(() => AppendLog(message));
return;
}
string line = $"[{DateTime.Now:HH:mm:ss}] {message}";
_logItems.Add(line);
while (_logItems.Count > MaxLogCount)
{
_logItems.RemoveAt(0);
}
LogListView.ScrollIntoView(_logItems[_logItems.Count - 1]);
}
private void UiMessageHandler_OnPostUIMsg(object sender, PostUIMsg e)
{
AppendLog($"string:{e.StringData} bool:{e.BoolData} number:{e.NumberData}");
}
}
}