From fbfb379e86941919782f66ab0f12788f843c63db Mon Sep 17 00:00:00 2001 From: marcsello Date: Wed, 28 Jul 2021 13:35:40 +0200 Subject: [PATCH] Added trigger level --- src/app.py | 18 ++++++++++-------- src/config.py | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/app.py b/src/app.py index ce59acd..33fd089 100644 --- a/src/app.py +++ b/src/app.py @@ -38,20 +38,19 @@ if config.SENTRY_DSN: 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) connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.RABBIT_HOSTNAME, credentials=credentials, heartbeat=30, socket_timeout=45)) channel = connection.channel() - channel.exchange_declare(exchange=config.RABBIT_EXCHANGE, - exchange_type='fanout') + channel.exchange_declare(exchange=config.RABBIT_EXCHANGE, exchange_type='fanout') queue = channel.queue_declare(durable=True, auto_delete=True, queue=uuid.uuid4().urn.split(':')[2], exclusive=True).method.queue channel.queue_bind(exchange=config.RABBIT_EXCHANGE, queue=queue) 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() @@ -71,7 +70,7 @@ def on_message_creator(mqtt_: MQTT): return # TODO: strurnus should not be hardcoded here - if (msg_json['class'] == 'sturnus') and (msg_json['probability'] > 0.51): + if (msg_json['class'] == 'sturnus') and (msg_json['probability'] > config.TRIGGER_LEVEL): r = requests.get(f"http://{config.INPUT_HOSTNAME}/sample/{msg_json['tag']}") r.raise_for_status() if 'device_id' not in r.json(): @@ -86,9 +85,12 @@ def on_message_creator(mqtt_: MQTT): if __name__ == "__main__": - logging.basicConfig(stream=sys.stdout, format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s", - level=logging.DEBUG if '--debug' in sys.argv else logging.INFO) - logging.info("Guard service starting") + logging.basicConfig( + stream=sys.stdout, + format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s", + level=logging.DEBUG if '--debug' in sys.argv else logging.INFO + ) + logging.info("Guard service starting...") mqtt = MQTT() mqtt.topic = config.MQTT_TOPIC mqtt.connect() diff --git a/src/config.py b/src/config.py index 327227c..b5552cd 100644 --- a/src/config.py +++ b/src/config.py @@ -30,3 +30,4 @@ MQTT_PASSWORD = os.getenv("GUARD_MQTT_PASSWORD", "guard-service") MQTT_TOPIC = os.getenv("GUARD_MQTT_TOPIC", "guard-service") INPUT_HOSTNAME = os.getenv("INPUT_SVC_HOSTNAME", "input-service") +TRIGGER_LEVEL = float(os.environ.get("TRIGGER_LEVEL", 0.51))