2020-10-19 19:11:18 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import json
|
2020-10-20 00:45:08 +02:00
|
|
|
from datetime import datetime
|
2020-10-19 19:11:18 +02:00
|
|
|
import sentry_sdk
|
|
|
|
from sentry_sdk.integrations.logging import LoggingIntegration
|
|
|
|
import paho.mqtt.client
|
|
|
|
import requests
|
|
|
|
|
|
|
|
"""
|
|
|
|
Main Entrypoint
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = "@tormakris"
|
|
|
|
__copyright__ = "Copyright 2020, Birbnetes Team"
|
|
|
|
__module_name__ = "app"
|
|
|
|
__version__text__ = "1"
|
|
|
|
|
|
|
|
SENTRY_DSN = os.environ.get("SENTRY_DSN")
|
|
|
|
RELEASE_ID = os.environ.get("RELEASE_ID", "test")
|
|
|
|
RELEASEMODE = os.environ.get("RELEASEMODE", "dev")
|
|
|
|
|
|
|
|
DEVICE_ID = os.environ.get("DEVICE_ID", "devraspi")
|
|
|
|
|
2020-10-20 00:45:08 +02:00
|
|
|
MQTT_USERNAME = "birbnetes"
|
|
|
|
MQTT_PASSWORD = "de4d2182"
|
2021-07-22 17:54:33 +02:00
|
|
|
MQTT_HOSTNAME = "10.6.6.7"
|
|
|
|
MQTT_PORT = 1884
|
2020-10-19 19:11:18 +02:00
|
|
|
|
|
|
|
STARTTIME = time.time()
|
|
|
|
ENDTIME = time.time()
|
|
|
|
|
2021-07-22 17:54:33 +02:00
|
|
|
URL = "http://10.6.6.7:8071/sample"
|
|
|
|
FILE = 'wwBVaeBZJteIXroQYRTPTUyEOXqH7C.positive.wav'
|
2020-10-19 19:11:18 +02:00
|
|
|
|
|
|
|
sentry_logging = LoggingIntegration(
|
|
|
|
level=logging.INFO,
|
|
|
|
event_level=logging.ERROR
|
|
|
|
)
|
|
|
|
|
|
|
|
if SENTRY_DSN:
|
|
|
|
sentry_sdk.init(
|
|
|
|
dsn=SENTRY_DSN,
|
|
|
|
integrations=[sentry_logging],
|
|
|
|
traces_sample_rate=1.0,
|
|
|
|
send_default_pii=True,
|
|
|
|
release=RELEASE_ID,
|
|
|
|
environment=RELEASEMODE,
|
|
|
|
_experiments={"auto_enabling_integrations": True}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def mqtt_on_connect(client, userdata, flags, rc):
|
2020-10-20 00:45:08 +02:00
|
|
|
global STARTTIME
|
|
|
|
client.subscribe(f"command/10")
|
2020-10-19 19:11:18 +02:00
|
|
|
logging.info("Sending Message")
|
|
|
|
files = {
|
|
|
|
"file": (
|
|
|
|
os.path.basename(FILE), open(FILE, 'rb').read(), 'audio/wave', {'Content-length': os.path.getsize(FILE)}),
|
2020-10-20 00:45:08 +02:00
|
|
|
"description": (None, json.dumps({'date': datetime.now().isoformat(), 'device_id': '10'}), "application/json")
|
2020-10-19 19:11:18 +02:00
|
|
|
}
|
2021-07-22 17:54:33 +02:00
|
|
|
r = requests.post(URL, files=files)
|
|
|
|
logging.info(f"Upload complete: {r.status_code}")
|
2020-10-19 19:11:18 +02:00
|
|
|
STARTTIME = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
def mqtt_on_command(client, userdata, message):
|
2020-10-20 00:45:08 +02:00
|
|
|
global ENDTIME
|
2020-10-19 19:11:18 +02:00
|
|
|
try:
|
|
|
|
msg = json.loads(message.payload.decode())
|
|
|
|
except (UnicodeError, json.JSONDecodeError) as e:
|
|
|
|
logging.error(f"MQTT Invalid message recieved: {e}")
|
|
|
|
return
|
|
|
|
|
|
|
|
if msg.get("command") == 'doAlert':
|
|
|
|
ENDTIME = time.time()
|
|
|
|
elapsed = ENDTIME - STARTTIME
|
|
|
|
logging.info(f"Elapsed time: {elapsed}")
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
"""
|
|
|
|
Main function
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
2020-10-20 00:45:08 +02:00
|
|
|
client = paho.mqtt.client.Client(client_id="10")
|
2020-10-19 19:11:18 +02:00
|
|
|
client.on_connect = mqtt_on_connect
|
|
|
|
client.on_message = mqtt_on_command
|
|
|
|
|
|
|
|
if MQTT_USERNAME:
|
|
|
|
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
|
|
|
|
|
|
|
|
client.connect(MQTT_HOSTNAME, MQTT_PORT, 60)
|
|
|
|
|
|
|
|
try:
|
|
|
|
client.loop_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.info("SIGINT recieved! Stopping...")
|
|
|
|
|
|
|
|
client.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|