This commit is contained in:
		
							
								
								
									
										12
									
								
								src/app.py
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/app.py
									
									
									
									
									
								
							@@ -47,6 +47,12 @@ app.config['MQTT_REFRESH_TIME'] = 1.0  # refresh time in seconds
 | 
				
			|||||||
api = Api(app)
 | 
					api = Api(app)
 | 
				
			||||||
mqtt.init_app(app)
 | 
					mqtt.init_app(app)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					api.add_resource(DeviceOfflineResrouce, "/devices/<string:deviceid>/offline")
 | 
				
			||||||
 | 
					api.add_resource(DeviceOnlineResrouce, "/devices/<string:deviceid>/online")
 | 
				
			||||||
 | 
					api.add_resource(AlertResource, "/devices/<string:deviceid>/alert")
 | 
				
			||||||
 | 
					api.add_resource(SensorOfflineResource, "/devices/<string:deviceid>/<string:sensorid>/offline")
 | 
				
			||||||
 | 
					api.add_resource(SensorOnlineResource, "/devices/<string:deviceid>/<string:sensorid>/online")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@mqtt.on_log()
 | 
					@mqtt.on_log()
 | 
				
			||||||
def handle_logging(client, userdata, level, buf):
 | 
					def handle_logging(client, userdata, level, buf):
 | 
				
			||||||
@@ -65,9 +71,3 @@ def handle_status_message_proxy(*args, **kwargs):
 | 
				
			|||||||
    """
 | 
					    """
 | 
				
			||||||
    with app.app_context():
 | 
					    with app.app_context():
 | 
				
			||||||
        handle_status_message(*args, **kwargs)
 | 
					        handle_status_message(*args, **kwargs)
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
api.add_resource(DeviceOfflineResrouce, "/devices/{deviceid}/offline")
 | 
					 | 
				
			||||||
api.add_resource(DeviceOnlineResrouce, "/devices/{deviceid}/online")
 | 
					 | 
				
			||||||
api.add_resource(SensorOfflineResource, "/devices/{deviceid}/{sensorid}/offline")
 | 
					 | 
				
			||||||
api.add_resource(SensorOnlineResource, "/devices/{deviceid}/{sensorid}/online")
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,6 @@
 | 
				
			|||||||
#!/usr/bin/env python3
 | 
					#!/usr/bin/env python3
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from flask_restful import Resource
 | 
					from flask_restful import Resource
 | 
				
			||||||
from mqtt_flask_instance import mqtt
 | 
					from mqtt_flask_instance import mqtt
 | 
				
			||||||
import config
 | 
					import config
 | 
				
			||||||
@@ -23,7 +25,7 @@ class DeviceOfflineResrouce(Resource):
 | 
				
			|||||||
        :param deviceid: ID of device
 | 
					        :param deviceid: ID of device
 | 
				
			||||||
        :return:
 | 
					        :return:
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}", {"command": "offline"})
 | 
					        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}", json.dumps({"command": "offline"}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class DeviceOnlineResrouce(Resource):
 | 
					class DeviceOnlineResrouce(Resource):
 | 
				
			||||||
@@ -36,7 +38,7 @@ class DeviceOnlineResrouce(Resource):
 | 
				
			|||||||
        :param deviceid: ID of device
 | 
					        :param deviceid: ID of device
 | 
				
			||||||
        :return:
 | 
					        :return:
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}", {"command": "online"})
 | 
					        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}", json.dumps({"command": "online"}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SensorOfflineResource(Resource):
 | 
					class SensorOfflineResource(Resource):
 | 
				
			||||||
@@ -50,7 +52,7 @@ class SensorOfflineResource(Resource):
 | 
				
			|||||||
        :param sensorid: ID of sensor
 | 
					        :param sensorid: ID of sensor
 | 
				
			||||||
        :return:
 | 
					        :return:
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}/{sensorid}", {"command": "offline"})
 | 
					        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}/{sensorid}", json.dumps({"command": "offline"}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SensorOnlineResource(Resource):
 | 
					class SensorOnlineResource(Resource):
 | 
				
			||||||
@@ -64,4 +66,17 @@ class SensorOnlineResource(Resource):
 | 
				
			|||||||
        :param sensorid: ID of sensor
 | 
					        :param sensorid: ID of sensor
 | 
				
			||||||
        :return:
 | 
					        :return:
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}/{sensorid}", {"command": "online"})
 | 
					        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}/{sensorid}", json.dumps({"command": "online"}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class AlertResource(Resource):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Force alert on device
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    def post(self, deviceid: str):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Shut down a sensor of a device
 | 
				
			||||||
 | 
					        :param deviceid: ID of device
 | 
				
			||||||
 | 
					        :return:
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        mqtt.publish(f"{config.MQTT_COMMAND_TOPIC}/{deviceid}", json.dumps({"command": "doAlert"}))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user