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 Datasource { get; protected set; } public T GetValue(string key) { var target = new Interpreter(); target.SetVariable("StationGlobals", new TestVariable { Name = "StationGlobals", Children = (ObservableCollection)ReadFromLocal(), Type = TestVariableType.Container }); return target.Eval($"StationGlobals.{key}"); } public void SetValue(string key, object value) { var target = new Interpreter(); target.SetVariable("StationGlobals", new TestVariable { Name = "StationGlobals", Children = (ObservableCollection)ReadFromLocal(), Type = TestVariableType.Container }); target.SetVariable("_temp", value); target.Eval($"StationGlobals.{key} = _temp"); } public virtual IList ReadFromLocal() { if (Datasource == null) { string path = Path.Combine(RegUtil.GetAppPath(), "Settings", "StationGlobals.data"); lock (this) { if (!File.Exists(path)) { return new ObservableCollection(); } Datasource = XmlHelper.DeserializeFromXml>(path); } } return Datasource; } public virtual ConcurrentDictionary ToRuntime(TestPlanRun run) { var VariablePool = new ConcurrentDictionary(); 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 datas) { if (datas == null) { datas = Datasource; } string path = Path.Combine(RegUtil.GetAppPath(), "Settings", "StationGlobals.data"); lock (this) { XmlHelper.SerializeToXml(path, datas); return true; } } } }