I need to do some basic logging (with a custom logging provider) of what a client (Session) is doing like reading or writing. I am not sure what is the easiest way of doing this? Is there any global event or Session attached event that I can listen?
The info I need to log is:
* the ID or Name of the Client (Session)
* the performed request (read/write)
* the name of target Node/variable/attribute
* the data sent (in case of writing) from the client
* the current value (when writing) of the target node/variable/attribute
Listen to client requests from Server SDK
Moderator: uasdknet
-
- Hero Member
- Posts: 21
- Joined: 14 Jun 2021, 23:39
-
- Hero Member
- Posts: 21
- Joined: 14 Jun 2021, 23:39
Re: Listen to client requests from Server SDK
In case any future user requires the same, the solution is to override the following methods in your ServerManager implementation:
For Sync scenarios:
For Async scenarios:
I would have preferred them to be events, but it is what it is.
For Sync scenarios:
Code: Select all
protected override ResponseHeader OnRequestComplete(RequestContext context)
protected override ResponseHeader OnRequestError(RequestContext context, Exception e)
Code: Select all
protected override void OnAsyncRequestComplete(RequestContext context, IEndpointIncomingRequest incoming, bool doNotBlockThread)
protected override void OnAsyncRequestError(RequestContext context, IEndpointIncomingRequest incoming, bool doNotBlockThread, Exception e)
-
- Hero Member
- Posts: 21
- Joined: 14 Jun 2021, 23:39
Re: Listen to client requests from Server SDK
I'm struggling a lot trying to obtain the NodeId for a MonitoredItem when a request of type PUBLISH is completed. The only info I can see is the current/new value (DataValue) asssigned for the MonitoredItem but no info about the object itself.
My current code (not working as needed):
I will appreciate your assistance
My current code (not working as needed):
Code: Select all
protected override void OnAsyncRequestComplete(RequestContext context, IEndpointIncomingRequest incoming, bool doNotBlockThread)
{
var client = context.Session.SessionName; //Client being notified
switch (context.RequestType)
{
case ServiceType.Publish:
var pubResponse = incoming.Response as PublishResponse;
foreach (var nd in pubResponse.NotificationMessage.NotificationData)
{
var body = nd.Body as DataChangeNotification;
foreach (var mi in body.MonitoredItems)
{
//var itemName = mi.NodeId; //<-- I need to find info related to the NodeId being modified (display name, namespace etc).
var itemValue = mi.Value.ToString();
}
}
break;
}
base.OnAsyncRequestComplete(context, incoming, doNotBlockThread);
}
-
- Hero Member
- Posts: 21
- Joined: 14 Jun 2021, 23:39
Re: Listen to client requests from Server SDK
Again replying to myself: the answer is to use the pubResponse.SubscriptionId and the mi.ClientHandle to query against the SubscriptionManager.jramirez-jh wrote: ↑07 Dec 2021, 09:38I'm struggling a lot trying to obtain the NodeId for a MonitoredItem when a request of type PUBLISH is completed. The only info I can see is the current/new value (DataValue) asssigned for the MonitoredItem but no info about the object itself.
My current code (not working as needed):
I will appreciate your assistanceCode: Select all
protected override void OnAsyncRequestComplete(RequestContext context, IEndpointIncomingRequest incoming, bool doNotBlockThread) { var client = context.Session.SessionName; //Client being notified switch (context.RequestType) { case ServiceType.Publish: var pubResponse = incoming.Response as PublishResponse; foreach (var nd in pubResponse.NotificationMessage.NotificationData) { var body = nd.Body as DataChangeNotification; foreach (var mi in body.MonitoredItems) { //var itemName = mi.NodeId; //<-- I need to find info related to the NodeId being modified (display name, namespace etc). var itemValue = mi.Value.ToString(); } } break; } base.OnAsyncRequestComplete(context, incoming, doNotBlockThread); }
Code: Select all
var subs = SubscriptionManager.GetSubscription(context, pubResponse.SubscriptionId);
var mi = subs.GetMonitoredItem(mi.ClientHandle);
return mi.NodeId;