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

@ -48,6 +48,7 @@ class State(rx.State):
_radio_client: Radio = Radio()
last_sports_update: float = 0.0
last_weather_fetch_time: float = 0.0
last_weather_image_path: str = ""
# --- Initialize Utility Client ---
def __init__(self, *args, **kwargs):
@ -183,9 +184,9 @@ class State(rx.State):
try:
if self.news:
async with self:
self.current_news_index = (self.current_news_index + 1) % len(
self.news
)
self.current_news_index = self.current_news_index + 1
if self.current_news_index >= len(self.news):
self.current_news_index = 0
yield
except Exception as e:
logging.error(
@ -219,8 +220,7 @@ class State(rx.State):
)
await asyncio.sleep(WEATHER_FETCH_INTERVAL)
continue
logging.info("deleting existing weather screenshot...")
self._weather_client.delete_old_screenshots(self.weather_img)
logging.info("Attempting to fetch weather screenshot...")
img_web_path = self._weather_client.get_weather_screenshot()
@ -232,6 +232,7 @@ class State(rx.State):
"%Y-%m-%d %H:%M:%S UTC"
)
self.last_weather_fetch_time = time.time()
logging.info(
f"State.weather_img updated to: {self.weather_img}"
)
@ -257,6 +258,7 @@ class State(rx.State):
logging.info("Weather fetch completed")
logging.info("Sleeping for next fetch")
await asyncio.sleep(WEATHER_FETCH_INTERVAL)
2
def index() -> rx.Component:

View file

@ -1,12 +1,16 @@
#!/usr/bin/python3
# connect to rda5807 chip and control it and display the current station
# TODO: reference rd library in readme
import logging
import reflex as rx
# from utils.python_rd5807m.radio import Radio as Radio_lib
# from utils.python_rd5807m.radio import Rda5807m as Radio_lib
DEBUG = True
CURRENT_STATION = "90.9 FM"
PLAYING = False
HARDWARE = True
class Radio(rx.Base):
@ -32,14 +36,63 @@ class Radio(rx.Base):
return radio_card
"""
class Radio_Control:
# Radio Control Class
#uses rda5807m library, if debugging populates false values for display
def __init__(self):
self.debug = DEBUG
self.bus = 1
self.poll_interval = 0.5
self.current_station = CURRENT_STATION
self.volume = 7
self.playing = False
self.signal = 0.0
self._device = None
self._display = None
def init_radio(self):
self.radio = Radio_lib()
self.radio.initialize()
# self._device = Radio_lib(self.bus)
self._device.init_chip()
def play_radio(self):
pass
if self.debug:
logging.debug("Playing fake radio")
self._display = rx.text("Playing") # may not work
self.playing = True
else:
self._device.on()
self.playing = True
def stop_radio(self):
if self.debug:
logging.debug("Stopping radio")
self._display = rx.text("Stopped") # may not work
self.playing = False
else:
self._device.off()
self.playing = False
def set_volume(self, volume):
if self.debug:
logging.debug(f"Setting volume to {volume}")
self.volume = volume
else:
self._device.set_volume(volume)
self.volume = volume
def set_station(self, station):
if self.debug:
logging.debug(f"Setting station to {station}")
self.current_station = station
else:
self._device.set_station(station)
self.current_station = station
logging.info(f"Station set to {station}")
"""
## for testing chip
# if __name__ == "__main__":

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}")