Fixed database operations in MQTT calls
	
		
			
	
		
	
	
		
	
		
			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:
		
							
								
								
									
										10
									
								
								src/app.py
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/app.py
									
									
									
									
									
								
							@@ -11,7 +11,7 @@ from db import db
 | 
				
			|||||||
from marshm import ma
 | 
					from marshm import ma
 | 
				
			||||||
from mqtt_flask_instance import mqtt
 | 
					from mqtt_flask_instance import mqtt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import mqtt_methods  # This file contains a decorator
 | 
					from mqtt_methods import handle_status_message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
Main Flask RESTful API
 | 
					Main Flask RESTful API
 | 
				
			||||||
@@ -77,6 +77,14 @@ def handle_connect(client, userdata, flags, rc):
 | 
				
			|||||||
    mqtt.subscribe(f"{MQTT_STATUS_TOPIC}/#")
 | 
					    mqtt.subscribe(f"{MQTT_STATUS_TOPIC}/#")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@mqtt.on_topic(f"{config.MQTT_STATUS_TOPIC}/#")
 | 
				
			||||||
 | 
					def handle_status_message_proxy(*args, **kwargs):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    This proxy puts the call into a Flask context, so database operations can be performed
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    with app.app_context():
 | 
				
			||||||
 | 
					        handle_status_message(*args, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
api.add_resource(AllDevicesResource, "/devices")
 | 
					api.add_resource(AllDevicesResource, "/devices")
 | 
				
			||||||
api.add_resource(AllDevicesOfflineResource, "/devices/offline")
 | 
					api.add_resource(AllDevicesOfflineResource, "/devices/offline")
 | 
				
			||||||
api.add_resource(AllDevicesOnlineResource, "/devices/online")
 | 
					api.add_resource(AllDevicesOnlineResource, "/devices/online")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,7 +20,6 @@ sensorschema = SensorSchema(many=False)
 | 
				
			|||||||
LOGGER = logging.getLogger(__name__)
 | 
					LOGGER = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@mqtt.on_topic(f"{config.MQTT_STATUS_TOPIC}/#")
 | 
					 | 
				
			||||||
def handle_status_message(client, userdata, message):
 | 
					def handle_status_message(client, userdata, message):
 | 
				
			||||||
    data = dict(
 | 
					    data = dict(
 | 
				
			||||||
        topic=message.topic,
 | 
					        topic=message.topic,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user