Error handling

This commit is contained in:
Asaril 2021-05-23 18:35:57 +02:00
parent 80fc25482c
commit b66b48fae6

View file

@ -62,7 +62,7 @@ time_str = time.strftime(CLOCK_FORMAT, now_parts)
temp_str = TEMP_FORMAT % -10.0
clock_box=TextBox( (10,10), (300,120), JITTER, (255,255,255), BKGND_COLOR, time_str)
temp_box =TextBox( (120,180), (190,50), JITTER, (255,0,0), BKGND_COLOR, temp_str)
temp_box =TextBox( (100,160), (200,60), JITTER, (255,0,0), BKGND_COLOR, temp_str)
display = Framebuffer(1)
fb_img = Image.new(mode="RGB", size=DISPLAY_SIZE)
@ -71,14 +71,27 @@ fb_draw = ImageDraw.Draw(fb_img)
mean_temp = 0
def fetch_temp(bridge, user, sensor):
r=requests.get(f"http://{bridge}/api/{user}/sensors/{str(sensor)}")
try:
r=requests.get(f"http://{bridge}/api/{user}/sensors/{str(sensor)}")
except requests.exceptions.ConnectionError:
r=None
if r:
return r.json()["state"]["temperature"]/100
json = r.json()
if 'state' in json and 'temperature' in json['state'] and json['state']['temperature']:
return json["state"]["temperature"]/100
return None
def jitter():
return (random.randint(0,JITTER), random.randint(0,JITTER))
def set_brightness(now_parts):
if now_parts.tm_hour > 7 and now_parts.tm_hour < 19:
subprocess.run(GPIO_TOOL+DAY_BRIGHT)
else:
subprocess.run(GPIO_TOOL+NIGHT_BRIGHT)
# Main loop:
jitter_pos = jitter()
@ -86,7 +99,8 @@ old_time=""
old_temp=""
subprocess.run(GPIO_TOOL+PWM_MODE)
subprocess.run(GPIO_TOOL+NIGHT_BRIGHT)
set_brightness(now_parts)
fb_draw.rectangle(((0,0),DISPLAY_SIZE),BKGND_COLOR, BKGND_COLOR,1)
temp_time=time.time()
@ -96,23 +110,21 @@ try:
now = time.time()
now_parts = time.localtime(now)
time_str = time.strftime(CLOCK_FORMAT, now_parts)
if now-temp_time > TEMP_DELAY:
cur_temp = fetch_temp("philips-hue", "GQ03rw1saUS0n88G5yj9j7-TsteFIE1yxtlBOgzD", 71)
if cur_temp:
mean_temp = cur_temp
temp_time = now
temp_str = TEMP_FORMAT % mean_temp
else:
temp_str = "---"
if now_parts.tm_sec == 0:
jitter_pos = jitter()
if (now_parts.tm_min == 0) and (now_parts.tm_hour > 7 and now_parts.tm_hour < 19):
subprocess.run(GPIO_TOOL+DAY_BRIGHT)
else:
subprocess.run(GPIO_TOOL+NIGHT_BRIGHT)
time_str = time.strftime(CLOCK_FORMAT, now_parts)
temp_str = TEMP_FORMAT % mean_temp
if now_parts.tm_min == 0:
set_brightness(now_parts)
if time_str != old_time:
clock_box.draw_text(fb_img, time_str, jitter_pos)