Implemented mqtt stuff
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		@@ -3,6 +3,8 @@ requests
 | 
			
		||||
 | 
			
		||||
cython
 | 
			
		||||
 | 
			
		||||
paho-mqtt
 | 
			
		||||
 | 
			
		||||
cycler==0.10.0
 | 
			
		||||
deprecation==2.0.7
 | 
			
		||||
eyeD3==0.9.5
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1 @@
 | 
			
		||||
from .loudspeaker import Loudspeaker
 | 
			
		||||
							
								
								
									
										36
									
								
								src/app.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								src/app.py
									
									
									
									
									
								
							@@ -5,6 +5,9 @@ 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 actuator import Loudspeaker
 | 
			
		||||
import paho.mqtt.client
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
Main Entrypoint
 | 
			
		||||
@@ -42,6 +45,28 @@ def timer_tick(*args) -> None:
 | 
			
		||||
        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:
 | 
			
		||||
    """
 | 
			
		||||
    Main function
 | 
			
		||||
@@ -52,6 +77,17 @@ def main() -> None:
 | 
			
		||||
    loopingtimer = LoopingTimer(function=timer_tick, args=[listofabcsignaprocessors], interval=config.TICK_INTERVAL)
 | 
			
		||||
    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__":
 | 
			
		||||
    main()
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,12 @@ 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))
 | 
			
		||||
 | 
			
		||||
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")
 | 
			
		||||
MODEL_ID = os.environ.get("MODEL_ID")
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user