Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
opcua-asyncio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nikola Balog
opcua-asyncio
Commits
f8945ce0
Commit
f8945ce0
authored
Jan 08, 2021
by
oroulet
Committed by
oroulet
Jan 11, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some typing add tests for spaces in custom enums and structs names and members
parent
d55283ce
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
13 deletions
+60
-13
asyncua/common/structures104.py
asyncua/common/structures104.py
+12
-5
examples/server-robotics.py
examples/server-robotics.py
+4
-6
tests/test_common.py
tests/test_common.py
+44
-2
No files found.
asyncua/common/structures104.py
View file @
f8945ce0
...
...
@@ -4,15 +4,18 @@ import uuid
from
enum
import
IntEnum
import
logging
import
re
from
typing
import
Union
,
List
,
TYPE_CHECKING
,
Tuple
from
asyncua
import
ua
from
asyncua
import
Node
from
asyncua.common.manage_nodes
import
create_encoding
,
create_data_type
if
TYPE_CHECKING
:
from
asyncua
import
Client
,
Server
logger
=
logging
.
getLogger
(
__name__
)
def
new_struct_field
(
name
,
dtype
,
array
=
False
,
optional
=
False
,
description
=
""
)
:
def
new_struct_field
(
name
:
str
,
dtype
:
Union
[
ua
.
NodeId
,
Node
,
ua
.
VariantType
],
array
:
bool
=
False
,
optional
:
bool
=
False
,
description
:
str
=
""
)
->
ua
.
StructureField
:
"""
simple way to create a StructureField
"""
...
...
@@ -40,12 +43,16 @@ def new_struct_field(name, dtype, array=False, optional=False, description=""):
return
field
async
def
new_struct
(
server
,
idx
,
name
,
fields
)
:
async
def
new_struct
(
server
:
Union
[
"Server"
,
"Client"
],
idx
:
Union
[
int
,
ua
.
NodeId
],
name
:
Union
[
int
,
ua
.
QualifiedName
],
fields
:
List
[
ua
.
StructureField
])
->
Tuple
[
Node
,
List
[
Node
]]
:
"""
simple way to create a new structure
return the created data type node and the list of encoding nodes
"""
dtype
=
await
create_data_type
(
server
.
nodes
.
base_structure_type
,
idx
,
name
)
if
isinstance
(
idx
,
ua
.
NodeId
):
# user has provided a node id, we cannot reuse it
idx
=
idx
.
NamespaceIndex
enc
=
await
create_encoding
(
dtype
,
idx
,
"Default Binary"
)
# TODO: add other encoding the day we support them
...
...
@@ -63,7 +70,7 @@ async def new_struct(server, idx, name, fields):
return
dtype
,
[
enc
]
async
def
new_enum
(
server
,
idx
,
name
,
values
)
:
async
def
new_enum
(
server
:
Union
[
"Server"
,
"Client"
],
idx
:
Union
[
int
,
ua
.
NodeId
],
name
:
Union
[
int
,
ua
.
QualifiedName
],
values
:
List
[
str
])
->
Node
:
edef
=
ua
.
EnumDefinition
()
counter
=
0
for
val_name
in
values
:
...
...
@@ -258,7 +265,7 @@ async def _recursive_parse(server, base_node, dtypes, parent_sdef=None):
await _recursive_parse(server, server.get_node(desc.NodeId), dtypes, parent_sdef=sdef)
async def load_data_type_definitions(server
, base_node=None)
:
async def load_data_type_definitions(server
: Union["Server", "Client"], base_node: Node = None) -> None
:
await load_enums(server) # we need all enums to generate structure code
if base_node is None:
base_node = server.nodes.base_structure_type
...
...
@@ -315,7 +322,7 @@ class {name}(IntEnum):
return code
async def load_enums(server
, base_node=None)
:
async def load_enums(server
: Union["Server", "Client"], base_node: Node = None) -> None
:
if base_node is None:
base_node = server.nodes.enum_data_type
for desc in await base_node.get_children_descriptions(refs=ua.ObjectIds.HasSubtype):
...
...
examples/server-robotics.py
View file @
f8945ce0
...
...
@@ -4,23 +4,21 @@ import sys
sys
.
path
.
insert
(
0
,
".."
)
from
IPython
import
embed
from
asyncua
import
ua
,
uamethod
,
Server
async
def
main
():
logging
.
basicConfig
(
level
=
logging
.
INFO
)
server
=
Server
()
await
server
.
init
()
# import some nodes from xml
await
server
.
import_xml
(
"../schemas/UA-Nodeset/DI/Opc.Ua.Di.NodeSet2.xml"
)
await
server
.
import_xml
(
"../schemas/UA-Nodeset/Robotics/Opc.Ua.Robotics.NodeSet2.xml"
)
await
server
.
import_xml
(
"../schemas/UA-Nodeset
-master
/DI/Opc.Ua.Di.NodeSet2.xml"
)
await
server
.
import_xml
(
"../schemas/UA-Nodeset
-master
/Robotics/Opc.Ua.Robotics.NodeSet2.xml"
)
# starting!
async
with
server
:
embed
()
while
True
:
await
asyncio
.
sleep
(
1
)
if
__name__
==
"__main__"
:
...
...
tests/test_common.py
View file @
f8945ce0
...
...
@@ -1248,14 +1248,14 @@ async def test_custom_struct_with_enum(opc):
async
def
test_two_times_enum
(
opc
):
idx
=
4
dtype
=
await
new_enum
(
opc
.
opc
,
idx
,
"MyCustEnum5"
,
[
await
new_enum
(
opc
.
opc
,
idx
,
"MyCustEnum5"
,
[
"titi"
,
"toto"
,
"tutu"
,
])
with
pytest
.
raises
(
ua
.
uaerrors
.
BadBrowseNameDuplicated
):
dtype
=
await
new_enum
(
opc
.
opc
,
idx
,
"MyCustEnum5"
,
[
await
new_enum
(
opc
.
opc
,
idx
,
"MyCustEnum5"
,
[
"titi"
,
])
...
...
@@ -1301,3 +1301,45 @@ async def test_custom_struct_import(opc):
assert
sdef
.
Fields
[
0
].
Name
==
"MyBool"
await
opc
.
opc
.
export_xml
(
nodes
,
"tests/custom_struct_v2.xml"
)
async
def
test_enum_string_identifier_and_spaces
(
opc
):
idx
=
4
nodeid
=
ua
.
NodeId
(
"My Identifier"
,
idx
)
qname
=
ua
.
QualifiedName
(
"My Enum"
,
idx
)
await
new_enum
(
opc
.
opc
,
nodeid
,
qname
,
[
"my name with hole"
,
"toto"
,
"tutu"
,
])
await
opc
.
opc
.
load_data_type_definitions
()
var
=
await
opc
.
opc
.
nodes
.
objects
.
add_variable
(
idx
,
"my enum"
,
ua
.
My_Enum
.
my_name_with_hole
)
val
=
await
var
.
read_value
()
assert
val
==
0
async
def
test_custom_struct_of_struct_with_spaces
(
opc
):
idx
=
6
nodeid
=
ua
.
NodeId
(
"toto.My Identifier"
,
idx
)
qname
=
ua
.
QualifiedName
(
"My Sub Struct 1"
,
idx
)
dtype
,
encs
=
await
new_struct
(
opc
.
opc
,
nodeid
,
qname
,
[
new_struct_field
(
"My Bool"
,
ua
.
VariantType
.
Boolean
),
new_struct_field
(
"My UInt32"
,
ua
.
VariantType
.
UInt32
),
])
await
new_struct
(
opc
.
opc
,
idx
,
"My Mother Struct"
,
[
new_struct_field
(
"My Bool"
,
ua
.
VariantType
.
Boolean
),
new_struct_field
(
"My Sub Struct"
,
dtype
),
])
await
opc
.
opc
.
load_data_type_definitions
()
mystruct
=
ua
.
My_Mother_Struct
()
mystruct
.
My_Sub_Struct
=
ua
.
My_Sub_Struct_1
()
mystruct
.
My_Sub_Struct
.
My_UInt32
=
78
var
=
await
opc
.
opc
.
nodes
.
objects
.
add_variable
(
idx
,
"my mother struct"
,
ua
.
Variant
(
mystruct
,
ua
.
VariantType
.
ExtensionObject
))
val
=
await
var
.
read_value
()
assert
val
.
My_Sub_Struct
.
My_UInt32
==
78
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment