""" Device information management Handles hostname and IP address of the device Uses file-based configuration for reliability """ import os from config_settings import DEVICE_INFO_FILE def get_device_info(): """ Get device hostname and IP from configuration file Returns tuple: (hostname, device_ip) The hostname and IP are read from device_info.txt which should be configured with the device's own hostname and IP address. """ hostname = None device_ip = None # Load device info from file (primary method - no socket resolution) try: os.makedirs(os.path.dirname(DEVICE_INFO_FILE), exist_ok=True) with open(DEVICE_INFO_FILE, "r") as f: lines = f.read().strip().split('\n') if len(lines) >= 2: hostname = lines[0].strip() device_ip = lines[1].strip() print(f"Device Info - Hostname: {hostname}, IP: {device_ip}") return hostname, device_ip else: print(f"Warning: {DEVICE_INFO_FILE} exists but lacks valid data") except FileNotFoundError: print(f"Device info file not found at {DEVICE_INFO_FILE}") except Exception as e: print(f"Error reading device info file: {e}") # Fallback if file doesn't exist or has issues print("Using default device values") hostname = "prezenta-device" device_ip = "192.168.1.100" # Create file with default values for future use try: os.makedirs(os.path.dirname(DEVICE_INFO_FILE), exist_ok=True) with open(DEVICE_INFO_FILE, "w") as f: f.write(f"{hostname}\n{device_ip}\n") print(f"Created device info file with defaults: {hostname}, {device_ip}") except Exception as e: print(f"Warning: Could not create device info file: {e}") return hostname, device_ip