add alert endpoint
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2021-06-12 21:07:43 +02:00
parent 22f4ed3c8d
commit 06597d46a5
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
2 changed files with 25 additions and 10 deletions

View File

@ -47,6 +47,12 @@ app.config['MQTT_REFRESH_TIME'] = 1.0 # refresh time in seconds
api = Api(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()
def handle_logging(client, userdata, level, buf):
@ -65,9 +71,3 @@ def handle_status_message_proxy(*args, **kwargs):
"""
with app.app_context():
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")

View File

@ -1,4 +1,6 @@
#!/usr/bin/env python3
import json
from flask_restful import Resource
from mqtt_flask_instance import mqtt
import config
@ -23,7 +25,7 @@ class DeviceOfflineResrouce(Resource):
:param deviceid: ID of device
: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):
@ -36,7 +38,7 @@ class DeviceOnlineResrouce(Resource):
:param deviceid: ID of device
: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):
@ -50,7 +52,7 @@ class SensorOfflineResource(Resource):
:param sensorid: ID of sensor
: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):
@ -64,4 +66,17 @@ class SensorOnlineResource(Resource):
:param sensorid: ID of sensor
: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"}))