Add comprehensive logging to calendar manager for debugging

This commit is contained in:
ChuckBuilds
2025-04-22 10:01:12 -05:00
parent 9f07b235ed
commit df0af37948

View File

@@ -13,8 +13,12 @@ from rgbmatrix import graphics
import pytz import pytz
from src.config_manager import ConfigManager from src.config_manager import ConfigManager
# Get logger without configuring
logger = logging.getLogger(__name__)
class CalendarManager: class CalendarManager:
def __init__(self, matrix, canvas, config): def __init__(self, matrix, canvas, config):
logger.info("Initializing CalendarManager")
self.matrix = matrix self.matrix = matrix
self.canvas = canvas self.canvas = canvas
self.config = config self.config = config
@@ -27,6 +31,8 @@ class CalendarManager:
self.events = [] self.events = []
self.service = None self.service = None
logger.info(f"Calendar configuration: enabled={self.enabled}, update_interval={self.update_interval}, max_events={self.max_events}, calendars={self.calendars}")
# Get display manager instance # Get display manager instance
from src.display_manager import DisplayManager from src.display_manager import DisplayManager
self.display_manager = DisplayManager._instance self.display_manager = DisplayManager._instance
@@ -53,27 +59,33 @@ class CalendarManager:
def authenticate(self): def authenticate(self):
"""Authenticate with Google Calendar API.""" """Authenticate with Google Calendar API."""
logger.info("Starting calendar authentication")
creds = None creds = None
token_file = self.calendar_config.get('token_file', 'token.pickle') token_file = self.calendar_config.get('token_file', 'token.pickle')
if os.path.exists(token_file): if os.path.exists(token_file):
logger.info(f"Loading credentials from {token_file}")
with open(token_file, 'rb') as token: with open(token_file, 'rb') as token:
creds = pickle.load(token) creds = pickle.load(token)
if not creds or not creds.valid: if not creds or not creds.valid:
logger.info("Credentials not found or invalid")
if creds and creds.expired and creds.refresh_token: if creds and creds.expired and creds.refresh_token:
logger.info("Refreshing expired credentials")
creds.refresh(Request()) creds.refresh(Request())
else: else:
logging.error("Calendar credentials not found or invalid. Please run calendar_registration.py first.") logger.info("Requesting new credentials")
self.enabled = False flow = InstalledAppFlow.from_client_secrets_file(
return self.calendar_config.get('credentials_file', 'credentials.json'),
['https://www.googleapis.com/auth/calendar.readonly'])
try: creds = flow.run_local_server(port=0)
self.service = build('calendar', 'v3', credentials=creds)
logging.info("Successfully authenticated with Google Calendar") logger.info(f"Saving credentials to {token_file}")
except Exception as e: with open(token_file, 'wb') as token:
logging.error(f"Error building calendar service: {str(e)}") pickle.dump(creds, token)
self.enabled = False
self.service = build('calendar', 'v3', credentials=creds)
logger.info("Calendar service built successfully")
def get_events(self): def get_events(self):
"""Fetch upcoming calendar events.""" """Fetch upcoming calendar events."""
@@ -99,6 +111,7 @@ class CalendarManager:
def draw_event(self, event, y_start=1): def draw_event(self, event, y_start=1):
"""Draw a single calendar event on the canvas. Returns True on success, False on error.""" """Draw a single calendar event on the canvas. Returns True on success, False on error."""
try: try:
logger.debug(f"Drawing event: {event.get('summary', 'No title')}")
# Get event details # Get event details
summary = event.get('summary', 'No Title') summary = event.get('summary', 'No Title')
time_str = self._format_event_time(event) time_str = self._format_event_time(event)
@@ -139,7 +152,7 @@ class CalendarManager:
return True # Return True on successful drawing return True # Return True on successful drawing
except Exception as e: except Exception as e:
logging.error(f"Error drawing calendar event: {str(e)}", exc_info=True) logger.error(f"Error drawing calendar event: {str(e)}", exc_info=True)
return False # Return False on error return False # Return False on error
def _wrap_text(self, text, max_width, font): def _wrap_text(self, text, max_width, font):
@@ -178,19 +191,21 @@ class CalendarManager:
def update(self, current_time): def update(self, current_time):
"""Update calendar events if needed.""" """Update calendar events if needed."""
if not self.enabled: if not self.enabled:
logger.debug("Calendar manager is disabled, skipping update")
return return
# Only fetch new events if the update interval has passed if current_time - self.last_update > self.update_interval:
if current_time - self.last_update >= self.update_interval: logger.info("Updating calendar events")
logging.info("Fetching new calendar events...")
self.events = self.get_events() self.events = self.get_events()
self.last_update = current_time self.last_update = current_time
if not self.events: if not self.events:
logging.info("No upcoming calendar events found.") logger.info("No upcoming calendar events found.")
else: else:
logging.info(f"Fetched {len(self.events)} calendar events.") logger.info(f"Fetched {len(self.events)} calendar events.")
# Reset index if events change # Reset index if events change
self.current_event_index = 0 self.current_event_index = 0
else:
logger.debug("Skipping calendar update - not enough time has passed")
def _format_event_date(self, event): def _format_event_date(self, event):
"""Format event date for display""" """Format event date for display"""
@@ -229,14 +244,13 @@ class CalendarManager:
def display(self): def display(self):
"""Display the current calendar event on the matrix""" """Display the current calendar event on the matrix"""
logging.debug(f"CalendarManager display called. Enabled: {self.enabled}, Events count: {len(self.events) if self.events is not None else 'None'}")
if not self.enabled: if not self.enabled:
logging.debug("CalendarManager display returning because not enabled.") logger.debug("Calendar manager is disabled, skipping display")
return return
if not self.events: if not self.events:
# Display "No Events" message if the list is empty # Display "No Events" message if the list is empty
logging.info("--> CalendarManager: Attempting to draw DEBUG (no events).") logger.info("--> CalendarManager: Attempting to draw DEBUG (no events).")
self.display_manager.clear() self.display_manager.clear()
self.display_manager.draw_text("Calendar DEBUG", small_font=True, color=self.text_color) self.display_manager.draw_text("Calendar DEBUG", small_font=True, color=self.text_color)
self.display_manager.update_display() self.display_manager.update_display()
@@ -246,10 +260,10 @@ class CalendarManager:
if self.current_event_index >= len(self.events): if self.current_event_index >= len(self.events):
self.current_event_index = 0 # Wrap around self.current_event_index = 0 # Wrap around
event_to_display = self.events[self.current_event_index] event_to_display = self.events[self.current_event_index]
logging.debug(f"CalendarManager displaying event index {self.current_event_index}: {event_to_display.get('summary')}") logger.debug(f"CalendarManager displaying event index {self.current_event_index}: {event_to_display.get('summary')}")
# Clear the display before drawing the current event # Clear the display before drawing the current event
logging.debug("CalendarManager clearing display for event.") logger.debug("CalendarManager clearing display for event.")
self.display_manager.clear() self.display_manager.clear()
# Draw the event # Draw the event
@@ -258,19 +272,20 @@ class CalendarManager:
if draw_successful: if draw_successful:
# Update the display # Update the display
self.display_manager.update_display() self.display_manager.update_display()
logging.debug("CalendarManager event display updated.") logger.debug("CalendarManager event display updated.")
else: else:
# Draw failed (error logged in draw_event), show debug message # Draw failed (error logged in draw_event), show debug message
logging.info("--> CalendarManager: Attempting to draw DEBUG (draw_event failed).") logger.info("--> CalendarManager: Attempting to draw DEBUG (draw_event failed).")
self.display_manager.clear() # Clear any partial drawing self.display_manager.clear() # Clear any partial drawing
self.display_manager.draw_text("Calendar DEBUG", small_font=True, color=self.text_color) self.display_manager.draw_text("Calendar DEBUG", small_font=True, color=self.text_color)
self.display_manager.update_display() self.display_manager.update_display()
def advance_event(self): def advance_event(self):
"""Advance to the next event. Called by DisplayManager when calendar display time is up.""" """Advance to the next event. Called by DisplayManager when calendar display time is up."""
if not self.events: if not self.enabled:
logger.debug("Calendar manager is disabled, skipping event advance")
return return
self.current_event_index += 1 self.current_event_index += 1
if self.current_event_index >= len(self.events): if self.current_event_index >= len(self.events):
self.current_event_index = 0 self.current_event_index = 0
logging.debug(f"CalendarManager advanced to event index {self.current_event_index}") logger.debug(f"CalendarManager advanced to event index {self.current_event_index}")