Commit 2d48c8aa authored by Ivan Tyagov's avatar Ivan Tyagov

Simplify.

parent 86a59abf
......@@ -14,9 +14,7 @@ import argparse
import logging
import __main__
# #################################
# Configure
# XXX: addn erp5_username and erp5_password!
# command line handling
parser = argparse.ArgumentParser(description='Run OPCUA Server.')
a = parser.add_argument
a('--ip', help='The IP address on which the OPCUA Server runs', default="127.0.0.1")
......@@ -34,6 +32,7 @@ erp5_username = args.erp5_username
erp5_password = args.erp5_password
ERP5_REQUEST_API = "ERP5Site_handleOPCUARequest"
# ERP5 backend storage for OPCUA Document
@dataclass(frozen=True)
class ERP5Handler(asyncua.common.subscription.SubHandler):
url: str
......@@ -58,16 +57,15 @@ class ERP5Handler(asyncua.common.subscription.SubHandler):
def event_notification(self, event):
self.call(event=event)
erp5_handler = None
if erp5_url is not None:
erp5_handler = ERP5Handler(erp5_url)
# init main ERP5 storage
erp5_handler = ERP5Handler(erp5_url)
class InternalSession(asyncua.server.internal_session.InternalSession):
async def read(self, params):
erp5_handler.call(params=params)
return await super().read(params)
class InternalSession(asyncua.server.internal_session.InternalSession):
async def read(self, params):
erp5_handler.call(params=params)
return await super().read(params)
# #################################
# Start OPCUA Server
async def main():
_logger = logging.getLogger(__name__)
......@@ -80,7 +78,6 @@ async def main():
if xml is not None:
await server.import_xml(xml)
# read previous state as saved in ERP5 backend
erp5_json = erp5_handler.call(http_method="GET").json()
_logger.error(erp5_json)
......@@ -91,22 +88,21 @@ async def main():
# XXX: this leads to a change notification which calls ERP5 and endless loop!
#await node.write_value(v)
if erp5_handler:
subscription = await server.create_subscription(1000, erp5_handler)
await subscription.subscribe_events()
nodes = await asyncua.common.ua_utils.get_nodes_of_namespace(server)
await subscription.subscribe_data_change(nodes)
subscription = await server.create_subscription(1000, erp5_handler)
await subscription.subscribe_events()
nodes = await asyncua.common.ua_utils.get_nodes_of_namespace(server)
await subscription.subscribe_data_change(nodes)
def create_session(name,
user=asyncua.server.users.User(role=asyncua.server.users.UserRole.Anonymous),
external=False):
self = server.iserver
return InternalSession(self, self.aspace, self.subscription_service, name, user=user, external=external)
def create_session(name,
user=asyncua.server.users.User(role=asyncua.server.users.UserRole.Anonymous),
external=False):
self = server.iserver
return InternalSession(self, self.aspace, self.subscription_service, name, user=user, external=external)
server.iserver.create_session = create_session
_logger.info("Added subscription for erp5 handler.")
_logger.info("Starting server!")
server.iserver.create_session = create_session
# start OPCUA server
_logger.info("Starting server!")
async with server:
while True:
await asyncio.sleep(1)
......
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