comparison service/rdf_to_mqtt/rdf_to_mqtt.py @ 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
comparison
equal deleted inserted replaced
1744:09df2b4b886f 1745:d90cb7c06f15
110 if brightness > 0: 110 if brightness > 0:
111 msg = 'ON' 111 msg = 'ON'
112 await self._publish(topic=attrs['root'], message=msg) 112 await self._publish(topic=attrs['root'], message=msg)
113 113
114 async def _publishRgbw(self, attrs, brightness): 114 async def _publishRgbw(self, attrs, brightness):
115 log.info(f'_publishRgbw {attrs=} {brightness}')
115 for chan, scale in [('w1', 1), ('r', 1), ('g', .8), ('b', .8)]: 116 for chan, scale in [('w1', 1), ('r', 1), ('g', .8), ('b', .8)]:
116 await self._publish(topic=f"{attrs['root']}/light/kit_{chan}/command", messageJson={'state': 'ON', 'brightness': int(brightness * 255)}) 117 await self._publish(topic=f"{attrs['root']}/light/kit_{chan}/command", messageJson={'state': 'ON', 'brightness': int(brightness * 255)})
117 118
118 async def _publishFrontScreenText(self, stmt): 119 async def _publishFrontScreenText(self, stmt):
119 ignored = True 120 ignored = True
127 @MQTT_PUBLISH.time() 128 @MQTT_PUBLISH.time()
128 async def _publish(self, topic: str, messageJson: object = None, message: str | None = None): 129 async def _publish(self, topic: str, messageJson: object = None, message: str | None = None):
129 log.debug(f'mqtt.publish {topic} {message} {messageJson}') 130 log.debug(f'mqtt.publish {topic} {message} {messageJson}')
130 if messageJson is not None: 131 if messageJson is not None:
131 message = json.dumps(messageJson) 132 message = json.dumps(messageJson)
132 assert mqtt is not None 133 if mqtt is None:
133 await mqtt.publish(topic, message) 134 os.abort()
135 try:
136 await mqtt.publish(topic, message)
137 except aiomqtt.error.MqttCodeError:
138 log.error(f"publish {topic=} {message=} failed:", exc_info=1)
139 os.abort()
134 140
135 141
136 def main(): 142 def main():
137 143
138 async def start2(): 144 async def start2():
150 on_startup=[start], 156 on_startup=[start],
151 routes=[ 157 routes=[
152 Route('/', StaticFiles(directory='.', html=True)), 158 Route('/', StaticFiles(directory='.', html=True)),
153 Route("/output", OutputPage().put, methods=["PUT"]), 159 Route("/output", OutputPage().put, methods=["PUT"]),
154 ]) 160 ])
155 app.add_middleware(PrometheusMiddleware, app_name='environment') 161 app.add_middleware(PrometheusMiddleware, app_name='rdf_to_mqtt')
156 app.add_route("/metrics", handle_metrics) 162 app.add_route("/metrics", handle_metrics)
157 log.info('return app') 163 log.info('return app')
158 return app 164 return app
159 165
160 166