Fixed missing reference to mqtt
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ab9c9fe40e
commit
41daa413f5
37
src/app.py
37
src/app.py
@ -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,23 +48,30 @@ 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(channel, method_frame, header_frame, body):
|
def on_message_creator(mqtt_: MQTT):
|
||||||
msg_json = json.loads(body)
|
"""
|
||||||
if 'probability' not in msg_json:
|
This generator is used, so that the mqtt object can be injected just when the callback is registered
|
||||||
logging.error("Malformed message from broker")
|
"""
|
||||||
if msg_json['probability'] > 0.5:
|
|
||||||
r = requests.get(f"http://{config.INPUT_HOSTNAME}/sample/{msg_json['tag']}")
|
def on_message(channel, method_frame, header_frame, body):
|
||||||
r.raise_for_status()
|
msg_json = json.loads(body)
|
||||||
if 'device_id' not in r.json():
|
if 'probability' not in msg_json:
|
||||||
logging.error("Input-service response invalid")
|
logging.error("Malformed message from broker")
|
||||||
logging.info(f"Sending alert command to device {r.json()['device_id']}")
|
if msg_json['probability'] > 0.5:
|
||||||
mqtt.publish(subtopic=r.json()['device_id'],
|
r = requests.get(f"http://{config.INPUT_HOSTNAME}/sample/{msg_json['tag']}")
|
||||||
message=json.dumps({"command": "doAlert"}))
|
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"}))
|
||||||
|
|
||||||
|
return on_message
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user