diff --git a/CMakeLists.txt b/CMakeLists.txt index 55885f0..0a9c56b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,6 @@ find_package(ament_cmake_python REQUIRED) ament_python_install_package(${PROJECT_NAME}) -install(DIRECTORY - front_end - DESTINATION share/${PROJECT_NAME}/ -) - install(DIRECTORY scripts/ DESTINATION lib/${PROJECT_NAME}/lib diff --git a/am_i_up/server.py b/am_i_up/server.py index f21822e..f5cba9e 100644 --- a/am_i_up/server.py +++ b/am_i_up/server.py @@ -8,66 +8,56 @@ # This Source Code Form is "Incompatible With Secondary Licenses", as # defined by the Mozilla Public License, v. 2.0. # +import asyncio from aiohttp import web -import time from ament_index_python import get_package_share_directory +import time import yaml import ipaddress import subprocess import os -from jinja2 import Environment, FileSystemLoader, select_autoescape + +import rclpy +from std_msgs.msg import String def main(): facts = Facts() - view = View(facts) api = Api(facts) + ros = Ros(facts) - app = web.Application() - app.add_routes([ - web.get('/', view.root), - web.get('/ping', view.ping), - web.get('/api/ping', api.ping), - web.get('/api/uptime', api.uptime), - web.get('/api/build_info', api.build_info), - web.get('/api/env', api.env), - web.static('/static', view.get_static_dir()) - ]) - web.run_app(app) + future = asyncio.gather(api.run(), ros.run()) + asyncio.get_event_loop().run_until_complete(future) -class View: + + +class Ros: def __init__(self, facts): + rclpy.init() self._facts = facts - self._env = Environment( - loader=FileSystemLoader(self._find_template_dirs()), - autoescape=select_autoescape() - ) + self._node = rclpy.create_node('am_i_up') - async def root(self, request): - text = self._env.get_template('home.html').render() - return web.Response(text=text, content_type='text/html') + self._node.create_subscription(String, "status", self.status_sub, 1) - async def ping(self, request): - text = self._env.get_template('ping.html').render() - return web.Response(text=text, content_type='text/html') + def status_sub(self, msg): + self._facts.set_status_string(msg.data) - def _find_template_dirs(self): - package_dir = get_package_share_directory('am_i_up') - template_dir = "{}/front_end/templates".format(package_dir) - if not os.path.exists(template_dir): - raise RuntimeError("Could not find template_dir: {}".format(template_dir)) - return [template_dir] + async def run(self): + while rclpy.ok(): + rclpy.spin_once(self._node, timeout_sec=0) + await asyncio.sleep(1e-4) - def get_static_dir(self): - package_dir = get_package_share_directory('am_i_up') - static_dir = "{}/front_end/static".format(package_dir) - if not os.path.exists(static_dir): - raise RuntimeError("Could not find static_dir: {}".format(static_dir)) - return static_dir class Facts: def __init__(self): self._start_time = time.monotonic() + self._status_string = None + + def set_status_string(self, status): + self._status_string = status + + def get_status(self): + return self._status_string def get_uptime(self): return time.monotonic() - self._start_time @@ -122,6 +112,25 @@ class Api: def __init__(self, facts): self._facts = facts + async def run(self): + app = web.Application() + app.add_routes([ + web.get('/api/ping', self.ping), + web.get('/api/uptime', self.uptime), + web.get('/api/build_info', self.build_info), + web.get('/api/env', self.env), + web.get('/api/status', self.status) + ]) + + runner = web.AppRunner(app) + await runner.setup() + site = web.TCPSite(runner, 'localhost', 8080) + await site.start() + + while rclpy.ok(): + await asyncio.sleep(3600) + await runner.cleanup() + async def ping(self, request): request_dict = await request.json() result = self._facts.do_ping(request_dict['address']) @@ -144,6 +153,13 @@ class Api: env = self._facts.get_env() return web.json_response(env) + async def status(self, request): + status = self._facts.get_status() + if not status: + status = "Nothing received!" + resp = {"message": status} + return web.json_response(resp) + def is_valid_ip(address): try: ipaddress.ip_address(address) diff --git a/front_end/static/css/base.css b/front_end/static/css/base.css deleted file mode 100644 index ffc40df..0000000 --- a/front_end/static/css/base.css +++ /dev/null @@ -1,6 +0,0 @@ -.padded { - padding-left: 10px; - padding-right: 10px; - padding-top: 10px; - padding-bottom: 10px; -} diff --git a/front_end/templates/base.html b/front_end/templates/base.html deleted file mode 100644 index c882a6a..0000000 --- a/front_end/templates/base.html +++ /dev/null @@ -1,39 +0,0 @@ - - -
- - - - - - - - - -Home
-{% endblock %} diff --git a/front_end/templates/ping.html b/front_end/templates/ping.html deleted file mode 100644 index 2e9abe1..0000000 --- a/front_end/templates/ping.html +++ /dev/null @@ -1,5 +0,0 @@ -{% extends "base.html" %} - -{% block content %} -Ping
-{% endblock %}