Commit cefe781c authored by Alyx's avatar Alyx
Browse files

erorr handling for when the thing doesn't have a valid ppm or temp but replies on spaceapi

parent f78536bb
Loading
Loading
Loading
Loading
+34 −6
Original line number Diff line number Diff line
@@ -5,16 +5,17 @@ import threading
import requests
import json
import datetime
from pathlib import Path

from flask import Flask, Response, abort


class Kronk:
    def __init__(self):

        self.ppm = 0
        self.panel_status = "init"
        self.panel = morbital.MatrixOrbitalPanel("/dev/ttyUSB0")
        self.space_status = "CLOSED"
        self.last_space_status_change = time.time()
        self.last_interaction_time = int(time.time())
        self.temperature = 0
        self.last_sensor_poll = int(time.time())
@@ -27,6 +28,26 @@ class Kronk:
        self.tasks = []
        self.running = True

        # load last state if available
        if Path("kronk.conf").exists():
            with open("kronk.conf", "r") as f:
                config = json.load(f)
                f.close()
                if (config["space_status"] in self.status_options):
                    self.space_status = config["space_status"]
                    self.last_space_status_change = config["last_space_status_change"]
                else:
                    self.space_status = "CLOSED"
                    self.last_space_status_change = time.time()
                print(f"Loaded previous state: {config}")
        else:
            self.space_status = "CLOSED"
            self.last_space_status_change = time.time()
            f = open("kronk.conf", "w")
            f.write(json.dumps(
                {'space_status': self.space_status, 'last_space_status_change': self.last_space_status_change}))
            f.close()

    def airquality_update(self):
        # if PPM CO2 is <1000, green light for bottom LED
        if self.ppm < 1000:
@@ -51,9 +72,16 @@ class Kronk:
            headers = {"User-Agent": "devhack kronk/python requests"}
            url = "https://devhack.net/spaceapi.json"
            sensors = requests.get(url, headers=headers).json()

            if (sensors["sensors"]["temperature"][0]["value"] is not None):
                self.temperature = sensors["sensors"]["temperature"][0]["value"]
            else:
                self.temperature = 0
            if (sensors["sensors"]["humidity"][1]["value"] is not None):
                self.ppm = sensors["sensors"]["humidity"][1]["value"]
        except (IndexError, requests.exceptions.RequestException) as e:
            else:
                self.ppm = 0
        except (TypeError, IndexError, requests.exceptions.RequestException) as e:
            print(f"error polling: {e}")
            self.temperature = 0
            self.ppm = 0
@@ -161,7 +189,7 @@ class Kronk:
                self.panel_status = "awake"
                # redraw panel
                self.full_draw()
            if char == "E" and self.panel_status not "in_menu":
            if char == "E" and self.panel_status != "in_menu":
                self.panel_status = "in_menu"
                self.draw_space_update_menu()
        if (self.panel_status == "in_menu" and self.menus[self.menu_index] == "DOORHOLD"):