Commit 72421950 authored by Philipp's avatar Philipp

adding client fhi

parent 3ed480ed
#!//usr/bin/python
"""
Basic client with minimal data
"""
import logging
import asyncio
from asyncua import Client, ua
import argparse
# command line handling
parser = argparse.ArgumentParser(description='Run OPCUA Server.')
a = parser.add_argument
a('--ipv6', help='The IPv6 address on which the OPCUA Server runs', default="::")
a('--ipv6-enabled', help='The IPv6 address check whether it is enabled or disabled', default="1")
a('--port', help='The port on which the OPCUA Server runs', default="4840")
a('--xml', help='Path of XML to configure Server. See asyncua doc for more details.', default=None)
args = parser.parse_args()
ipv6 = args.ipv6
ipv6 = "2001:67c:1254:108:404e::a"
ipv6_enabled = args.ipv6_enabled
port = args.port
xml = args.xml
# Server information
if bool(int(ipv6_enabled)):
url = "opc.tcp://[{ipv6}]:{port}/PwDC"
#url = "opc.tcp://0.0.0.0:4880/PwDC"
async def main():
_logger = logging.getLogger(__name__)
_logger.info("Connecting to %s ...", url)
async with Client(url=url, timeout=2) as client:
while True:
await asyncio.sleep(1)
# Get the root node
#namespace_uri = "http://examples.freeopcua.github.io"
#nsidx = await client.get_namespace_index(namespace_uri)
nsidx = 1
# Get the Powerfuse object
s="Powerfuse_Maschinenstatus_Status_Rot"
var = await client.nodes.root.get_child(f"0:Objects/{nsidx}:Powerfuse/{nsidx}:{s}")
value = await var.read_value()
name = await var.read_browse_name()
_logger.info(f"Value of {s} ({var}): {value}")
s="Powerfuse_Maschinenstatus_Status_Gelb"
var = await client.nodes.root.get_child(f"0:Objects/{nsidx}:Powerfuse/{nsidx}:{s}")
value = await var.read_value()
name = await var.read_browse_name()
_logger.info(f"Value of {s} ({var}): {value}")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.run(main())
...@@ -17,17 +17,13 @@ a('--ipv6', help='The IPv6 address on which the OPCUA Server runs', default="::" ...@@ -17,17 +17,13 @@ a('--ipv6', help='The IPv6 address on which the OPCUA Server runs', default="::"
a('--ipv6-enabled', help='The IPv6 address check whether it is enabled or disabled', default="1") a('--ipv6-enabled', help='The IPv6 address check whether it is enabled or disabled', default="1")
a('--port', help='The port on which the OPCUA Server runs', default="4840") a('--port', help='The port on which the OPCUA Server runs', default="4840")
a('--xml', help='Path of XML to configure Server. See asyncua doc for more details.', default=None) a('--xml', help='Path of XML to configure Server. See asyncua doc for more details.', default=None)
args = parser.parse_args() args = parser.parse_args()
ipv6 = args.ipv6 ipv6 = args.ipv6
ipv6_enabled = args.ipv6_enabled ipv6_enabled = args.ipv6_enabled
port = args.port port = args.port
xml = args.xml xml = args.xml
@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
...@@ -35,38 +31,41 @@ async def main(): ...@@ -35,38 +31,41 @@ async def main():
await server.init() await server.init()
if bool(int(ipv6_enabled)): if bool(int(ipv6_enabled)):
server.set_endpoint(f"opc.tcp://[{ipv6}]:{port}/freeopcua/server/") server.set_endpoint(f"opc.tcp://[{ipv6}]:{port}/PwDC")
if xml is not None: if xml is not None:
await server.import_xml(xml) await server.import_xml(xml)
# set up our own namespace, not really necessary but should as spec idx = 1
uri = "http://examples.freeopcua.github.io" # Create an object to hold the variables
idx = await server.register_namespace(uri) objects = server.get_objects_node()
# populating our address space powerfuse_obj = await objects.add_object(idx, "Powerfuse")
# server.nodes, contains links to very common nodes like objects and root
myobj = await server.nodes.objects.add_object(idx, "MyObject") # Create variables
myvar = await myobj.add_variable(idx, "MyVariable", 6.7) machine_status_rot = await powerfuse_obj.add_variable(idx, "Powerfuse_Maschinenstatus_Status_Rot", False)
# Set MyVariable to be writable by clients machine_status_gelb = await powerfuse_obj.add_variable(idx, "Powerfuse_Maschinenstatus_Status_Gelb", False)
await myvar.set_writable()
await server.nodes.objects.add_method( # Set the variables as writable
ua.NodeId("ServerMethod", idx), await machine_status_rot.set_writable()
ua.QualifiedName("ServerMethod", idx), await machine_status_gelb.set_writable()
func,
[ua.VariantType.Int64], # Start the server
[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) # Randomly change the machine status
new_val = await myvar.get_value() + 0.1 new_status_rot = random.choice([True, False])
_logger.info("Set value of %s to %.1f", myvar, new_val) new_status_gelb = random.choice([True, False])
await myvar.write_value(new_val)
await machine_status_rot.write_value(new_status_rot)
await machine_status_gelb.write_value(new_status_gelb)
val_machine_status_rot = await machine_status_rot.get_value()
val_machine_status_gelb = await machine_status_gelb.get_value()
_logger.info("New machine status: Rot=%r, Gelb=%r",
val_machine_status_rot,
val_machine_status_gelb)
await asyncio.sleep(1)
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
asyncio.run(main(), debug=True) asyncio.run(main(), debug=True)
[instance-profile]
filename = instance.cfg.in
md5sum = e02dafa3ef034684052c6cdc5da9d09f
[buildout]
parts =
directory
opcua-xml-url
opcua-client-fhi-service
publish-connection-parameter
eggs-directory = {{ buildout["eggs-directory"] }}
develop-eggs-directory = {{ buildout["develop-eggs-directory"] }}
offline = true
[check-port-listening-promise]
recipe = slapos.cookbook:check_port_listening
path = ${directory:promise}/${:_buildout_section_name_}
[directory]
recipe = slapos.cookbook:mkdirectory
home = ${buildout:directory}
etc = ${:home}/etc
var = ${:home}/var
script = ${:etc}/run/
service = ${:etc}/service
promise = ${:etc}/promise/
log = ${:var}/log
bin = ${:home}/bin
[instance-parameter]
recipe = slapos.cookbook:slapconfiguration
computer = ${slap-connection:computer-id}
partition = ${slap-connection:partition-id}
url = ${slap-connection:server-url}
key = ${slap-connection:key-file}
cert = ${slap-connection:cert-file}
configuration.opcua-xml-url = "https://lab.nexedi.com/Demonkey/osie/raw/philipp_opcua/opcua-to-http-gw/opcua-server-config.xml"
[opcua-xml-url]
recipe = slapos.recipe.build:download
url = https://lab.nexedi.com/Demonkey/osie/raw/philipp_opcua/opcua-to-http-gw/opcua-server-config.xml
offline = false
destination = ${directory:etc}/schema.xml
[opcua-client-fhi-service]
recipe = slapos.cookbook:wrapper
command-line = {{ interpreter_location }}/py {{ osie_repository_location }}/opcua-client-fhi/minimal-client.py --xml ${opcua-xml-url:destination} --port '4840' --ipv6 ${instance-parameter:ipv6-random} --ipv6-enabled '1'
wrapper-path = ${directory:service}/opcua-client-fhi-service
output = $${:wrapper-path}
[publish-connection-parameter]
recipe = slapos.cookbook:publish
ipv6 = ${instance-parameter:ipv6-random}
[buildout]
extends =
buildout.hash.cfg
https://lab.nexedi.com/nexedi/slapos/raw/master/stack/monitor/buildout.cfg
https://lab.nexedi.com/nexedi/slapos/raw/master/stack/slapos.cfg
https://lab.nexedi.com/nexedi/slapos/raw/master/component/python3/buildout.cfg
https://lab.nexedi.com/nexedi/slapos/raw/master/component/git/buildout.cfg
https://lab.nexedi.com/nexedi/slapos/raw/master/component/python-cryptography/buildout.cfg
https://lab.nexedi.com/nexedi/slapos/raw/master/component/lxml-python/buildout.cfg
parts =
slapos-cookbook
interpreter
osie-repository
instance-profile
[python]
part = python3
[interpreter]
recipe = zc.recipe.egg:script
interpreter = py
eggs =
${lxml-python:egg}
${python-cryptography:egg}
requests
asyncua
[osie-repository]
recipe = slapos.recipe.build:gitclone
git-executable = ${git:location}/bin/git
repository = https://lab.nexedi.com/Demonkey/osie.git
location = ${buildout:parts-directory}/osie
branch = philipp_opcua
[versions]
aiosqlite = 0.17.0
aiofiles = 23.1.0
cryptography = 3.3.2
asyncua = 0.9.2
lxml = 4.9.1
[instance-profile]
recipe = slapos.recipe.template:jinja2
template = ${:_profile_base_location_}/instance.cfg.in
rendered = ${buildout:directory}/instance.cfg
context =
section buildout buildout
key interpreter_location buildout:bin-directory
key osie_repository_location osie-repository:location
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