Last active
July 12, 2016 06:15
-
-
Save LarsBergqvist/1bbd0aa6aeaa896da5267e11889fc3db to your computer and use it in GitHub Desktop.
A flask server for IoT chart data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| from flask import Flask, render_template, jsonify, request | |
| from datetime import datetime, timedelta | |
| import sys | |
| import data_fake | |
| import data_mongodb | |
| app = Flask(__name__) | |
| def get_labels_and_values_for_topic(topic_name, numdays): | |
| if (numdays < 1): | |
| numdays = 1 | |
| if app.config['FAKE'] == False: | |
| repo = data_mongodb.MongoDBRepository | |
| else: | |
| repo = data_fake.FakeRepository | |
| return repo.get_data(repo,topic_name,numdays) | |
| @app.route("/ChartData") | |
| def index(): | |
| return render_template('index.html') | |
| @app.route('/ChartData/api/<string:location>/<string:measurement>') | |
| def get_measurements_as_labels_and_values(location,measurement): | |
| numdays = request.args.get('numdays', default=1, type=int) | |
| topic = "Home/" + location + "/" + measurement | |
| # Get all measurements that matches a specific topic from the database | |
| # Fetch data from today and numdays backwards in time | |
| # The measurements are split into two arrays, one with measurement times (=labels) | |
| # and one with the actual values. | |
| labels, values = get_labels_and_values_for_topic(topic,numdays) | |
| return jsonify({"measurements":{'labels':labels,'values':values}}) | |
| if __name__ == "__main__": | |
| for arg in sys.argv: | |
| if arg.lower() == "--fake": | |
| print("Using fake data") | |
| app.config['FAKE'] = True | |
| else: | |
| app.config['FAKE'] = False | |
| app.run(host='0.0.0.0', port=6001,debug=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment