def do_test_client_Client_kwargs_sessionSettings(self, service): sessionSettings = SessionSettings() sessionSettings.connectTimeoutSec = 5.0 result = self.doService(service, sessionSettings=sessionSettings) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 1) self.assertEqual(allSessionInfos[0].sessionSettings, sessionSettings) # check if the following session is reused result = self.doService(service, sessionSettings=sessionSettings) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 1) self.assertEqual(allSessionInfos[0].sessionSettings, sessionSettings) self.assertEqual(result.targets[0].clientConnectionId, 0) # check if the following session is not reused sessionSettings2 = SessionSettings() sessionSettings2.connectTimeoutSec = 6.0 result = self.doService(service, sessionSettings=sessionSettings2) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 2) self.assertEqual(allSessionInfos[1].sessionSettings, sessionSettings2) self.assertEqual(result.targets[0].clientConnectionId, 1) # check if the following session is not reused sessionSettings.unique = True result = self.doService(service, sessionSettings=sessionSettings) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 3) self.assertEqual(allSessionInfos[2].sessionSettings, sessionSettings) self.assertEqual(result.targets[0].clientConnectionId, 2) # check if the following session is not reused result = self.doService(service, sessionSettings=sessionSettings) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 4) self.assertEqual(allSessionInfos[3].sessionSettings, sessionSettings) self.assertEqual(result.targets[0].clientConnectionId, 3) # check if a specific ClientConnectionId may be reused: result = self.doService(service, clientConnectionId=3) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 4) self.assertEqual(result.targets[0].clientConnectionId, 3)
def do_test_client_Client_kwargs_specificSessionSettings(self, service): clientSettings = self.client.clientSettings() clientSettings.specificSessionSettings[self.serverUri] = SessionSettings() clientSettings.specificSessionSettings[self.serverUri].connectTimeoutSec = 6.0 self.client.setClientSettings(clientSettings) result = self.doService(service) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 1) self.assertEqual(allSessionInfos[0].sessionSettings, clientSettings.specificSessionSettings[self.serverUri]) self.assertNotEqual(allSessionInfos[0].sessionSettings, clientSettings.defaultSessionSettings) # check if the session is reused result = self.doService(service) self.assertTrue(result.overallStatus.isGood()) self.assertEqual(len(allSessionInfos), 1)
def do_test_client_Client_kwargs_sessionSettings(self, service): sessionSettings = SessionSettings() sessionSettings.connectTimeoutSec = 5.0 result = self.doService(service, sessionSettings = sessionSettings) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 1) self.assertEqual(allSessionInfos[0].sessionSettings, sessionSettings) # check if the following session is reused result = self.doService(service, sessionSettings = sessionSettings) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 1) self.assertEqual(allSessionInfos[0].sessionSettings, sessionSettings) self.assertEqual(result.targets[0].clientConnectionId, 0) # check if the following session is not reused sessionSettings2 = SessionSettings() sessionSettings2.connectTimeoutSec = 6.0 result = self.doService(service, sessionSettings = sessionSettings2) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 2) self.assertEqual(allSessionInfos[1].sessionSettings, sessionSettings2) self.assertEqual(result.targets[0].clientConnectionId, 1) # check if the following session is not reused sessionSettings.unique = True result = self.doService(service, sessionSettings = sessionSettings) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 3) self.assertEqual(allSessionInfos[2].sessionSettings, sessionSettings) self.assertEqual(result.targets[0].clientConnectionId, 2) # check if the following session is not reused result = self.doService(service, sessionSettings = sessionSettings) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 4) self.assertEqual(allSessionInfos[3].sessionSettings, sessionSettings) self.assertEqual(result.targets[0].clientConnectionId, 3) # check if a specific ClientConnectionId may be reused: result = self.doService(service, clientConnectionId = 3) self.assertTrue(result.overallStatus.isGood()) allSessionInfos = self.client.allSessionInformations() self.assertEqual(len(allSessionInfos), 4) self.assertEqual(result.targets[0].clientConnectionId, 3)
result = certificate.toDERFile(answer) if result == -1: raise Exception("Could not save the client certificate to %s" % answer) print("") print( "===========================================================================================" ) print("STEP 5: Configure the session to be created") print( "===========================================================================================" ) print("") # suppose we want Basic128Rsa15 encryption + signed communication: sessionSettings = SessionSettings() sessionSettings.securitySettings.securityPolicy = securitypolicies.UA_Basic128Rsa15 sessionSettings.securitySettings.messageSecurityMode = messagesecuritymodes.Mode_Sign print("") print( "===========================================================================================" ) print("STEP 6: Register a callback function to handle the server certificate") print( "===========================================================================================" ) print("") def myCallback(certificate, cause):
# when the next line is executed, then: # - the session we created before will be reused (as we're requesting a session with the # defaultSessionSettings, but such a session exists already, so it can be reused!) # - the Write service will be invoked, with the defaultWriteSettings we configured before myClient.write( [address0, address1] , [ UInt32(60), Boolean(True) ] ) # ================================================================================================== # 2) Overriding the defaults # ================================================================================================== # If needed, you can also configure the settings for each service call, using the **kwargs arguments # of read/write/call/... # e.g. set some special session settings for a specific read() call: mySessionSettings = SessionSettings() mySessionSettings.connectTimeoutSec = 0.5 mySessionSettings.sessionTimeoutSec = 100.0 # simply mention 'sessionSettings = ...' to use these settings as a **kwarg: myClient.read( [address0, address1], sessionSettings = mySessionSettings ) # e.g. set some special service settings for a specific read() call: myReadSettings = ReadSettings() myReadSettings.callTimeoutSec = 0.5 myReadSettings.maxAgeSec = 1.0 # simply mention 'serviceSettings = ...' to use these settings as a **kwarg: myClient.read( [address0, address1], serviceSettings = myReadSettings ) # or combine them: myClient.read( [address0, address1], serviceSettings = myReadSettings, sessionSettings = mySessionSettings ) # e.g. set some special subscription settings for a specific createMonitoredData() call:
from pyuaf.client.configs import SessionConfig print( "This example will fail when executed, since it tries to connect to non-existing servers" ) # create a single SessionConfig object that we will use for all our service calls cfg = SessionConfig() # configure the default settings for the sessions that will be created cfg.defaultSessionSettings.sessionTimeoutSec = 100.0 cfg.defaultSessionSettings.connectTimeoutSec = 2.0 # for a particular server (an embedded device) we expect an unreliable connection, # so we specify some settings that are more suitable for this particular server cfg.specificSessionSettings["My/Unreliable/Device/Server"] = SessionSettings() cfg.specificSessionSettings[ "My/Unreliable/Device/Server"].sessionTimeoutSec = 200.0 cfg.specificSessionSettings[ "My/Unreliable/Device/Server"].connectTimeoutSec = 5.0 # we also have a particular very reliable and fast server (some PLC), for which we want to # specify other settings: cfg.specificSessionSettings["My/Reliable/PLC/Server"] = SessionSettings() cfg.specificSessionSettings["My/Reliable/PLC/Server"].connectTimeoutSec = 0.5 # Now we can use the same config for many different service calls. # Sessions will be created silently in the background by the UAF, based on the # settings we provided via the 'cfg' object. # If sessions need to be created to the "My/Unreliable/Device/Uri" or "My/Reliable/PLC/Uri" # servers, they will be created according to the specified settings.
# create a request with 1 target request = HistoryReadRawModifiedRequest(1) request.targets[0].address = doubleAddress # configure the request further: serviceSettings = HistoryReadRawModifiedSettings() serviceSettings.startTime = DateTime(time.time() - 1) serviceSettings.endTime = DateTime(time.time()) serviceSettings.callTimeoutSec = 2.0 # optional of course serviceSettings.numValuesPerNode = 100 serviceSettings.maxAutoReadMore = 10 serviceSettings.isReadModified = False # we want raw historical data request.serviceSettings = serviceSettings request.serviceSettingsGiven = True sessionSettings = SessionSettings() sessionSettings.sessionTimeoutSec = 600.0 # optional of course request.sessionSettings = sessionSettings request.sessionSettingsGiven = True # process the request result = myClient.processRequest(request) # print the result print(str(result)) # process the result in the same way as before except UafError, e: print("Some error occurred: %s" %e) raise
print("===================================================") # now browse the root node # (notice that there is also an argument called 'maxAutoBrowseNext', which we don't mention # here because we can leave it at the default value (100), to make sure that BrowseNext is # automatically being called for us as much as needed!) firstLevelBrowseResult = myClient.browse([rootNode]) # Notice too that you can optionally provide a BrowseSettings and/or a SessionSettings argument # for more detailed configuration, like this: # OPTIONAL: let's specify a small call timeout, since the UaDemoServer is running # on the local machine anyway: browseSettings = BrowseSettings() browseSettings.callTimeoutSec = 2.0 # OPTIONAL: and finally let's also specify that sessions should have a timeout of 600 seconds: sessionSettings = SessionSettings() sessionSettings.sessionTimeoutSec = 600.0 # now call the browse() function: myClient.browse([rootNode], browseSettings = browseSettings, sessionSettings = sessionSettings) # print the result print(" - Browse result of the first level:") print(" ---------------------------------") print(firstLevelBrowseResult) # we don't expect that "manual" BrowseNext calls are still needed, as the UAF will # have done the BrowseNext calls up to 100 times automatically for us! If there would still be # some continuation points left, then we surely have some unexpected problem! assert len(firstLevelBrowseResult.targets[0].continuationPoint) == 0 # we can now continue browsing the other nodes that we discovered, all simultaneously!!!
print("===================================================") # now browse the root node # (notice that there is also an argument called 'maxAutoBrowseNext', which we don't mention # here because we can leave it at the default value (100), to make sure that BrowseNext is # automatically being called for us as much as needed!) firstLevelBrowseResult = myClient.browse([rootNode]) # Notice too that you can optionally provide a BrowseSettings and/or a SessionSettings argument # for more detailed configuration, like this: # OPTIONAL: let's specify a small call timeout, since the UaDemoServer is running # on the local machine anyway: browseSettings = BrowseSettings() browseSettings.callTimeoutSec = 2.0 # OPTIONAL: and finally let's also specify that sessions should have a timeout of 600 seconds: sessionSettings = SessionSettings() sessionSettings.sessionTimeoutSec = 600.0 # now call the browse() function: myClient.browse([rootNode], browseSettings=browseSettings, sessionSettings=sessionSettings) # print the result print(" - Browse result of the first level:") print(" ---------------------------------") print(firstLevelBrowseResult) # we don't expect that "manual" BrowseNext calls are still needed, as the UAF will # have done the BrowseNext calls up to 100 times automatically for us! If there would still be # some continuation points left, then we surely have some unexpected problem! assert len(firstLevelBrowseResult.targets[0].continuationPoint) == 0
myClientSettings.defaultSessionSettings.connectTimeoutSec = 2.0 myClientSettings.defaultSubscriptionSettings.publishingIntervalSec = 0.5 myClientSettings.defaultSubscriptionSettings.lifeTimeCount = 600 myClientSettings.defaultReadSettings.callTimeoutSec = 2.0 myClientSettings.defaultReadSettings.maxAgeSec = 10.0 myClientSettings.defaultMethodCallSettings.callTimeoutSec = 2.0 myClientSettings.defaultWriteSettings.callTimeoutSec = 2.0 myClientSettings.defaultBrowseSettings.callTimeoutSec = 2.0 # and so on, and so on ... # the clientSettings are in use as soon as you provide them again to the Client: myClient.setClientSettings(myClientSettings) # In case you want to choose different settings for specific read/write/... calls, then you can # provide them also: mySpecialSessionSettings = SessionSettings() mySpecialSessionSettings.sessionTimeoutSec = 500.0 mySpecialSessionSettings.connectTimeoutSec = 0.5 mySpecialReadSettings = ReadSettings() mySpecialReadSettings.callTimeoutSec = 2.0 mySpecialReadSettings.maxAgeSec = 10.0 result = myClient.read(addresses = [address_TSetpoint, address_HeaterStatus], attributeId = pyuaf.util.attributeids.DisplayName, sessionSettings = mySpecialSessionSettings, serviceSettings = mySpecialReadSettings) print("") print("################################################################################################################")
clientSettings.defaultSubscriptionSettings.lifeTimeCount = 600 # etc. ... # now configure the default settings for the Read service: clientSettings.defaultReadSettings.callTimeoutSec = 2.0 clientSettings.defaultReadSettings.maxAgeSec = 10.0 # same for the writeSettings, methodCallSettings, ... clientSettings.defaultMethodCallSettings.callTimeoutSec = 2.0 clientSettings.defaultWriteSettings.callTimeoutSec = 2.0 clientSettings.defaultBrowseSettings.callTimeoutSec = 2.0 # and so on, and so on ... # NOTE: for clientSettings, you may also configure SessionSettings for specific servers: clientSettings.specificSessionSettings[ "My/Unreliable/Device/Server/URI"] = SessionSettings() clientSettings.specificSessionSettings[ "My/Unreliable/Device/Server/URI"].sessionTimeoutSec = 200.0 clientSettings.specificSessionSettings[ "My/Unreliable/Device/Server/URI"].connectTimeoutSec = 5.0 # If the client connects to the server with ServerUri = "My/Unreliable/Device/Server/URI", # then it will use these settings instead of the defaultSessionSettings! # Now you can simply create a client that uses these settings: myClient = Client(clientSettings) # or alternatively: # myClient = Client() # myClient.setClientSettings(clientSettings) # let's read 2 nodes: address0 = Address(NodeId("Demo.SimulationSpeed", NAMESPACE_URI), SERVER_URI)
myClientSettings.defaultSessionSettings.connectTimeoutSec = 2.0 myClientSettings.defaultSubscriptionSettings.publishingIntervalSec = 0.5 myClientSettings.defaultSubscriptionSettings.lifeTimeCount = 600 myClientSettings.defaultReadSettings.callTimeoutSec = 2.0 myClientSettings.defaultReadSettings.maxAgeSec = 10.0 myClientSettings.defaultMethodCallSettings.callTimeoutSec = 2.0 myClientSettings.defaultWriteSettings.callTimeoutSec = 2.0 myClientSettings.defaultBrowseSettings.callTimeoutSec = 2.0 # and so on, and so on ... # the clientSettings are in use as soon as you provide them again to the Client: myClient.setClientSettings(myClientSettings) # In case you want to choose different settings for specific read/write/... calls, then you can # provide them also: mySpecialSessionSettings = SessionSettings() mySpecialSessionSettings.sessionTimeoutSec = 500.0 mySpecialSessionSettings.connectTimeoutSec = 0.5 mySpecialReadSettings = ReadSettings() mySpecialReadSettings.callTimeoutSec = 2.0 mySpecialReadSettings.maxAgeSec = 10.0 result = myClient.read( addresses=[address_TSetpoint, address_HeaterStatus], attributeId=pyuaf.util.attributeids.DisplayName, sessionSettings=mySpecialSessionSettings, serviceSettings=mySpecialReadSettings, ) print("")