This commit is contained in:
45
src/app.py
45
src/app.py
@ -1,8 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
import logging
|
||||
import json
|
||||
import sentry_sdk
|
||||
|
||||
from config import *
|
||||
import pika
|
||||
import requests
|
||||
import config
|
||||
from mqtt_helper import MQTT
|
||||
|
||||
"""
|
||||
Main entry point
|
||||
@ -13,13 +15,40 @@ __copyright__ = "Copyright 2020, Birbnetes Team"
|
||||
__module_name__ = "app"
|
||||
__version__text__ = "1"
|
||||
|
||||
if SENTRY_DSN:
|
||||
if config.SENTRY_DSN:
|
||||
sentry_sdk.init(
|
||||
dsn=SENTRY_DSN,
|
||||
dsn=config.SENTRY_DSN,
|
||||
send_default_pii=True,
|
||||
release=RELEASE_ID,
|
||||
environment=RELEASEMODE
|
||||
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__":
|
||||
pass
|
||||
mqtt = MQTT()
|
||||
mqtt.connect()
|
||||
setup_rabbit()
|
||||
|
Reference in New Issue
Block a user