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
ec7832ab
Commit
ec7832ab
authored
Feb 25, 2019
by
oroulet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add sync/client-example.py and make it work
parent
56cf68df
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
126 additions
and
0 deletions
+126
-0
asyncua/sync.py
asyncua/sync.py
+34
-0
examples/sync/client-example.py
examples/sync/client-example.py
+92
-0
No files found.
asyncua/sync.py
View file @
ec7832ab
...
...
@@ -95,6 +95,8 @@ def syncmethod(func):
return
[
Node
(
i
)
for
i
in
result
]
if
isinstance
(
result
,
server
.
event_generator
.
EventGenerator
):
return
EventGenerator
(
result
)
if
isinstance
(
result
,
subscription
.
Subscription
):
return
Subscription
(
result
)
return
result
return
wrapper
...
...
@@ -113,6 +115,18 @@ class Client:
def
disconnect
(
self
):
pass
@
syncmethod
def
load_type_definitions
(
self
,
nodes
=
None
):
pass
@
syncmethod
async
def
create_subscription
(
self
,
period
,
handler
):
pass
@
syncmethod
def
get_namespace_index
(
self
,
url
):
pass
def
get_node
(
self
,
nodeid
):
return
Node
(
self
.
aio_obj
.
get_node
(
nodeid
))
...
...
@@ -240,7 +254,27 @@ class Node:
def
get_value
(
self
,
val
):
pass
@
syncmethod
def
call_method
(
self
,
methodid
,
*
args
):
pass
def
__eq__
(
self
,
other
):
return
self
.
aio_obj
==
other
.
aio_obj
class
Subscription
:
def
__init__
(
self
,
sub
):
self
.
aio_obj
=
sub
@
syncmethod
def
subscribe_data_change
(
self
,
nodes
,
attr
=
ua
.
AttributeIds
.
Value
,
queuesize
=
0
):
pass
@
syncmethod
def
subscribe_events
(
self
,
sourcenode
=
ua
.
ObjectIds
.
Server
,
evtypes
=
ua
.
ObjectIds
.
BaseEventType
,
evfilter
=
None
,
queuesize
=
0
):
pass
@
syncmethod
async
def
create_monitored_items
(
self
,
monitored_items
):
pass
examples/sync/client-example.py
0 → 100644
View file @
ec7832ab
import
sys
sys
.
path
.
insert
(
0
,
".."
)
import
logging
import
time
try
:
from
IPython
import
embed
except
ImportError
:
import
code
def
embed
():
vars
=
globals
()
vars
.
update
(
locals
())
shell
=
code
.
InteractiveConsole
(
vars
)
shell
.
interact
()
from
asyncua
import
ua
from
asyncua.sync
import
Client
,
start_thread_loop
,
stop_thread_loop
class
SubHandler
(
object
):
"""
Subscription Handler. To receive events from server for a subscription
data_change and event methods are called directly from receiving thread.
Do not do expensive, slow or network operation there. Create another
thread if you need to do such a thing
"""
def
datachange_notification
(
self
,
node
,
val
,
data
):
print
(
"Python: New data change event"
,
node
,
val
)
def
event_notification
(
self
,
event
):
print
(
"Python: New event"
,
event
)
if
__name__
==
"__main__"
:
logging
.
basicConfig
(
level
=
logging
.
WARN
)
#logger = logging.getLogger("KeepAlive")
#logger.setLevel(logging.DEBUG)
start_thread_loop
()
client
=
Client
(
"opc.tcp://localhost:4840/freeopcua/server/"
)
# client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
try
:
client
.
connect
()
client
.
load_type_definitions
()
# load definition of server specific structures/extension objects
# Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
print
(
"Objects node is: "
,
client
.
nodes
.
objects
)
# Node objects have methods to read and write node attributes as well as browse or populate address space
print
(
"Children of root are: "
,
client
.
nodes
.
root
.
get_children
())
# get a specific node knowing its node id
#var = client.get_node(ua.NodeId(1002, 2))
#var = client.get_node("ns=3;i=2002")
#print(var)
#var.get_data_value() # get value of node as a DataValue object
#var.get_value() # get value of node as a python builtin
#var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
#var.set_value(3.9) # set node value using implicit data type
# gettting our namespace idx
uri
=
"http://examples.freeopcua.github.io"
idx
=
client
.
get_namespace_index
(
uri
)
# Now getting a variable node using its browse path
myvar
=
client
.
nodes
.
root
.
get_child
([
"0:Objects"
,
"{}:MyObject"
.
format
(
idx
),
"{}:MyVariable"
.
format
(
idx
)])
obj
=
client
.
nodes
.
root
.
get_child
([
"0:Objects"
,
"{}:MyObject"
.
format
(
idx
)])
print
(
"myvar is: "
,
myvar
)
# subscribing to a variable node
handler
=
SubHandler
()
sub
=
client
.
create_subscription
(
500
,
handler
)
handle
=
sub
.
subscribe_data_change
(
myvar
)
time
.
sleep
(
0.1
)
# we can also subscribe to events from server
sub
.
subscribe_events
()
# sub.unsubscribe(handle)
# sub.delete()
# calling a method on server
res
=
obj
.
call_method
(
"{}:multiply"
.
format
(
idx
),
3
,
"klk"
)
print
(
"method result is: "
,
res
)
embed
()
finally
:
client
.
disconnect
()
stop_thread_loop
()
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