This commit is contained in:
parent
caf9e1c0f3
commit
4d91a3f6ce
@ -35,6 +35,7 @@ if config.SENTRY_DSN:
|
||||
|
||||
|
||||
def setup_rabbit() -> None:
|
||||
logging.info("Connecting to RabbitMQ")
|
||||
credentials = pika.PlainCredentials(config.RABBIT_USERNAME, config.RABBIT_PASSWORD)
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.RABBIT_HOSTNAME,
|
||||
credentials=credentials,
|
||||
@ -49,6 +50,7 @@ def setup_rabbit() -> None:
|
||||
exclusive=True).method.queue
|
||||
channel.queue_bind(exchange=config.RABBIT_EXCHANGE, queue=queue)
|
||||
channel.basic_consume(queue=queue, on_message_callback=on_message, auto_ack=True)
|
||||
logging.debug("Starting consumption")
|
||||
channel.start_consuming()
|
||||
|
||||
|
||||
@ -61,11 +63,13 @@ def on_message(channel, method_frame, header_frame, body):
|
||||
r.raise_for_status()
|
||||
if 'device_id' not in r.json():
|
||||
logging.error("Input-service response invalid")
|
||||
logging.info(f"Sending alert command to device {r.json()['device_id']}")
|
||||
mqtt.publish(subtopic=r.json()['device_id'],
|
||||
message=json.dumps({"command": "doAlert"}))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.info("Guard service starting")
|
||||
mqtt = MQTT()
|
||||
mqtt.topic = config.MQTT_TOPIC
|
||||
mqtt.connect()
|
||||
|
@ -58,6 +58,7 @@ class MQTT:
|
||||
Setup client and connect to broker
|
||||
:return:
|
||||
"""
|
||||
logging.info("Connecting to MQTT")
|
||||
self.client = mqtt.Client(client_id=self.client_id, clean_session=True, userdata=None, protocol=mqtt.MQTTv311,
|
||||
transport="tcp")
|
||||
self.port = int(self.port)
|
||||
@ -70,6 +71,7 @@ class MQTT:
|
||||
:param subtopic:
|
||||
:return:
|
||||
"""
|
||||
logging.debug(f"MQTT Topic: {self.topic}/{subtopic} Message: {message} QOS: {self.qos} Retain: {self.retain}")
|
||||
if subtopic:
|
||||
self.client.publish(f"{self.topic}/{subtopic}", message, qos=self.qos, retain=self.retain)
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user