-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrun_bridge.py
More file actions
112 lines (97 loc) · 3.39 KB
/
Copy pathrun_bridge.py
File metadata and controls
112 lines (97 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# run_bridge.py
"""
Executable script to initialize and run the Akita Meshtastic Meshcore
Bridge (AMMB).
This script handles:
- Checking for essential dependencies.
- Loading configuration from 'config.ini'.
- Setting up application-wide logging.
- Creating and running the main Bridge instance.
- Handling graceful shutdown on KeyboardInterrupt (Ctrl+C).
"""
import logging
import os
import sys
# Ensure the script can find the 'ammb' package
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, project_root)
# --- Dependency Check ---
try:
import configparser # noqa: F401
import json # noqa: F401
import queue # noqa: F401
import threading # noqa: F401
import time # noqa: F401
import meshtastic # noqa: F401
import meshtastic.serial_interface # noqa: F401
import paho.mqtt.client as paho_mqtt # noqa: F401
import serial # noqa: F401
from pubsub import pub # noqa: F401
except ImportError as e:
print("ERROR: Missing required library - %s" % e.name, file=sys.stderr)
print("Please install required libraries by running:", file=sys.stderr)
print(
" pip install -r %s" % os.path.join(project_root, 'requirements.txt'),
file=sys.stderr,
)
sys.exit(1)
# --- Imports ---
try:
from ammb import Bridge
from ammb.config_handler import CONFIG_FILE, load_config
from ammb.utils import setup_logging
except ImportError as e:
print(f"ERROR: Failed to import AMMB modules: {e}", file=sys.stderr)
print(
"Ensure the script is run from the project root directory",
file=sys.stderr,
)
sys.exit(1)
# --- Main Execution ---
def main():
# Basic logging setup until config is loaded
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info("--- Akita Meshtastic Meshcore Bridge Starting ---")
# --- Configuration Loading ---
config_path = os.path.join(project_root, CONFIG_FILE)
logging.info(f"Loading configuration from: {config_path}")
config = load_config(config_path)
if not config:
logging.critical("Failed to load configuration. Bridge cannot start.")
sys.exit(1)
logging.info("Configuration loaded successfully.")
logging.info(f"Selected external transport: {config.external_transport}")
# --- Logging Setup ---
setup_logging(config.log_level)
logging.debug(f"Logging level set to {config.log_level}")
# --- Bridge Initialization and Execution ---
logging.info("Initializing bridge instance...")
bridge = Bridge(config)
# Check if external handler was successfully created
if not bridge.external_handler:
logging.critical(
"Bridge initialization failed (likely handler issue). Exiting."
)
sys.exit(1)
try:
logging.info("Starting bridge run loop...")
bridge.run()
except KeyboardInterrupt:
logging.info(
"KeyboardInterrupt received. Initiating graceful shutdown..."
)
except Exception as e:
logging.critical(
f"Unhandled critical exception in bridge execution: {e}",
exc_info=True,
)
logging.info("Attempting emergency shutdown...")
bridge.stop()
sys.exit(1)
logging.info("--- Akita Meshtastic Meshcore Bridge Stopped ---")
sys.exit(0)
if __name__ == "__main__":
main()