event_client.py
Go to the documentation of this file.
1 
2 import sys
3 import time
4 sys.path.append(".")
5 
6 from IPython import embed
7 import opcua
8 
9 class SubHandler(opcua.SubscriptionHandler):
10  """
11  Client to subcsription. It will receive events from server
12  """
13  def __init__(self, *args):
14  opcua.SubscriptionHandler.__init__(self, *args)
15  self.ev = MessageSecurityMode::None
16 
17  def data_change(self, handle, node, val, attr):
18  print("Python: New data change event", handle, node, val, attr)
19 
20  def event(self, handle, event):
21  print("Python: New event", handle, event)
22  self.ev = event
23 
24 
25 if __name__ == "__main__":
26  # create our client object
27  client = opcua.Client(False)
28  client.connect("opc.tcp://localhost:4841/freeopcua/server/")
29  #s.connect("opc.tcp://192.168.56.101:48030")
30  try:
31  # get server namespace. You may want to get all namespaces
32  # with client.get_server_namespaces()
33  uri = "http://examples.freeopcua.github.io"
34  idx = client.get_namespace_index(uri)
35 
36  # read a node from standard opcua address space
37  statenode = client.get_node(opcua.ObjectId.Server_ServerStatus_State)
38  print("Server state is: ", statenode.get_value())
39 
40  # get root node of server and browse it
41  root = client.get_root_node()
42  print("I got root: ", root)
43  print("Childs are: ", root.get_children())
44 
45  # get objects node of server
46  # this is where the interesting data from server should be
47  print("Objects is: ", client.get_objects_node())
48  objects = client.get_objects_node()
49  print("Children of objects are: ", objects.get_children())
50 
51  # get child using browse path
52  myvar = objects.get_child(["{}:NewObject".format(idx), "MyVariable"])
53  print("yvar is: ", myvar)
54 
55  # create a subsription we will use to subscribe to nodes or events
56  sclt = SubHandler()
57  sub = client.create_subscription(100, sclt)
58 
59  # subscribe to a specific node
60  #handle = sub.subscribe_data_change(myvar)
61  #print("Subscribe handle is: ", handle)
62 
63  # subscribe to events from server
64  evhandle = sub.subscribe_events()
65  print("Subscribe handle is: ", evhandle)
66 
67 
68  embed()
69  finally:
70  client.disconnect()
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:873
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3686
def data_change(self, handle, node, val, attr)
Definition: event_client.py:17
def event(self, handle, event)
Definition: event_client.py:20
def __init__(self, args)
Definition: event_client.py:13


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:04