forked from adilosa/weathermon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
138 lines (115 loc) · 3.82 KB
/
weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from datetime import datetime, timedelta
from collections import Counter
from flask import Flask, render_template, jsonify
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
from flask_bootstrap import Bootstrap
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///weather.db"
db = SQLAlchemy(app)
bootstrap = Bootstrap(app)
class Reading(db.Model):
__tablename__ = "reading"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
channel = db.Column(db.Integer)
temperature = db.Column(db.Integer)
humidity = db.Column(db.Integer)
battery = db.Column(db.Integer)
timestamp = db.Column(db.DateTime)
@property
def data(self):
return {
"id": self.id,
"channel": self.channel,
"temperature": self.temperature,
"humidity": self.humidity,
"timestamp": self.timestamp
}
def __repr__(self):
return "<Reading {id} - Ch {ch} {temp}F {hum}% at {time}>".format(
id=self.id,
ch=self.channel,
temp=self.temperature,
hum=self.humidity,
time=self.timestamp.strftime("%Y-%m-%d %H:%M:%S.%f")
)
class Sensor(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
channel = db.Column(db.Integer)
name = db.Column(db.String(64))
@property
def data(self):
return {
"id": self.id,
"channel": self.channel,
"name": self.name,
"temperature": self.temperature,
"humidity": self.humidity
}
@property
def temperature(self):
return self.latest()["temperature"]
@property
def humidity(self):
return self.latest()["humidity"]
def latest(self):
if hasattr(self, 'latest_data'):
return self.latest_data
limit = 15
query = db.session.query(
Reading.temperature,
Reading.humidity,
Reading.timestamp
)\
.filter_by(channel=self.channel)\
.order_by(Reading.timestamp.desc())\
.limit(limit)
results = query.all()
self.latest_data = {
"temperature": sum([r[0] for r in results]) / limit,
"humidity": sum([r[1] for r in results]) / limit,
"timestamp": results[0][2]
}
return self.latest_data
def last_day(self):
query = db.session.query(
func.round(func.avg(Reading.temperature)),
func.round(func.avg(Reading.humidity)),
Reading.timestamp
)\
.filter_by(channel=self.channel)\
.group_by(func.strftime('%H', Reading.timestamp))\
.order_by(Reading.timestamp.desc())\
.limit(30)
return [
{
"temperature": r[0],
"humidity": r[1],
"timestamp": r[2]
} for r in query.all()
]
def __repr__(self):
return "<Sensor {id}: \"{name}\" on Ch {ch}>".format(
id=self.id,
name=self.name,
ch=self.channel
)
@app.route("/")
def main_page():
return render_template(
"home.jinja2",
sensors=Sensor.query.order_by(Sensor.channel.asc()).all()
)
@app.route("/sensors", defaults={"_id": None})
@app.route("/sensors/<int:_id>")
def sensors(_id):
return jsonify(_sensors(_id))
def _sensors(_id=None):
if _id is not None:
sensor = Sensor.query.filter_by(id=_id).first_or_404()
data = sensor.data
data.update({"readings": sensor.last_day()})
return data
return {"sensors": [sensor.data for sensor in Sensor.query.all()]}
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)