using AddInPlugin.Util;
|
using DynamicExpresso;
|
using OpenTap;
|
using OpenTap.Addin;
|
using OpenTap.Addin.Util;
|
using System.Collections.Concurrent;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.IO;
|
using UtilLib;
|
|
namespace AddInPlugin
|
{
|
public class DefaultStationGlobalsManager : IStationGlobalsManager
|
{
|
public virtual ObservableCollection<TestVariable> Datasource { get; protected set; }
|
|
public T GetValue<T>(string key)
|
{
|
var target = new Interpreter();
|
target.SetVariable("StationGlobals", new TestVariable
|
{
|
Name = "StationGlobals",
|
Children = (ObservableCollection<TestVariable>)ReadFromLocal(),
|
Type = TestVariableType.Container
|
});
|
return target.Eval<T>($"StationGlobals.{key}");
|
}
|
|
public void SetValue(string key, object value)
|
{
|
var target = new Interpreter();
|
target.SetVariable("StationGlobals", new TestVariable
|
{
|
Name = "StationGlobals",
|
Children = (ObservableCollection<TestVariable>)ReadFromLocal(),
|
Type = TestVariableType.Container
|
});
|
target.SetVariable("_temp", value);
|
target.Eval($"StationGlobals.{key} = _temp");
|
}
|
|
public virtual IList<TestVariable> ReadFromLocal()
|
{
|
if (Datasource == null)
|
{
|
string path = Path.Combine(RegUtil.GetAppPath(), "Settings", "StationGlobals.data");
|
lock (this)
|
{
|
if (!File.Exists(path))
|
{
|
return new ObservableCollection<TestVariable>();
|
}
|
Datasource = XmlHelper.DeserializeFromXml<ObservableCollection<TestVariable>>(path);
|
}
|
}
|
return Datasource;
|
}
|
|
|
public virtual ConcurrentDictionary<string, RuntimeVariable> ToRuntime(TestPlanRun run)
|
{
|
var VariablePool = new ConcurrentDictionary<string, RuntimeVariable>();
|
foreach (var data in ReadFromLocal())
|
{
|
RuntimeVariable runTime = data.ToRuntime(run);
|
if (runTime is null) continue;
|
VariablePool[runTime.__name___] = runTime;
|
}
|
return VariablePool;
|
}
|
|
public virtual bool WriteToLocal(IEnumerable<TestVariable> datas)
|
{
|
if (datas == null)
|
{
|
datas = Datasource;
|
}
|
string path = Path.Combine(RegUtil.GetAppPath(), "Settings", "StationGlobals.data");
|
lock (this)
|
{
|
XmlHelper.SerializeToXml(path, datas);
|
return true;
|
}
|
}
|
}
|
}
|