changeset 1745:d90cb7c06f15

try to crash if mqtt doesn't connect
author drewp@bigasterisk.com
date Thu, 09 Nov 2023 17:21:59 -0800
parents 09df2b4b886f
children 3d69222680c8
files service/rdf_to_mqtt/rdf_to_mqtt.py
diffstat 1 files changed, 10 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/service/rdf_to_mqtt/rdf_to_mqtt.py	Thu Nov 09 17:21:33 2023 -0800
+++ b/service/rdf_to_mqtt/rdf_to_mqtt.py	Thu Nov 09 17:21:59 2023 -0800
@@ -112,6 +112,7 @@
         await self._publish(topic=attrs['root'], message=msg)
 
     async def _publishRgbw(self, attrs, brightness):
+        log.info(f'_publishRgbw {attrs=} {brightness}')
         for chan, scale in [('w1', 1), ('r', 1), ('g', .8), ('b', .8)]:
             await self._publish(topic=f"{attrs['root']}/light/kit_{chan}/command", messageJson={'state': 'ON', 'brightness': int(brightness * 255)})
 
@@ -129,8 +130,13 @@
         log.debug(f'mqtt.publish {topic} {message} {messageJson}')
         if messageJson is not None:
             message = json.dumps(messageJson)
-        assert mqtt is not None
-        await mqtt.publish(topic, message)
+        if mqtt is None:
+            os.abort()
+        try:
+            await mqtt.publish(topic, message)
+        except aiomqtt.error.MqttCodeError:
+            log.error(f"publish {topic=} {message=} failed:", exc_info=1)
+            os.abort()
 
 
 def main():
@@ -152,10 +158,10 @@
                         Route('/', StaticFiles(directory='.', html=True)),
                         Route("/output", OutputPage().put, methods=["PUT"]),
                     ])
-    app.add_middleware(PrometheusMiddleware, app_name='environment')
+    app.add_middleware(PrometheusMiddleware, app_name='rdf_to_mqtt')
     app.add_route("/metrics", handle_metrics)
     log.info('return app')
     return app
 
 
-app = main()
\ No newline at end of file
+app = main()