def connect():
    global client
    cs = ClientSettings("myClient", [server_address])
    # Uncomment below for session logging
    #	cs.logToStdOutLevel = loglevels.Info
    max_attempts = 40
    num_attempts = 0
    while (True):
        print "Trying to connect to the OPC UA Server. Attempt #" + str(
            num_attempts)
        result = None
        try:
            client = Client(cs)
            rootNode = Address(NodeId(opcuaidentifiers.OpcUaId_RootFolder, 0),
                               server_uri)
            result = client.browse([rootNode])
        except Exception as e:
            print "Got exception: " + str(e)
            num_attempts = num_attempts + 1
            if num_attempts > max_attempts:
                msg = 'Despite ' + str(
                    max_attempts
                ) + ' attempts couldnt establish OPC UA connection: exception is ' + str(
                    e)
                print msg
                dcs_test_utils.log_detail(False, msg, '')
                raise msg
            time.sleep(3)
            continue
        print 'Connected to OPC UA Server'
        return
# create the client
myClient = Client(settings)

try:
    print("")
    print("First option: use the convenience function \"browse()\"")
    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!)
    # (notice too that you can optionally provide a BrowseConfig and a SessionConfig for
    #  more detailed configuration)
    firstLevelBrowseResult = myClient.browse([rootNode])

    # 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!!!
    noOfFoundReferences = len(firstLevelBrowseResult.targets[0].references)
    newNodesToBeBrowsed = []
    for i in xrange(noOfFoundReferences):
Example #3
0
# create the client
myClient = Client(settings)



try:
    print("")
    print("First option: use the convenience function \"browse()\"")
    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:")
Example #4
0
from pyuaf.client import Client
from pyuaf.client.settings import ClientSettings, SessionSettings
from pyuaf.util import Address, NodeId, opcuaidentifiers
import uao

server_uri = 'urn:CERN:QuasarOpcUaServer'  # needs to match the factual server URI
server_address = 'opc.tcp://127.0.0.1:4841'
cs = ClientSettings("myClient", [server_address])
client = Client(cs)
# next 2 lines are not necessary but help to ensure good state of OPC-UA connectivity
rootNode = Address(NodeId(opcuaidentifiers.OpcUaId_RootFolder, 0), server_uri)
result = client.browse([rootNode])

session = uao.Session(client, server_uri)

obj = session.get_object('anObject',
                         2)  # needs to match factuall existing object
Example #5
0
if result.overallStatus.isNotGood():
    print("Couldn't start the simulation (status=%s)" %
          result.targets[0].status)

# =====================================================================================================================
print("")
print("Synchronous browsing")
print("====================")
print("")

# We may also want to
#  - browse the Root node and the Demo node simultaneously
#  - print the DisplayName of all nodes that we found

result = myClient.browse([address_Root, address_Demo])

print("Nodes below the Root node:")
for ref in result.targets[0].references:
    print(" - %s" % ref.displayName)

print("Nodes below the Demo node:")
for ref in result.targets[1].references:
    print(" - %s" % ref.displayName)

# =====================================================================================================================
print("")
print("Synchronous creating monitored data items")
print("=========================================")
print("")
settings.applicationName = "MyClient"
settings.discoveryUrls.append("opc.tcp://localhost:48010")

# create the client
myClient = Client(settings)

try:
    print("")
    print("First option: use the convenience function \"browse()\"")
    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)
if result.overallStatus.isNotGood():
    print("Couldn't start the simulation (status=%s)" %result.targets[0].status)


# =====================================================================================================================
print("")
print("Synchronous browsing")
print("====================")
print("")

# We may also want to 
#  - browse the Root node and the Demo node simultaneously
#  - print the DisplayName of all nodes that we found

result = myClient.browse( [address_Root, address_Demo] )

print("Nodes below the Root node:")
for ref in result.targets[0].references:
    print(" - %s" %ref.displayName) 

print("Nodes below the Demo node:")
for ref in result.targets[1].references:
    print(" - %s" %ref.displayName) 


# =====================================================================================================================
print("")
print("Synchronous creating monitored data items")
print("=========================================")
print("")
# The client now discovers the "opc.tcp://localhost:48010", and fetches the application description
# of the UaServerCpp demo server. 
# Let's print out what the client discovered:
listOfApplicationDescriptions = myClient.serversFound()
print(listOfApplicationDescriptions)

# It's a list of one server description (the demo server). It tells us the application URI, or in 
# this case, the server uri:
SERVER_URI = listOfApplicationDescriptions[0].applicationUri

# Okay now that we know the server URI of the demo server, we can connect to it. 
# We'll just dynamically browse the address space, and let the UAF do the connection for us:

rootNode = Address( NodeId(opcuaidentifiers.OpcUaId_RootFolder, 0), SERVER_URI )
firstLevelBrowseResult = myClient.browse([rootNode])
print("BROWSE RESULT:")
print(firstLevelBrowseResult)
    
# Note that the 0 is the namespace index for the OPC UA standard. It always corresponds to the 
# OPC UA standard URI 'http://opcfoundation.org/UA/'
# Instead of the above, we therefore could have also specified the namespace URI:
#rootNode = Address(NodeId(opcuaidentifiers.OpcUaId_RootFolder, 
#                          'http://opcfoundation.org/UA/'), 
#                   SERVER_URI)
# It woulnd't make a difference, as the UAF will automatically "translate" the namespace URI
# 'http://opcfoundation.org/UA/' to the namespace index 0 for you.
# The mapping between namespace URIs and namespace indexes is called the NamespaceArray.
# Let's print this mapping:
result = myClient.read(Address(NodeId(opcuaidentifiers.OpcUaId_Server_NamespaceArray, 
                                      pyuaf.util.constants.OPCUA_NAMESPACE_URI), # same as 'http://opcfoundation.org/UA/'
myClient = Client(settings)



try:
    print("")
    print("First option: use the convenience function \"browse()\"")
    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!)
    # (notice too that you can optionally provide a BrowseConfig and a SessionConfig for 
    #  more detailed configuration)
    firstLevelBrowseResult = myClient.browse([rootNode])
    
    # 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!!!
    noOfFoundReferences = len(firstLevelBrowseResult.targets[0].references)
    newNodesToBeBrowsed = []
    for i in xrange(noOfFoundReferences):
Example #10
0
def connect( ):
    global client
    cs = ClientSettings("myClient", [server_address])
    client = Client(cs)
    rootNode = Address( NodeId(opcuaidentifiers.OpcUaId_RootFolder, 0), server_uri )
    result=client.browse ([ rootNode ])
Example #11
0
# The client now discovers the "opc.tcp://localhost:48010", and fetches the application description
# of the UaServerCpp demo server.
# Let's print out what the client discovered:
listOfApplicationDescriptions = myClient.serversFound()
print(listOfApplicationDescriptions)

# It's a list of one server description (the demo server). It tells us the application URI, or in
# this case, the server uri:
SERVER_URI = listOfApplicationDescriptions[0].applicationUri

# Okay now that we know the server URI of the demo server, we can connect to it.
# We'll just dynamically browse the address space, and let the UAF do the connection for us:

rootNode = Address(NodeId(opcuaidentifiers.OpcUaId_RootFolder, 0), SERVER_URI)
firstLevelBrowseResult = myClient.browse([rootNode])
print("BROWSE RESULT:")
print(firstLevelBrowseResult)

# Note that the 0 is the namespace index for the OPC UA standard. It always corresponds to the
# OPC UA standard URI 'http://opcfoundation.org/UA/'
# Instead of the above, we therefore could have also specified the namespace URI:
#rootNode = Address(NodeId(opcuaidentifiers.OpcUaId_RootFolder,
#                          'http://opcfoundation.org/UA/'),
#                   SERVER_URI)
# It woulnd't make a difference, as the UAF will automatically "translate" the namespace URI
# 'http://opcfoundation.org/UA/' to the namespace index 0 for you.
# The mapping between namespace URIs and namespace indexes is called the NamespaceArray.
# Let's print this mapping:
result = myClient.read(
    Address(