From 851c098be71368a4410fdf15d3d2af727e0f999e Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Mon, 22 May 2023 14:09:43 +0200 Subject: [PATCH] add subscribe to drone status --- .../api_communication/api_listener.py | 11 +++++++++-- src/api_communication/package.xml | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/api_communication/api_communication/api_listener.py b/src/api_communication/api_communication/api_listener.py index c9f7166b..219a969e 100644 --- a/src/api_communication/api_communication/api_listener.py +++ b/src/api_communication/api_communication/api_listener.py @@ -1,23 +1,30 @@ import rclpy from rclpy.node import Node +from drone_services.msg import DroneStatus + import asyncio import websockets.server -import threading + + class ApiListener(Node): def __init__(self): super().__init__('api_listener') self.get_logger().info('ApiListener node started') - self.messages = 0 + self.drone_status_subscriber = self.create_subscription(DroneStatus, '/drone/status', self.drone_status_callback, 10) self.server = None + def drone_status_callback(self, msg): + self.get_logger().info('Received drone battery and cpu: {0} {1}'.format(msg.battery_percentage,msg.cpu_usage)) + async def spin(self): self.get_logger().info('Starting API') self.server = await websockets.serve(self.api_handler, '0.0.0.0', 9001) self.get_logger().info('API started') await self.server.wait_closed() + async def api_handler(self, websocket): try: while rclpy.ok(): diff --git a/src/api_communication/package.xml b/src/api_communication/package.xml index be9c3731..2f4475b4 100644 --- a/src/api_communication/package.xml +++ b/src/api_communication/package.xml @@ -7,6 +7,7 @@ sem Apache License 2.0 rclpy + drone_services ament_copyright ament_flake8