Files
POE-sensor/main.py

66 lines
1.9 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 os
if __name__ == "__main__":
# Setup logging first
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler(sys.stdout)]
)
logging.info("POE Sensor Bridge starting up...")
try:
# Start health check server first (non-blocking)
try:
from health_check import create_health_server
health_server = create_health_server()
if health_server:
logging.info("Health check server started successfully")
else:
logging.warning("Health check server failed to start, continuing without it")
except Exception as e:
logging.warning(f"Health check setup failed: {e}, continuing without it")
health_server = None
# Give health server time to initialize
time.sleep(2)
# Import and start main loop
from sensor_bridge import main_loop
logging.info("Starting main sensor loop...")
main_loop()
except ImportError as e:
logging.error(f"Import error: {e}")
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:
# Cleanup health server if it exists
try:
if 'health_server' in locals() and health_server:
health_server.stop()
except:
pass
logging.info("POE Sensor Bridge shutdown complete")