Files
prezenta_work/test_monitoring_server.py
RPI User e0f7ec34f2 Implement dual logging workflow: noconfig vs configured mode
NOCONFIG MODE (device not configured):
- Sends 'App initialized' message on startup
- Sends 'RFID initialized' message after RFID ready
- Sends 'App working' message on startup
- Sends periodic 'App working' health status every 5 minutes
- Card inserted/removed events sent directly to monitoring server

CONFIGURED MODE (table ID set):
- Sends 'App initialized' and startup messages
- Card events POST to Harting API first
- Only sends to monitoring server on successful API post
- Failed posts saved to tag.txt backup file
- Backup data sent with summary when internet restored

Changes:
- Added send_health_status() function for periodic monitoring
- Updated process_card_events() to handle both workflows
- Updated main() to send startup messages and start health monitor
- Improved monitoring server URL to use rpi-ansible hostname
- Added retry logic for monitoring server requests
2025-12-19 15:08:02 +02:00

79 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify connection to monitoring server
"""
import requests
import socket
import sys
# Configuration
MONITORING_SERVER = "http://rpi-ansible:80/logs"
def test_connection():
"""Test if monitoring server is reachable"""
print(f"Testing connection to: {MONITORING_SERVER}\n")
# Get local info
try:
hostname = socket.gethostname()
device_ip = socket.gethostbyname(hostname)
print(f"Local hostname: {hostname}")
print(f"Local IP: {device_ip}\n")
except Exception as e:
print(f"Error getting local info: {e}\n")
hostname = "unknown"
device_ip = "0.0.0.0"
# Test 1: Simple GET request
print("1. Testing simple GET request...")
try:
response = requests.get(MONITORING_SERVER, timeout=5)
print(f" ✓ GET successful (Status: {response.status_code})")
print(f" Response: {response.text[:200]}\n")
except Exception as e:
print(f" ✗ GET failed: {e}\n")
# Test 2: POST with test data
print("2. Testing POST with test data...")
try:
test_data = {
"hostname": hostname,
"device_ip": device_ip,
"nume_masa": "TEST_STATION",
"log_message": "Test message from client"
}
print(f" Payload: {test_data}")
response = requests.post(MONITORING_SERVER, json=test_data, timeout=5)
print(f" ✓ POST successful (Status: {response.status_code})")
print(f" Response: {response.text}\n")
except requests.exceptions.ConnectionError as e:
print(f" ✗ Connection error: {e}")
print(f" - Server may be unreachable at {MONITORING_SERVER}\n")
except requests.exceptions.Timeout:
print(f" ✗ Request timeout (5s)")
print(f" - Server may be offline or slow\n")
except Exception as e:
print(f" ✗ POST failed: {e}\n")
# Test 3: Ping server IP
print("3. Testing network connectivity to server...")
import subprocess
try:
server_ip = "192.168.1.103"
result = subprocess.run(
["ping", "-c", "1", server_ip],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=5
)
if result.returncode == 0:
print(f" ✓ Server IP {server_ip} is reachable\n")
else:
print(f" ✗ Server IP {server_ip} is NOT reachable\n")
except Exception as e:
print(f" ✗ Ping failed: {e}\n")
if __name__ == "__main__":
test_connection()