-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
149 lines (124 loc) · 3.64 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
from flask import Flask, render_template, Markup
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_caching import Cache
from datetime import datetime
import datetime
from datetime import datetime, date, timedelta
from sqlalchemy.sql.expression import func, select
import json
def default(o):
return o._asdict()
from influxdb import InfluxDBClient
app = Flask(__name__)
# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
# Check Configuring Flask-Caching section for more details
cache = Cache(app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR': "/home/pi/gardener/cache"})
app.jinja_env.filters['json'] = lambda v: Markup(json.dumps(v))
class Sensor():
humidity_sensors_count = 12
all_sensors = [
{
"id": id,
"name": "HUM %s" % id,
"max_value": 1 if (id >= 5) else 1024
} for id in range(1, humidity_sensors_count)
]
all_sensors.append(
{
"id": humidity_sensors_count,
"name": "Light",
"max_value": 1024
}
)
def __init__(self, attrs):
self.id = attrs["id"]
self.name = attrs["name"]
self.max_value = attrs["max_value"]
def __repr__(self):
return "(%s, '%s')" %(self.id, self.name)
def _asdict(self):
return {"id": self.id, "name": self.name}
@staticmethod
def all():
return [Sensor(sensor_data) for sensor_data in Sensor.all_sensors]
@staticmethod
def by_id(id):
return Sensor.all()[id - 1]
class Plant():
all_plants = [
{
"id": 1,
"name": "Basil #1",
"sensor_ids": [1, 12],
"hose": None
},
{
"id": 2,
"name": "Parsley #1",
"sensor_ids": [2, 10, 12],
"hose": 3
},
{
"id": 3,
"name": "Radishes #2",
"sensor_ids": [5,6,12],
"hose": 4
},
{
"id": 4,
"name": "Baby Basils #1",
"sensor_ids": [3,4,12],
"hose": 2
},
# {
# "id": 3,
# "name": "Radishes #1",
# "sensor_ids": [3,4,9,11,12],
# "archived": True
# },
]
def __init__(self, attrs):
self.id = attrs["id"]
self.name = attrs["name"]
self.sensor_ids = attrs["sensor_ids"]
self.hose = attrs["hose"]
def __repr__(self):
return "(%s, '%s', %s)" %(self.id, self.name, self.sensors())
@staticmethod
def all():
return [Plant(sensor_data) for sensor_data in Plant.all_plants]
@staticmethod
def by_id(id):
return Plant.all()[id - 1]
def sensors(self):
return [Sensor.by_id(sensor_id) for sensor_id in self.sensor_ids]
def ago(delta):
return datetime.now() - delta
## Refactor me. Use OO
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'gardener_db')
def current_value(sensor_id, max_value):
if max_value == 1024:
query = client.query(
"""SELECT (1023 - mean(value)) / 1023
FROM SENSOR_%s
WHERE time > now() - 1m
""" % (sensor_id)
)
try:
return query.raw['series'][0]["values"][0][1]
except KeyError:
return 0
else:
query = client.query(
"""SELECT 1.0 - mean(value)
FROM SENSOR_%s
WHERE time > now() - 1m
""" % (sensor_id)
)
try:
return query.raw['series'][0]["values"][0][1]
except KeyError:
return 0