Skip to content

Instantly share code, notes, and snippets.

@sysarcher
Created July 5, 2021 09:18
Show Gist options
  • Save sysarcher/12d3350fe6ac348d85f41bba860e7bbd to your computer and use it in GitHub Desktop.
Save sysarcher/12d3350fe6ac348d85f41bba860e7bbd to your computer and use it in GitHub Desktop.
#import logging
import asyncio
import sys
sys.path.insert(0, "..")
from asyncua import ua, Server
from asyncua.common.methods import uamethod
@uamethod
def func(parent, value):
return value * 2
async def main():
#_logger = logging.getLogger('asyncua')
# setup our server
server = Server()
await server.init()
server.set_endpoint('opc.tcp://0.0.0.0:4855/')
# 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, 'v1')
myvar = await myobj.add_variable(idx, 'v1', 6.7)
# Set MyVariable to be writable by clients
await myvar.set_writable()
await server.nodes.objects.add_method(ua.NodeId('v1', 2), ua.QualifiedName('v1', 2), func, [ua.VariantType.Int64], [ua.VariantType.Int64])
#_logger.info('Starting server!')
async with server:
while True:
await asyncio.sleep(0.5)
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)
if __name__ == '__main__':
#logging.basicConfig(level=logging.DEBUG)
asyncio.run(main(), debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment