Exemplo n.º 1
0
 def do_test_client_Client_kwargs_subscriptionSettings(self, service):
     subscriptionSettings = SubscriptionSettings()
     subscriptionSettings.maxNotificationsPerPublish = 111
     result = self.doService(service, subscriptionSettings=subscriptionSettings)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 1)
     self.assertEqual(allSubscriptionInfos[0].subscriptionSettings, subscriptionSettings)
     # check if the following subscription is reused
     result = self.doService(service, subscriptionSettings=subscriptionSettings)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 1)
     self.assertEqual(allSubscriptionInfos[0].subscriptionSettings, subscriptionSettings)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 0)
     # check if the following subscription is not reused
     subscriptionSettings2 = SubscriptionSettings()
     subscriptionSettings2.connectTimeoutSec = 6.0
     result = self.doService(service, subscriptionSettings=subscriptionSettings2)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 2)
     self.assertEqual(allSubscriptionInfos[1].subscriptionSettings, subscriptionSettings2)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 1)
     # check if the following subscription is not reused
     subscriptionSettings.unique = True
     result = self.doService(service, subscriptionSettings=subscriptionSettings)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 3)
     self.assertEqual(allSubscriptionInfos[2].subscriptionSettings, subscriptionSettings)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 2)
     # check if the following subscription is not reused
     result = self.doService(service, subscriptionSettings=subscriptionSettings)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 4)
     self.assertEqual(allSubscriptionInfos[3].subscriptionSettings, subscriptionSettings)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 3)
     # check if a specific clientSubscriptionHandle may be reused:
     result = self.doService(service, clientSubscriptionHandle=3)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 4)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 3)
Exemplo n.º 2
0
 def do_test_client_Client_kwargs_subscriptionSettings(self, service):
     subscriptionSettings = SubscriptionSettings()
     subscriptionSettings.maxNotificationsPerPublish = 111
     result = self.doService(service, subscriptionSettings = subscriptionSettings)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 1)
     self.assertEqual(allSubscriptionInfos[0].subscriptionSettings, subscriptionSettings)
     # check if the following subscription is reused
     result = self.doService(service, subscriptionSettings = subscriptionSettings)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 1)
     self.assertEqual(allSubscriptionInfos[0].subscriptionSettings, subscriptionSettings)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 0)
     # check if the following subscription is not reused
     subscriptionSettings2 = SubscriptionSettings()
     subscriptionSettings2.connectTimeoutSec = 6.0
     result = self.doService(service, subscriptionSettings = subscriptionSettings2)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 2)
     self.assertEqual(allSubscriptionInfos[1].subscriptionSettings, subscriptionSettings2)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 1)
     # check if the following subscription is not reused
     subscriptionSettings.unique = True
     result = self.doService(service, subscriptionSettings = subscriptionSettings)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 3)
     self.assertEqual(allSubscriptionInfos[2].subscriptionSettings, subscriptionSettings)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 2)
     # check if the following subscription is not reused
     result = self.doService(service, subscriptionSettings = subscriptionSettings)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 4)
     self.assertEqual(allSubscriptionInfos[3].subscriptionSettings, subscriptionSettings)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 3)
     # check if a specific clientSubscriptionHandle may be reused:
     result = self.doService(service, clientSubscriptionHandle = 3)
     self.assertTrue(result.overallStatus.isGood())
     allSubscriptionInfos = self.client.allSubscriptionInformations()
     self.assertEqual(len(allSubscriptionInfos), 4)
     self.assertEqual(result.targets[0].clientSubscriptionHandle, 3)
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:
mySubscriptionSettings = SubscriptionSettings()
mySubscriptionSettings.publishingIntervalSec = 2.0
mySubscriptionSettings.priority = 20
myClient.createMonitoredData( [address0, address1], subscriptionSettings = mySubscriptionSettings )

# Both the SessionSettings and SubscriptionSettings have a special 'unique' attribute, which 
# can be True or False.
# By default it is False, which tells the PyUAF Client that it may re-use a session (or subscription)
# if there exists one with the same SessionSettings (or SubscriptionSettings).
# However, if you set the unique attribute to True, then the PyUAF Client will ignore any existing
# sessions (or subscriptions) and create a new unique one.
# This is useful, e.g. if your HMI (Human Machine Interface) has several tabs, then you can create
# a subscription for each tab (to hold all monitored items for that specific tab). Using the
# setPublishingMode() service, you can then temporarily enable the subscription for a visible
# tab, and temporarily disable the subscription for a hidden one. Like this: 
# tab 1:  
print(
    "This example will fail when executed, since it tries to connect to non-existing servers"
)

# create a single SubscriptionConfig object that we will use for all our service calls
cfg = SubscriptionConfig()

# configure the default settings for the sessions that will be created
cfg.defaultSubscriptionSettings.publishingIntervalSec = 1.0
cfg.defaultSubscriptionSettings.priority = 50

# for a particular server (a small embedded device) we want to create a subscription with a
# slow publishing interval, so that we don't require too much resources from the device:
cfg.specificSubscriptionSettings[
    "My/Slow/Device/Server"] = SubscriptionSettings()
cfg.specificSubscriptionSettings[
    "My/Slow/Device/Server"].publishingIntervalSec = 2.0

# we also have a very fast server (some high-end PLC), from which we want to receive updates
# much faster (4 times per second!):
cfg.specificSubscriptionSettings["My/Fast/PLC/Server"] = SubscriptionSettings()
cfg.specificSubscriptionSettings[
    "My/Fast/PLC/Server"].publishingIntervalSec = 0.25

# Now we can use the same config for many different service calls.
# Subscriptions will be created silently in the background by the UAF, based on the
# settings we provided via the 'cfg' object.
# If subscriptions need to be created to the embedded device or the fast PLC, they will be
# created according to the specified settings.
# If subscriptions need to be created to other servers, they will be created according to the
# define the namespace URI and server URI of the UaServerCPP demo server
demoServerUri = "urn:UnifiedAutomation:UaServerCpp"
demoNamespaceUri = "http://www.unifiedautomation.com/DemoServer/"

# define the ClientSettings:
settings = ClientSettings()
settings.applicationName = "MyClient"
settings.discoveryUrls.append("opc.tcp://localhost:48010")

# create the client
myClient = Client(settings)


# 1) optionally: configure the subscription
# ------------------------------------------
subSettings = SubscriptionSettings()
subSettings.publishingIntervalSec = 1.0 
subSettings.maxKeepAliveCount = 5
 
# Explanation:
# - The subscription will send at most one bunch of changed data samples per second, due to the 
#   publishing interval of 1 second.
# - If the sampled data does not change, then the server does not have to send notifications to 
#   the client. However, when the sampled data does not change for 5 seconds in a row 
#   (5 seconds = maxKeepAliveCount * publishingIntervalSec), then the server 
#   will send a KeepAlive notification to the client, as a kind of "heartbeat".


# 2) configure the monitored items
# --------------------------------
target0 = CreateMonitoredDataRequestTarget()
Exemplo n.º 6
0
# define the namespace URI and server URI of the UaServerCPP demo server
demoServerUri = "urn:UnifiedAutomation:UaServerCpp"
demoNamespaceUri = "http://www.unifiedautomation.com/DemoServer/"

# define the ClientSettings:
settings = ClientSettings()
settings.applicationName = "MyClient"
settings.discoveryUrls.append("opc.tcp://localhost:48010")

# create the client
myClient = Client(settings)

# 1) optionally: configure the subscription
# ------------------------------------------
subSettings = SubscriptionSettings()
subSettings.publishingIntervalSec = 1.0
subSettings.maxKeepAliveCount = 5

# Explanation:
# - The subscription will send at most one bunch of changed data samples per second, due to the
#   publishing interval of 1 second.
# - If the sampled data does not change, then the server does not have to send notifications to
#   the client. However, when the sampled data does not change for 5 seconds in a row
#   (5 seconds = maxKeepAliveCount * publishingIntervalSec), then the server
#   will send a KeepAlive notification to the client, as a kind of "heartbeat".

# 2) configure the monitored items
# --------------------------------
target0 = CreateMonitoredDataRequestTarget()
target0.address = Address(
# 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:
mySubscriptionSettings = SubscriptionSettings()
mySubscriptionSettings.publishingIntervalSec = 2.0
mySubscriptionSettings.priority = 20
myClient.createMonitoredData([address0, address1],
                             subscriptionSettings=mySubscriptionSettings)

# Both the SessionSettings and SubscriptionSettings have a special 'unique' attribute, which
# can be True or False.
# By default it is False, which tells the PyUAF Client that it may re-use a session (or subscription)
# if there exists one with the same SessionSettings (or SubscriptionSettings).
# However, if you set the unique attribute to True, then the PyUAF Client will ignore any existing
# sessions (or subscriptions) and create a new unique one.
# This is useful, e.g. if your HMI (Human Machine Interface) has several tabs, then you can create
# a subscription for each tab (to hold all monitored items for that specific tab). Using the
# setPublishingMode() service, you can then temporarily enable the subscription for a visible
# tab, and temporarily disable the subscription for a hidden one. Like this: