Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasHeine committed Jun 7, 2021
1 parent e346ca9 commit 04c6a41
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
env
19 changes: 9 additions & 10 deletions client/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,33 @@
async def main():
client = Client(url="opc.tcp://127.0.0.1:48010", timeout=8)
await client.connect()
obj = client.get_objects_node()

print("-----------------------------------------------------")
# get the references of a single node
obj = client.get_objects_node()
refs = await obj.get_references()
children = []
type_definitions = []
organizes = []
typedefinitions = []
print(f"Node-Id {obj} has following References:")
for ref in refs:
print(ref)
if ref.ReferenceTypeId.Identifier == ua.ObjectIds.Organizes:
# Organizes = 35
children.append(ref)
organizes.append(ref)
elif ref.ReferenceTypeId.Identifier == ua.ObjectIds.HasTypeDefinition:
# HasTypeDefinition = 40
type_definitions.append(ref)
typedefinitions.append(ref)
else:
pass
print("-----------------------------------------------------")
print(f"Node-Id {obj} has following Childnodes:")
for child in children:
print(child.BrowseName.Name)
for each in organizes:
print(each.BrowseName.Name)
print("-----------------------------------------------------")
print(f"Node-Id {obj} is from Type:")
for type_definition in type_definitions:
print(type_definition.BrowseName.Name)
for each in typedefinitions:
print(each.BrowseName.Name)

print("-----------------------------------------------------")
await client.disconnect()

if __name__ == "__main__":
Expand Down
26 changes: 25 additions & 1 deletion client/call_method.py
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# coming soon!
import asyncio
import logging
from asyncua import Client, ua

logging.basicConfig(level=logging.WARNING)
_logger = logging.getLogger('asyncua')

async def main():
client = Client(url="opc.tcp://127.0.0.1:48010", timeout=8)
await client.connect()

method_parent = client.get_node("ns=7;s=Demo.CTT.Methods")
method_node = client.get_node("ns=7;s=Demo.CTT.Methods.MethodIO") # the method just adds two Int32 numbers

# always specify the correct VariantType!
inarg1 = ua.Variant(20, ua.VariantType.UInt32)
inarg2 = ua.Variant(13, ua.VariantType.UInt32)

result = await method_parent.call_method(method_node, inarg1, inarg2) # the order of args matters!
print(result)

await client.disconnect()

if __name__ == "__main__":
asyncio.run(main())
31 changes: 17 additions & 14 deletions client/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ async def main():
print("-----------------------------------------------------")
# read one node attribute at a time
node = client.get_node("ns=0;i=2267") # ServiceLevel
print("Value: ", await node.read_value())
print("DataValue: ", await node.read_data_value())
print("BrowseName: ", await node.read_browse_name())
print("DataType: ", await node.read_data_type())
print("DisplayName: ", await node.read_display_name())
print("Value: ", await node.read_value()) # returns just the Value
print("DataValue: ", await node.read_data_value()) # returns the DataValue (Value + StatusCode + Timestamps)
print("BrowseName: ", await node.read_browse_name()) # returns the BrowseName ("0:ServiceLevel")
print("DataType: ", await node.read_data_type()) # returns the DataType of the Value (i=3 -> Byte)
print("DisplayName: ", await node.read_display_name()) # returns the DisplayName in form of LocalizedText

print("-----------------------------------------------------")
# read multiple node attributes at once
# read multiple node attributes at once (HighLevel)
print("ReadValues:")
result = await client.read_values([
client.get_node("ns=0;i=2267"),
client.get_node("ns=0;i=2256")
client.get_node("ns=0;i=2267"), # Node-Class
client.get_node("ns=0;i=2256") # Node-Class
# ...
])
for res in result:
Expand All @@ -32,36 +32,39 @@ async def main():
print("-----------------------------------------------------")
print("ReadAttributeValues:")
result = await client.uaclient.read_attributes([
client.get_node("ns=0;i=2267").nodeid,
client.get_node("ns=0;i=2256").nodeid
ua.NodeId.from_string("ns=0;i=2267"), # NodeId-Class
ua.NodeId.from_string("ns=0;i=2256") # NodeId-Class
# ...
],
ua.AttributeIds.Value
)
# read_attributes returns a list of DataValue-Class instances (Value + StatusCode + Timestamps) which represent the Values
for res in result:
print(res)

print("-----------------------------------------------------")
print("ReadBrowseNames:")
result = await client.uaclient.read_attributes([
client.get_node("ns=0;i=2267").nodeid,
client.get_node("ns=0;i=2256").nodeid
ua.NodeId.from_string("ns=0;i=2267"), # NodeId-Class
ua.NodeId.from_string("ns=0;i=2256") # NodeId-Class
# ...
],
ua.AttributeIds.BrowseName
)
# read_attributes returns a list of DataValue-Class instances (Value + StatusCode + Timestamps) which represent the BrowseNames
for res in result:
print(res)

print("-----------------------------------------------------")
print("ReadDataTypes:")
result = await client.uaclient.read_attributes([
client.get_node("ns=0;i=2267").nodeid,
client.get_node("ns=0;i=2256").nodeid
ua.NodeId.from_string("ns=0;i=2267"), # NodeId-Class
ua.NodeId.from_string("ns=0;i=2256") # NodeId-Class
# ...
],
ua.AttributeIds.DataType
)
# read_attributes returns a list of DataValue-Class instances (Value + StatusCode + Timestamps) which represent the DataTypes
for res in result:
print(res)

Expand Down
11 changes: 11 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
aiofiles==0.7.0
aiosqlite==0.17.0
asyncua==0.9.14
cffi==1.14.5
cryptography==3.4.7
pycparser==2.20
python-dateutil==2.8.1
pytz==2021.1
six==1.16.0
sortedcontainers==2.4.0
typing-extensions==3.10.0.0

0 comments on commit 04c6a41

Please sign in to comment.