updated for test device and not configured
This commit is contained in:
@@ -216,7 +216,7 @@ def load_config():
|
|||||||
|
|
||||||
def _get_mac_address():
|
def _get_mac_address():
|
||||||
"""Return the MAC address of the primary network interface."""
|
"""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'
|
mac_path = f'/sys/class/net/{iface}/address'
|
||||||
try:
|
try:
|
||||||
if os.path.exists(mac_path):
|
if os.path.exists(mac_path):
|
||||||
@@ -382,8 +382,24 @@ def sync_config_with_server():
|
|||||||
# Load global application configuration
|
# Load global application configuration
|
||||||
APP_CONFIG = load_config()
|
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)
|
# 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
|
# Card presence flag – controls whether RFID functions are started
|
||||||
@@ -555,11 +571,18 @@ try:
|
|||||||
if early_launch_configuration_mode():
|
if early_launch_configuration_mode():
|
||||||
print("🚀 Configuration mode active - application will run in setup mode")
|
print("🚀 Configuration mode active - application will run in setup mode")
|
||||||
print("⚠️ Network connectivity checks are DISABLED")
|
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")
|
print("🔧 Change config.txt [device] name to exit configuration mode")
|
||||||
CONFIGURATION_MODE = True
|
CONFIGURATION_MODE = True
|
||||||
print("🔧 Configuration mode activated - continuing with RFID reader initialization")
|
print("🔧 Configuration mode activated - continuing with RFID reader initialization")
|
||||||
print("🔧 Network and server monitoring will remain disabled")
|
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:
|
else:
|
||||||
print("❌ Failed to launch configuration mode, continuing with normal operation")
|
print("❌ Failed to launch configuration mode, continuing with normal operation")
|
||||||
CONFIGURATION_MODE = False
|
CONFIGURATION_MODE = False
|
||||||
@@ -977,7 +1000,7 @@ def send_log_to_server(log_message, n_masa, hostname, device_ip):
|
|||||||
"mac_address": _get_mac_address(),
|
"mac_address": _get_mac_address(),
|
||||||
"card_presence": APP_CONFIG.get("card_presence", "enable"),
|
"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
|
print(log_data) # Debugging: Print log_data to verify its contents
|
||||||
response = requests.post(server_url, json=log_data, timeout=5)
|
response = requests.post(server_url, json=log_data, timeout=5)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
@@ -990,15 +1013,17 @@ def log_info_with_server(message):
|
|||||||
formatted_message = f"{message} (n_masa: {n_masa})" # Format the message
|
formatted_message = f"{message} (n_masa: {n_masa})" # Format the message
|
||||||
logging.info(formatted_message) # Log the formatted 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:
|
try:
|
||||||
if not CONFIGURATION_MODE:
|
if not TEMPLATE_IMAGE:
|
||||||
send_log_to_server(message, n_masa, hostname, device_ip) # Send the original message to the server
|
send_log_to_server(message, n_masa, hostname, device_ip)
|
||||||
else:
|
else:
|
||||||
logging.info("Configuration mode: Server logging disabled")
|
logging.info("Template image: Server logging disabled")
|
||||||
except NameError:
|
except NameError:
|
||||||
# CONFIGURATION_MODE not defined yet (during initialization)
|
# TEMPLATE_IMAGE not defined yet (during initialization)
|
||||||
send_log_to_server(message, n_masa, hostname, device_ip) # Send the original message to the server
|
send_log_to_server(message, n_masa, hostname, device_ip)
|
||||||
|
|
||||||
# Call the function to delete old logs
|
# Call the function to delete old logs
|
||||||
delete_old_logs()
|
delete_old_logs()
|
||||||
@@ -1127,10 +1152,12 @@ def _periodic_config_sync():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Periodic config sync error: {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 = threading.Thread(target=_periodic_config_sync, daemon=True, name="config-sync")
|
||||||
_sync_thread.start()
|
_sync_thread.start()
|
||||||
print("✅ Periodic server config sync started (every 5 min)")
|
print("✅ Periodic server config sync started (every 5 min)")
|
||||||
|
elif TEMPLATE_IMAGE:
|
||||||
|
print("🔧 Template image: Periodic config sync DISABLED")
|
||||||
else:
|
else:
|
||||||
print("🔧 Configuration mode: Periodic config sync DISABLED")
|
print("🔧 Configuration mode: Periodic config sync DISABLED")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user