Add timezone configuration and update health check timestamps in POE project. Introduced LOCAL_TIMEZONE in config.py to support timezone offset configuration. Updated health_check.py, sensor_bridge.py, and sensor_tracker.py to utilize LOCAL_TIMEZONE for accurate timestamping in health responses and sensor data publishing. Enhanced README.md to document the new TIMEZONE_OFFSET environment variable, ensuring clarity for configuration options.

This commit is contained in:
Naab2k3
2025-06-24 09:26:08 +07:00
parent 7b8934b1d0
commit 84e7babf7e
5 changed files with 18 additions and 13 deletions

View File

@ -6,7 +6,7 @@ import logging
from config import (
MODBUS_HOSTS, SENSOR_TIMEOUT_THRESHOLD, RECOVERY_CONFIRMATION_COUNT,
ALERTING_ENABLED
ALERTING_ENABLED, LOCAL_TIMEZONE
)
class SensorTracker:
@ -37,7 +37,7 @@ class SensorTracker:
def record_success(self, host_info, mqtt_client=None):
"""Record a successful sensor reading"""
sensor_id = f"{host_info['ip']}_{host_info['location']}"
current_time = datetime.now(timezone.utc)
current_time = datetime.now(LOCAL_TIMEZONE)
with self.lock:
sensor = self.sensor_status[sensor_id]
@ -64,7 +64,7 @@ class SensorTracker:
def record_failure(self, host_info, error_message, mqtt_client=None):
"""Record a failed sensor reading"""
sensor_id = f"{host_info['ip']}_{host_info['location']}"
current_time = datetime.now(timezone.utc)
current_time = datetime.now(LOCAL_TIMEZONE)
with self.lock:
sensor = self.sensor_status[sensor_id]
@ -107,7 +107,7 @@ class SensorTracker:
alert_message = {
"alert_type": "sensor_failure",
"timestamp": datetime.now(timezone.utc).isoformat(),
"timestamp": datetime.now(LOCAL_TIMEZONE).isoformat(),
"sensor_id": sensor_id,
"sensor_ip": sensor["ip"],
"sensor_location": sensor["location"],
@ -140,7 +140,7 @@ class SensorTracker:
alert_message = {
"alert_type": "sensor_recovery",
"timestamp": datetime.now(timezone.utc).isoformat(),
"timestamp": datetime.now(LOCAL_TIMEZONE).isoformat(),
"sensor_id": sensor_id,
"sensor_ip": sensor["ip"],
"sensor_location": sensor["location"],
@ -168,7 +168,7 @@ class SensorTracker:
# Create base topic path
base_topic = f"Location/{location}/{sensor_type}"
current_time = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
current_time = datetime.now(LOCAL_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S")
# Publish offline status to individual topics
topics_data = [
@ -198,7 +198,7 @@ class SensorTracker:
"""Get status of all sensors"""
with self.lock:
return {
"timestamp": datetime.now(timezone.utc).isoformat(),
"timestamp": datetime.now(LOCAL_TIMEZONE).isoformat(),
"total_sensors": len(self.sensor_status),
"online_sensors": len([s for s in self.sensor_status.values() if s["status"] == "online"]),
"offline_sensors": len([s for s in self.sensor_status.values() if s["status"] == "offline"]),