-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathPublisherSource.cs
More file actions
117 lines (98 loc) · 3.3 KB
/
PublisherSource.cs
File metadata and controls
117 lines (98 loc) · 3.3 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Opc.Ua;
using System.Text;
using System.Globalization;
namespace UaPublisher
{
internal class PublisherSource
{
protected readonly object m_lock = new();
protected Dictionary<string, FieldMetaData> m_fields = new();
protected DateTime m_lastScanTime = DateTime.MinValue;
protected DataSetMetaDataType m_metadata;
public PublisherSource()
{
}
public int Id { get; protected set; }
public string Name { get; protected set; }
public DataSetMetaDataType MetaData => m_metadata;
public ConfigurationVersionDataType MetaDataVersion => m_metadata?.ConfigurationVersion ?? new ConfigurationVersionDataType();
private static readonly DateTime kBaseDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
protected static uint GetVersionTime()
{
var ticks = (DateTime.UtcNow - kBaseDateTime).TotalMilliseconds;
return (uint)ticks;
}
protected string GetPath(params string[] paths)
{
if (paths == null || paths.Length == 0)
{
return String.Empty;
}
StringBuilder sb = new();
foreach (var path in paths)
{
if (sb.Length > 0) sb.Append("/");
sb.Append(String.Format(CultureInfo.InvariantCulture, path, Id));
}
return sb.ToString();
}
protected DataValue GetDataValue(IDictionary<string, Variant> cache, string path, Variant newValue, DateTime timestamp, bool isKeyFrame = false)
{
if (!isKeyFrame && cache.TryGetValue(path, out var lastValue))
{
if (newValue.Equals(lastValue))
{
return null;
}
}
cache[path] = newValue;
return new DataValue()
{
WrappedValue = newValue,
SourceTimestamp = timestamp,
ServerTimestamp = DateTime.UtcNow
};
}
protected bool ReadFieldValue(
List<KeyValuePair<FieldMetaData, DataValue>> fields,
IDictionary<string, Variant> cache,
string path,
Variant newValue,
DateTime timestamp,
bool isKeyFrame = false)
{
var dv = GetDataValue(
cache,
path,
newValue,
timestamp,
isKeyFrame);
if (dv != null)
{
lock (m_lock)
{
if (m_fields.TryGetValue(path, out var field))
{
fields.Add(new(field, dv));
return true;
}
}
}
return false;
}
public virtual List<KeyValuePair<FieldMetaData, DataValue>> ReadChangedFields(
IDictionary<string, Variant> cache,
bool isKeyFrame)
{
return new List<KeyValuePair<FieldMetaData, DataValue>>();
}
public virtual DataSetMetaDataType BuildMetaData()
{
return null;
}
public virtual Task Update()
{
return Task.CompletedTask;
}
}
}