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
21d88b2c
Commit
21d88b2c
authored
Jul 05, 2016
by
bitkeeper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Example custom enums
parent
6d2c46e4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
95 additions
and
0 deletions
+95
-0
examples/server-enum.py
examples/server-enum.py
+95
-0
No files found.
examples/server-enum.py
0 → 100644
View file @
21d88b2c
'''
This example demonstrates the use of custom enums by:
- Create a custom enum type
- Create an object that contains a variable of this type
'''
try
:
from
IPython
import
embed
except
ImportError
:
import
code
def
embed
():
vars
=
globals
()
vars
.
update
(
locals
())
shell
=
code
.
InteractiveConsole
(
vars
)
shell
.
interact
()
interactive
=
True
from
opcua
import
ua
,
Server
from
opcua.common
import
node
from
enum
import
IntEnum
# Not required just for convenience
# Because this example is based on EnumStrings, the values should start at 0 and no gaps are allowed.
class
MyEnum
(
IntEnum
):
ok
=
0
idle
=
1
# helper method to automatically create string list
def
enum_to_stringlist
(
a_enum
):
items
=
[]
for
value
in
a_enum
:
items
.
append
(
ua
.
LocalizedText
(
value
.
name
))
return
items
if
__name__
==
"__main__"
:
# setup our server
server
=
Server
()
server
.
set_endpoint
(
"opc.tcp://0.0.0.0:4840/freeopcua/server/"
)
# setup our own namespace, not really necessary but should as spec
uri
=
"http://examples.freeopcua.github.io"
nsidx
=
server
.
register_namespace
(
uri
)
# --------------------------------------------------------
# create custom enum data type
# --------------------------------------------------------
enums
=
server
.
get_root_node
().
get_child
([
"0:Types"
,
"0:DataTypes"
,
"0:BaseDataType"
,
"0:Enumeration"
])
# 1.
# Create Enum Type
myenum_type
=
enums
.
add_subtype
(
nsidx
,
'MyEnum'
,
ua
.
NodeClass
.
DataType
)
# 2.
# Add enumerations as EnumStrings (Not yet tested with EnumValues)
# Essential to use namespace 0 for EnumStrings !
# By hand
# es = myenum_type.add_variable(0, "EnumStrings" , [ua.LocalizedText("ok"),
# ua.LocalizedText("idle")])
# Or convert the existing IntEnum MyEnum
es
=
myenum_type
.
add_variable
(
0
,
"EnumStrings"
,
enum_to_stringlist
(
MyEnum
))
es
.
set_value_rank
(
1
)
es
.
set_array_dimensions
([
0
])
# --------------------------------------------------------
# create object with enum variable
# --------------------------------------------------------
# get Objects node, this is where we should put our custom stuff
objects
=
server
.
get_objects_node
()
# create object
myobj
=
objects
.
add_object
(
nsidx
,
'MyObjectWithEnumVar'
)
# add var with as type the custom enumeration
myenum_var
=
myobj
.
add_variable
(
nsidx
,
'MyEnum2Var'
,
MyEnum
.
ok
,
myenum_type
.
nodeid
)
myenum_var
.
set_writable
()
myenum_var
.
set_value
(
MyEnum
.
idle
)
# change value of enumeration
server
.
start
()
try
:
if
interactive
:
embed
()
else
:
while
True
:
time
.
sleep
(
0.5
)
except
IOError
:
pass
finally
:
server
.
stop
()
print
(
"done"
)
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