From 3a129f85188e7807c035ed4c42af97c589325700 Mon Sep 17 00:00:00 2001 From: marcsello Date: Wed, 30 Sep 2020 03:35:20 +0200 Subject: [PATCH] Implemented platform specific stuff --- src/app.py | 2 ++ src/sensor/soundsensor.py | 19 +++++++++++----- src/signal_processor/soundsignalprocessor.py | 23 ++++++++++++++++++-- src/utils/config.py | 1 + 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/app.py b/src/app.py index a2032a1..ae9ac16 100644 --- a/src/app.py +++ b/src/app.py @@ -4,6 +4,7 @@ import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration from utils import config, LoopingTimer from signal_processor import SoundSignalProcessor +from birbnetes_iot_platform_raspberry import BirbnetesIoTPlatformStatusDriver """ Main Entrypoint @@ -46,6 +47,7 @@ def main() -> None: Main function :return: """ + BirbnetesIoTPlatformStatusDriver.init() listofabcsignaprocessors = (SoundSignalProcessor()) loopingtimer = LoopingTimer(function=timer_tick, args=[listofabcsignaprocessors], interval=config.TICK_INTERVAL) loopingtimer.start() diff --git a/src/sensor/soundsensor.py b/src/sensor/soundsensor.py index e6257d7..c12773d 100644 --- a/src/sensor/soundsensor.py +++ b/src/sensor/soundsensor.py @@ -1,5 +1,8 @@ #!/usr/bin/env python3 +from typing import Optional from .abcsensor import AbcSensor +from utils import config +from birbnetes_iot_platform_raspberry import BirbnetesIoTPlatformRecordDriver """ Sound sensor high level API @@ -15,11 +18,17 @@ class SoundSensor(AbcSensor): """ SoundSensor class. Responsible for sound retrieval from any microphones present. """ - def getvalue(self) -> str: + + def __init__(self): + BirbnetesIoTPlatformRecordDriver.init(config.SAMPLE_LENGTH) + + def getvalue(self) -> Optional[str]: """ - Retrieve a 5 second sound clip from a microphone. + Retrieve a configurable length second sound clip from a microphone. :return: """ - # Recieve sound from microphone and save it to a file on the filesystem. - # Then return the filename - return "filename.wav" + return BirbnetesIoTPlatformRecordDriver.get_recording(False) + + def __del__(self): + # In theory you should not put anything to __del__ as it is not guaranteed to be called. + BirbnetesIoTPlatformRecordDriver.cleanup() diff --git a/src/signal_processor/soundsignalprocessor.py b/src/signal_processor/soundsignalprocessor.py index 4895236..3770665 100644 --- a/src/signal_processor/soundsignalprocessor.py +++ b/src/signal_processor/soundsignalprocessor.py @@ -1,9 +1,13 @@ #!/usr/bin/env python3 +import logging +import requests from sensor import SoundSensor from sender import SoundSender from preprocessor import SoundPreProcessor from .abcsignalprocessor import AbcSignalProcessor +from birbnetes_iot_platform_raspberry import BirbnetesIoTPlatformStatusDriver + """ Abstract base class for signalprocessor """ @@ -33,5 +37,20 @@ class SoundSignalProcessor(AbcSignalProcessor): :return: """ soundsample_name = self.soundsensor.getvalue() - sample_decision = self.soundpreprocessor.preprocesssignal(soundsample_name) - self.soundsender.sendvalue(soundsample_name, sample_decision) + + try: + sample_decision = self.soundpreprocessor.preprocesssignal(soundsample_name) + except Exception as e: + logging.exception(e) + BirbnetesIoTPlatformStatusDriver.enqueue_pattern('red', [1, 1, 1]) + return + + if sample_decision: + BirbnetesIoTPlatformStatusDriver.enqueue_pattern('green', [1, 0, 1]) + + try: + self.soundsender.sendvalue(soundsample_name, sample_decision) + except (ConnectionError, requests.HTTPError) as e: + logging.exception(e) + BirbnetesIoTPlatformStatusDriver.enqueue_pattern('red', [1, 0, 1, 0, 1]) + return diff --git a/src/utils/config.py b/src/utils/config.py index 8b8cca0..7ac632e 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -17,6 +17,7 @@ RELEASEMODE = os.environ.get("RELEASEMODE", "dev") DEVICE_ID = os.environ.get("DEVICE_ID", "devraspi") TICK_INTERVAL = float(os.environ.get("TICK_INTERVAL", 0.4)) +SAMPLE_LENGTH = float(os.environ.get("SAMPLE_LENGTH", 1)) TARGET_NAME = os.environ.get("TARGET_CLASS_NAME")