From 48e1ce8f5b163ad279b483a162fb4baed6f55b9b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 30 May 2023 16:18:22 +0200 Subject: [PATCH] try --- api/index.js | 23 +++++++++++-------- .../api_communication/api_listener.py | 14 +++++++---- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/api/index.js b/api/index.js index bd1960bb..8a7df15f 100644 --- a/api/index.js +++ b/api/index.js @@ -23,15 +23,20 @@ var connect_to_api = function () { }); ws.on("message", function message(message) { - var msg = JSON.parse(message); - if (msg.type == "STATUS") { - last_status = msg.data; - } else if (msg.type == "IMAGE") { - console.log("got picture"); - // console.log(msg.image); - last_image = msg.image; - received_picture = true; - } + try { + var msg = JSON.parse(message); + if (msg.type == "STATUS") { + last_status = msg.data; + } else if (msg.type == "IMAGE") { + console.log("got picture"); + // console.log(msg.image); + last_image = msg.image; + received_picture = true; + } + } catch (error) { + console.log("could not parse as json") + + } }); ws.on("error", function error(err) { diff --git a/src/api_communication/api_communication/api_listener.py b/src/api_communication/api_communication/api_listener.py index ab2259cb..5832d205 100644 --- a/src/api_communication/api_communication/api_listener.py +++ b/src/api_communication/api_communication/api_listener.py @@ -90,7 +90,11 @@ class ApiListener(Node): def publish_message(self, message): # self.get_logger().info(f'Publishing message: {message}') if self.websocket is not None: - asyncio.run(self.websocket.send(message)) + try: + asyncio.run(self.websocket.send(message)) + except Exception as e: + self.get_logger().error( + 'Something went wrong while sending a message to the websocket: ' + str(e)) else: self.get_logger().error('Trying to publish message but no websocket connection') @@ -134,12 +138,12 @@ class ApiListener(Node): with open(result_filename, 'rb') as f: self.get_logger().info('Reading image') read_file = f.read() - # base64_img = base64.b64encode(read_file) - # self.message_queue.append(json.dumps( - # {'type': ResponseMessage.IMAGE.name, 'image': base64_img.decode('utf-8')})) + base64_img = base64.b64encode(read_file) + self.message_queue.append(json.dumps( + {'type': ResponseMessage.IMAGE.name, 'image': base64_img.decode('utf-8')})) # send image as binary file - self.message_queue.append(read_file) + # self.message_queue.append(read_file) except Exception as e: self.get_logger().error('Something went wrong while sending a take picture request and waiting for the response: %r' % (e))