""" RFID reader initialization and handling using RDM6300 library Extends rdm6300.BaseReader to handle card events (insert/remove) and integrate with batch logging system """ import logging import time from config_settings import SERIAL_DEVICES, CONFIG_CARD_ID, DEVICE_INFO_FILE from logger_module import log_with_server from logger_batch_module import queue_log_message class RFIDReaderHandler: """Custom RFID Reader extending rdm6300.BaseReader for event handling""" def __init__(self, device_hostname, device_ip): """Initialize the reader handler""" self.device_hostname = device_hostname self.device_ip = device_ip self.reader = None def card_inserted(self, card): """Handle RFID card insertion event""" try: # Special handling for config card if card.value == CONFIG_CARD_ID: logging.info(f"Config card detected: {card.value}") queue_log_message("CONFIG_CARD_DETECTED", self.device_hostname) return # Log card insertion timestamp = time.strftime("%Y-%m-%d %H:%M:%S") msg = f"Card inserted - ID: {card.value}" logging.info(msg) queue_log_message(msg, self.device_hostname) except Exception as e: logging.error(f"Error handling card insertion: {e}") def card_removed(self, card): """Handle RFID card removal event""" try: # Special handling for config card if card.value == CONFIG_CARD_ID: logging.info(f"Config card removed: {card.value}") queue_log_message("CONFIG_CARD_REMOVED", self.device_hostname) return # Log card removal timestamp = time.strftime("%Y-%m-%d %H:%M:%S") msg = f"Card removed - ID: {card.value}" logging.info(msg) queue_log_message(msg, self.device_hostname) except Exception as e: logging.error(f"Error handling card removal: {e}") def initialize_rfid_reader(device_hostname=None, device_ip=None): """ Initialize RFID reader with RDM6300 library and multiple device attempts Args: device_hostname: Device hostname for logging device_ip: Device IP for logging Returns: Reader object or None if initialization fails """ try: from rdm6300 import BaseReader except ImportError: logging.error("✗ rdm6300 module not installed") print("✗ rdm6300 module not installed") log_with_server("ERROR: rdm6300 not installed", device_hostname, device_ip) return None logging.info("Initializing RFID reader with RDM6300...") print("Initializing RFID reader...") # Create handler for card events handler = RFIDReaderHandler(device_hostname or "unknown", device_ip or "0.0.0.0") for device in SERIAL_DEVICES: try: logging.info(f"Attempting to initialize RFID reader on {device}...") print(f"Attempting to initialize RFID reader on {device}...") # Create custom reader class that extends BaseReader class CustomReader(BaseReader): def card_inserted(self, card): handler.card_inserted(card) def card_removed(self, card): handler.card_removed(card) # Initialize reader reader = CustomReader(device) reader.start() logging.info(f"✓ RFID reader successfully initialized on {device}") print(f"✓ RFID reader successfully initialized on {device}") log_with_server(f"RFID reader started on {device}", device_hostname, device_ip) return reader except FileNotFoundError: logging.warning(f"✗ Device {device} not found") print(f"✗ Device {device} not found") continue except PermissionError: logging.warning(f"✗ Permission denied for {device}") print(f"✗ Permission denied for {device}") print(f" Hint: Try adding user to dialout group: sudo usermod -a -G dialout $USER") continue except Exception as e: logging.warning(f"✗ Failed to initialize on {device}: {e}") print(f"✗ Failed to initialize on {device}: {e}") continue # If we get here, all devices failed logging.error("✗ Could not initialize RFID reader on any device") print("✗ Could not initialize RFID reader on any device") print("Available solutions:") print(" 1. Check hardware connections") print(" 2. Enable UART: sudo raspi-config -> Interface Options -> Serial") print(" 3. Add user to dialout group: sudo usermod -a -G dialout $USER") print(" 4. Reboot the system after making changes") log_with_server("ERROR: RFID reader initialization failed on all devices", device_hostname, device_ip) return None