Added report capability
This commit is contained in:
		
							
								
								
									
										30
									
								
								src/app.py
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								src/app.py
									
									
									
									
									
								
							@@ -4,10 +4,11 @@ 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
 | 
			
		||||
from birbnetes_iot_platform_raspberry import BirbnetesIoTPlatformStatusDriver, BirbnetesIoTPlatformRecordDriver
 | 
			
		||||
from actuator import Loudspeaker
 | 
			
		||||
import paho.mqtt.client
 | 
			
		||||
import json
 | 
			
		||||
import requests
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
@@ -71,6 +72,24 @@ def mqtt_on_command(client, userdata, message):
 | 
			
		||||
        userdata.act()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def do_report():
 | 
			
		||||
    report = {
 | 
			
		||||
        "client": config.DEVICE_ID,
 | 
			
		||||
        "cloud": config.API_URL,
 | 
			
		||||
        "measurements": {
 | 
			
		||||
            "queue": BirbnetesIoTPlatformRecordDriver.get_queue_length()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    print("Reporting queue length of", report)
 | 
			
		||||
 | 
			
		||||
    r = requests.post(config.REPORT_URL, json=report)
 | 
			
		||||
 | 
			
		||||
    r.raise_for_status()
 | 
			
		||||
    if r.status_code != 201:
 | 
			
		||||
        print(config.REPORT_URL, "Wrong response")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main() -> None:
 | 
			
		||||
    """
 | 
			
		||||
    Main function
 | 
			
		||||
@@ -83,6 +102,12 @@ def main() -> None:
 | 
			
		||||
    loopingtimer = LoopingTimer(function=timer_tick, tick_args=listofabcsignaprocessors, interval=config.TICK_INTERVAL)
 | 
			
		||||
    loopingtimer.start()
 | 
			
		||||
 | 
			
		||||
    report_timer = None
 | 
			
		||||
    if config.REPORT_URL:
 | 
			
		||||
        print("Setting up queue length reporting...")
 | 
			
		||||
        report_timer = LoopingTimer(function=do_report, tick_args=None, interval=config.REPORT_INTERVAL)
 | 
			
		||||
        report_timer.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
 | 
			
		||||
@@ -98,6 +123,9 @@ def main() -> None:
 | 
			
		||||
    except KeyboardInterrupt:
 | 
			
		||||
        logging.info("SIGINT recieved! Stopping...")
 | 
			
		||||
 | 
			
		||||
    if report_timer:
 | 
			
		||||
        report_timer.stop()
 | 
			
		||||
 | 
			
		||||
    client.disconnect()
 | 
			
		||||
    loopingtimer.stop()
 | 
			
		||||
    BirbnetesIoTPlatformStatusDriver.cleanup()
 | 
			
		||||
 
 | 
			
		||||
@@ -29,4 +29,6 @@ MQTT_PASSWORD = os.getenv("GUARD_MQTT_PASSWORD", None)
 | 
			
		||||
 | 
			
		||||
SVM_MODEL_ID = os.environ.get("SVM_MODEL_ID")
 | 
			
		||||
 | 
			
		||||
API_URL = os.environ.get("API_URL", "http://localhost:8080")
 | 
			
		||||
API_URL = os.environ.get("API_URL", "http://localhost:8080")
 | 
			
		||||
REPORT_URL = os.environ.get("REPORT_URL", None)
 | 
			
		||||
REPORT_INTERVAL = float(os.environ.get("REPORT_INTERVAL", 15))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user