#!/usr/bin/env python3 import json import sentry_sdk import pika import requests import config from mqtt_helper import MQTT """ Main entry point """ __author__ = "@tormakris" __copyright__ = "Copyright 2020, Birbnetes Team" __module_name__ = "app" __version__text__ = "1" if config.SENTRY_DSN: sentry_sdk.init( dsn=config.SENTRY_DSN, send_default_pii=True, release=config.RELEASE_ID, environment=config.RELEASEMODE ) def setup_rabbit() -> None: credentials = pika.PlainCredentials(config.RABBIT_USERNAME, config.RABBIT_PASSWORD) connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.RABBIT_HOSTNAME, credentials=credentials, heartbeat=0, socket_timeout=5)) channel = connection.channel() exchange = channel.exchange_declare(exchange=config.RABBIT_EXCHANGE, exchange_type='fanout', durable=True, auto_delete=False) queue = channel.queue_declare(durable=True, auto_delete=False) queue.bind(exchange) queue.basic_consume(on_message, no_ack=True) def on_message(channel, method_frame, header_frame, body): msg_json = json.loads(body) if msg_json['probability'] > 0.5: r = requests.get(f"http://{config.INPUT_HOSTNAME}/sample/{msg_json['tag']}") r.raise_for_status() mqtt.publish(json.dumps({"deviceID": r.json()['device_id'], "sensorID": "", "command": "doAlert"})) if __name__ == "__main__": mqtt = MQTT() mqtt.topic = config.MQTT_TOPIC mqtt.connect() setup_rabbit()