From 132a5729b3c54a33461a3e26e34aa89fc57b3052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Sun, 4 Nov 2018 13:16:50 +0100 Subject: [PATCH] Add Mqtt config endpoint --- app/api/blueprint.py | 4 +- app/api/resources/app.py | 77 +++++++++++++++++++ .../swagger/get_mqtt_config_spec.yaml | 16 ++++ app/swagger/template.yaml | 48 ++++++++++++ 4 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 app/api/resources/app.py create mode 100644 app/api/resources/swagger/get_mqtt_config_spec.yaml diff --git a/app/api/blueprint.py b/app/api/blueprint.py index 7a6d10e..42cb530 100644 --- a/app/api/blueprint.py +++ b/app/api/blueprint.py @@ -5,8 +5,6 @@ from flask import Blueprint, jsonify api_bp = Blueprint('api', __name__) - - api = Api(api_bp) @@ -34,6 +32,7 @@ def add_resources(): DashboardListResource, DashboardWidgetResource, DashboardWidgetListResource) + from .resources.app import MqttConfigResource api.add_resource(AccountResource, '/v1/accounts/') api.add_resource(AccountListResource, '/v1/accounts') @@ -74,6 +73,7 @@ def add_resources(): '/v1/dashboards//widgets/') api.add_resource(DashboardWidgetListResource, '/v1/dashboards//widgets') + api.add_resource(MqttConfigResource, '/v1/config/mqtt') add_resources() diff --git a/app/api/resources/app.py b/app/api/resources/app.py new file mode 100644 index 0000000..4e874b3 --- /dev/null +++ b/app/api/resources/app.py @@ -0,0 +1,77 @@ +from flask import current_app as app +from marshmallow import Schema, fields +from flasgger import swag_from +from app.api.auth_protection import ProtectedResource +from app.api.schemas import BaseResourceSchema + + +class BasicMqttBrokerSchema(Schema): + url = fields.String() + port = fields.String() + + +class MqttBrokerSchema(BaseResourceSchema, BasicMqttBrokerSchema): + pass + + +class BasicMqttEndpointSchema(Schema): + topic = fields.String() + description = fields.String() + body_example = fields.Raw() + + +class MqttEndpointSchema(BaseResourceSchema, BasicMqttEndpointSchema): + pass + + +class MqttConfigSchema(BaseResourceSchema): + broker = fields.Nested(BasicMqttBrokerSchema) + endpoints = fields.Nested(BasicMqttEndpointSchema, many=True) + + +def get_mqtt_broker_info(config): + return { + 'url': config['MQTT_BROKER_URL'], + 'port': config['MQTT_BROKER_PORT'] + } + + +def get_mqtt_endpoints(config): + return [ + { + 'topic': 'device/', + 'description': 'Used by devices to send data to server. ' + + 'All messages sent to this endpoint must be in ' + + 'JSON format and signed with device secret (sign' + + 'ature should be added to original JSON after ' + + 'signing as "hmac" key in root object). JSON can ' + + 'contain any number of keys, but there are a few ' + + 'mandatory fields.', + 'body_example': { + 'record_type': 1, + 'record_value': 123, + 'recorded_at': 1537379424, + 'hmac': "jfdhslfh12383j12l3j12oirjfkdsfd" + } + }, + { + 'topic': 'device//config', + 'description': 'Used by server to send config to ' + + 'devices. Devices should listen to this endpoint ' + + 'in order to properly receive updated ' + + 'configuration.', + 'body_example': { + 'update_rate': 4, + 'mode': 'passive' + } + } + ] + + +class MqttConfigResource(ProtectedResource): + @swag_from('swagger/get_mqtt_config_spec.yaml') + def get(self): + return { + 'broker': get_mqtt_broker_info(app.config), + 'endpoints': get_mqtt_endpoints(app.config) + }, 200 diff --git a/app/api/resources/swagger/get_mqtt_config_spec.yaml b/app/api/resources/swagger/get_mqtt_config_spec.yaml new file mode 100644 index 0000000..8dccfa4 --- /dev/null +++ b/app/api/resources/swagger/get_mqtt_config_spec.yaml @@ -0,0 +1,16 @@ +Gets server Mqtt configuration and description +--- +tags: + - Mqtt + - Config +responses: + 200: + description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/MqttConfig' + diff --git a/app/swagger/template.yaml b/app/swagger/template.yaml index 5d98951..2b94189 100644 --- a/app/swagger/template.yaml +++ b/app/swagger/template.yaml @@ -328,6 +328,54 @@ definitions: name: ref: '#definitions/genericname' + MqttBroker: + type: object + required: + - url + - port + properties: + url: + type: string + description: Url of the used MQTT broker + example: broker.hivemq.com + port: + type: number + description: Port of the used MQTT broker + example: 1883 + + MqttEndpoint: + type: object + required: + - topic + - description + - body_example + properties: + topic: + type: string + description: Topic of this endpoint + example: device/ + description: + type: string + description: Description of usage of this endpoint + example: Used to send data to devices + body_example: + type: object + description: Example of body of messages used on this endpoint + example: {} + + MqttConfig: + type: object + required: + - broker + - endpoints + properties: + broker: + $ref: '#/definitions/MqttBroker' + endpoints: + type: array + items: + $ref: '#/definitions/MqttEndpoint' + Widget: type: object required: