diff --git a/app.py b/app.py index d4a2a70..91c5a48 100644 --- a/app.py +++ b/app.py @@ -216,7 +216,7 @@ def load_config(): def _get_mac_address(): """Return the MAC address of the primary network interface.""" - for iface in ['eth0', 'wlan0', 'eth1']: + for iface in ['wlan0', 'eth1', 'eth0']: mac_path = f'/sys/class/net/{iface}/address' try: if os.path.exists(mac_path): @@ -382,8 +382,24 @@ def sync_config_with_server(): # Load global application configuration APP_CONFIG = load_config() +# --------------------------------------------------------------------------- +# Template image guard +# --------------------------------------------------------------------------- +# If the hostname is "TestTemplate" this SD card is the master deployment image. +# It must NEVER contact the monitoring server – doing so would create a ghost +# device record for every freshly cloned card before it is configured. +TEMPLATE_IMAGE = APP_CONFIG.get("device_hostname", "").strip() == "TestTemplate" +if TEMPLATE_IMAGE: + print("=" * 60) + print("TEMPLATE IMAGE DETECTED (hostname = TestTemplate)") + print("ALL server communication DISABLED") + print("Clone and configure this image before deploying.") + print("=" * 60) + # Attempt to sync with server at startup (non-blocking – failures are logged and ignored) -sync_config_with_server() +# Skipped for the base template image. +if not TEMPLATE_IMAGE: + sync_config_with_server() # --------------------------------------------------------------------------- # Card presence flag – controls whether RFID functions are started @@ -555,11 +571,18 @@ try: if early_launch_configuration_mode(): print("🚀 Configuration mode active - application will run in setup mode") print("âš ī¸ Network connectivity checks are DISABLED") - print("âš ī¸ Server status messages are DISABLED") + print("â„šī¸ Server logs ENABLED so admin can see this unconfigured device") print("🔧 Change config.txt [device] name to exit configuration mode") CONFIGURATION_MODE = True print("🔧 Configuration mode activated - continuing with RFID reader initialization") print("🔧 Network and server monitoring will remain disabled") + # Notify server that an unconfigured device is alive and waiting + if not TEMPLATE_IMAGE: + try: + _send_first_registration_log() + print("✅ Sent 'waiting for configuration' heartbeat to server") + except Exception as _hb_err: + print(f"Warning: could not send notconfig heartbeat: {_hb_err}") else: print("❌ Failed to launch configuration mode, continuing with normal operation") CONFIGURATION_MODE = False @@ -977,7 +1000,7 @@ def send_log_to_server(log_message, n_masa, hostname, device_ip): "mac_address": _get_mac_address(), "card_presence": APP_CONFIG.get("card_presence", "enable"), } - server_url = APP_CONFIG.get("server_log_url", "http://rpi-ansible:80/logs") + server_url = APP_CONFIG.get("server_log_url", "http://rpi-ansible.sibiusb.harting.intra:80/logs") print(log_data) # Debugging: Print log_data to verify its contents response = requests.post(server_url, json=log_data, timeout=5) response.raise_for_status() @@ -989,16 +1012,18 @@ def log_info_with_server(message): n_masa = read_name_from_file() # Read work place name from config formatted_message = f"{message} (n_masa: {n_masa})" # Format the message logging.info(formatted_message) # Log the formatted message - - # Only send to server if not in configuration mode + + # Template images must never contact the server. + # notconfig devices ARE allowed to send logs so the server can show them + # in the "To Be Configured" section of the Update Requests page. try: - if not CONFIGURATION_MODE: - send_log_to_server(message, n_masa, hostname, device_ip) # Send the original message to the server + if not TEMPLATE_IMAGE: + send_log_to_server(message, n_masa, hostname, device_ip) else: - logging.info("Configuration mode: Server logging disabled") + logging.info("Template image: Server logging disabled") except NameError: - # CONFIGURATION_MODE not defined yet (during initialization) - send_log_to_server(message, n_masa, hostname, device_ip) # Send the original message to the server + # TEMPLATE_IMAGE not defined yet (during initialization) + send_log_to_server(message, n_masa, hostname, device_ip) # Call the function to delete old logs delete_old_logs() @@ -1127,10 +1152,12 @@ def _periodic_config_sync(): except Exception as e: print(f"Periodic config sync error: {e}") -if not CONFIGURATION_MODE: +if not CONFIGURATION_MODE and not TEMPLATE_IMAGE: _sync_thread = threading.Thread(target=_periodic_config_sync, daemon=True, name="config-sync") _sync_thread.start() print("✅ Periodic server config sync started (every 5 min)") +elif TEMPLATE_IMAGE: + print("🔧 Template image: Periodic config sync DISABLED") else: print("🔧 Configuration mode: Periodic config sync DISABLED")