-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
executable file
·326 lines (291 loc) · 11.8 KB
/
database.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import sqlite3
from datetime import datetime, timedelta, time
import random
import argparse
DATABASE = 'sqlite.db'
def init_db():
with sqlite3.connect(DATABASE) as db:
db.execute('''
CREATE TABLE IF NOT EXISTS sensors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
time TIMESTAMP NOT NULL,
temperature REAL NOT NULL,
humidity REAL NOT NULL,
soil_humidity REAL NOT NULL
)
''')
db.execute('''
CREATE TABLE IF NOT EXISTS actions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
time TIMESTAMP NOT NULL,
actor TEXT NOT NULL,
state INTEGER NOT NULL
)
''')
db.execute('''
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date DATE NOT NULL,
comment TEXT NOT NULL
)
''')
db.commit()
def insert_sensor_data(temperature, humidity, soil_humidity):
with sqlite3.connect(DATABASE) as db:
db.execute('''
INSERT INTO sensors (time, temperature, humidity, soil_humidity)
VALUES (?, ?, ?, ?)
''', (datetime.now(), temperature, humidity, soil_humidity))
db.commit()
def insert_action(actor, state, time=datetime.now()):
with sqlite3.connect(DATABASE) as db:
db.execute('''
INSERT INTO actions (time, actor, state)
VALUES (?, ?, ?)
''', (time, actor, state))
db.commit()
def fetch_latest_sensor_data():
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('SELECT * FROM sensors ORDER BY time DESC LIMIT 1')
row = cursor.fetchone()
return row
def fetch_first_sensor_data():
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('SELECT * FROM sensors ORDER BY time ASC LIMIT 1')
row = cursor.fetchone()
return row
def fetch_sensor_data(dt_from, dt_to):
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('''
SELECT * FROM sensors
WHERE time BETWEEN ? AND ?
ORDER BY time
''', (dt_from, dt_to))
rows = cursor.fetchall()
return rows
def fetch_average_sensor_data_per_day(dt_from, dt_to):
dt_to = dt_to + timedelta(days=1)
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('''
SELECT strftime('%Y-%m-%dT00:00:00', time) as time, AVG(temperature) as temperature, AVG(humidity) as humidity, AVG(soil_humidity) as soil_humidity
FROM sensors
WHERE time BETWEEN ? AND ?
GROUP BY strftime('%Y%m%d', time)
ORDER BY strftime('%Y%m%d', time)
''', (dt_from, dt_to))
rows = cursor.fetchall()
return rows
def fetch_average_sensor_data_per_hour(dt_from, dt_to):
dt_from = dt_from + timedelta(days=1)
dt_to = dt_to + timedelta(days=1)
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('''
SELECT strftime('%Y-%m-%dT%H:00:00', time) as time, AVG(temperature) as temperature, AVG(humidity) as humidity, AVG(soil_humidity) as soil_humidity
FROM sensors
WHERE time BETWEEN ? AND ?
GROUP BY strftime('%Y%m%d%H', time)
ORDER BY strftime('%Y%m%d%H', time)
''', (dt_from, dt_to))
rows = cursor.fetchall()
return rows
def fetch_actions(dt_from, dt_to):
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('''
SELECT id, strftime('%Y-%m-%dT%H:%M:%S', time) as time, actor, state FROM actions
WHERE time BETWEEN ? AND ?
ORDER BY time
''', (dt_from, dt_to))
rows = cursor.fetchall()
return rows
def fetch_actions_and_prepare(dt_from, dt_to):
dt_from = dt_from + timedelta(days=1)
dt_to = dt_to + timedelta(days=1)
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('SELECT DISTINCT actor FROM actions')
actors = [row[0] for row in cursor.fetchall()]
actors_spans = {}
for actor in actors:
actors_spans[actor] = []
cursor = db.execute('''
SELECT strftime('%Y-%m-%dT%H:%M:%S', time) as time, state FROM actions
WHERE actor = ? and time BETWEEN ? AND ?
ORDER BY time
''', (actor, dt_from, dt_to))
rows = cursor.fetchall()
if len(rows) == 0:
continue
spans = []
last_state = 1 if rows[0][1] == 0 else 0
for action in rows:
time = action[0]
state = action[1]
if last_state == 0 and state == 1:
spans.append({'dt_from': time})
last_state = 1
elif last_state == 1 and state == 0 and len(spans) > 0:
spans[-1]['dt_to'] = time
print('____ add to date')
last_state = 0
if rows[0][1] == 0:
cursor = db.execute('''
SELECT strftime('%Y-%m-%dT%H:%M:%S', time) as time, state FROM actions
WHERE actor = ? and time < ? and state = 1
ORDER BY time DESC
''', (actor, dt_from))
row = cursor.fetchone()
if row:
dt = datetime.strptime(rows[0][0], '%Y-%m-%dT%H:%M:%S')
spans.insert(0, {'dt_from': row[0], 'dt_to': min(dt, dt_to)})
if rows[-1][1] == 1 and len(spans) > 0:
spans[-1]['dt_to'] = min(datetime.now(), dt_to).strftime('%Y-%m-%dT%H:%M:%S')
actors_spans[actor] = spans
return actors_spans
def insert_or_update_comment(comment, date=datetime.now().date()):
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('''
SELECT id
FROM comments
WHERE date(date) = date(?)
''', (date,))
existing_comment = cursor.fetchone()
if existing_comment:
# Update existing comment
db.execute('''
UPDATE comments
SET comment = ?
WHERE id = ?
''', (comment, existing_comment[0]))
else:
# Insert new comment
db.execute('''
INSERT INTO comments (date, comment)
VALUES (?, ?)
''', (date, comment))
db.commit()
def get_comments_by_date(target_date):
with sqlite3.connect(DATABASE) as db:
cursor = db.execute('''
SELECT id, date, comment
FROM comments
WHERE date(date) = date(?)
''', (target_date,))
comments = cursor.fetchall()
if len(comments) > 0:
return comments[0][2]
else:
return ''
def clear_db():
with sqlite3.connect(DATABASE) as db:
db.execute('DELETE FROM sensors')
db.execute('DELETE FROM actions')
db.execute('DELETE FROM comments')
db.commit()
def generate_example_data(dt_from, dt_to):
generate_example_data_sensors(dt_from, dt_to)
generate_example_data_actions(dt_from, dt_to)
generate_example_data_comments(dt_from, dt_to)
def generate_example_data_sensors(dt_from, dt_to):
with sqlite3.connect(DATABASE) as db:
current = dt_from
while current <= dt_to:
db.execute('''
INSERT INTO sensors (time, temperature, humidity, soil_humidity)
VALUES (?, ?, ?, ?)
''', (
current,
random.uniform(10, 30),
random.uniform(30, 90),
random.uniform(10, 50)
))
current += timedelta(seconds=60)
db.commit()
def generate_example_data_actions(dt_from, dt_to):
with sqlite3.connect(DATABASE) as db:
current = dt_from
while current <= dt_to:
# Light on at 06:00
light_on_time = datetime.combine(current.date(), datetime.min.time()) + timedelta(hours=6)
db.execute('''
INSERT INTO actions (time, actor, state)
VALUES (?, ?, ?)
''', (light_on_time, 'light', True))
# Light off at 22:00
light_off_time = datetime.combine(current.date(), datetime.min.time()) + timedelta(hours=22)
db.execute('''
INSERT INTO actions (time, actor, state)
VALUES (?, ?, ?)
''', (light_off_time, 'light', False))
# Pump on/off every 4 hours
pump_time = datetime.combine(current.date(), datetime.min.time())
for i in range(0, 24, 4):
pump_on_time = pump_time + timedelta(hours=i)
pump_off_time = pump_on_time + timedelta(seconds=5)
db.execute('''
INSERT INTO actions (time, actor, state)
VALUES (?, ?, ?)
''', (pump_on_time, 'pump', True))
db.execute('''
INSERT INTO actions (time, actor, state)
VALUES (?, ?, ?)
''', (pump_off_time, 'pump', False))
# Fan on every 1-3 hours for 30 - 120 minutes
fan_time = datetime.combine(current.date(), datetime.min.time())
for i in range(8, 18, random.randint(2, 3)):
fan_on_time = fan_time + timedelta(hours=i)
fan_off_time = fan_on_time + timedelta(minutes=random.randint(30, 110))
db.execute('''
INSERT INTO actions (time, actor, state)
VALUES (?, ?, ?)
''', (fan_on_time, 'fan', True))
db.execute('''
INSERT INTO actions (time, actor, state)
VALUES (?, ?, ?)
''', (fan_off_time, 'fan', False))
current += timedelta(days=1)
db.commit()
def generate_example_data_comments(dt_from, dt_to):
with sqlite3.connect(DATABASE) as db:
current = dt_from
while current <= dt_to:
db.execute('''
INSERT INTO comments (date, comment)
VALUES (?, ?)
''', (
current.date(),
f"Example comment for {current.date()}"
))
current += timedelta(days=1)
db.commit()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Rufe Funktionen aus einem Python-Skript auf.')
parser.add_argument('funktion', choices=['clear', 'init', 'generate', 'actions', 'prepared_actions', 'clear_actions'], help='Die Funktion, die aufgerufen werden soll')
args = parser.parse_args()
if args.funktion == 'clear':
print('clear db...')
clear_db()
print('db cleard')
elif args.funktion == 'init':
print('init db...')
init_db()
print('db inited')
elif args.funktion == 'generate':
print('generate example data for last 182 days...')
generate_example_data(datetime.now() - timedelta(days=182), datetime.now() - timedelta(days=1))
print('example data generated')
elif args.funktion == 'actions':
import pprint
pprint.pprint(fetch_actions(datetime(2000, 1, 1), datetime(2030, 1, 1)))
elif args.funktion == 'prepared_actions':
from util import Util
import pprint
now = datetime.now()
# today = datetime.combine(now.date(), time.min)
today = now
today = today - timedelta(days=1)
tomorrow = today + timedelta(days=1)
print(f'From {today} to {tomorrow}')
pprint.pprint(Util.fetch_and_separete_actions(today, tomorrow))
pprint.pprint(fetch_actions_and_prepare(today, tomorrow))
elif args.funktion == 'clear_actions':
with sqlite3.connect(DATABASE) as db:
db.execute('DELETE FROM actions', ())