server-callback.py
Go to the documentation of this file.
1 import sys
2 sys.path.insert(0, "..")
3 import logging
4 from datetime import datetime
5 
6 try:
7  from IPython import embed
8 except ImportError:
9  import code
10 
11  def embed():
12  vars = globals()
13  vars.update(locals())
14  shell = code.InteractiveConsole(vars)
15  shell.interact()
16 
17 
18 from opcua import ua, uamethod, Server
19 from opcua.common.callback import CallbackType
20 
21 
22 
23 def create_monitored_items(event, dispatcher):
24  print("Monitored Item")
25 
26  for idx in range(len(event.response_params)) :
27  if (event.response_params[idx].StatusCode.is_good()) :
28  nodeId = event.request_params.ItemsToCreate[idx].ItemToMonitor.NodeId
29  print("Node {0} was created".format(nodeId))
30 
31 
32 def modify_monitored_items(event, dispatcher):
33  print('modify_monitored_items')
34 
35 
36 def delete_monitored_items(event, dispatcher):
37  print('delete_monitored_items')
38 
39 
40 if __name__ == "__main__":
41  # optional: setup logging
42  logging.basicConfig(level=logging.WARN)
43  #logger = logging.getLogger("opcua.address_space")
44  # logger.setLevel(logging.DEBUG)
45  #logger = logging.getLogger("opcua.internal_server")
46  # logger.setLevel(logging.DEBUG)
47  #logger = logging.getLogger("opcua.binary_server_asyncio")
48  # logger.setLevel(logging.DEBUG)
49  #logger = logging.getLogger("opcua.uaprocessor")
50  # logger.setLevel(logging.DEBUG)
51  logger = logging.getLogger("opcua.subscription_service")
52  logger.setLevel(logging.DEBUG)
53 
54  # now setup our server
55  server = Server()
56  #server.disable_clock()
57  #server.set_endpoint("opc.tcp://localhost:4840/freeopcua/server/")
58  server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
59  server.set_server_name("FreeOpcUa Example Server")
60 
61  # setup our own namespace
62  uri = "http://examples.freeopcua.github.io"
63  idx = server.register_namespace(uri)
64 
65  # get Objects node, this is where we should put our custom stuff
66  objects = server.get_objects_node()
67 
68  # populating our address space
69  myfolder = objects.add_folder(idx, "myEmptyFolder")
70  myobj = objects.add_object(idx, "MyObject")
71  myvar = myobj.add_variable(idx, "MyVariable", 6.7)
72  myvar.set_writable() # Set MyVariable to be writable by clients
73 
74 
75  # starting!
76  server.start()
77 
78  # Create Callback for item event
79  server.subscribe_server_callback(CallbackType.ItemSubscriptionCreated, create_monitored_items)
80  server.subscribe_server_callback(CallbackType.ItemSubscriptionModified, modify_monitored_items)
81  server.subscribe_server_callback(CallbackType.ItemSubscriptionDeleted, delete_monitored_items)
82 
83 
84  print("Available loggers are: ", logging.Logger.manager.loggerDict.keys())
85  try:
86  # enable following if you want to subscribe to nodes on server side
87  embed()
88  finally:
89  server.stop()
def delete_monitored_items(event, dispatcher)
def modify_monitored_items(event, dispatcher)
def create_monitored_items(event, dispatcher)


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Tue Jan 19 2021 03:12:44