-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (161 loc) · 7.26 KB
/
Copy pathmain.py
File metadata and controls
177 lines (161 loc) · 7.26 KB
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
"""
Copyright (C) 2025 Mauricio Bustos (m@bustos.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import asyncio
import datetime
import logging
from logging.handlers import RotatingFileHandler
import math
import os
import python_weather
import colorbrewer
from pixelblaze import Pixelblaze
FORMAT = "%(asctime)s-%(module)s-%(lineno)d-%(message)s"
logger = logging.getLogger("binary-clock")
logging.basicConfig(level=logging.INFO,
format=FORMAT,
handlers=[
RotatingFileHandler("binary-clock.log", maxBytes=40000, backupCount=5),
logging.StreamHandler()
])
LOWER_TEMP_BOUND = 30.0
UPPER_TEMP_BOUND = 90.0
moonIndex = 2
MOON_PHASES = {"NEW_MOON" : 0,
"WAXING_CRESCENT": 1,
"FIRST_QUARTER": 2,
"WAXING_GIBBOUS": 3,
"FULL_MOON": 4,
"WANING_GIBBOUS": 5,
"LAST_QUARTER": 6,
"WANING_CRESCENT": 7}
# SUNNY: 0
# PARTLY CLOUDY: 1
# CLOUDY: 2
# VERY CLOUDY, FOG: 3
# LIGHT SHOWERS, LIGHT_SLEET_SHOWERS, LIGHT_SLEET, THUNDERY_SHOWERS, LIGHT_RAIN, HEAVY_SHOWERS, HEAVY_RAIN, THUNDERY_HEAVY_RAIN: 4
# LIGHT_SNOW, HEAVY_SNOW, LIGHT_SNOW_SHOWERS, HEAVY_SNOW_SHOWER, THUNDERY_SNOW_SHOWERS: 5
# From: python_weather
# SUNNY = 113
# PARTLY_CLOUDY = 116
# CLOUDY = 119
# VERY_CLOUDY = 122
# FOG = 143
# LIGHT_SHOWERS = 176
# LIGHT_SLEET_SHOWERS = 179
# LIGHT_SLEET = 182
# THUNDERY_SHOWERS = 200
# LIGHT_SNOW = 227
# HEAVY_SNOW = 230
# LIGHT_RAIN = 266
# HEAVY_SHOWERS = 299
# HEAVY_RAIN = 302
# LIGHT_SNOW_SHOWERS = 323
# HEAVY_SNOW_SHOWERS = 335
# THUNDERY_HEAVY_RAIN = 389
# THUNDERY_SNOW_SHOWERS = 392
WEATHER_KIND = {113: 0,
116: 1,
119: 2,
122: 3,
143: 3,
176: 4,
179: 4,
182: 4,
200: 4,
266: 4,
299: 4,
302: 4,
389: 4,
227: 5,
230: 5,
323: 5,
335: 5,
392: 5}
async def get_weather() -> None:
pixelblazes = list(Pixelblaze.EnumerateAddresses(timeout=5000))
clock_ipaddress = None
for ipAddress in pixelblazes:
with Pixelblaze(ipAddress) as pb:
logger.info(f"Checking to see if '{ipAddress}' is the correct one...")
config = pb.getConfigSettings()
if config["name"] == "rafa_binary_clock":
clock_ipaddress = ipAddress
logger.info(f"Found rafa_binary_clock at '{clock_ipaddress}'")
break
if clock_ipaddress is None:
logger.info("Rebooting due to rafa_binary_clock not found on the network")
os.system("sudo reboot")
while True:
async with python_weather.Client(unit=python_weather.IMPERIAL) as client:
try:
now = datetime.datetime.now()
brightness = (-math.cos((now.hour + (now.minute / 60.0)) / 24.0 * 2.0 * math.pi) + 1.0) / 2.0 * 0.30 + 0.04
logger.info("Getting weather update...")
# weather = await client.get('San+Luis+Obispo')
weather = await client.get('Oakland')
hour = datetime.datetime.now().time().hour
current_daily_forecast = None
current_forecast = None
for forecast in weather.daily_forecasts[0].hourly_forecasts:
if forecast.time.hour >= hour + 6:
current_daily_forecast = weather.daily_forecasts[0]
current_forecast = forecast
break
if not current_forecast:
for forecast in weather.daily_forecasts[1].hourly_forecasts:
if forecast.time.hour >= hour - 18:
current_daily_forecast = weather.daily_forecasts[1]
current_forecast = forecast
break
if not current_forecast:
logger.info(f"\tCurrent forecast logic failed")
current_daily_forecast = weather.daily_forecasts[0]
current_forecast = weather.daily_forecasts[0].hourly_forecasts[0]
logger.info(f"\tForecast for: {current_daily_forecast.date.strftime('%Y-%m-%d')} @ {current_forecast.time.strftime('%H:%M')}")
temp_bucket = -max(0, min(10, int((weather.daily_forecasts[0].highest_temperature - LOWER_TEMP_BOUND) /
(UPPER_TEMP_BOUND - LOWER_TEMP_BOUND) * 10))) + 10
color = colorbrewer.diverging["RdBu"][11][temp_bucket]
red = color.split(",")[0].split("(")[1]
green = color.split(",")[1]
blue = color.split(",")[2].split(")")[0]
pattern_name = "binary clock - shooting star"
logger.info(f"\tPattern name: {pattern_name}")
logger.info(f"\tMoon phase: {weather.daily_forecasts[0].moon_phase.name}")
logger.info(f"\tToday Weather: {weather.daily_forecasts[0].hourly_forecasts[4].kind.name}")
logger.info(f"\tTomorrow Weather: {weather.daily_forecasts[1].hourly_forecasts[4].kind.name}")
logger.info(f"\tWeather in 6 Hours: {current_forecast.kind.name}")
logger.info(f"\tBrightness: {brightness:.2f}")
with Pixelblaze(clock_ipaddress) as pb:
logger.info(f"\tSetting parameters for: {ipAddress}")
try:
pb.setActivePatternByName(pattern_name)
pb.setActiveControls({"sliderTempRed": float(red) / 256.0})
pb.setActiveControls({"sliderTempGreen": float(green) / 256.0})
pb.setActiveControls({"sliderTempBlue": float(blue) / 256.0})
pb.setActiveVariables({"moonIndex": float(MOON_PHASES[weather.daily_forecasts[0].moon_phase.name])})
pb.setBrightnessSlider(brightness)
if current_forecast.kind.value in WEATHER_KIND:
pb.setActiveVariables({"weatherIndex": float(WEATHER_KIND[current_forecast.kind.value])})
else:
pb.setActiveVariables({"weatherIndex": -1.0})
except Exception as e:
logger.info(f"Exception during setting variables {e}")
os.system("sudo reboot")
logger.info("Complete")
except Exception as e:
logger.info(f"Exception during setting weather and variables {e}")
#os.system("sudo reboot")
await asyncio.sleep(900)
if __name__ == '__main__':
asyncio.run(get_weather())