Skip to content
Snippets Groups Projects
Commit 2267f36e authored by jkerdreu's avatar jkerdreu
Browse files

Added test w/ asyncio (aiohttp)



git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2706 b32b6428-25c9-4566-ad07-03861ab6144f
parent ccf98c8d
Branches
No related tags found
No related merge requests found
"""
Simple web-frontend that use aiohttp
- pip install aiohttp
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.monitor import Monitor
from xaal.schemas import devices
PORT = 4444
mon = None
routes = web.RouteTableDef()
@routes.get('/')
async def dev_list(request):
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>'
return html("Devices list<br>%s" % r)
@routes.get('/devices/{addr}')
async def dev_info(request):
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>")
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')
# setup log & Engine
helpers.setup_console_logger()
eng = AsyncEngine()
# Monitoring device
dev = devices.hmi()
dev.info = "AioHTTP Monitor example"
dev.url = f'http://{platform.node()}:{PORT}'
eng.add_device(dev)
# Let's start
mon = Monitor(dev)
asyncio.ensure_future(eng.run())
# Web apps..
app = web.Application()
app.add_routes(routes)
web.run_app(app, port=PORT)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment