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:
34
app.py
34
app.py
@@ -165,8 +165,8 @@ def post_to_harting(url, verify=False, timeout=3):
|
|||||||
except requests.exceptions.RequestException:
|
except requests.exceptions.RequestException:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def post_backup_data():
|
def post_backup_data(hostname=None, device_ip=None, name=None):
|
||||||
"""Send queued card data from tag.txt to Harting API"""
|
"""Send queued card data from tag.txt to Harting API and log summary to monitoring server"""
|
||||||
if not os.path.exists(TAG_FILE):
|
if not os.path.exists(TAG_FILE):
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -174,7 +174,12 @@ def post_backup_data():
|
|||||||
with open(TAG_FILE, "r") as f:
|
with open(TAG_FILE, "r") as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
|
|
||||||
|
if not lines:
|
||||||
|
return
|
||||||
|
|
||||||
remaining = []
|
remaining = []
|
||||||
|
posted_count = 0
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line:
|
if not line:
|
||||||
@@ -182,7 +187,8 @@ def post_backup_data():
|
|||||||
|
|
||||||
# Try to post the URL
|
# Try to post the URL
|
||||||
if post_to_harting(line):
|
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
|
continue # Success, don't keep it
|
||||||
else:
|
else:
|
||||||
remaining.append(line) # Failed, keep for retry
|
remaining.append(line) # Failed, keep for retry
|
||||||
@@ -191,6 +197,12 @@ def post_backup_data():
|
|||||||
with open(TAG_FILE, "w") as f:
|
with open(TAG_FILE, "w") as f:
|
||||||
for line in remaining:
|
for line in remaining:
|
||||||
f.write(line + "\n")
|
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:
|
except Exception as e:
|
||||||
logging.error(f"Error posting backup data: {e}")
|
logging.error(f"Error posting backup data: {e}")
|
||||||
@@ -340,7 +352,9 @@ def process_card_events(hostname, device_ip):
|
|||||||
|
|
||||||
# Try to post
|
# Try to post
|
||||||
if post_to_harting(url):
|
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:
|
else:
|
||||||
logging.warning(f"✗ Offline: Saving card {card_id} to backup")
|
logging.warning(f"✗ Offline: Saving card {card_id} to backup")
|
||||||
try:
|
try:
|
||||||
@@ -350,9 +364,6 @@ def process_card_events(hostname, device_ip):
|
|||||||
logging.error(f"Failed to save backup: {e}")
|
logging.error(f"Failed to save backup: {e}")
|
||||||
else:
|
else:
|
||||||
logging.debug(f"Device not configured (noconfig). Skipping API post for card {card_id}")
|
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
|
# Check for removed cards
|
||||||
card_id, timestamp = card_state.get_removed()
|
card_id, timestamp = card_state.get_removed()
|
||||||
@@ -366,7 +377,9 @@ def process_card_events(hostname, device_ip):
|
|||||||
|
|
||||||
# Try to post
|
# Try to post
|
||||||
if post_to_harting(url):
|
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:
|
else:
|
||||||
logging.warning(f"✗ Offline: Saving card {card_id} to backup")
|
logging.warning(f"✗ Offline: Saving card {card_id} to backup")
|
||||||
try:
|
try:
|
||||||
@@ -376,9 +389,6 @@ def process_card_events(hostname, device_ip):
|
|||||||
logging.error(f"Failed to save backup: {e}")
|
logging.error(f"Failed to save backup: {e}")
|
||||||
else:
|
else:
|
||||||
logging.debug(f"Device not configured (noconfig). Skipping API post for card {card_id}")
|
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)
|
# Very small sleep for fast response (10ms = check 100x per second)
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
@@ -483,7 +493,7 @@ def check_internet_connection(hostname, device_ip):
|
|||||||
print("✓ Connection OK")
|
print("✓ Connection OK")
|
||||||
|
|
||||||
# Try to send any backed-up data
|
# Try to send any backed-up data
|
||||||
post_backup_data()
|
post_backup_data(hostname, device_ip, name)
|
||||||
|
|
||||||
# Wait 40 minutes before next check
|
# Wait 40 minutes before next check
|
||||||
time.sleep(WIFI_CHECK_INTERVAL)
|
time.sleep(WIFI_CHECK_INTERVAL)
|
||||||
|
|||||||
Reference in New Issue
Block a user