-
Notifications
You must be signed in to change notification settings - Fork 10
/
CommandProcessor.cs
198 lines (166 loc) · 8.01 KB
/
CommandProcessor.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
namespace Opc.Ua.Cloud.Publisher
{
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Opc.Ua;
using Opc.Ua.Cloud.Publisher.Interfaces;
using Opc.Ua.Cloud.Publisher.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class CommandProcessor : ICommandProcessor
{
private readonly ILogger _logger;
private readonly IUAClient _uaClient;
public CommandProcessor(ILoggerFactory loggerFactory, IUAClient client)
{
_logger = loggerFactory.CreateLogger("CommandProcessor");
_uaClient = client;
}
public byte[] PublishNodes(string payload)
{
UserAuthModeEnum desiredAuthenticationMode = UserAuthModeEnum.Anonymous;
List<string> statusResponse = new List<string>();
PublishNodesInterfaceModel publishNodesMethodData = JsonConvert.DeserializeObject<PublishNodesInterfaceModel>(payload);
if (publishNodesMethodData.OpcAuthenticationMode == UserAuthModeEnum.UsernamePassword)
{
if (string.IsNullOrWhiteSpace(publishNodesMethodData.UserName) && string.IsNullOrWhiteSpace(publishNodesMethodData.Password))
{
throw new ArgumentException($"If {nameof(publishNodesMethodData.OpcAuthenticationMode)} is set to '{UserAuthModeEnum.UsernamePassword}', you have to specify username and password.");
}
}
// check for events
if (publishNodesMethodData.OpcEvents != null)
{
foreach (EventModel opcEvent in publishNodesMethodData.OpcEvents)
{
NodePublishingModel node = new NodePublishingModel()
{
ExpandedNodeId = ExpandedNodeId.Parse(opcEvent.ExpandedNodeId),
EndpointUrl = new Uri(publishNodesMethodData.EndpointUrl).ToString(),
Username = publishNodesMethodData.UserName,
Password = publishNodesMethodData.Password,
OpcAuthenticationMode = desiredAuthenticationMode
};
node.Filter = new List<FilterModel>();
node.Filter.AddRange(opcEvent.Filter);
_uaClient.PublishNodeAsync(node).GetAwaiter().GetResult();
string statusMessage = $"Event {node.ExpandedNodeId} on endpoint {node.EndpointUrl} published successfully.";
statusResponse.Add(statusMessage);
_logger.LogInformation(statusMessage);
}
}
// check for variables
if (publishNodesMethodData.OpcNodes != null)
{
foreach (VariableModel nodeOnEndpoint in publishNodesMethodData.OpcNodes)
{
NodePublishingModel node = new NodePublishingModel
{
ExpandedNodeId = ExpandedNodeId.Parse(nodeOnEndpoint.Id),
EndpointUrl = new Uri(publishNodesMethodData.EndpointUrl).ToString(),
SkipFirst = nodeOnEndpoint.SkipFirst,
HeartbeatInterval = nodeOnEndpoint.HeartbeatInterval,
OpcPublishingInterval = nodeOnEndpoint.OpcPublishingInterval,
OpcSamplingInterval = nodeOnEndpoint.OpcSamplingInterval,
Username = publishNodesMethodData.UserName,
Password = publishNodesMethodData.Password,
OpcAuthenticationMode = desiredAuthenticationMode
};
_uaClient.PublishNodeAsync(node).GetAwaiter().GetResult();
string statusMessage = $"Node {node.ExpandedNodeId} on endpoint {node.EndpointUrl} published successfully.";
statusResponse.Add(statusMessage);
_logger.LogInformation(statusMessage);
}
}
return BuildResponseAndCropStatus(statusResponse);
}
public byte[] UnpublishNodes(string payload)
{
List<string> statusResponse = new List<string>();
UnpublishNodesInterfaceModel unpublishNodesMethodData = JsonConvert.DeserializeObject<UnpublishNodesInterfaceModel>(payload);
// check for events
if (unpublishNodesMethodData.OpcEvents != null)
{
foreach (EventModel opcEvent in unpublishNodesMethodData.OpcEvents)
{
NodePublishingModel node = new NodePublishingModel()
{
ExpandedNodeId = ExpandedNodeId.Parse(opcEvent.ExpandedNodeId),
EndpointUrl = new Uri(unpublishNodesMethodData.EndpointUrl).ToString()
};
_uaClient.UnpublishNode(node);
string statusMessage = $"Event {node.ExpandedNodeId} on endpoint {node.EndpointUrl} unpublished successfully.";
statusResponse.Add(statusMessage);
_logger.LogInformation(statusMessage);
}
}
// check for variables
if (unpublishNodesMethodData.OpcNodes != null)
{
foreach (VariableModel nodeOnEndpoint in unpublishNodesMethodData.OpcNodes)
{
NodePublishingModel node = new NodePublishingModel
{
ExpandedNodeId = ExpandedNodeId.Parse(nodeOnEndpoint.Id),
EndpointUrl = new Uri(unpublishNodesMethodData.EndpointUrl).ToString()
};
_uaClient.UnpublishNode(node);
string statusMessage = $"Node {node.ExpandedNodeId} on endpoint {node.EndpointUrl} unpublished successfully.";
statusResponse.Add(statusMessage);
_logger.LogInformation(statusMessage);
}
}
return BuildResponseAndCropStatus(statusResponse);
}
public byte[] UnpublishAllNodes()
{
_uaClient.UnpublishAllNodes();
return BuildResponseAndTruncateResult("All nodes unpublished successfully.");
}
public byte[] GetPublishedNodes()
{
return BuildResponseAndTruncateResult(_uaClient.GetPublishedNodes());
}
public byte[] GetInfo()
{
return BuildResponseAndTruncateResult(Diagnostics.Singleton.Info);
}
private byte[] BuildResponseAndCropStatus(List<string> statusResponse)
{
byte[] result;
int maxIndex = statusResponse.Count();
while (true)
{
result = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(statusResponse.GetRange(0, maxIndex)));
if (result.Length > Settings.MaxResponsePayloadLength)
{
maxIndex /= 2;
continue;
}
else
{
break;
}
}
if (maxIndex != statusResponse.Count())
{
statusResponse.RemoveRange(maxIndex, statusResponse.Count() - maxIndex);
statusResponse.Add("Results have been cropped due to message size limitations!");
}
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(statusResponse.GetRange(0, maxIndex)));
}
private byte[] BuildResponseAndTruncateResult(object result)
{
byte[] response = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(result));
if (response.Length > Settings.MaxResponsePayloadLength)
{
_logger.LogError("Results have been cropped due to message size limitations!");
Array.Resize(ref response, response.Length > Settings.MaxResponsePayloadLength ? Settings.MaxResponsePayloadLength : response.Length);
}
return response;
}
}
}