Compare commits

5 Commits
v0.1 ... trunk

Author SHA1 Message Date
1fb2074e3e feat(airly): reorganise gauges into own lines, drop displaying advice
Advice provided by airly seldom corresponds to air quality.
2022-03-18 08:13:47 +01:00
dacce95f79 refactor: small changes and reformats 2022-01-30 10:38:47 +01:00
c79320409b fix: output leading zeros in minutes 2022-01-29 14:05:07 +01:00
5d1905df00 refactor: extract data resolution logic into config 2022-01-28 16:46:12 +01:00
27a6846c64 refactor: work around os.environ missing in micropython
Work with micropython, but keep ability to override values with
enviromental variables (for possible running in k8s).
2022-01-28 09:46:04 +01:00
2 changed files with 45 additions and 39 deletions

View File

@@ -1,20 +1,19 @@
import os LATITUDE = ""
LONGITUDE = ""
def configure_env(): GOOGLE_API_KEY = ""
""" exports environment variables with configuration """ TRAFFIC_FROM = "…+FM"
#["TRAFFIC_FROM = f"${LATITUDE}-${LONGITUDE}"
TRAFFIC_1_THROUGH = "…+4FP"
TRAFFIC_2_THROUGH = "…+V9J"
TRAFFIC_TO = "…+GX"
os.environ["LATITUDE"] = "" AIRLY_API_KEY = ""
os.environ["LONGITUDE"] = ""
os.environ["GOOGLE_API_KEY"] = "" OPENWEATHERMAP_API_KEY = ""
os.environ["TRAFFIC_FROM"] = "…+FM"
#os.environ["TRAFFIC_FROM"] = f"${LATITUDE}-${LONGITUDE}"
os.environ["TRAFFIC_1_THROUGH"] = "…+4FP"
os.environ["TRAFFIC_2_THROUGH"] = "…+V9J"
os.environ["TRAFFIC_TO"] = "…+GX"
os.environ["AIRLY_API_KEY"] = ""
os.environ["OPENWEATHERMAP_API_KEY"] = ""
# the plan is to have morning high resolution information display
# and refresh, and lower resolution during rest of the day
# (at night potentially off)
low_resolution = False

View File

@@ -45,9 +45,9 @@ class ClockSegment(DisplaySegment):
def update(self): def update(self):
now = datetime.datetime.now() now = datetime.datetime.now()
if now.hour >= 6 and now.hour < 7: if not config.low_resolution:
self.validity = 60 self.validity = 60
self.time_display = f"{now.hour}:{now.minute}" self.time_display = f"{now.hour}:{now.minute:02d}"
return return
self.validity = 5*60 self.validity = 5*60
@@ -64,27 +64,26 @@ class TrafficSegment(DisplaySegment):
duration = {} duration = {}
def update(self): def update(self):
now = datetime.datetime.now() if config.low_resolution:
if now.hour >= 6 and now.hour < 7:
self.validity = 60
else:
self.validity = 60*60 self.validity = 60*60
logging.debug(f"{type(self)}: Not in the morning, skipping update") logging.debug(f"{type(self)}: Not in the morning, skipping update")
return return ""
else:
self.validity = 60
url = "https://maps.googleapis.com/maps/api/directions/json?" url = "https://maps.googleapis.com/maps/api/directions/json?"
travel_parameters = { travel_parameters = {
"origin": os.getenv("TRAFFIC_FROM"), "origin": os.getenv("TRAFFIC_FROM") or config.TRAFFIC_FROM,
"destination": os.getenv("TRAFFIC_TO"), "destination": os.getenv("TRAFFIC_TO") or config.TRAFFIC_TO,
"waypoints": "to be filled", "waypoints": "to be filled",
"departure_time": int(time.time() + 5*60), # leave in 5 minutes "departure_time": int(time.time() + 5*60), # leave in 5 minutes
"mode": "driving", "mode": "driving",
"key": os.getenv("GOOGLE_API_KEY") "key": os.getenv("GOOGLE_API_KEY") or config.GOOGLE_API_KEY
} }
for i_point in (1, 2): for i_point in (1, 2):
travel_parameters["waypoints"] = f"via:{os.getenv(f'TRAFFIC_{i_point}_THROUGH')}" through = os.getenv(f'TRAFFIC_{i_point}_THROUGH') or eval(f"config.TRAFFIC_{i_point}_THROUGH")
travel_parameters["waypoints"] = f"via:{through}"
req = requests.get(url=url + urllib.parse.urlencode(travel_parameters)) req = requests.get(url=url + urllib.parse.urlencode(travel_parameters))
if not req.ok: if not req.ok:
logging.info(f"{type(self)}: Google Traffic API call via point {i_point} failed {req.status_code}") logging.info(f"{type(self)}: Google Traffic API call via point {i_point} failed {req.status_code}")
@@ -120,11 +119,12 @@ class WeatherSegment(DisplaySegment):
return m_per_s * 3600 / 1000 return m_per_s * 3600 / 1000
def update(self): def update(self):
lat = os.getenv("LATITUDE") lat = os.getenv("LATITUDE") or config.LATITUDE
long = os.getenv("LONGITUDE") long = os.getenv("LONGITUDE") or config.LONGITUDE
owm_api_key = os.getenv("OPENWEATHERMAP_API_KEY") or config.OPENWEATHERMAP_API_KEY
self.validity = 6*60*60 self.validity = 6*60*60
req = requests.get(f'https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={long}&appid={os.getenv("OPENWEATHERMAP_API_KEY")}&units=metric&exclude=minutely,hourly,alerts&lang=pl') req = requests.get(f'https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={long}&appid={owm_api_key}&units=metric&exclude=minutely,hourly,alerts&lang=pl')
if not req.ok: if not req.ok:
logging.info(f"{type(self)}: OpenWeatherMap API call failed {req.status_code}") logging.info(f"{type(self)}: OpenWeatherMap API call failed {req.status_code}")
else: else:
@@ -176,7 +176,7 @@ class AirSegment(DisplaySegment):
return "[]" return "[]"
per_character = (end - start) / (length - 2) per_character = (end - start) / (length - 2)
middle = (end - start) / 2 middle = (end - start) / 2
fill_char = "+" fill_char = "#"
gauge = "[" gauge = "["
for i in range(0, length - 2): for i in range(0, length - 2):
@@ -190,20 +190,21 @@ class AirSegment(DisplaySegment):
return gauge return gauge
def _get_data_text(self): def _get_data_text(self):
ret = f"{self.text_gauge(value=self.percent_pm25)} PM2,5: {self.percent_pm25}% ; " ret = f"{self.text_gauge(value=self.percent_pm25)} PM2,5: {self.percent_pm25}%\n"
ret+= f"{self.text_gauge(value=self.percent_pm10)} PM10: {self.percent_pm10}% \n " ret+= f"{self.text_gauge(value=self.percent_pm10)} PM10: {self.percent_pm10}%\n"
ret+= f"{self.description} / {self.advice}" ret+= f"{self.description}"
#ret+= f"{self.advice}"
return ret return ret
def update(self): def update(self):
lat = os.getenv("LATITUDE") lat = os.getenv("LATITUDE") or config.LATITUDE
long = os.getenv("LONGITUDE") long = os.getenv("LONGITUDE") or config.LONGITUDE
self.validity = 30*60 self.validity = 30*60
headers = { headers = {
"Accept": "application/json", "Accept": "application/json",
"Accept-Language": "pl", "Accept-Language": "pl",
"apikey": os.getenv("AIRLY_API_KEY") "apikey": os.getenv("AIRLY_API_KEY") or config.AIRLY_API_KEY
} }
req = requests.get(f"https://airapi.airly.eu/v2/measurements/point?lat={lat}&lng={long}&l=pl", req = requests.get(f"https://airapi.airly.eu/v2/measurements/point?lat={lat}&lng={long}&l=pl",
@@ -227,8 +228,7 @@ class AirSegment(DisplaySegment):
self.temperature = measurement["value"] self.temperature = measurement["value"]
if __name__ == "__main__": if __name__ == "__main__":
config.configure_env() # logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
segments = [ segments = [
ClockSegment(), ClockSegment(),
WeatherSegment(), WeatherSegment(),
@@ -239,3 +239,10 @@ if __name__ == "__main__":
for segment in segments: for segment in segments:
print(segment.get_data_text()) print(segment.get_data_text())
print("---") print("---")
now = datetime.datetime.now()
if now.hour >= 6 and now.hour < 7:
config.low_resolution = False
else:
config.low_resolution = True