async def create_subscription( self, period, handler, publishing=True ) -> Subscription: """ Create a subscription. Returns a Subscription object which allows to subscribe to events or data changes on server. :param period: Either a publishing interval in milliseconds or a `CreateSubscriptionParameters` instance. The second option should be used, if the asyncua-server has problems with the default options. :param handler: Class instance with data_change and/or event methods (see `SubHandler` base class for details). Remember not to block the main event loop inside the handler methods. """ if isinstance(period, ua.CreateSubscriptionParameters): params = period else: params = ua.CreateSubscriptionParameters() params.RequestedPublishingInterval = period params.RequestedLifetimeCount = 10000 params.RequestedMaxKeepAliveCount = self.get_keepalive_count(period) params.MaxNotificationsPerPublish = 10000 params.PublishingEnabled = publishing params.Priority = 0 subscription = Subscription(self.uaclient, params, handler) results = await subscription.init() new_params = self.get_subscription_revised_params(params, results) if new_params: results = await subscription.update(new_params) _logger.info(f"Result from subscription update: {results}") return subscription
async def create_subscription(self, period, handler): """ Create a subscription. returns a Subscription object which allow to subscribe to events or data on server handler argument is a class with data_change and/or event methods. period argument is either a publishing interval in milliseconds or a CreateSubscriptionParameters instance. The second option should be used, if the asyncua-server has problems with the default options. These methods will be called when notfication from server are received. See example-client.py. Do not do expensive/slow or network operation from these methods since they are called directly from receiving thread. This is a design choice, start another thread if you need to do such a thing. """ if isinstance(period, ua.CreateSubscriptionParameters): subscription = Subscription(self.uaclient, period, handler) await subscription.init() return subscription params = ua.CreateSubscriptionParameters() params.RequestedPublishingInterval = period params.RequestedLifetimeCount = 10000 params.RequestedMaxKeepAliveCount = 3000 params.MaxNotificationsPerPublish = 10000 params.PublishingEnabled = True params.Priority = 0 subscription = Subscription(self.uaclient, params, handler) await subscription.init() return subscription
async def test_maxkeepalive_count(opc, mocker): sub_handler = MySubHandler() client, server = opc period = 1 max_keepalive_count = client.get_keepalive_count(period) mock_period = 500 mock_max_keepalive_count = client.get_keepalive_count(mock_period) mock_response = ua.CreateSubscriptionResult( SubscriptionId=78, RevisedPublishingInterval=mock_period, RevisedLifetimeCount=10000, RevisedMaxKeepAliveCount=2700 ) mock_create_subscription = mocker.patch.object( client.uaclient, "create_subscription", new=CoroutineMock(return_value=mock_response) ) mock_update_subscription = mocker.patch.object( client.uaclient, "update_subscription", new=CoroutineMock() ) sub = await client.create_subscription(period, sub_handler) assert sub.parameters.RequestedMaxKeepAliveCount == mock_max_keepalive_count assert mock_max_keepalive_count != max_keepalive_count # mock point to the object at its finale state, # here the subscription params have already been updated mock_create_subscription.assert_awaited_with( ua.CreateSubscriptionParameters( RequestedPublishingInterval=mock_period, RequestedLifetimeCount=10000, RequestedMaxKeepAliveCount=mock_max_keepalive_count, MaxNotificationsPerPublish=10000, PublishingEnabled=True, Priority=0 ), callback=mocker.ANY ) mock_update_subscription.assert_awaited_with( ua.ModifySubscriptionParameters( SubscriptionId=78, RequestedPublishingInterval=mock_period, RequestedLifetimeCount=10000, RequestedMaxKeepAliveCount=mock_max_keepalive_count, MaxNotificationsPerPublish=10000 ) ) # we don't update when sub params == revised params mock_update_subscription.reset_mock() mock_create_subscription.reset_mock() sub = await client.create_subscription(mock_period, sub_handler) mock_update_subscription.assert_not_called()
async def _create_subscription(self, handler): params = ua.CreateSubscriptionParameters() params.RequestedPublishingInterval = 10 params.RequestedLifetimeCount = 3000 params.RequestedMaxKeepAliveCount = 10000 params.MaxNotificationsPerPublish = 0 params.PublishingEnabled = True params.Priority = 0 subscription = Subscription(self.iserver.isession, params, handler) await subscription.init() return subscription
async def _subscribe(self): handler_params = ua.CreateSubscriptionParameters() handler_params.RequestedPublishingInterval = self.publishing_interval_requested handler_params.RequestedLifetimeCount = 10000 handler_params.RequestedMaxKeepAliveCount = self._opc_client.get_keepalive_count( self.publishing_interval_requested) handler_params.MaxNotificationsPerPublish = 10000 handler_params.PublishingEnabled = True handler_params.Priority = 0 handler: OPCUASubHandler = OPCUASubHandler() subscription: Subscription = await self._opc_client.create_subscription( handler_params, handler, publishing=True) await subscription.subscribe_data_change(self.nodes)
async def create_subscription(self, period, handler): """ Create a subscription. Returns a Subscription object which allow to subscribe to events or data changes on server :param period: Period in milliseconds :param handler: A class instance - see `SubHandler` base class for details """ params = ua.CreateSubscriptionParameters() params.RequestedPublishingInterval = period params.RequestedLifetimeCount = 3000 params.RequestedMaxKeepAliveCount = 10000 params.MaxNotificationsPerPublish = 0 params.PublishingEnabled = True params.Priority = 0 subscription = Subscription(self.iserver.isession, params, handler) await subscription.init() return subscription
async def create_subscription(self, period, handler): """ Create a subscription. returns a Subscription object which allow to subscribe to events or data on server period is in milliseconds handler is a python object with following methods: def datachange_notification(self, node, val, data): def event_notification(self, event): def status_change_notification(self, status): """ params = ua.CreateSubscriptionParameters() params.RequestedPublishingInterval = period params.RequestedLifetimeCount = 3000 params.RequestedMaxKeepAliveCount = 10000 params.MaxNotificationsPerPublish = 0 params.PublishingEnabled = True params.Priority = 0 subscription = Subscription(self.iserver.isession, params, handler) await subscription.init() return subscription