Keeping track of nodes subscribed by clients #464
-
Hello there, I'm programming a server and would like to keep track which items are currently subscribed by clients. My problem is that I don't know how to detect for which node the subscription was cancelled in case of an ItemSubscriptionDeleted callback. The event object is not of the same type as the event object during the creation of subscriptions, so I can't just copy my logic from the detection of a new subscription. I might be missing something really simple but I don't have the correct idea. Here is a simplified version of my code. (Mostly ripped from the example I mentioned.) def create_monitored_items(event, dispatcher):
#Get node id of newly subscribed item
print("Monitored Item")
for idx in range(len(event.response_params)) :
if (event.response_params[idx].StatusCode.is_good()) :
nodeId = event.request_params.ItemsToCreate[idx].ItemToMonitor.NodeId
print(f"Node {nodeId} was subscribed")
def delete_monitored_items(event, dispatcher):
#How do i get the node id of the unsubscribed item?
???
nodeId = ???
print(f"Node {nodeId} was unsubscribed")
...
server.subscribe_server_callback(CallbackType.ItemSubscriptionCreated, create_monitored_items)
server.subscribe_server_callback(CallbackType.ItemSubscriptionDeleted, delete_monitored_items)
... I hope somebody knows what I should do. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I am not sure I competely undertsand but the SubscriptionResult should contain some "handle" that you should find back in "params" of subcsriptoin deletes |
Beta Was this translation helpful? Give feedback.
-
so if save your nodes in a Dict[Hanlde, List[nodes]] you can get back to what nodes have been unsubscribed |
Beta Was this translation helpful? Give feedback.
-
Yes. Thank you. That was what I was missing. Im adding the edited handle methods in case anybody would like to do something similar def create_monitored_items(event, dispatcher):
for idx in range(len(event.response_params)) :
if (event.response_params[idx].StatusCode.is_good()) :
nodeId = event.request_params.ItemsToCreate[idx].ItemToMonitor.NodeId
subscription_id = event.request_params.SubscriptionId
monitored_item_id = event.response_params[idx].MonitoredItemId
#Save the connection between node id and the handle ids
...
def delete_monitored_items(event, dispatcher):
for idx in range(len(event.response_params)) :
if (event.response_params[idx].is_good()) :
subscription_id = event.request_params.SubscriptionId
monitored_item_id = event.request_params.MonitoredItemIds[idx]
#Use handle ids to get node id of unsubscribed nodes
... |
Beta Was this translation helpful? Give feedback.
so if save your nodes in a Dict[Hanlde, List[nodes]] you can get back to what nodes have been unsubscribed