From 0aa83798628638f6b04099f99613ee9b3dd642fb Mon Sep 17 00:00:00 2001
From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr>
Date: Wed, 15 Jan 2025 16:08:37 +0100
Subject: [PATCH] Fix the web app not starting

Once again, Py changed the asyncio API. It takes me a while to figure
out why this simple piece of code doesn't work.

In fact, if not provided aiohttp create another loop.
---
 libs/monitor/tests/test_aiohttp.py | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/libs/monitor/tests/test_aiohttp.py b/libs/monitor/tests/test_aiohttp.py
index 5a994a11..05607cfe 100644
--- a/libs/monitor/tests/test_aiohttp.py
+++ b/libs/monitor/tests/test_aiohttp.py
@@ -5,10 +5,9 @@ Open http://localhost:4444/
 """
 
 from aiohttp import web
-import asyncio
 import platform
-from xaal.lib.asyncio import AsyncEngine
-from xaal.lib import helpers,tools
+from xaal.lib import AsyncEngine
+from xaal.lib import helpers, tools
 from xaal.monitor import Monitor
 from xaal.schemas import devices
 
@@ -16,27 +15,35 @@ PORT = 4444
 mon = None
 routes = web.RouteTableDef()
 
+
 @routes.get('/')
 async def dev_list(request):
+    assert mon is not None
     r = '<ul>'
     for dev in mon.devices:
-        r = r+ f'<li><a href="/devices/{dev.address}"><tt>{dev.address}</tt></a> {dev.dev_type}</li>'
-    r = r+'<ul>'
+        r = r + f'<li><a href="/devices/{dev.address}"><tt>{dev.address}</tt></a> {dev.dev_type}</li>'
+    r = r + '<ul>'
     return html("Devices list<br>%s" % r)
 
+
 @routes.get('/devices/{addr}')
 async def dev_info(request):
+    assert mon is not None
     addr = tools.get_uuid(request.match_info['addr'])
     dev = mon.devices.get_with_addr(addr)
     r = ''
     if dev:
-        return html(f"<h1>Device: {dev.address} / {dev.dev_type}</h1> <h2>Attributes</h2><pre>{dev.attributes}</pre> <h2>Description</h2><pre>{dev.description}</pre>")
+        return html(
+            f"<h1>Device: {dev.address} / {dev.dev_type}</h1> <h2>Attributes</h2><pre>{dev.attributes}</pre> <h2>Description</h2><pre>{dev.description}</pre>"
+        )
     else:
         return html('device not found')
 
+
 def html(content):
     content = f'<html><body>\n{content}\n</body></html>'
-    return web.Response(text=content,content_type='text/html')
+    return web.Response(text=content, content_type='text/html')
+
 
 # setup log & Engine
 helpers.setup_console_logger()
@@ -50,9 +57,10 @@ eng.add_device(dev)
 
 # Let's start
 mon = Monitor(dev)
-asyncio.ensure_future(eng.run())
+eng.start()
+# eng.get_loop().set_debug(True)
 
 # Web apps..
 app = web.Application()
 app.add_routes(routes)
-web.run_app(app, port=PORT)
+web.run_app(app, port=PORT, loop=eng.get_loop())
-- 
GitLab