diff --git a/data/device_info.txt b/data/device_info.txt index 8c72c65..c83108f 100644 --- a/data/device_info.txt +++ b/data/device_info.txt @@ -1,2 +1,2 @@ -TestLinux -127.0.1.1 +RPI-Device +192.168.1.100 diff --git a/device_module.py b/device_module.py index 8aa4594..d2c3c0b 100644 --- a/device_module.py +++ b/device_module.py @@ -1,85 +1,53 @@ """ Device information management -Handles hostname, IP address, and device configuration +Handles hostname and IP address of the device +Uses file-based configuration for reliability """ -import socket import os from config_settings import DEVICE_INFO_FILE def get_device_info(): """ - Get hostname and device IP with file-based fallback + 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 - # Try to get current hostname and IP + # Load device info from file (primary method - no socket resolution) try: - hostname = socket.gethostname() - device_ip = socket.gethostbyname(hostname) - print(f"Successfully resolved - Hostname: {hostname}, IP: {device_ip}") - - # Save the working values to file for future fallback - 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"Saved device info to {DEVICE_INFO_FILE}") - except Exception as e: - print(f"Warning: Could not save device info to file: {e}") - - return hostname, device_ip - - except socket.gaierror as e: - print(f"Socket error occurred: {e}") - print("Attempting to load device info from file...") - - # Try to load from file - try: - 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"Loaded from file - Hostname: {hostname}, IP: {device_ip}") - return hostname, device_ip - else: - print("File exists but doesn't contain valid data") - except FileNotFoundError: - print(f"No fallback file found at {DEVICE_INFO_FILE}") - except Exception as e: - print(f"Error reading fallback file: {e}") - + 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"Unexpected error getting device info: {e}") - - # Try to load from file as fallback - try: - 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"Loaded from file after error - Hostname: {hostname}, IP: {device_ip}") - return hostname, device_ip - except Exception as file_error: - print(f"Could not load from file: {file_error}") + print(f"Error reading device info file: {e}") - # Final fallback if everything fails - print("All methods failed - Using default values") - hostname = hostname or "unknown-device" - device_ip = "127.0.0.1" + # Fallback if file doesn't exist or has issues + print("Using default device values") + hostname = "prezenta-device" + device_ip = "192.168.1.100" - # Try to save these default values for next time + # 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"Saved fallback values to {DEVICE_INFO_FILE}") + print(f"Created device info file with defaults: {hostname}, {device_ip}") except Exception as e: - print(f"Could not save fallback values: {e}") + print(f"Warning: Could not create device info file: {e}") return hostname, device_ip