-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathState.cs
198 lines (179 loc) · 7.58 KB
/
State.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* Cognite Extractor for OPC-UA
Copyright (C) 2021 Cognite AS
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
using Cognite.OpcUa.Config;
using Cognite.OpcUa.History;
using Cognite.OpcUa.Nodes;
using Cognite.OpcUa.Types;
using Opc.Ua;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace Cognite.OpcUa
{
/// <summary>
/// Used to wrap the significant state of the extractor and provide utility functions and properties
/// </summary>
public class State
{
private readonly ConcurrentDictionary<NodeId, VariableExtractionState> nodeStates =
new ConcurrentDictionary<NodeId, VariableExtractionState>();
private readonly ConcurrentDictionary<string, VariableExtractionState> nodeStatesByExtId =
new ConcurrentDictionary<string, VariableExtractionState>();
private readonly ConcurrentDictionary<NodeId, EventExtractionState> emitterStates =
new ConcurrentDictionary<NodeId, EventExtractionState>();
private readonly ConcurrentDictionary<string, NodeId> externalToNodeId =
new ConcurrentDictionary<string, NodeId>();
private readonly ConcurrentDictionary<string, EventExtractionState> emitterStatesByExtId =
new ConcurrentDictionary<string, EventExtractionState>();
public ConcurrentDictionary<NodeId, UAObjectType> ActiveEvents { get; }
= new ConcurrentDictionary<NodeId, UAObjectType>();
private readonly ConcurrentDictionary<NodeId, MappedNode> mappedNodes =
new ConcurrentDictionary<NodeId, MappedNode>();
public ICollection<VariableExtractionState> NodeStates => nodeStates.Values;
public ICollection<EventExtractionState> EmitterStates => emitterStates.Values;
/// <summary>
/// Return a NodeExtractionState by externalId
/// </summary>
/// <param name="externalId">ExternalId for lookup</param>
/// <returns>State if it exists</returns>
public VariableExtractionState? GetNodeState(string externalId)
{
if (externalId == null) return null;
return nodeStatesByExtId.GetValueOrDefault(externalId);
}
/// <summary>
/// Return a NodeExtractionState by nodeId
/// </summary>
/// <param name="id">NodeId for lookup</param>
/// <returns>State if it exists</returns>
public VariableExtractionState? GetNodeState(NodeId id)
{
if (id == null || id.IsNullNodeId) return null;
return nodeStates.GetValueOrDefault(id);
}
/// <summary>
/// Return an EventExtractionState by externalId
/// </summary>
/// <param name="externalId">ExternalId for lookup</param>
/// <returns>State if it exists</returns>
public EventExtractionState? GetEmitterState(string? externalId)
{
if (externalId == null) return null;
return emitterStatesByExtId.GetValueOrDefault(externalId);
}
/// <summary>
/// Return an EventExtractionState by nodeId
/// </summary>
/// <param name="id">NodeId for lookup</param>
/// <returns>State if it exists</returns>
public EventExtractionState? GetEmitterState(NodeId id)
{
if (id == null || id.IsNullNodeId) return null;
return emitterStates.GetValueOrDefault(id);
}
/// <summary>
/// Add node state to storage
/// </summary>
/// <param name="state">State to add</param>
/// <param name="uniqueId">ExternalId, leave empty to auto generate</param>
public void SetNodeState(VariableExtractionState state, string? uniqueId = null)
{
nodeStates[state.SourceId] = state;
nodeStatesByExtId[uniqueId ?? state.Id] = state;
}
/// <summary>
/// Add event state to storage
/// </summary>
/// <param name="state">State to add</param>
/// <param name="uniqueId">ExternalId, leave empty to auto generate</param>
public void SetEmitterState(EventExtractionState state)
{
emitterStates[state.SourceId] = state;
emitterStatesByExtId[state.Id] = state;
}
/// <summary>
/// Returns corresponding NodeId to given uniqueId if it exists.
/// </summary>
/// <param name="uniqueId">Id to map</param>
/// <returns>NodeId if it exists</returns>
public NodeId GetNodeId(string? uniqueId)
{
if (string.IsNullOrEmpty(uniqueId)) return NodeId.Null;
return externalToNodeId.GetValueOrDefault(uniqueId) ?? NodeId.Null;
}
/// <summary>
/// Register a reverse node mapping
/// </summary>
/// <param name="nodeId">NodeId value</param>
/// <param name="id">UniqueId key</param>
public void RegisterNode(NodeId nodeId, string? id)
{
if (id == null) return;
externalToNodeId[id] = nodeId;
}
/// <summary>
/// Returns true if given NodeId is a managed node
/// </summary>
/// <param name="id">NodeId to test</param>
/// <returns>True if id exists in managed nodes</returns>
public bool IsMappedNode(NodeId id)
{
if (id == null || id.IsNullNodeId) return false;
return mappedNodes.ContainsKey(id);
}
/// <summary>
/// Add node to overview of known mapped nodes
/// </summary>
/// <param name="node">Node to add</param>
public void AddActiveNode(BaseUANode node, TypeUpdateConfig update, bool dataTypeMetadata, bool nodeTypeMetadata)
{
mappedNodes[node.Id] = new MappedNode(node, update, dataTypeMetadata, nodeTypeMetadata);
}
/// <summary>
/// Get node checksum by NodeId and index if it exists
/// </summary>
/// <param name="id">NodeId to use for lookup</param>
/// <returns></returns>
public MappedNode? GetMappedNode(NodeId id)
{
if (id == null || id.IsNullNodeId) return null;
if (mappedNodes.TryGetValue(id, out var checksum)) return checksum;
return null;
}
public void PopulateActiveEventTypes(Dictionary<NodeId, UAObjectType> types)
{
foreach (var tp in types.Values)
{
ActiveEvents[tp.Id] = tp;
}
}
/// <summary>
/// Number of currently managed non-property nodes.
/// </summary>
public int NumActiveNodes => mappedNodes.Count(kvp => !kvp.Value.IsProperty);
/// <summary>
/// Wipe the state
/// </summary>
public void Clear()
{
mappedNodes.Clear();
nodeStates.Clear();
nodeStatesByExtId.Clear();
emitterStates.Clear();
emitterStatesByExtId.Clear();
externalToNodeId.Clear();
ActiveEvents.Clear();
}
}
}