Refactor monitoring server logging: only send on successful API post

Changes:
- Monitoring server logs only sent after successful Harting API POST
- Failed API posts are saved to tag.txt backup file
- post_backup_data() now accepts hostname, device_ip, name parameters
- When internet returns, backup data is pushed to server in order
- Single summary message sent to monitoring server: 'Backup card data pushed to server (X records)'
- Improves reliability: no duplicate logs for offline/failed posts
This commit is contained in:
RPI User
2025-12-19 14:46:59 +02:00
parent 6b199a0e41
commit f82424cffb

34
app.py
View File

@@ -165,8 +165,8 @@ def post_to_harting(url, verify=False, timeout=3):
except requests.exceptions.RequestException:
return False
def post_backup_data():
"""Send queued card data from tag.txt to Harting API"""
def post_backup_data(hostname=None, device_ip=None, name=None):
"""Send queued card data from tag.txt to Harting API and log summary to monitoring server"""
if not os.path.exists(TAG_FILE):
return
@@ -174,7 +174,12 @@ def post_backup_data():
with open(TAG_FILE, "r") as f:
lines = f.readlines()
if not lines:
return
remaining = []
posted_count = 0
for line in lines:
line = line.strip()
if not line:
@@ -182,7 +187,8 @@ def post_backup_data():
# Try to post the URL
if post_to_harting(line):
logging.info(f"Posted backed-up data: {line}")
logging.info(f"Posted backed-up data: {line}")
posted_count += 1
continue # Success, don't keep it
else:
remaining.append(line) # Failed, keep for retry
@@ -192,6 +198,12 @@ def post_backup_data():
for line in remaining:
f.write(line + "\n")
# Send summary to monitoring server only if backups were posted
if posted_count > 0 and hostname and device_ip and name:
summary_msg = f"Backup card data pushed to server ({posted_count} records)"
send_log_to_server(summary_msg, hostname, device_ip, name)
logging.info(f"{summary_msg}")
except Exception as e:
logging.error(f"Error posting backup data: {e}")
@@ -340,7 +352,9 @@ def process_card_events(hostname, device_ip):
# Try to post
if post_to_harting(url):
logging.info(f"✓ Card event posted to API: {card_id}")
logging.info(f"✓ Card {card_id} inserted - Posted to API")
# Send to monitoring server ONLY on successful API post
send_log_to_server(f"Card {card_id} inserted", hostname, device_ip, name)
else:
logging.warning(f"✗ Offline: Saving card {card_id} to backup")
try:
@@ -351,9 +365,6 @@ def process_card_events(hostname, device_ip):
else:
logging.debug(f"Device not configured (noconfig). Skipping API post for card {card_id}")
# ALWAYS send log to monitoring server (regardless of API post result)
send_log_to_server(f"Card {card_id} inserted", hostname, device_ip, name)
# Check for removed cards
card_id, timestamp = card_state.get_removed()
if card_id is not None:
@@ -366,7 +377,9 @@ def process_card_events(hostname, device_ip):
# Try to post
if post_to_harting(url):
logging.info(f"✓ Card event posted to API: {card_id}")
logging.info(f"✓ Card {card_id} removed - Posted to API")
# Send to monitoring server ONLY on successful API post
send_log_to_server(f"Card {card_id} removed", hostname, device_ip, name)
else:
logging.warning(f"✗ Offline: Saving card {card_id} to backup")
try:
@@ -377,9 +390,6 @@ def process_card_events(hostname, device_ip):
else:
logging.debug(f"Device not configured (noconfig). Skipping API post for card {card_id}")
# ALWAYS send log to monitoring server (regardless of API post result)
send_log_to_server(f"Card {card_id} removed", hostname, device_ip, name)
# Very small sleep for fast response (10ms = check 100x per second)
time.sleep(0.01)
@@ -483,7 +493,7 @@ def check_internet_connection(hostname, device_ip):
print("✓ Connection OK")
# Try to send any backed-up data
post_backup_data()
post_backup_data(hostname, device_ip, name)
# Wait 40 minutes before next check
time.sleep(WIFI_CHECK_INTERVAL)