diff options
-rw-r--r-- | schema.sql | 8 | ||||
-rw-r--r-- | sensor.py | 64 | ||||
-rw-r--r-- | static/style.css | 18 | ||||
-rw-r--r-- | templates/layout.html | 17 | ||||
-rw-r--r-- | templates/show_entries.html | 23 |
5 files changed, 130 insertions, 0 deletions
diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..7652fe8 --- /dev/null +++ b/schema.sql @@ -0,0 +1,8 @@ +drop table if exists weather; +create table weather ( + id integer primary key autoincrement, + date timestamp, + temperature float, + humidity float, + pressure float +); diff --git a/sensor.py b/sensor.py new file mode 100644 index 0000000..a0123d0 --- /dev/null +++ b/sensor.py @@ -0,0 +1,64 @@ +# imports +import sqlite3 +from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash +from contextlib import closing +from datetime import datetime + +# configuration +DATABASE = '/tmp/sensors.db' +DEBUG = True +SECRET_KEY = 'development key' + +# app creation +app = Flask(__name__) +app.config.from_object(__name__) +# To read from settings file, set SENSOR_SETTINGS in envvar +#app.config.from_envvar('SENSOR_SETTINGS', slient=True) + +# initialize database +def init_db(): + with closing(connect_db()) as db: + with app.open_resource('schema.sql', mode='r') as f: + db.cursor().executescript(f.read()) + db.commit() + +# connect to database +def connect_db(): + return sqlite3.connect(app.config['DATABASE']) + +# handle requests +@app.before_request +def before_request(): + g.db = connect_db() + +@app.teardown_request +def teardown_request(exception): + db = getattr(g, 'bd', None) + if db is not None: + db.close() + +# viewing database +@app.route('/') +def show_entries(): + cur = g.db.execute('SELECT date, temperature, humidity, pressure FROM weather ORDER BY id desc') + entries = [dict(date=row[0], temperature=row[1], humidity=row[2], pressure=row[3]) for row in cur.fetchall()] + return render_template('show_entries.html', weather=entries) + +# adding entries to database +@app.route('/data', methods=['POST']) +def add_data(): + mkey = request.args.get('key') + if mkey != SECRET_KEY: + abort(401) + temp = request.args.get('temp') + humidity = request.args.get('humidity') + pressure = request.args.get('pressure') + now = datetime.now() + g.db.execute('INSERT INTO weather (date, temperature, humidity, pressure) VALUES (?, ?, ?, ?)', \ + [now, temp, humidity, pressure]) + g.db.commit() + return 200 + + +if __name__ == '__main__': + app.run() diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..da1cb34 --- /dev/null +++ b/static/style.css @@ -0,0 +1,18 @@ +body { font-family: sans-serif; background: #eee; } +a, h1, h2 { color: #377ba8; } +h1, h2 { font-family: 'Georgia', serif; margin: 0; } +h1 { border-bottom: 2px solid #eee; } +h2 { font-size: 1.2em; } + +.page { margin: 2em auto; width: 35em; border: 5px solid #ccc; + padding: 0.8em; background: white; } +.weather { list-style: none; margin: 0; padding: 0; } +.weather li { margin: 0.8em 1.2em; } +.weather li h2 { margin-left: -1em; } +.add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; } +.add-entry dl { font-weight: bold; } +.metanav { text-align: right; font-size: 0.8em; padding: 0.3em; + margin-bottom: 1em; background: #fafafa; } +.flash { background: #cee5F5; padding: 0.5em; + border: 1px solid #aacbe2; } +.error { background: #f0d6d6; padding: 0.5em; } diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..0f32195 --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,17 @@ +<!doctype html> +<title>Weather</title> +<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}"> +<div class=page> + <h1>Weather</h1> + <div class=metanav> + {% if not session.logged_in %} + <a href="{{ url_for('login') }}">log in</a> + {% else %} + <a href="{{ url_for('logout') }}">log out</a> + {% endif %} + </div> + {% for message in get_flashed_messages() %} + <div class=flash>{{ message }}</div> + {% endfor %} + {% block body %}{% endblock %} +</div> diff --git a/templates/show_entries.html b/templates/show_entries.html new file mode 100644 index 0000000..5d9ff1a --- /dev/null +++ b/templates/show_entries.html @@ -0,0 +1,23 @@ +{% extends "layout.html" %} +{% block body %} + {% if session.logged_in %} + <form action="{{ url_for('add_entry') }}" method=post class=add-entry> + <dl> + <dt>Temperature: + <dd><input type=number size=30 name=temperature> + <dt>Humidity: + <dd><input type=number size=30 name=humidity> + <dt>Pressure: + <dd><input type=number size=30 name=pressure> + <dd><input type=submit value=Submit> + </dl> + </form> + {% endif %} + <ul class=weather> + {% for entry in weather %} + <li><h2>{{ entry.date }}</h2>Temp: {{ entry.temperature }}, Humidity: {{ entry.humidity }}, Pressure: {{ entry.pressure }} + {% else %} + <li><em>No entries here so far</em> + {% endfor %} + </ul> +{% endblock %} |