server.py
Go to the documentation of this file.
1 import datetime
2 import time
3 import sys
4 sys.path.append(".")
5 
6 from IPython import embed
7 import opcua
8 
9 class SubHandler(opcua.SubscriptionHandler):
10  def data_change(self, handle, node, val, attr):
11  print("New data change event", handle, node, val, attr)
12 
13  def event(self, handle, event):
14  print("Python: New event", handle, event)
15 
16 
17 if __name__ == "__main__":
18  # create our server, the argument is for debugging
19  server = opcua.Server(True)
20  server.set_endpoint("opc.tcp://localhost:4841/freeopcua/server/")
21  server.set_server_name("FreeOpcUa Example Server")
22 
23  # start the server
24  server.start()
25 
26  try:
27  # setup our own namespace
28  uri = "http://examples.freeopcua.github.io"
29  idx = server.register_namespace(uri)
30 
31  # get Objects node, this is where we should put our custom stuff
32  objects = server.get_objects_node()
33  print("I got objects folder: ", objects)
34 
35  # now adding some object to our addresse space
36  myobject = objects.add_object(idx, "NewObject")
37  myvar = myobject.add_variable(idx, "MyVariable", [16, 56])
38  myprop = myobject.add_property(idx, "myprop", 9.9)
39  myfolder = myobject.add_folder(idx, "myfolder")
40 
41  # uncomment next lines to subscribe to changes on server side
42  #sclt = SubHandler()
43  #sub = server.create_subscription(100, sclt)
44  #handle = sub.subscribe_data_change(myvar) #keep handle if you want to delete the particular subscription later
45 
46  ev = opcua.Event()
47  counter = 0
48  while True:
49  counter += 1
50  ev.message = "This is event nr: " + str(counter)
51  ev.source_node = objects.get_id()
52  ev.time = datetime.datetime.now()
53  print("Sending event: ", ev)
54  server.trigger_event(ev)
55  myvar.set_value([counter, counter+10])
56  time.sleep(1)
57  # start ipython shell so users can test things
58  #embed()
59  finally:
60  server.stop()
61 
def data_change(self, handle, node, val, attr)
Definition: server.py:10
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:873
def event(self, handle, event)
Definition: server.py:13


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