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

component/opcua-server: make configurable

parent 753d06ad
......@@ -12,46 +12,38 @@ egg-versions =
python-dateutil = 2.8.2:whl
entry-points = ${:_buildout_section_name_}=__main__:main
initialization =
import sys
ip = sys.argv[1]
port = 2262 # hard coded here + in stack/erp5/haproxy.cfg.in
# Adjusted minimal server example from opcua-asyncio documentation
# https://opcua-asyncio.readthedocs.io/en/latest/usage/get-started/minimal-server.html
# ######################
# Configure
import argparse
parser = argparse.ArgumentParser(description='Run OPCUA Server.')
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 logging
from asyncua import Server, ua
from asyncua.common.methods import uamethod
@uamethod
def func(parent, value):
return value * 2
from asyncua import Server
async def main():
_logger = logging.getLogger(__name__)
# setup our server
server = Server()
await server.init()
server.set_endpoint(f"tcp://{ip}:{port}")
# setup our own namespace, not really necessary but should as spec
uri = "http://examples.freeopcua.github.io"
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],
)
if xml is not None:
await server.import_xml(xml)
_logger.info("Starting server!")
async with server:
while True:
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)
asyncio.run(main(), debug=True)
......@@ -106,4 +106,4 @@ md5sum = eb4be2669a9a56187cc4366272e11d18
[instance-opcua-server.cfg.in]
filename = instance-opcua-server.cfg.in
md5sum = aeb48d87bac998402aba6ca829d62466
md5sum = 207b458e52de1a038af807274345b5ff
{% set ipv4 = (ipv4_set | list)[0] -%}
{# port is hard coded here + in stack/erp5/haproxy.cfg.in #}
{% set port = 2262 %}
[buildout]
extends = {{ template_monitor }}
......@@ -18,5 +20,6 @@ var = ${buildout:directory}/var
[opcua-server]
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
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