add callback for service response
This commit is contained in:
@@ -10,6 +10,7 @@ import websockets.server
|
||||
import threading
|
||||
import json
|
||||
from enum import Enum
|
||||
from functools import partial
|
||||
|
||||
# communication: client always sends commands that have a command id.
|
||||
# server always sends messages back that have a message type
|
||||
@@ -120,15 +121,23 @@ class ApiListener(Node):
|
||||
self.get_logger().info(
|
||||
f'Filename: {message_json["filename"]}')
|
||||
self.take_picture_request.input_name = message_json['filename']
|
||||
self.future = self.take_picture_client.call_async(self.take_picture_request)
|
||||
rclpy.spin_until_future_complete(self, self.future)
|
||||
result_filename = self.future.result()
|
||||
self.get_logger().info("Received result filename: " + result_filename)
|
||||
with open(result_filename, 'rb') as f:
|
||||
self.get_logger().info('Reading image')
|
||||
image_data = f.read()
|
||||
self.message_queue.append(json.dumps(
|
||||
{'type': ResponseMessage.IMAGE.name, 'image': image_data}))
|
||||
self.get_logger().info('Calling take picture service')
|
||||
future = self.take_picture_client.call_async(self.take_picture_request)
|
||||
future.add_done_callback(partial(self.image_request_callback))
|
||||
|
||||
|
||||
def image_request_callback(self, future):
|
||||
try:
|
||||
result_filename = future.result()
|
||||
self.get_logger().info("Received result filename: " + result_filename)
|
||||
with open(result_filename, 'rb') as f:
|
||||
self.get_logger().info('Reading image')
|
||||
image_data = f.read()
|
||||
self.message_queue.append(json.dumps(
|
||||
{'type': ResponseMessage.IMAGE.name, 'image': image_data}))
|
||||
except Exception as e:
|
||||
self.get_logger().error('Something went wrong while sending a take picture request and waiting for the response: %r' % (e))
|
||||
|
||||
|
||||
def send_available_commands(self):
|
||||
print('Sending available commands')
|
||||
@@ -228,7 +237,7 @@ class ApiListener(Node):
|
||||
self.get_logger().error('Received unknown command ' + str(message_json['command']) )
|
||||
self.send_available_commands()
|
||||
except TypeError:
|
||||
self.get_logger().error('Received unknown type')
|
||||
self.get_logger().error('Received unknown type: ' +str(type(message)) + " " + str(message))
|
||||
self.send_available_commands()
|
||||
except json.JSONDecodeError:
|
||||
self.get_logger().error('Received invalid JSON')
|
||||
|
||||
Reference in New Issue
Block a user