Commit 664bbd62 authored by Levin Zimmermann's avatar Levin Zimmermann

component/opcua-server: make configurable

parent 753d06ad
...@@ -12,46 +12,38 @@ egg-versions = ...@@ -12,46 +12,38 @@ egg-versions =
python-dateutil = 2.8.2:whl python-dateutil = 2.8.2:whl
entry-points = ${:_buildout_section_name_}=__main__:main entry-points = ${:_buildout_section_name_}=__main__:main
initialization = initialization =
import sys # ######################
ip = sys.argv[1] # Configure
port = 2262 # hard coded here + in stack/erp5/haproxy.cfg.in import argparse
# Adjusted minimal server example from opcua-asyncio documentation parser = argparse.ArgumentParser(description='Run OPCUA Server.')
# https://opcua-asyncio.readthedocs.io/en/latest/usage/get-started/minimal-server.html parser.add_argument(
'--ip', help='The IP address on which the OPCUA Server runs', default="127.0.0.1"
)
parser.add_argument(
'--port', help='The port on which the OPCUA Server runs', default="2262"
)
parser.add_argument(
'--xml',
help='Path of XML to configure Server. See asyncua doc for more details.',
default=None
)
args = parser.parse_args()
ip, port, xml = args.ip, args.port, args.xml
# ######################
import asyncio import asyncio
import logging import logging
from asyncua import Server, ua from asyncua import Server
from asyncua.common.methods import uamethod
@uamethod
def func(parent, value):
return value * 2
async def main(): async def main():
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
# setup our server # setup our server
server = Server() server = Server()
await server.init() await server.init()
server.set_endpoint(f"tcp://{ip}:{port}") server.set_endpoint(f"tcp://{ip}:{port}")
# setup our own namespace, not really necessary but should as spec if xml is not None:
uri = "http://examples.freeopcua.github.io" await server.import_xml(xml)
idx = await server.register_namespace(uri)
# populating our address space
# server.nodes, contains links to very common nodes like objects and root
myobj = await server.nodes.objects.add_object(idx, "MyObject")
myvar = await myobj.add_variable(idx, "MyVariable", 6.7)
# Set MyVariable to be writable by clients
await myvar.set_writable()
await server.nodes.objects.add_method(
ua.NodeId("ServerMethod", idx),
ua.QualifiedName("ServerMethod", idx),
func,
[ua.VariantType.Int64],
[ua.VariantType.Int64],
)
_logger.info("Starting server!") _logger.info("Starting server!")
async with server: async with server:
while True: while True:
await asyncio.sleep(1) await asyncio.sleep(1)
new_val = await myvar.get_value() + 0.1
_logger.info("Set value of %s to %.1f", myvar, new_val)
await myvar.write_value(new_val)
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
asyncio.run(main(), debug=True) asyncio.run(main(), debug=True)
...@@ -106,4 +106,4 @@ md5sum = eb4be2669a9a56187cc4366272e11d18 ...@@ -106,4 +106,4 @@ md5sum = eb4be2669a9a56187cc4366272e11d18
[instance-opcua-server.cfg.in] [instance-opcua-server.cfg.in]
filename = instance-opcua-server.cfg.in filename = instance-opcua-server.cfg.in
md5sum = aeb48d87bac998402aba6ca829d62466 md5sum = 207b458e52de1a038af807274345b5ff
{% set ipv4 = (ipv4_set | list)[0] -%} {% set ipv4 = (ipv4_set | list)[0] -%}
{# port is hard coded here + in stack/erp5/haproxy.cfg.in #}
{% set port = 2262 %}
[buildout] [buildout]
extends = {{ template_monitor }} extends = {{ template_monitor }}
...@@ -18,5 +20,6 @@ var = ${buildout:directory}/var ...@@ -18,5 +20,6 @@ var = ${buildout:directory}/var
[opcua-server] [opcua-server]
recipe = slapos.cookbook:wrapper recipe = slapos.cookbook:wrapper
command-line = {{ bin_directory }}/opcua-server {{ ipv4 }} command-line =
{{ bin_directory }}/opcua-server --ip {{ ipv4 }} --port {{ port }}
wrapper-path = ${directory:service-on-watch}/opcua-server wrapper-path = ${directory:service-on-watch}/opcua-server
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment