-
Notifications
You must be signed in to change notification settings - Fork 3
/
display_combined.py
114 lines (95 loc) · 3.54 KB
/
display_combined.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
#!/usr/bin/python3
import sys
import os
import datetime
import logging
from PIL import ImageOps, Image, ImageFont, ImageDraw
from inky.auto import auto
imgdir = os.path.join(
os.path.dirname(os.path.dirname(os.path.relpath(__file__))), "assets"
)
if os.path.exists("lib"):
sys.path.append("lib")
from carbonintensityforecast import CarbonIntensityForecastGB
import fonts
def main():
inky_display = auto()
img = Image.new("P", inky_display.resolution)
draw = ImageDraw.Draw(img)
template = Image.open(os.path.join(imgdir, "template-combined.png"))
img.paste(template, (0, 0))
try:
carbon_intensity = CarbonIntensityForecastGB()
except Exception as e:
return logging.error('Unable to update combined display')
intensity_data = carbon_intensity.now()
ci_daily_max = carbon_intensity.forecast_max()
total_value = carbon_intensity.now("total")
total = 0
draw.text((15, 3), "Now", inky_display.WHITE, font=fonts.raleway_reg_30)
for value in intensity_data:
percentage_value = int(round(value["perc"]))
coordsx = 100
coordsy = 0
total += percentage_value
if value["fuel"] == "wind":
coordsy = 75
if value["fuel"] == "hydro":
coordsy = 150
if value["fuel"] == "solar":
coordsy = 225
draw.text(
(coordsx, coordsy),
str(percentage_value) + "%",
inky_display.WHITE,
font=fonts.raleway_reg_40,
)
draw.text((270, 3), "Forecast", inky_display.BLACK, font=fonts.raleway_reg_30)
rhs_start = 215
draw.text((248, 50), "Ngt", inky_display.BLACK, font=fonts.raleway_light_15)
draw.text((291, 50), "Mor", inky_display.BLACK, font=fonts.raleway_light_15)
draw.text((330, 50), "Aft", inky_display.BLACK, font=fonts.raleway_light_15)
draw.text((369, 50), "Eve", inky_display.BLACK, font=fonts.raleway_light_15)
# Display icons
trueImage = Image.open(os.path.join(imgdir, "check.png"))
falseImage = Image.open(os.path.join(imgdir, "remove.png"))
index = 1
max_x_options = {"night": 245, "morning": 285, "afternoon": 325, "evening": 365}
for date, value in carbon_intensity.forecast("bool").items():
if index > 4:
break
day = datetime.datetime.strptime(date, "%Y-%m-%d")
dayname = day.strftime("%A")
yValue = (index * 50) + 35
draw.text(
(210, yValue + 3),
dayname[0:1],
inky_display.BLACK,
font=fonts.raleway_reg_25,
)
if "night" in value:
img.paste(trueImage if value["night"] else falseImage, (245, yValue))
if "morning" in value:
img.paste(trueImage if value["morning"] else falseImage, (285, yValue))
if "afternoon" in value:
img.paste(trueImage if value["afternoon"] else falseImage, (325, yValue))
if "evening" in value:
img.paste(trueImage if value["evening"] else falseImage, (365, yValue))
max_slot, max_value = ci_daily_max[date]
max_value = max_x_options.get(max_slot, "Invalid max")
draw.rectangle(
[(max_value + 4, yValue + 33), (max_value + 28, yValue + 35)],
inky_display.BLACK,
)
index += 1
date_now = datetime.datetime.now()
draw.text(
(305, 280),
"Updated :" + date_now.strftime("%H:%M"),
inky_display.BLACK,
font=fonts.raleway_light_13,
)
inky_display.set_image(img)
inky_display.show()
if __name__ == "__main__":
main()