spejsiot-api/main.py

97 lines
2.8 KiB
Python
Raw Normal View History

2016-10-02 21:37:51 +00:00
import flask
import logging
import json
2017-01-20 21:26:16 +00:00
import threading
2016-10-02 21:37:51 +00:00
2017-01-20 21:26:16 +00:00
from spejsiot.manager import SpejsiotManager
from spejsiot.rendering import render_endpoint
2016-10-02 21:37:51 +00:00
2017-01-20 21:26:16 +00:00
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)-15s] %(name)-10s %(levelname)7s: %(message)s')
2016-10-02 21:37:51 +00:00
app = flask.Flask(__name__)
2017-01-20 21:33:11 +00:00
app.config['DISABLE_GUI'] = True
2017-01-20 22:22:07 +00:00
app.config['PORT'] = 5100
2017-01-20 21:26:16 +00:00
app.config['BROKER'] = ('mqtt.waw.hackerspace.pl', 1883)
app.config['PREFIX'] = 'iot/'
2016-10-02 21:37:51 +00:00
app.config['TEMPLATES_AUTO_RELOAD'] = True
2017-01-20 21:26:16 +00:00
manager = SpejsiotManager(app)
2016-12-05 12:28:16 +00:00
@app.context_processor
def utility_processor():
return {
'render_endpoint': render_endpoint,
}
2016-10-02 21:37:51 +00:00
@app.route('/')
def index():
2017-01-20 21:33:11 +00:00
if app.config.get('DISABLE_GUI'):
2017-01-21 15:27:21 +00:00
return flask.redirect(flask.url_for(
'static', filename='apidocs/index.html'))
2017-01-20 21:33:11 +00:00
2016-10-02 21:37:51 +00:00
return flask.render_template('index.html', devices=manager.devices)
@app.route('/api/1/devices/')
2016-10-02 21:37:51 +00:00
def api_devices():
2017-01-21 15:27:21 +00:00
"""Lists all known devices.
:status 200: list of all devices
:>json object [device_id]: listed device info object
"""
2016-12-05 12:28:16 +00:00
return flask.jsonify({k: v.dictify() for k, v in manager.devices.items()})
@app.route('/api/1/devices/<node_id>/')
def api_device_info(node_id):
2017-01-21 15:27:21 +00:00
"""Returns specified device info object.
:param node_id: device ID or device name
:status 200: device found
:status 404: no device found
:>json string $name: human-readable device name
:>json boolean $online: device is currently online
:>json object [endpoint_name]: endpoint object
:>json [endpoint_name].[prop_name]: last endpoint property value
"""
node = manager.find_node(node_id)
2017-01-20 21:26:16 +00:00
if not node:
flask.abort(404)
return flask.jsonify(node.dictify())
2017-01-21 15:27:21 +00:00
# @app.route('/api/1/device/<node_id>/<endpoint>/<prop>/<value>')
@app.route('/api/1/devices/<node_id>/<endpoint>/<prop>/<value>')
2016-12-05 12:28:16 +00:00
def device_write(node_id, endpoint, prop, value):
2017-01-21 15:27:21 +00:00
"""Sets specified value to device endpoint and prop.
:param node_id: device ID or device name
:param endpoint: endpoint name
:param prop: endpoint property name
:param value: value to set
:status 200: device found
:status 404: no device found
"""
2017-01-20 21:33:11 +00:00
return flask.jsonify({
"result": manager.handle_request(node_id, endpoint, prop, value)
})
2016-10-02 21:37:51 +00:00
2017-01-20 21:26:16 +00:00
@app.route('/api/1/devices/<node_id>/<endpoint>/<prop>', methods=['PUT'])
def device_put(node_id, endpoint, prop):
2017-01-21 15:27:21 +00:00
"""Sets specified value to device endpoint and prop.
:param node_id: device ID or device name
:param endpoint: endpoint name
:param prop: endpoint property name
:<json value: value to set
:status 200: device found
:status 404: no device found
"""
2017-01-20 21:33:11 +00:00
return device_write(node_id, endpoint, prop, flask.request.json['value'])
2017-01-20 21:26:16 +00:00
2016-10-02 21:37:51 +00:00
if __name__ == "__main__":
2017-01-20 21:26:16 +00:00
manager.run(*app.config['BROKER'])
app.run(port=app.config['PORT'])