Implemented Phase 2.4 (Dependency Injection Pattern): - Added new dependencies module with DI container and runtime context - RuntimeContainer for injectable factories - RuntimeContext for resolved runtime objects - create_service_manager() factory - build_runtime_context() composition root - Refactored main.py to use dependency container wiring - Main orchestration now resolves runtime via DI factories - Reduced direct constructor coupling in entrypoint - Added unit tests for DI behavior with mocked dependencies - Verifies factory injection for service manager creation - Verifies runtime composition uses injected callables - Updated optimization plan checkboxes for Phase 2.4 Validation: - py_compile passed for new/changed files - tests/unit/test_dependencies.py passed - regression test test_event_queue::test_enqueue passed Notes: - Keeps existing runtime behavior unchanged - Establishes clear composition root for future testability improvements
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
IoT Bridge Main Entry Point.
|
|
|
|
Clean entry point that delegates to bootstrap and service_manager modules.
|
|
This file should remain minimal (~80 lines) and focused on orchestration.
|
|
"""
|
|
|
|
from dependencies import build_runtime_context
|
|
|
|
|
|
def main():
|
|
"""
|
|
Main entry point for IoT Bridge.
|
|
|
|
Flow:
|
|
1. Bootstrap: Load config, setup logging, parse args
|
|
2. Service Manager: Initialize and start all services
|
|
3. Main Loop: Run until shutdown signal
|
|
4. Shutdown: Gracefully stop all services
|
|
"""
|
|
# Phase 1-2: Resolve runtime context via dependency container
|
|
runtime = build_runtime_context()
|
|
service_manager = runtime.service_manager
|
|
|
|
# Phase 3: Setup signal handlers for graceful shutdown
|
|
service_manager.setup_signal_handlers()
|
|
|
|
# Phase 4: Get MQTT configuration
|
|
mqtt_config = runtime.mqtt_config
|
|
|
|
# Phase 5: Start all services
|
|
# - Odoo client
|
|
# - Event queue
|
|
# - Device status monitor
|
|
# - MQTT client
|
|
# - Device manager
|
|
# - HTTP config server
|
|
service_manager.start_services(mqtt_config)
|
|
|
|
# Phase 6: Run main event loop
|
|
# Checks timeouts and waits for shutdown signal
|
|
service_manager.run_main_loop()
|
|
|
|
# Phase 7: Graceful shutdown
|
|
# Stop all services in correct order
|
|
service_manager.shutdown()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|