2024-02-12 18:21:08 +02:00
|
|
|
import logging
|
2026-03-23 21:31:31 +02:00
|
|
|
import os
|
2024-02-12 18:21:08 +02:00
|
|
|
from app.adapters.agent_mqtt_adapter import AgentMQTTAdapter
|
|
|
|
|
from app.adapters.hub_http_adapter import HubHttpAdapter
|
|
|
|
|
from app.adapters.hub_mqtt_adapter import HubMqttAdapter
|
|
|
|
|
from config import (
|
|
|
|
|
MQTT_BROKER_HOST,
|
|
|
|
|
MQTT_BROKER_PORT,
|
|
|
|
|
MQTT_TOPIC,
|
|
|
|
|
HUB_URL,
|
|
|
|
|
HUB_MQTT_BROKER_HOST,
|
|
|
|
|
HUB_MQTT_BROKER_PORT,
|
|
|
|
|
HUB_MQTT_TOPIC,
|
2026-03-23 21:31:31 +02:00
|
|
|
HUB_CONNECTION_TYPE,
|
2024-02-12 18:21:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
# Configure logging settings
|
|
|
|
|
logging.basicConfig(
|
2026-03-23 21:31:31 +02:00
|
|
|
level=logging.INFO,
|
2024-02-12 18:21:08 +02:00
|
|
|
format="[%(asctime)s] [%(levelname)s] [%(module)s] %(message)s",
|
|
|
|
|
handlers=[
|
2026-03-23 21:31:31 +02:00
|
|
|
logging.StreamHandler(),
|
|
|
|
|
logging.FileHandler("app.log"),
|
2024-02-12 18:21:08 +02:00
|
|
|
],
|
|
|
|
|
)
|
2026-03-23 21:31:31 +02:00
|
|
|
|
|
|
|
|
# Logic to select the adapter based on configuration (SCRUM-93 & SCRUM-94)
|
|
|
|
|
# This allows easy switching between HTTP and MQTT protocols
|
|
|
|
|
if HUB_CONNECTION_TYPE.lower() == "http":
|
|
|
|
|
logging.info("Initializing HubHttpAdapter (SCRUM-93 integration)")
|
|
|
|
|
hub_adapter = HubHttpAdapter(
|
|
|
|
|
api_base_url=HUB_URL,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
logging.info("Initializing HubMqttAdapter (SCRUM-94 integration)")
|
|
|
|
|
hub_adapter = HubMqttAdapter(
|
|
|
|
|
broker=HUB_MQTT_BROKER_HOST,
|
|
|
|
|
port=HUB_MQTT_BROKER_PORT,
|
|
|
|
|
topic=HUB_MQTT_TOPIC,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Create an instance of the AgentMQTTAdapter using the selected hub adapter
|
|
|
|
|
# This adapter acts as a bridge between the Agent and the Hub
|
2024-02-12 18:21:08 +02:00
|
|
|
agent_adapter = AgentMQTTAdapter(
|
|
|
|
|
broker_host=MQTT_BROKER_HOST,
|
|
|
|
|
broker_port=MQTT_BROKER_PORT,
|
|
|
|
|
topic=MQTT_TOPIC,
|
|
|
|
|
hub_gateway=hub_adapter,
|
|
|
|
|
)
|
2026-03-23 21:31:31 +02:00
|
|
|
|
2024-02-12 18:21:08 +02:00
|
|
|
try:
|
2026-03-24 13:58:32 +02:00
|
|
|
logging.info(f"Connecting to MQTT broker at {MQTT_BROKER_HOST}:{MQTT_BROKER_PORT}")
|
2024-02-12 18:21:08 +02:00
|
|
|
agent_adapter.connect()
|
2026-03-23 23:28:17 +02:00
|
|
|
|
2026-03-24 13:58:32 +02:00
|
|
|
logging.info("Broker connection success. Waiting for data...")
|
|
|
|
|
agent_adapter.loop_forever()
|
2024-02-12 18:21:08 +02:00
|
|
|
except KeyboardInterrupt:
|
2026-03-24 13:58:32 +02:00
|
|
|
logging.info("Interrupt signal received. Shutting down...")
|
|
|
|
|
agent_adapter.disconnect()
|
|
|
|
|
logging.info("Disconnected from MQTT broker.")
|