2020-08-19 00:44:04 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import logging
|
|
|
|
import sentry_sdk
|
|
|
|
from sentry_sdk.integrations.logging import LoggingIntegration
|
2020-08-25 01:40:09 +02:00
|
|
|
from utils import config, LoopingTimer
|
|
|
|
from signal_processor import SoundSignalProcessor
|
2020-09-30 03:35:20 +02:00
|
|
|
from birbnetes_iot_platform_raspberry import BirbnetesIoTPlatformStatusDriver
|
2020-09-30 04:08:36 +02:00
|
|
|
from actuator import Loudspeaker
|
|
|
|
import paho.mqtt.client
|
|
|
|
import json
|
2020-09-30 04:40:54 +02:00
|
|
|
import sys
|
2020-08-19 00:44:04 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Main Entrypoint
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = "@tormakris"
|
|
|
|
__copyright__ = "Copyright 2020, Birbnetes Team"
|
|
|
|
__module_name__ = "app"
|
|
|
|
__version__text__ = "1"
|
|
|
|
|
|
|
|
sentry_logging = LoggingIntegration(
|
|
|
|
level=logging.INFO,
|
|
|
|
event_level=logging.ERROR
|
|
|
|
)
|
|
|
|
|
|
|
|
if config.SENTRY_DSN:
|
|
|
|
sentry_sdk.init(
|
|
|
|
dsn=config.SENTRY_DSN,
|
|
|
|
integrations=[sentry_logging],
|
|
|
|
traces_sample_rate=1.0,
|
|
|
|
send_default_pii=True,
|
|
|
|
release=config.RELEASE_ID,
|
|
|
|
environment=config.RELEASEMODE,
|
|
|
|
_experiments={"auto_enabling_integrations": True}
|
|
|
|
)
|
|
|
|
|
2020-08-24 20:58:26 +02:00
|
|
|
|
2020-08-25 01:40:09 +02:00
|
|
|
def timer_tick(*args) -> None:
|
|
|
|
"""
|
|
|
|
Tick of a timer
|
|
|
|
:param listofabcsignaprocessors:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
for abcsignaprocessor in args:
|
|
|
|
abcsignaprocessor.processcurrentsignal()
|
|
|
|
|
|
|
|
|
2020-09-30 04:08:36 +02:00
|
|
|
def mqtt_on_connect(client, userdata, flags, rc):
|
|
|
|
BirbnetesIoTPlatformStatusDriver.enqueue_pattern('green', [1])
|
|
|
|
client.subscribe(f"command/{config.DEVICE_ID}")
|
2020-10-02 16:37:00 +02:00
|
|
|
logging.info("MQTT Connected!")
|
2020-09-30 04:08:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def mqtt_on_disconnect(client, userdata, rc):
|
2020-10-02 16:37:00 +02:00
|
|
|
if rc != 0:
|
|
|
|
logging.error(f"Unexpected MQTT Disconnect. RC: {rc}")
|
|
|
|
BirbnetesIoTPlatformStatusDriver.enqueue_pattern('red', [1])
|
|
|
|
else:
|
|
|
|
logging.debug("MQTT Disconnected")
|
2020-09-30 04:08:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def mqtt_on_command(client, userdata, message):
|
|
|
|
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':
|
|
|
|
userdata.act()
|
|
|
|
|
|
|
|
|
2020-08-25 01:40:09 +02:00
|
|
|
def main() -> None:
|
2020-08-24 20:58:26 +02:00
|
|
|
"""
|
|
|
|
Main function
|
|
|
|
:return:
|
|
|
|
"""
|
2020-09-30 04:40:54 +02:00
|
|
|
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-09-30 03:35:20 +02:00
|
|
|
BirbnetesIoTPlatformStatusDriver.init()
|
2020-09-30 05:34:22 +02:00
|
|
|
listofabcsignaprocessors = [SoundSignalProcessor()]
|
|
|
|
loopingtimer = LoopingTimer(function=timer_tick, tick_args=listofabcsignaprocessors, interval=config.TICK_INTERVAL)
|
2020-08-25 01:40:09 +02:00
|
|
|
loopingtimer.start()
|
2020-08-24 20:58:26 +02:00
|
|
|
|
2020-09-30 04:08:36 +02:00
|
|
|
client = paho.mqtt.client.Client(userdata=Loudspeaker(config.ENEMY_SOUNDS), client_id=config.DEVICE_ID)
|
|
|
|
client.on_connect = mqtt_on_connect
|
|
|
|
client.on_disconnect = mqtt_on_disconnect
|
|
|
|
client.on_message = mqtt_on_command
|
|
|
|
|
|
|
|
if config.MQTT_USERNAME:
|
|
|
|
client.username_pw_set(config.MQTT_USERNAME, config.MQTT_PASSWORD)
|
|
|
|
|
|
|
|
client.connect(config.MQTT_HOSTNAME, config.MQTT_PORT, 60)
|
2020-10-02 16:11:46 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
client.loop_forever() # This blocks
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.info("SIGINT recieved! Stopping...")
|
|
|
|
|
|
|
|
client.disconnect()
|
|
|
|
loopingtimer.stop()
|
|
|
|
BirbnetesIoTPlatformStatusDriver.cleanup()
|
2020-09-30 04:08:36 +02:00
|
|
|
|
2020-08-24 20:58:26 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|