diff --git a/__pycache__/dependency_utils.cpython-311.pyc b/__pycache__/dependency_utils.cpython-311.pyc new file mode 100644 index 0000000..313fe97 Binary files /dev/null and b/__pycache__/dependency_utils.cpython-311.pyc differ diff --git a/app.py b/app.py index 03d51f1..f4e2092 100644 --- a/app.py +++ b/app.py @@ -2,50 +2,24 @@ import os import sys import subprocess -import importlib -import importlib.util import stat import pwd import grp +# Import dependency check/install functions from dependency_utils +from dependency_utils import install_package_from_wheel, check_and_install_dependencies + # Global configuration mode flag CONFIGURATION_MODE = False -def install_package_from_wheel(wheel_path, package_name): - """ - Install a Python package from a wheel file - """ - try: - print(f"Installing {package_name} from {wheel_path}...") - result = subprocess.run([ - sys.executable, "-m", "pip", "install", wheel_path, - "--no-index", "--no-deps", "--break-system-packages", - "--no-warn-script-location", "--force-reinstall" - ], capture_output=True, text=True, timeout=60) - - if result.returncode == 0: - print(f"✓ {package_name} installed successfully") - return True - else: - print(f"✗ Failed to install {package_name}: {result.stderr}") - return False - except Exception as e: - print(f"✗ Error installing {package_name}: {e}") - return False - -def check_and_install_dependencies(): - """ - Check if required packages are installed and install them from local repository if needed - """ - print("Checking and installing dependencies...") - - # Define required packages and their corresponding wheel files - required_packages = { +# Run dependency check before importing anything else +try: + check_and_install_dependencies({ 'rdm6300': 'rdm6300-0.1.1-py3-none-any.whl', - 'gpiozero': None, # System package, should be pre-installed + 'gpiozero': None, 'requests': 'requests-2.32.3-py3-none-any.whl', 'aiohttp': 'aiohttp-3.11.18-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl', - 'flask': None, # Will try to install if needed + 'flask': None, 'urllib3': 'urllib3-2.3.0-py3-none-any.whl', 'certifi': 'certifi-2025.1.31-py3-none-any.whl', 'charset_normalizer': 'charset_normalizer-3.4.1-py3-none-any.whl', @@ -57,78 +31,7 @@ def check_and_install_dependencies(): 'yarl': 'yarl-1.20.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl', 'aiohappyeyeballs': 'aiohappyeyeballs-2.6.1-py3-none-any.whl', 'propcache': 'propcache-0.3.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl' - } - - repository_path = "./Files/reposytory" - missing_packages = [] - - # Check each required package - for package_name, wheel_file in required_packages.items(): - try: - # Use importlib to check if package exists - spec = importlib.util.find_spec(package_name) - if spec is not None: - print(f"✓ {package_name} is already installed") - else: - raise ImportError(f"Package {package_name} not found") - - except ImportError: - print(f"✗ {package_name} is not installed") - missing_packages.append((package_name, wheel_file)) - except Exception as e: - print(f"✗ Error checking {package_name}: {e}") - missing_packages.append((package_name, wheel_file)) - - # Install missing packages - if missing_packages: - print(f"\nInstalling {len(missing_packages)} missing packages...") - - for package_name, wheel_file in missing_packages: - if wheel_file is None: - # Try to install via pip from internet or system packages - try: - print(f"Attempting to install {package_name} via pip...") - result = subprocess.run([ - sys.executable, "-m", "pip", "install", package_name, - "--break-system-packages", "--no-warn-script-location" - ], capture_output=True, text=True, timeout=120) - - if result.returncode == 0: - print(f"✓ {package_name} installed via pip") - else: - print(f"✗ Could not install {package_name} via pip: {result.stderr}") - # Try system package manager for common packages - if package_name in ['flask', 'gpiozero']: - try: - print(f"Trying to install {package_name} via apt...") - apt_name = f"python3-{package_name.replace('_', '-')}" - apt_result = subprocess.run([ - "sudo", "apt", "install", "-y", apt_name - ], capture_output=True, text=True, timeout=300) - if apt_result.returncode == 0: - print(f"✓ {package_name} installed via apt") - else: - print(f"✗ Could not install {package_name} via apt") - except Exception as apt_e: - print(f"✗ Error installing {package_name} via apt: {apt_e}") - except Exception as e: - print(f"✗ Error installing {package_name} via pip: {e}") - continue - - wheel_path = os.path.join(repository_path, wheel_file) - - if not os.path.exists(wheel_path): - print(f"✗ Wheel file not found: {wheel_path}") - continue - - # Install the package - install_package_from_wheel(wheel_path, package_name) - - print("Dependency check completed.\n") - -# Run dependency check before importing anything else -try: - check_and_install_dependencies() + }, "./Files/reposytory") except Exception as e: print(f"Warning: Dependency check failed: {e}") print("Continuing with existing packages...") @@ -246,31 +149,29 @@ def early_launch_configuration_mode(): print(f"📄 Loading Screen.html from: {screen_html_path}") - # Launch chromium in kiosk mode with the local HTML file - chromium_command = [ - 'chromium-browser', - '--kiosk', - '--no-sandbox', - '--disable-dev-shm-usage', - '--disable-gpu', - '--disable-software-rasterizer', - '--disable-background-timer-throttling', - '--disable-backgrounding-occluded-windows', - '--disable-renderer-backgrounding', - f'file://{screen_html_path}' - ] - - print("🚀 Starting Chromium in configuration mode...") - process = subprocess.Popen(chromium_command, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL) - + + # Terminate any existing Chromium processes + chromium_process_name = "chromium" + log_info_with_server("Refreshing Chromium process.") + try: + subprocess.run(["pkill", "-f", chromium_process_name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + time.sleep(5) # Wait for processes to terminate + # Launch Chromium with Screen.html + url = f"file://{screen_html_path}" + subprocess.Popen( + ["chromium", "--test-type", "--noerrors", "--kiosk", "--start-fullscreen", + url], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL, start_new_session=True + ) + log_info_with_server("Chromium process restarted successfully.") + except Exception as e: + log_info_with_server(f"Failed to refresh Chromium process: {e}") + return False print("✅ Configuration mode launched successfully") print("ℹ️ Network connectivity checks disabled") print("ℹ️ Server status messages disabled") print("🔧 Device running in configuration mode") print("🔧 To exit configuration mode, change idmasa.txt content to device name") - return True except FileNotFoundError: diff --git a/data/log.txt b/data/log.txt index 743e7d9..f566186 100644 --- a/data/log.txt +++ b/data/log.txt @@ -28,3 +28,35 @@ 2025-09-24 16:26:44,794 - INFO - LED controls initialized 2025-09-24 16:26:44,794 - INFO - Variabila Id Masa A fost initializata 2025-09-24 16:26:44,799 - INFO - idmasa +2025-09-24 17:16:50,868 - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:80 + * Running on http://10.76.157.86:80 +2025-09-24 17:16:50,869 - INFO - Press CTRL+C to quit +2025-09-24 17:16:52,857 - INFO - Log file is not older than 10 days: log.txt (n_masa: notconfig) +2025-09-24 17:16:52,857 - INFO - Configuration mode: Server logging disabled +2025-09-24 17:16:52,866 - INFO - LED controls initialized (n_masa: notconfig) +2025-09-24 17:16:52,867 - INFO - Configuration mode: Server logging disabled +2025-09-24 17:16:52,867 - INFO - LED controls initialized +2025-09-24 17:16:52,867 - INFO - Variabila Id Masa A fost initializata +2025-09-24 17:16:52,867 - INFO - idmasa +2025-09-24 17:33:06,772 - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:80 + * Running on http://10.89.219.105:80 +2025-09-24 17:33:06,773 - INFO - Press CTRL+C to quit +2025-09-24 17:33:08,754 - INFO - Log file is not older than 10 days: log.txt (n_masa: notconfig) +2025-09-24 17:33:08,762 - INFO - 127.0.0.1 - - [24/Sep/2025 17:33:08] "POST /logs HTTP/1.1" 404 - +2025-09-24 17:33:08,764 - ERROR - Failed to send log to server: 404 Client Error: NOT FOUND for url: http://rpi-ansible:80/logs +2025-09-24 17:33:08,767 - INFO - Internet connection check loaded (n_masa: notconfig) +2025-09-24 17:33:08,775 - INFO - 127.0.0.1 - - [24/Sep/2025 17:33:08] "POST /logs HTTP/1.1" 404 - +2025-09-24 17:33:08,777 - ERROR - Failed to send log to server: 404 Client Error: NOT FOUND for url: http://rpi-ansible:80/logs +2025-09-24 17:33:08,777 - INFO - Log file is not older than 10 days: log.txt (n_masa: notconfig) +2025-09-24 17:33:08,782 - INFO - 127.0.0.1 - - [24/Sep/2025 17:33:08] "POST /logs HTTP/1.1" 404 - +2025-09-24 17:33:08,784 - ERROR - Failed to send log to server: 404 Client Error: NOT FOUND for url: http://rpi-ansible:80/logs +2025-09-24 17:33:08,785 - INFO - LED controls initialized (n_masa: notconfig) +2025-09-24 17:33:08,799 - INFO - 127.0.0.1 - - [24/Sep/2025 17:33:08] "POST /logs HTTP/1.1" 404 - +2025-09-24 17:33:08,802 - ERROR - Failed to send log to server: 404 Client Error: NOT FOUND for url: http://rpi-ansible:80/logs +2025-09-24 17:33:08,802 - INFO - LED controls initialized +2025-09-24 17:33:08,802 - INFO - Variabila Id Masa A fost initializata +2025-09-24 17:33:08,803 - INFO - idmasa diff --git a/dependency_utils.py b/dependency_utils.py new file mode 100644 index 0000000..d9acad6 --- /dev/null +++ b/dependency_utils.py @@ -0,0 +1,58 @@ +import importlib.util +import subprocess +import sys +import os + +def install_package_from_wheel(wheel_path, package_name): + """ + Install a Python package from a wheel file + """ + try: + print(f"Installing {package_name} from {wheel_path}...") + result = subprocess.run([ + sys.executable, "-m", "pip", "install", wheel_path, + "--no-index", "--no-deps", "--break-system-packages", + "--no-warn-script-location", "--force-reinstall" + ], capture_output=True, text=True, timeout=60) + if result.returncode == 0: + print(f"✓ {package_name} installed successfully") + return True + else: + print(f"✗ Failed to install {package_name}: {result.stderr}") + return False + except Exception as e: + print(f"✗ Error installing {package_name}: {e}") + return False + +def check_and_install_dependencies(required_packages, repository_path): + """ + Check if required packages are installed and install them from local repository if needed + """ + print("Checking and installing dependencies...") + missing_packages = [] + for package_name, wheel_file in required_packages.items(): + try: + spec = importlib.util.find_spec(package_name) + if spec is not None: + continue # Already installed + else: + print(f"✗ {package_name} is not installed") + missing_packages.append((package_name, wheel_file)) + except ImportError: + print(f"✗ {package_name} is not installed") + missing_packages.append((package_name, wheel_file)) + except Exception as e: + print(f"✗ Error checking {package_name}: {e}") + missing_packages.append((package_name, wheel_file)) + if missing_packages: + print(f"\nInstalling {len(missing_packages)} missing packages...") + for package_name, wheel_file in missing_packages: + if wheel_file is None: + print(f"No wheel file for {package_name}, please install manually if needed.") + continue + wheel_path = os.path.join(repository_path, wheel_file) + if not os.path.exists(wheel_path): + print(f"✗ Wheel file not found: {wheel_path}") + continue + install_package_from_wheel(wheel_path, package_name) + print("Dependency check completed.\n")