More code clean up

This commit is contained in:
Admar Schoonen 2023-05-27 09:43:20 +02:00
parent bfebe037d7
commit 6451752cf2
1 changed files with 51 additions and 59 deletions

110
clock.py
View File

@ -293,6 +293,17 @@ class MyClockWidget(FloatLayout):
uvi = []
uvi_thresholds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
def get_air_quality_metric(self, j, t, metric, periodicity):
x = []
try:
for i in j[metric]:
if i["time"]>= t - periodicity:
x.append(i["value"])
except:
print("Couldn't find metric " + metric)
return x
def get_air_quality(self, *args):
response = requests.get("https://sinoptik.luon.net/forecast?address=telefoonstraat%2018,%20eindhoven&metrics=precipitation&metrics=UVI&metrics=AQI&metrics=pollen&metrics=PAQI")
@ -301,45 +312,16 @@ class MyClockWidget(FloatLayout):
self.aqi = []
self.paqi = []
self.uvi = []
if response.status_code == 200:
t = datetime.datetime.now().timestamp()
j = json.loads(response.text)
try:
for i in j["precipitation"]:
if i["time"]>= t - 300:
self.rain.append(i["value"])
except:
print("Couldn't find precipitation")
try:
for i in j["pollen"]:
if i["time"] >= t - 3600:
self.pollen.append(i["value"])
except:
print("Couldn't find pollen")
try:
for i in j["AQI"]:
if i["time"] >= t - 3600:
self.aqi.append(i["value"])
except:
print("Couldn't find AQI")
try:
for i in j["PAQI"]:
if i["time"] >= t - 3600:
self.paqi.append(i["value"])
except:
print("Couldn't find PAQI")
try:
for i in j["UVI"]:
if i["time"] >= t - 24 * 3600:
self.uvi.append(i["value"])
except:
print("Couldn't find UVI")
self.rain = self.get_air_quality_metric(j, t, "precipitation", 300)
self.pollen = self.get_air_quality_metric(j, t, "pollen", 3600)
self.aqi = self.get_air_quality_metric(j, t, "AQI", 3600)
self.paqi = self.get_air_quality_metric(j, t, "PAQI", 3600)
self.uvi = self.get_air_quality_metric(j, t, "UVI", 3600 * 24)
else:
print("Error retrieving air quality; got response " + str(response))
@ -591,6 +573,24 @@ class MyClockWidget(FloatLayout):
))
self.ids["face"].add_widget(self.face_numbers[i - 1])
def draw_colored_circle(self, reference, x, y, r, color):
if color != [0.0, 0.0, 0.0]:
if color == [1, 1, 1]:
# Draw black circle
p = [x - r, y - r]
self.draw_list_curr_frame.append(["Color", reference.canvas, 0, 0, 0])
self.draw_list_curr_frame.append(["Ellipse", reference.canvas, [2 * r, 2 * r], p])
# Draw slightly smaller white circle
p = [x - r * 0.95, y - r * 0.95]
self.draw_list_curr_frame.append(["Color", reference.canvas, color[0], color[1], color[2]])
self.draw_list_curr_frame.append(["Ellipse", reference.canvas, [2 * r * 0.95, 2 * r * 0.95], p])
else:
# Draw slightly smaller colored circle
p = [x - r, y - r]
self.draw_list_curr_frame.append(["Color", reference.canvas, color[0], color[1], color[2]])
self.draw_list_curr_frame.append(["Ellipse", reference.canvas, [2 * r, 2 * r], p])
def draw_rain_aqi_expectation(self, x, x_thresholds, location):
if len(x) == 0:
return
@ -608,23 +608,15 @@ class MyClockWidget(FloatLayout):
idx = len(self.aqi_colors) - 1
color = self.aqi_colors[idx]
if color != [0.0, 0.0, 0.0]:
# Draw black circle
R = face_plate.size[0] / 2
r = R / 10 * 1.05
coord_x = 0.5 + location*math.sin(2 * math.pi * i/12)
coord_y = 0.5 + location*math.cos(2 * math.pi * i/12)
p = [face_plate.pos[0] + 2 * R * coord_x - r, face_plate.pos[1] + 2 * R * coord_y - r]
self.draw_list_curr_frame.append(["Color", face_plate.canvas, 0, 0, 0])
self.draw_list_curr_frame.append(["Ellipse", face_plate.canvas, [2 * r, 2 * r], p])
# Draw slightly smaller colored circle
r = R / 10
coord_x = 0.5 + location*math.sin(2 * math.pi * i/12)
coord_y = 0.5 + location*math.cos(2 * math.pi * i/12)
p = [face_plate.pos[0] + 2 * R * coord_x - r, face_plate.pos[1] + 2 * R * coord_y - r]
self.draw_list_curr_frame.append(["Color", face_plate.canvas, color[0], color[1], color[2]])
self.draw_list_curr_frame.append(["Ellipse", face_plate.canvas, [2 * r, 2 * r], p])
R = face_plate.size[0] / 2
r = R / 10
tmp_x = 0.5 + location*math.sin(2 * math.pi * i/12)
tmp_y = 0.5 + location*math.cos(2 * math.pi * i/12)
coord_x = face_plate.pos[0] + 2 * R * tmp_x
coord_y = face_plate.pos[1] + 2 * R * tmp_y
self.draw_colored_circle(face_plate, coord_x, coord_y, r, color)
def draw_rain_expectation(self):
self.draw_rain_aqi_expectation(self.rain, self.rain_thresholds, 0.45)
@ -642,26 +634,26 @@ class MyClockWidget(FloatLayout):
def update_face(self):
face_plate = self.ids["face_plate"]
self.draw_list_curr_frame.append(["canvas.clear()", face_plate])
with face_plate.canvas:
color = self.theme.color_numbers
self.draw_list_curr_frame.append(["Color", face_plate.canvas, color[0], color[1], color[2]])
r = face_plate.size[0] / 2
p = [face_plate.pos[0] + r, face_plate.pos[1] + r]
#self.draw_list_curr_frame.append(["Circle", face_plate.canvas, p, r])
#self.draw_list_curr_frame.append(["Ellipse", face_plate.canvas, face_plate.size, face_plate.pos])
if self.view == "set_alarm":
t = self.settings.alarm_time
else:
t = datetime.datetime.now()
if self.view == "clock":
if (self.view == "clock") and (self.rain != []) and (self.paqi != []):
for i in range(0, 12):
self.face_numbers[i].text = ""
self.draw_rain_expectation()
self.draw_aqi_expectation()
else:
with face_plate.canvas:
color = self.theme.color_numbers
self.draw_list_curr_frame.append(["Color", face_plate.canvas, color[0], color[1], color[2]])
r = face_plate.size[0] / 2
p = [face_plate.pos[0] + r, face_plate.pos[1] + r]
self.draw_list_curr_frame.append(["Circle", face_plate.canvas, p, r])
for i in range(0, 12):
if t.hour < 12:
offset = 0