55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
import flask
|
|
import logging
|
|
import json
|
|
import threading
|
|
|
|
from spejsiot.manager import SpejsiotManager
|
|
from spejsiot.rendering import render_endpoint
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)-15s] %(name)-10s %(levelname)7s: %(message)s')
|
|
|
|
app = flask.Flask(__name__)
|
|
app.config['PORT'] = 5000
|
|
app.config['BROKER'] = ('mqtt.waw.hackerspace.pl', 1883)
|
|
app.config['PREFIX'] = 'iot/'
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
|
|
manager = SpejsiotManager(app)
|
|
|
|
@app.context_processor
|
|
def utility_processor():
|
|
return {
|
|
'render_endpoint': render_endpoint,
|
|
}
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return flask.render_template('index.html', devices=manager.devices)
|
|
|
|
@app.route('/api/1/devices/')
|
|
def api_devices():
|
|
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):
|
|
node = manager.find_node(node_id)
|
|
|
|
if not node:
|
|
flask.abort(404)
|
|
|
|
return flask.jsonify(node.dictify())
|
|
|
|
@app.route('/api/1/device/<node_id>/<endpoint>/<prop>/<value>')
|
|
@app.route('/api/1/devices/<node_id>/<endpoint>/<prop>/<value>')
|
|
def device_write(node_id, endpoint, prop, value):
|
|
return flask.jsonify(manager.handle_request(node_id, endpoint, prop, value))
|
|
|
|
@app.route('/api/1/devices/<node_id>/<endpoint>/<prop>', methods=['PUT'])
|
|
def device_put(node_id, endpoint, prop):
|
|
return flask.jsonify(manager.handle_request(node_id, endpoint, prop, flask.request.json['value']))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
manager.run(*app.config['BROKER'])
|
|
app.run(port=app.config['PORT'])
|