alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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;
            }
        }
    }
}