forked from open62541pp/open62541pp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices_monitoreditem.cpp
296 lines (272 loc) · 10.8 KB
/
services_monitoreditem.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "open62541pp/services/monitoreditem.hpp"
#ifdef UA_ENABLE_SUBSCRIPTIONS
#include <algorithm>
#include <cstddef>
#include "open62541pp/client.hpp"
#include "open62541pp/detail/client_context.hpp"
#include "open62541pp/detail/exceptioncatcher.hpp"
#include "open62541pp/detail/server_context.hpp"
#include "open62541pp/server.hpp"
namespace opcua::services {
namespace detail {
template <typename T>
static auto createMonitoredItemContext(
T& connection,
const ReadValueId& itemToMonitor,
DataChangeNotificationCallback dataChangeCallback,
EventNotificationCallback eventCallback,
DeleteMonitoredItemCallback deleteCallback
) {
auto context = std::make_unique<MonitoredItemContext>();
context->catcher = &opcua::detail::getExceptionCatcher(connection);
context->itemToMonitor = itemToMonitor;
context->dataChangeCallback = std::move(dataChangeCallback);
context->eventCallback = std::move(eventCallback);
context->deleteCallback = std::move(deleteCallback);
return context;
}
std::vector<std::unique_ptr<MonitoredItemContext>> createMonitoredItemContexts(
Client& connection,
const CreateMonitoredItemsRequest& request,
// NOLINTBEGIN(performance-unnecessary-value-param)
DataChangeNotificationCallback dataChangeCallback,
EventNotificationCallback eventCallback,
DeleteMonitoredItemCallback deleteCallback
// NOLINTEND(performance-unnecessary-value-param)
) {
// TODO: move first callback, then copy
const auto items = request.itemsToCreate();
std::vector<std::unique_ptr<MonitoredItemContext>> contexts(items.size());
std::transform(items.begin(), items.end(), contexts.begin(), [&](const auto& item) {
return createMonitoredItemContext(
connection, item.itemToMonitor(), dataChangeCallback, eventCallback, deleteCallback
);
});
return contexts;
}
void convertMonitoredItemContexts(
Span<const std::unique_ptr<MonitoredItemContext>> contexts,
Span<MonitoredItemContext*> contextsPtr,
Span<UA_Client_DataChangeNotificationCallback> dataChangeCallbacksNative,
Span<UA_Client_EventNotificationCallback> eventCallbacksNative,
Span<UA_Client_DeleteMonitoredItemCallback> deleteCallbacksNative
) noexcept {
assert(contextsPtr.size() == contexts.size());
std::transform(
contexts.begin(),
contexts.end(),
contextsPtr.begin(),
[](const auto& context) noexcept { return context.get(); }
);
if (!dataChangeCallbacksNative.empty()) {
assert(dataChangeCallbacksNative.size() == contexts.size());
std::fill(
dataChangeCallbacksNative.begin(),
dataChangeCallbacksNative.end(),
MonitoredItemContext::dataChangeCallbackNativeClient
);
}
if (!eventCallbacksNative.empty()) {
assert(eventCallbacksNative.size() == contexts.size());
std::fill(
eventCallbacksNative.begin(),
eventCallbacksNative.end(),
MonitoredItemContext::eventCallbackNative
);
}
if (!deleteCallbacksNative.empty()) {
assert(deleteCallbacksNative.size() == contexts.size());
std::fill(
deleteCallbacksNative.begin(),
deleteCallbacksNative.end(),
MonitoredItemContext::deleteCallbackNative
);
}
}
template <typename T>
static void storeMonitoredItemContext(
T& connection,
IntegerId subscriptionId,
const MonitoredItemCreateResult& result,
std::unique_ptr<MonitoredItemContext>& context
) {
if (result.statusCode().isGood()) {
auto* contextPtr = context.get();
opcua::detail::getContext(connection)
.monitoredItems.insert({subscriptionId, result.monitoredItemId()}, std::move(context));
contextPtr->inserted = true;
}
}
void storeMonitoredItemContexts(
Client& connection,
IntegerId subscriptionId,
const CreateMonitoredItemsResponse& response,
Span<std::unique_ptr<MonitoredItemContext>> contexts
) {
if (getServiceResult(response).isGood()) {
const auto results = response.results();
for (size_t i = 0; i < results.size(); ++i) {
storeMonitoredItemContext(connection, subscriptionId, results[i], contexts[i]);
}
}
}
} // namespace detail
CreateMonitoredItemsResponse createMonitoredItemsDataChange(
Client& connection,
const CreateMonitoredItemsRequest& request,
DataChangeNotificationCallback dataChangeCallback,
DeleteMonitoredItemCallback deleteCallback
) {
// TODO: avoid heap allocations for single item?
auto contexts = detail::createMonitoredItemContexts(
connection, request, std::move(dataChangeCallback), {}, std::move(deleteCallback)
);
std::vector<detail::MonitoredItemContext*> contextsPtr(contexts.size());
std::vector<UA_Client_DataChangeNotificationCallback> dataChangeCallbacks(contexts.size());
std::vector<UA_Client_DeleteMonitoredItemCallback> deleteCallbacks(contexts.size());
detail::convertMonitoredItemContexts(
contexts, contextsPtr, dataChangeCallbacks, {}, deleteCallbacks
);
CreateMonitoredItemsResponse response = UA_Client_MonitoredItems_createDataChanges(
connection.handle(),
request,
reinterpret_cast<void**>(contextsPtr.data()), // NOLINT
dataChangeCallbacks.data(),
deleteCallbacks.data()
);
detail::storeMonitoredItemContexts(connection, request.subscriptionId(), response, contexts);
return response;
}
template <>
MonitoredItemCreateResult createMonitoredItemDataChange<Client>(
Client& connection,
IntegerId subscriptionId,
const ReadValueId& itemToMonitor,
MonitoringMode monitoringMode,
const MonitoringParametersEx& parameters,
DataChangeNotificationCallback dataChangeCallback,
DeleteMonitoredItemCallback deleteCallback
) {
auto item = detail::createMonitoredItemCreateRequest(itemToMonitor, monitoringMode, parameters);
const auto request = detail::createCreateMonitoredItemsRequest(
subscriptionId, parameters.timestamps, {&item, 1}
);
auto response = createMonitoredItemsDataChange(
connection,
asWrapper<CreateMonitoredItemsRequest>(request),
std::move(dataChangeCallback),
std::move(deleteCallback)
);
return detail::wrapSingleResultWithStatus<MonitoredItemCreateResult>(response);
}
template <>
MonitoredItemCreateResult createMonitoredItemDataChange<Server>(
Server& connection,
[[maybe_unused]] IntegerId subscriptionId,
const ReadValueId& itemToMonitor,
MonitoringMode monitoringMode,
const MonitoringParametersEx& parameters,
DataChangeNotificationCallback dataChangeCallback,
DeleteMonitoredItemCallback deleteCallback
) {
auto context = detail::createMonitoredItemContext(
connection, itemToMonitor, std::move(dataChangeCallback), {}, std::move(deleteCallback)
);
MonitoredItemCreateResult result = UA_Server_createDataChangeMonitoredItem(
connection.handle(),
static_cast<UA_TimestampsToReturn>(parameters.timestamps),
detail::createMonitoredItemCreateRequest(itemToMonitor, monitoringMode, parameters),
context.get(),
detail::MonitoredItemContext::dataChangeCallbackNativeServer
);
detail::storeMonitoredItemContext(connection, 0U, result, context);
return result;
}
CreateMonitoredItemsResponse createMonitoredItemsEvent(
Client& connection,
const CreateMonitoredItemsRequest& request,
EventNotificationCallback eventCallback,
DeleteMonitoredItemCallback deleteCallback
) {
// TODO: avoid heap allocations for single item?
auto contexts = detail::createMonitoredItemContexts(
connection, request, {}, std::move(eventCallback), std::move(deleteCallback)
);
std::vector<detail::MonitoredItemContext*> contextsPtr(contexts.size());
std::vector<UA_Client_EventNotificationCallback> eventCallbacks(contexts.size());
std::vector<UA_Client_DeleteMonitoredItemCallback> deleteCallbacks(contexts.size());
detail::convertMonitoredItemContexts(
contexts, contextsPtr, {}, eventCallbacks, deleteCallbacks
);
CreateMonitoredItemsResponse response = UA_Client_MonitoredItems_createEvents(
connection.handle(),
request,
reinterpret_cast<void**>(contextsPtr.data()), // NOLINT
eventCallbacks.data(),
deleteCallbacks.data()
);
detail::storeMonitoredItemContexts(connection, request.subscriptionId(), response, contexts);
return response;
}
MonitoredItemCreateResult createMonitoredItemEvent(
Client& connection,
IntegerId subscriptionId,
const ReadValueId& itemToMonitor,
MonitoringMode monitoringMode,
const MonitoringParametersEx& parameters,
EventNotificationCallback eventCallback,
DeleteMonitoredItemCallback deleteCallback
) {
auto item = detail::createMonitoredItemCreateRequest(itemToMonitor, monitoringMode, parameters);
const auto request = detail::createCreateMonitoredItemsRequest(
subscriptionId, parameters.timestamps, {&item, 1}
);
auto response = createMonitoredItemsEvent(
connection,
asWrapper<CreateMonitoredItemsRequest>(request),
std::move(eventCallback),
std::move(deleteCallback)
);
return detail::wrapSingleResultWithStatus<MonitoredItemCreateResult>(response);
}
ModifyMonitoredItemsResponse modifyMonitoredItems(
Client& connection, const ModifyMonitoredItemsRequest& request
) noexcept {
return UA_Client_MonitoredItems_modify(connection.handle(), request);
}
SetMonitoringModeResponse setMonitoringMode(
Client& connection, const SetMonitoringModeRequest& request
) noexcept {
return UA_Client_MonitoredItems_setMonitoringMode(connection.handle(), request);
}
SetTriggeringResponse setTriggering(
Client& connection, const SetTriggeringRequest& request
) noexcept {
return UA_Client_MonitoredItems_setTriggering(connection.handle(), request);
}
DeleteMonitoredItemsResponse deleteMonitoredItems(
Client& connection, const DeleteMonitoredItemsRequest& request
) noexcept {
return UA_Client_MonitoredItems_delete(connection.handle(), request);
}
template <>
StatusCode deleteMonitoredItem<Client>(
Client& connection, IntegerId subscriptionId, IntegerId monitoredItemId
) {
const auto request = detail::createDeleteMonitoredItemsRequest(
subscriptionId, {&monitoredItemId, 1}
);
return detail::getSingleStatus(
deleteMonitoredItems(connection, asWrapper<DeleteMonitoredItemsRequest>(request))
);
}
template <>
StatusCode deleteMonitoredItem<Server>(
Server& connection, [[maybe_unused]] IntegerId subscriptionId, IntegerId monitoredItemId
) {
const auto status = UA_Server_deleteMonitoredItem(connection.handle(), monitoredItemId);
opcua::detail::getContext(connection).monitoredItems.erase({0U, monitoredItemId});
return status;
}
} // namespace opcua::services
#endif