From c37017d291671b61ffc80c65364eb44e842372ce Mon Sep 17 00:00:00 2001 From: Tony Yang Date: Mon, 1 May 2023 15:10:04 +0800 Subject: [PATCH] fix: runtime errors --- led_control.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/led_control.py b/led_control.py index 2a86409..35bde9b 100644 --- a/led_control.py +++ b/led_control.py @@ -1,7 +1,9 @@ +import sys import json import uuid import asyncio import datetime +import traceback import cv2 import requests @@ -76,7 +78,7 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs): def parse_led_payload(payload): - data = json.loads(payload) + data = json.loads(payload.decode()) if data.get("faces"): led_control(True) else: @@ -91,7 +93,7 @@ def led_control(light): def parse_s3_url_payload(payload): - data = json.dumps(payload) + data = json.loads(payload.decode()) put_presigned_s3_url(data["url"], take_picture()) @@ -136,6 +138,7 @@ async def main(): ) subscribe_result = subscribe_future.result() + print(subscribe_result) print(f"Subscribed to {LED_TOPIC}") subscribe_future, packet_id = mqtt_connection.subscribe( @@ -145,7 +148,8 @@ async def main(): ) subscribe_result = subscribe_future.result() - print(f"Subscribed to {LED_TOPIC}") + print(subscribe_result) + print(f"Subscribed to {S3_URL_TOPIC}") while True: command = input("> ") @@ -154,17 +158,28 @@ async def main(): filename = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d-%H-%M-%S") + "." + FORMAT publish_future, packet_id = mqtt_connection.publish( topic=S3_UPLOAD_TOPIC, - qos=1, - payload=json.dumps({ "filename": filename }) + payload=json.dumps({ "filename": filename }), + qos=mqtt.QoS.AT_LEAST_ONCE ) publish_result = publish_future.result() + print(publish_result) print(f"Upload request {filename} sent!") + elif command == "q": + break +def handler(loop, context): + e = context['exception'] + print(*traceback.format_exception(None, e, e.__traceback__), file=sys.stderr, flush=True) + if not DEBUG: + GPIO.cleanup() + + loop.stop() + # run loop = asyncio.get_event_loop() -loop.set_exception_handler(lambda loop, context: [print(context['message']), GPIO.cleanup()]) +loop.set_exception_handler(handler) loop.create_task(main()) -loop.run_forever() \ No newline at end of file +loop.run_forever()