-
Notifications
You must be signed in to change notification settings - Fork 27
/
octoprice_main_dothat.py
187 lines (137 loc) · 3.77 KB
/
octoprice_main_dothat.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
# this is the script you run every half hour by cron, best done about 20-30 seconds after the half hour to ensure
# that the right datetime is read in.
# For example ---> */30 * * * * sleep 20; /usr/bin/python3 octoprice_main_inky.py > /home/pi/cron.log
# NOTE - this version is designed for the pimoroni "display-o-tron" hat. You need to install all the libraries as per
# pimoroni's one line script.
import dothat.lcd as lcd
import dothat.backlight as backlight
import sqlite3
conn = sqlite3.connect('octoprice.sqlite')
cur = conn.cursor()
import datetime
import requests
# find current time and convert to year month day etc
the_now = datetime.datetime.now()
the_year = the_now.year
the_month = the_now.month
the_hour = the_now.hour
the_day = the_now.day
if the_now.minute < 30:
the_segment = 0
else:
the_segment = 1
print ('segment:')
print (the_segment)
# select from db where record = ^
cur.execute("SELECT * FROM prices WHERE year=? AND month=? AND day=? AND hour=? AND segment=?",
(the_year, the_month, the_day, the_hour, the_segment))
rows = cur.fetchall()
for row in rows:
print(row[5])
# get price
current_price = row[5] # literally this is peak tuple. DONT ADD ANY EXTRA FIELDS TO THAT TABLE
# display price on LCD
# Clear the LCD and display Hello World
lcd.clear()
lcd.write("Now:")
lcd.write(str(current_price))
lcd.set_cursor_position(1, 0)
lcd.set_contrast(52)
# change colour to match the badness of the price.
backlight.off()
# doing nothing with the graph so far
backlight.set_graph(0)
if current_price < 9.8: #greens
g=255
if current_price < 6:
r = 0
b = 0
elif current_price < 7:
r = 30
b = 30
elif current_price < 8:
r = 60
b = 60
else:
r = 90
b = 90
elif current_price < 14.8: # blues
b = 255
if current_price < 11:
r = 30
g = 30
else:
r = 60
g = 60
else: #expensive!
r = 255
if current_price < 20:
b = 100
g = 100
backlight.set_graph(0.3)
elif current_price < 30:
b = 30
g = 30
backlight.set_graph(0.5)
else:
b = 0
g = 0
backlight.set_graph(1)
#backlight.rgb(r,g,b)
#backlight.rgb(0,0,0)
if the_hour < 9:
backlight.rgb(0,0,0)
else:
backlight.rgb(int(r/2),int(g/2),int(b/2))
# Find Next Price
# find current time and convert to year month day etc
the_now = datetime.datetime.now()
now_plus_10 = the_now + datetime.timedelta(minutes = 30)
the_year = now_plus_10.year
the_month = now_plus_10.month
the_hour = now_plus_10.hour
the_day = now_plus_10.day
if now_plus_10.minute < 30:
the_segment = 0
else:
the_segment = 1
print ('segment:')
print (the_segment)
# select from db where record = ^
cur.execute("SELECT * FROM prices WHERE year=? AND month=? AND day=? AND hour=? AND segment=?",
(the_year, the_month, the_day, the_hour, the_segment))
rows = cur.fetchall()
for row in rows:
print(row[5])
# get price
next_price = row[5] # literally this is peak tuple. DONT ADD ANY EXTRA FIELDS TO THAT TABLE
# put that on the lcd
lcd.set_cursor_position(0, 1)
lcd.write("Soon:")
lcd.write(str(next_price))
# Find Next+1 Price
# find current time and convert to year month day etc
the_now = datetime.datetime.now()
now_plus_10 = the_now + datetime.timedelta(minutes = 60)
the_year = now_plus_10.year
the_month = now_plus_10.month
the_hour = now_plus_10.hour
the_day = now_plus_10.day
if now_plus_10.minute < 30:
the_segment = 0
else:
the_segment = 1
print ('segment:')
print (the_segment)
# select from db where record = ^
cur.execute("SELECT * FROM prices WHERE year=? AND month=? AND day=? AND hour=? AND segment=?",
(the_year, the_month, the_day, the_hour, the_segment))
rows = cur.fetchall()
for row in rows:
print(row[5])
# get price
next_price = row[5] # literally this is peak tuple. DONT ADD ANY EXTRA FIELDS TO THAT TABLE
# put that on the lcd
lcd.set_cursor_position(0, 2)
lcd.write("Then:")
lcd.write(str(next_price))