Fixed missing reference to mqtt
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pünkösd Marcell 2020-09-19 17:01:41 +02:00
parent ab9c9fe40e
commit 41daa413f5

View File

@ -35,7 +35,7 @@ if config.SENTRY_DSN:
) )
def setup_rabbit() -> None: def setup_rabbit(mqtt_: MQTT) -> None:
logging.info("Connecting to RabbitMQ") logging.info("Connecting to RabbitMQ")
credentials = pika.PlainCredentials(config.RABBIT_USERNAME, config.RABBIT_PASSWORD) credentials = pika.PlainCredentials(config.RABBIT_USERNAME, config.RABBIT_PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.RABBIT_HOSTNAME, connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.RABBIT_HOSTNAME,
@ -48,11 +48,16 @@ def setup_rabbit() -> None:
queue = channel.queue_declare(durable=True, auto_delete=True, queue=uuid.uuid4().urn.split(':')[2], queue = channel.queue_declare(durable=True, auto_delete=True, queue=uuid.uuid4().urn.split(':')[2],
exclusive=True).method.queue exclusive=True).method.queue
channel.queue_bind(exchange=config.RABBIT_EXCHANGE, queue=queue) channel.queue_bind(exchange=config.RABBIT_EXCHANGE, queue=queue)
channel.basic_consume(queue=queue, on_message_callback=on_message, auto_ack=True) channel.basic_consume(queue=queue, on_message_callback=on_message_creator(mqtt_), auto_ack=True)
logging.debug("Starting consumption") logging.debug("Starting consumption")
channel.start_consuming() channel.start_consuming()
def on_message_creator(mqtt_: MQTT):
"""
This generator is used, so that the mqtt object can be injected just when the callback is registered
"""
def on_message(channel, method_frame, header_frame, body): def on_message(channel, method_frame, header_frame, body):
msg_json = json.loads(body) msg_json = json.loads(body)
if 'probability' not in msg_json: if 'probability' not in msg_json:
@ -63,9 +68,11 @@ def on_message(channel, method_frame, header_frame, body):
if 'device_id' not in r.json(): if 'device_id' not in r.json():
logging.error("Input-service response invalid") logging.error("Input-service response invalid")
logging.info(f"Sending alert command to device {r.json()['device_id']}") logging.info(f"Sending alert command to device {r.json()['device_id']}")
mqtt.publish(subtopic=r.json()['device_id'], mqtt_.publish(subtopic=r.json()['device_id'],
message=json.dumps({"command": "doAlert"})) message=json.dumps({"command": "doAlert"}))
return on_message
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(stream=sys.stdout, format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s", logging.basicConfig(stream=sys.stdout, format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s",
@ -74,4 +81,4 @@ if __name__ == "__main__":
mqtt = MQTT() mqtt = MQTT()
mqtt.topic = config.MQTT_TOPIC mqtt.topic = config.MQTT_TOPIC
mqtt.connect() mqtt.connect()
setup_rabbit() setup_rabbit(mqtt)