feat: better logging that keeps input prompt
This commit is contained in:
parent
bb41e4d8ed
commit
8eed050252
@ -40,17 +40,21 @@ if not DEBUG:
|
|||||||
GPIO.setup(LED_PIN, GPIO.OUT)
|
GPIO.setup(LED_PIN, GPIO.OUT)
|
||||||
|
|
||||||
|
|
||||||
|
def prompt_logging(message, prompt=PROMPT):
|
||||||
|
print("\r" + message + "\n", end=PROMPT, flush=True)
|
||||||
|
|
||||||
|
|
||||||
# Callback when connection is accidentally lost.
|
# Callback when connection is accidentally lost.
|
||||||
def on_connection_interrupted(connection, error, **kwargs):
|
def on_connection_interrupted(connection, error, **kwargs):
|
||||||
print("Connection interrupted. error: {}".format(error))
|
prompt_logging("Connection interrupted. error: {}".format(error))
|
||||||
|
|
||||||
|
|
||||||
# Callback when an interrupted connection is re-established.
|
# Callback when an interrupted connection is re-established.
|
||||||
def on_connection_resumed(connection, return_code, session_present, **kwargs):
|
def on_connection_resumed(connection, return_code, session_present, **kwargs):
|
||||||
print("Connection resumed. return_code: {} session_present: {}".format(return_code, session_present))
|
prompt_logging("Connection resumed. return_code: {} session_present: {}".format(return_code, session_present))
|
||||||
|
|
||||||
if return_code == mqtt.ConnectReturnCode.ACCEPTED and not session_present:
|
if return_code == mqtt.ConnectReturnCode.ACCEPTED and not session_present:
|
||||||
print("Session did not persist. Resubscribing to existing topics...")
|
prompt_logging("Session did not persist. Resubscribing to existing topics...")
|
||||||
resubscribe_future, _ = connection.resubscribe_existing_topics()
|
resubscribe_future, _ = connection.resubscribe_existing_topics()
|
||||||
|
|
||||||
# Cannot synchronously wait for resubscribe result because we're on the connection's event-loop thread,
|
# Cannot synchronously wait for resubscribe result because we're on the connection's event-loop thread,
|
||||||
@ -60,11 +64,11 @@ def on_connection_resumed(connection, return_code, session_present, **kwargs):
|
|||||||
|
|
||||||
def on_resubscribe_complete(resubscribe_future):
|
def on_resubscribe_complete(resubscribe_future):
|
||||||
resubscribe_results = resubscribe_future.result()
|
resubscribe_results = resubscribe_future.result()
|
||||||
print("Resubscribe results: {}".format(resubscribe_results))
|
prompt_logging("Resubscribe results: {}".format(resubscribe_results))
|
||||||
|
|
||||||
for topic, qos in resubscribe_results['topics']:
|
for topic, qos in resubscribe_results['topics']:
|
||||||
if qos is None:
|
if qos is None:
|
||||||
print("Server rejected resubscribe to topic: {}".format(topic))
|
prompt_logging("Server rejected resubscribe to topic: {}".format(topic))
|
||||||
|
|
||||||
|
|
||||||
# Callback when the subscribed topic receives a message
|
# Callback when the subscribed topic receives a message
|
||||||
@ -79,6 +83,7 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
|
|||||||
|
|
||||||
def parse_led_payload(payload):
|
def parse_led_payload(payload):
|
||||||
data = json.loads(payload.decode())
|
data = json.loads(payload.decode())
|
||||||
|
prompt_logging(f"detected faces: {data.get('faces')}")
|
||||||
if data.get("faces"):
|
if data.get("faces"):
|
||||||
led_control(True)
|
led_control(True)
|
||||||
else:
|
else:
|
||||||
@ -86,7 +91,7 @@ def parse_led_payload(payload):
|
|||||||
|
|
||||||
|
|
||||||
def led_control(light):
|
def led_control(light):
|
||||||
print(["Light off", "Light on"][light])
|
prompt_logging(["Light off", "Light on"][light])
|
||||||
|
|
||||||
if not DEBUG:
|
if not DEBUG:
|
||||||
GPIO.output(LED_PIN, GPIO.HIGH if light else GPIO.LOW)
|
GPIO.output(LED_PIN, GPIO.HIGH if light else GPIO.LOW)
|
||||||
@ -107,7 +112,7 @@ def take_picture():
|
|||||||
def post_presigned_s3_url(data, img_bytes):
|
def post_presigned_s3_url(data, img_bytes):
|
||||||
fn = data['fields']['key']
|
fn = data['fields']['key']
|
||||||
res = requests.post(data['url'], data=data['fields'], files={ "file": (fn, img_bytes) })
|
res = requests.post(data['url'], data=data['fields'], files={ "file": (fn, img_bytes) })
|
||||||
print(f"Put to S3. status code: {res.status_code}")
|
prompt_logging(f"Put to S3. status code: {res.status_code}")
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
@ -137,7 +142,7 @@ async def main(loop):
|
|||||||
)
|
)
|
||||||
|
|
||||||
subscribe_result = subscribe_future.result()
|
subscribe_result = subscribe_future.result()
|
||||||
print(subscribe_result)
|
# print(subscribe_result)
|
||||||
print(f"Subscribed to {LED_TOPIC}")
|
print(f"Subscribed to {LED_TOPIC}")
|
||||||
|
|
||||||
subscribe_future, packet_id = mqtt_connection.subscribe(
|
subscribe_future, packet_id = mqtt_connection.subscribe(
|
||||||
@ -147,11 +152,11 @@ async def main(loop):
|
|||||||
)
|
)
|
||||||
|
|
||||||
subscribe_result = subscribe_future.result()
|
subscribe_result = subscribe_future.result()
|
||||||
print(subscribe_result)
|
# print(subscribe_result)
|
||||||
print(f"Subscribed to {S3_URL_TOPIC}")
|
print(f"Subscribed to {S3_URL_TOPIC}")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
command = input("> ")
|
command = input(PROMPT)
|
||||||
|
|
||||||
if command == "p":
|
if command == "p":
|
||||||
filename = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d-%H-%M-%S") + FORMAT
|
filename = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d-%H-%M-%S") + FORMAT
|
||||||
@ -161,7 +166,7 @@ async def main(loop):
|
|||||||
qos=mqtt.QoS.AT_MOST_ONCE
|
qos=mqtt.QoS.AT_MOST_ONCE
|
||||||
)
|
)
|
||||||
publish_result = publish_future.result()
|
publish_result = publish_future.result()
|
||||||
print(publish_result)
|
# print(publish_result)
|
||||||
print(f"Upload request {filename} sent!")
|
print(f"Upload request {filename} sent!")
|
||||||
elif command == "q":
|
elif command == "q":
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user