-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdate_display.py
executable file
·78 lines (57 loc) · 2.36 KB
/
update_display.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
#!/usr/bin/env python3
# pylint: disable=invalid-name
"""Read Octopus Agile price data from an existing SQLite database and update a
Pimoroni Blinkt! display."""
import sqlite3
import os
import sys
from urllib.request import pathname2url
import argparse
import eco_indicator
# Blinkt! defaults
DEFAULT_BRIGHTNESS = 10
# Inky pHAT defaults
DEFAULT_HIGHPRICE = 30.0
DEFAULT_LOWSLOTDURATION = 3
parser = argparse.ArgumentParser(description=('Update Eco Indicator display using SQLite data'))
parser.add_argument('--demo', '-d', action='store_true', help='display demo data')
parser.add_argument('--conf', '-c', default='config.yaml', help='specify config file')
args = parser.parse_args()
conf_file = args.conf
os.chdir(sys.path[0])
try:
# connect to the database in rw mode so we can catch the error if it doesn't exist
DB_URI = 'file:{}?mode=rw'.format(pathname2url('eco_indicator.sqlite'))
conn = sqlite3.connect(DB_URI, uri=True)
cursor = conn.cursor()
print('Connected to database...')
except sqlite3.OperationalError as error:
# handle missing database case
raise SystemExit('Database not found - you need to run store_data.py first.') from error
config = eco_indicator.get_config(conf_file)
if 'agile' in config['Mode'] or config['Mode'] == 'tracker':
field_name = 'value_inc_vat'
elif config['Mode'] == 'carbon':
field_name = 'intensity'
else:
raise SystemExit('Error: invalid mode ' + config['Mode'] + ' in config.')
if config['Mode'] == "tracker":
cursor.execute("SELECT * FROM eco ORDER BY valid_from DESC")
else:
cursor.execute("SELECT * FROM eco WHERE valid_from > datetime('now', '-30 minutes') AND " + field_name + " IS NOT NULL")
data_rows = cursor.fetchall()
if len(data_rows) == 0:
raise SystemExit('Error: No data found - perhaps you need to run store_data.py.')
if config['DisplayType'] == 'blinkt':
eco_indicator.update_blinkt(config, data_rows, args.demo)
elif config['DisplayType'] == 'inkyphat':
if 'agile' in config['Mode'] or config['Mode'] == 'carbon':
eco_indicator.update_inky(config, data_rows, args.demo)
elif config['Mode'] == 'tracker':
eco_indicator.update_inky_tracker(config, data_rows, args.demo)
else:
raise SystemExit('Error: invalid display type ' + config['DisplayType'] + 'in config.')
# finish up the database operation
if conn:
conn.commit()
conn.close()