79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Modbus to MQTT Bridge Service
|
|
|
|
This service reads temperature and humidity data from a Modbus TCP server
|
|
and publishes the data to an MQTT broker.
|
|
|
|
Usage:
|
|
python main.py
|
|
|
|
Author: POE Project
|
|
"""
|
|
|
|
import time
|
|
import logging
|
|
import sys
|
|
import signal
|
|
from health_check import HealthCheckServer
|
|
from sensor_bridge import main_loop
|
|
|
|
# Global health server for cleanup
|
|
health_server = None
|
|
|
|
def signal_handler(signum, frame):
|
|
"""Handle shutdown signals gracefully"""
|
|
global health_server
|
|
logging.info(f"Received signal {signum}, shutting down gracefully...")
|
|
if health_server:
|
|
health_server.stop()
|
|
sys.exit(0)
|
|
|
|
if __name__ == "__main__":
|
|
# Setup logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
handlers=[
|
|
logging.StreamHandler(sys.stdout),
|
|
]
|
|
)
|
|
|
|
# Setup signal handlers
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
logging.info("POE Sensor Bridge starting up...")
|
|
|
|
try:
|
|
# Start health check server first
|
|
logging.info("Starting health check server...")
|
|
health_server = HealthCheckServer()
|
|
|
|
if health_server.start():
|
|
logging.info("Health check server started successfully")
|
|
|
|
# Give health check server time to be ready
|
|
time.sleep(3)
|
|
|
|
# Verify health check is working
|
|
if health_server.is_running():
|
|
logging.info("Health check server verified as running")
|
|
logging.info("Starting main sensor loop...")
|
|
main_loop()
|
|
else:
|
|
logging.error("Health check server failed to start properly")
|
|
sys.exit(1)
|
|
else:
|
|
logging.error("Failed to start health check server")
|
|
sys.exit(1)
|
|
|
|
except KeyboardInterrupt:
|
|
logging.info("Received keyboard interrupt, shutting down...")
|
|
except Exception as e:
|
|
logging.error(f"Fatal error: {e}", exc_info=True)
|
|
sys.exit(1)
|
|
finally:
|
|
if health_server:
|
|
health_server.stop()
|
|
logging.info("POE Sensor Bridge shutdown complete") |