Add Mqtt config endpoint
parent
49f9e96b40
commit
132a5729b3
|
@ -5,8 +5,6 @@ from flask import Blueprint, jsonify
|
||||||
|
|
||||||
|
|
||||||
api_bp = Blueprint('api', __name__)
|
api_bp = Blueprint('api', __name__)
|
||||||
|
|
||||||
|
|
||||||
api = Api(api_bp)
|
api = Api(api_bp)
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,6 +32,7 @@ def add_resources():
|
||||||
DashboardListResource,
|
DashboardListResource,
|
||||||
DashboardWidgetResource,
|
DashboardWidgetResource,
|
||||||
DashboardWidgetListResource)
|
DashboardWidgetListResource)
|
||||||
|
from .resources.app import MqttConfigResource
|
||||||
|
|
||||||
api.add_resource(AccountResource, '/v1/accounts/<int:account_id>')
|
api.add_resource(AccountResource, '/v1/accounts/<int:account_id>')
|
||||||
api.add_resource(AccountListResource, '/v1/accounts')
|
api.add_resource(AccountListResource, '/v1/accounts')
|
||||||
|
@ -74,6 +73,7 @@ def add_resources():
|
||||||
'/v1/dashboards/<int:dashboard_id>/widgets/<int:widget_id>')
|
'/v1/dashboards/<int:dashboard_id>/widgets/<int:widget_id>')
|
||||||
api.add_resource(DashboardWidgetListResource,
|
api.add_resource(DashboardWidgetListResource,
|
||||||
'/v1/dashboards/<int:dashboard_id>/widgets')
|
'/v1/dashboards/<int:dashboard_id>/widgets')
|
||||||
|
api.add_resource(MqttConfigResource, '/v1/config/mqtt')
|
||||||
|
|
||||||
|
|
||||||
add_resources()
|
add_resources()
|
||||||
|
|
|
@ -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/<device_id>',
|
||||||
|
'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/<device_id>/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
|
|
@ -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'
|
||||||
|
|
|
@ -328,6 +328,54 @@ definitions:
|
||||||
name:
|
name:
|
||||||
ref: '#definitions/genericname'
|
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/<device_id>
|
||||||
|
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:
|
Widget:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
|
|
Loading…
Reference in New Issue