cleaned up weather, added logging instead of prints, began radio control

This commit is contained in:
death916 2025-12-19 05:03:38 -08:00
parent 28bd2c6078
commit b2320fd073
3 changed files with 93 additions and 22 deletions

View file

@ -1,8 +1,8 @@
# /home/death916/code/python/deathclock/utils/weather.py
import datetime
import logging # Optional: Use logging for better error messages
import os
import subprocess
import time
import reflex as rx
@ -11,9 +11,10 @@ logging.basicConfig(
)
# Define the target filename consistently
WEATHER_FILENAME = "weather.png"
WEATHER_FILENAME = ""
# Define the web path expected by the frontend
WEATHER_WEB_PATH = f"/{WEATHER_FILENAME}" # This should be relative to the assets dir
LAST_FILENAME = WEATHER_FILENAME
class Weather(rx.Base):
@ -34,15 +35,26 @@ class Weather(rx.Base):
logging.error(f"Failed to create assets directory {assets_dir}: {e}")
return assets_dir
def delete_old_screenshots(self, assets_dir: str):
def delete_old_screenshots(self):
"""Deletes the specific weather file in the given 'assets' directory."""
target_file = os.path.join(assets_dir, WEATHER_FILENAME)
if os.path.exists(target_file):
try:
os.remove(target_file)
logging.info(f"Deleted old weather file: {target_file}")
except OSError as e:
logging.error(f"Failed to delete old weather file {target_file}: {e}")
assets_dir = self._get_assets_dir()
global LAST_FILENAME
try:
for fn in os.listdir(assets_dir):
if not fn.endswith("weather.png"):
continue
if LAST_FILENAME and fn == LAST_FILENAME:
logging.info(f"Skipping deletion of current weather file: {fn}")
continue
path = os.path.join(assets_dir, fn)
if os.path.isfile(path):
try:
os.remove(path)
logging.info(f"Deleted old weather file: {path}")
except OSError as e:
logging.error(f"Failed to delete old weather file {path}: {e}")
except Exception as e:
logging.error(f"Error cleaning old weather files in {assets_dir}: {e}")
def get_weather_screenshot(self) -> str | None:
"""
@ -50,10 +62,12 @@ class Weather(rx.Base):
Returns the web path (e.g., '/weather.jpg') or None on failure.
"""
assets_dir = self._get_assets_dir()
global WEATHER_FILENAME
global LAST_FILENAME
WEATHER_FILENAME = str(time.time()) + "weather.png"
screenshot_path = os.path.join(assets_dir, WEATHER_FILENAME)
# Delete the old file before creating the new one
if os.path.exists(screenshot_path):
self.delete_old_screenshots(assets_dir)
LAST_FILENAME = screenshot_path
curl_command = [
"curl",
@ -70,6 +84,8 @@ class Weather(rx.Base):
logging.info(
f"Curl command successful. Weather image saved to: {screenshot_path}"
)
global WEATHER_WEB_PATH
WEATHER_WEB_PATH = f"/{WEATHER_FILENAME}"
return WEATHER_WEB_PATH
except subprocess.CalledProcessError as e:
logging.error(f"Curl command failed for path {screenshot_path}: {e}")