Implemented mqtt stuff
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pünkösd Marcell 2020-09-30 04:08:36 +02:00
parent 4cc347074a
commit 8f44e7d13a
4 changed files with 45 additions and 0 deletions

View File

@ -3,6 +3,8 @@ requests
cython cython
paho-mqtt
cycler==0.10.0 cycler==0.10.0
deprecation==2.0.7 deprecation==2.0.7
eyeD3==0.9.5 eyeD3==0.9.5

View File

@ -0,0 +1 @@
from .loudspeaker import Loudspeaker

View File

@ -5,6 +5,9 @@ from sentry_sdk.integrations.logging import LoggingIntegration
from utils import config, LoopingTimer from utils import config, LoopingTimer
from signal_processor import SoundSignalProcessor from signal_processor import SoundSignalProcessor
from birbnetes_iot_platform_raspberry import BirbnetesIoTPlatformStatusDriver from birbnetes_iot_platform_raspberry import BirbnetesIoTPlatformStatusDriver
from actuator import Loudspeaker
import paho.mqtt.client
import json
""" """
Main Entrypoint Main Entrypoint
@ -42,6 +45,28 @@ def timer_tick(*args) -> None:
abcsignaprocessor.processcurrentsignal() abcsignaprocessor.processcurrentsignal()
def mqtt_on_connect(client, userdata, flags, rc):
BirbnetesIoTPlatformStatusDriver.enqueue_pattern('green', [1])
client.subscribe(f"command/{config.DEVICE_ID}")
logging.error("MQTT Connected!")
def mqtt_on_disconnect(client, userdata, rc):
BirbnetesIoTPlatformStatusDriver.enqueue_pattern('red', [1])
logging.error("Unexpected MQTT Disconnect")
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()
def main() -> None: def main() -> None:
""" """
Main function Main function
@ -52,6 +77,17 @@ def main() -> None:
loopingtimer = LoopingTimer(function=timer_tick, args=[listofabcsignaprocessors], interval=config.TICK_INTERVAL) loopingtimer = LoopingTimer(function=timer_tick, args=[listofabcsignaprocessors], interval=config.TICK_INTERVAL)
loopingtimer.start() loopingtimer.start()
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)
client.loop_forever()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -19,6 +19,12 @@ DEVICE_ID = os.environ.get("DEVICE_ID", "devraspi")
TICK_INTERVAL = float(os.environ.get("TICK_INTERVAL", 0.4)) TICK_INTERVAL = float(os.environ.get("TICK_INTERVAL", 0.4))
SAMPLE_LENGTH = float(os.environ.get("SAMPLE_LENGTH", 1)) SAMPLE_LENGTH = float(os.environ.get("SAMPLE_LENGTH", 1))
ENEMY_SOUNDS = os.environ.get("ENEMY_SOUNDS", '/var/lib/birbnetes/enemy_sounds')
MQTT_HOSTNAME = os.getenv("GUARD_MQTT_HOSTNAME", "localhost")
MQTT_PORT = int(os.getenv("GUARD_MQTT_PORT", 1883))
MQTT_USERNAME = os.getenv("GUARD_MQTT_USERNAME", None)
MQTT_PASSWORD = os.getenv("GUARD_MQTT_PASSWORD", None)
TARGET_NAME = os.environ.get("TARGET_CLASS_NAME") TARGET_NAME = os.environ.get("TARGET_CLASS_NAME")
MODEL_ID = os.environ.get("MODEL_ID") MODEL_ID = os.environ.get("MODEL_ID")