-
Notifications
You must be signed in to change notification settings - Fork 0
/
eink-weather.sh
executable file
·154 lines (135 loc) · 4.56 KB
/
eink-weather.sh
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
#!/usr/bin/env bash
set -euo pipefail
temp_image=$(mktemp /tmp/eink-weather.bmp.XXXXXXXX)
trap 'rm -rf "${temp_image}"' EXIT
pirateweather_api_url="https://api.pirateweather.net/forecast"
pirateweather_api_key="${EINK_WEATHER_PIRATEWEATHER_KEY}"
pirateweather_location="${EINK_WEATHER_PIRATEWEATHER_LOCATION}"
pirateweather_optional_params="?units=si"
pirateweather_request_url="${pirateweather_api_url}/${pirateweather_api_key}/${pirateweather_location}${pirateweather_optional_params}"
background_color="white"
foreground_color="black"
# Get the time.
time=$(date +"%H:%M")
# Get the pirateweather weather.
pirateweather_json=$(curl "${pirateweather_request_url}")
pirateweather_current_temperature=$(echo ${pirateweather_json} | jq '.currently.temperature')
pirateweather_current_temperature=$(printf %.0f ${pirateweather_current_temperature})
pirateweather_current_summary=$(echo ${pirateweather_json} | jq -r '.currently.summary')
pirateweather_hourly_summary=$(echo ${pirateweather_json} | jq -r '.hourly.summary')
pirateweather_daily_summary=$(echo ${pirateweather_json} | jq -r '.daily.summary')
sunrise_today=$(date --date=@$(echo ${pirateweather_json} | jq -r '.daily.data[0].sunriseTime') +'%H:%M')
sunset_today=$(date --date=@$(echo ${pirateweather_json} | jq -r '.daily.data[0].sunsetTime') +'%H:%M')
# Construct the image to be shown.
# Background.
convert \
-size 600x448 \
xc:${background_color} \
bmp:${temp_image}
# Date, sunrise, and sunset.
convert \
${temp_image} \
-background none \
-gravity north \
-fill ${foreground_color} \
-font "Iosevka-Bold" \
-size 600x40 \
label:"↑${sunrise_today} $(date +'%A %-d %B %Y') ${sunset_today}↓" \
-geometry +0+20 \
-compose over -composite \
bmp:${temp_image}
# Time.
convert \
${temp_image} \
-background none \
-gravity north \
-fill ${foreground_color} \
-font "Iosevka-Bold" \
-size 600x280 \
label:"${time}" \
-geometry +0+20 \
-compose over -composite \
bmp:${temp_image}
# Darksky weather. Temperature and current weather.
convert \
${temp_image} \
-background none \
-gravity north \
-fill ${foreground_color} \
-font "Iosevka-Bold" \
-size 600x60 \
label:"${pirateweather_current_temperature}° ${pirateweather_hourly_summary}" \
-geometry +0+280 \
-compose over -composite \
bmp:${temp_image}
# Weekly weather
weekdays=$(echo ${pirateweather_json} | jq '.daily.data[].time' | head -n 7 | xargs -I '{}' date --date=@'{}' +' %a' | tr -d '\n' | sed 's|^ ||')
convert \
${temp_image} \
-background none \
-gravity north \
-fill ${foreground_color} \
-font "Iosevka-Bold" \
-size 600x36 \
label:"\\${weekdays}" \
-geometry +0+340 \
-compose over -composite \
bmp:${temp_image}
weekday_temperatures_high=$(echo ${pirateweather_json} | jq '.daily.data[].temperatureHigh' | head -n 7 | xargs -I '{}' printf '%6.0f' '{}' | tr -d '\n' | sed 's|^ ||')
convert \
${temp_image} \
-background none \
-gravity north \
-fill ${foreground_color} \
-font "Iosevka-Bold" \
-size 600x36 \
label:"\\${weekday_temperatures_high}" \
-geometry +0+370 \
-compose over -composite \
bmp:${temp_image}
weekday_temperatures_low=$(echo ${pirateweather_json} | jq '.daily.data[].temperatureLow' | head -n 7 | xargs -I '{}' printf '%6.0f' '{}' | tr -d '\n' | sed 's|^ ||')
convert \
${temp_image} \
-background none \
-gravity north \
-fill ${foreground_color} \
-font "Iosevka-Bold" \
-size 600x36 \
label:"\\${weekday_temperatures_low}" \
-geometry +0+400 \
-compose over -composite \
bmp:${temp_image}
for i in {0..6}; do
precip_intensity=$(echo ${pirateweather_json} | jq ".daily.data[${i}].precipIntensity")
bar_top=$(echo "scale=5; 448 - ($precip_intensity * 100)" | bc)
bar_left=$((90 + i * 78))
bar_bottom=448
bar_right=$((bar_left + 10))
convert \
"${temp_image}" \
-size 600x448 \
xc:transparent \
-fill ${foreground_color} \
-stroke ${background_color} \
-draw "rectangle ${bar_left},${bar_top}, ${bar_right},${bar_bottom}" \
-compose over -composite \
bmp:${temp_image}
done
# Invert if night.
sunrise_seconds=$(date --date=@$(echo ${pirateweather_json} | jq -r '.daily.data[0].sunriseTime') +'%s')
sunset_seconds=$(date --date=@$(echo ${pirateweather_json} | jq -r '.daily.data[0].sunsetTime') +'%s')
now_seconds=$(date +'%s')
if ((now_seconds <= sunrise_seconds || sunset_seconds <= now_seconds)); then
convert \
${temp_image} \
-negate \
bmp:${temp_image}
fi
image_color="black"
if ((pirateweather_current_temperature >= 20)); then
image_color="red"
fi
echo "Displaying \"${temp_image}\" at ${time}"
/home/pi/src/eink-weather/python/main.py \
"${temp_image}" \
"${image_color}"