Files
prezenta_work/device_module.py
Developer a50197a9d6 Refactor: Skip socket resolution, load device hostname/IP directly from file
- Remove socket.gethostname() and socket.gethostbyname() calls
- Device info now loads exclusively from device_info.txt
- Device hostname and IP are independent of server hostname
- Resolves DNS/socket errors on startup
- Device info file is primary config source for device identity
2025-12-18 14:14:52 +02:00

54 lines
1.8 KiB
Python

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