-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (82 loc) · 2.64 KB
/
main.py
File metadata and controls
92 lines (82 loc) · 2.64 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
# main.py updates
import logging
import sys
import yaml
from core.command_dispatcher import CommandDispatcher
from core.database import Database
from core.event_bus import EventBus
from core.plugin_manager import PluginManager
from core.scheduler import BotScheduler
from services.meshtastic_service import MeshtasticService
from ui.dashboard import BotDashboard
from ui.log_handler import TextualLogHandler
def load_config():
"""
Load configuration from config.yaml file.
"""
try:
with open("config.yaml", "r") as f:
return yaml.safe_load(f)
except FileNotFoundError:
print("config.yaml not found. Exiting.")
sys.exit(1)
if __name__ == "__main__":
# Load Config
config = load_config()
# Extract Settings from the 'logging' section
log_cfg = config.get('logging', {})
file_level_str = log_cfg.get('file_level', 'DEBUG').upper()
log_filename = log_cfg.get('log_file', 'bot_activity.log')
# Configure Basic Logging (Startup Mode)
logging.basicConfig(
level=getattr(logging, file_level_str, logging.DEBUG),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename=log_filename,
filemode='a'
)
my_node_id: str = config['core'].get('my_node_id', 'ERROR')
# Initialize Global Components
event_bus = EventBus()
db = Database(event_bus, config.get(
'core', {}).get('database', {}))
meshtastic_service = MeshtasticService(
event_bus,
db,
config.get('services', {}).get('meshtastic_service', {}),
my_node_id
)
global_services = {
"bus": event_bus,
"db": db,
"mesh": meshtastic_service
}
# Connect
db.connect()
meshtastic_service.connect()
# Initialize Core Components
plugin_mgr = PluginManager(
config=config,
global_services=global_services,
my_node=my_node_id,
)
scheduler = BotScheduler(plugin_manager=plugin_mgr)
dispatcher = CommandDispatcher(global_services, my_node=my_node_id)
dispatcher.load_commands()
dispatcher.start()
# Load & Start
plugin_mgr.discover_and_load()
scheduler.reload_jobs()
scheduler.start()
# Start Dashboard UI
app = BotDashboard(plugin_mgr, scheduler, event_bus)
try:
app.run() # This blocks and takes over the terminal
except KeyboardInterrupt:
print("\nForce closing...")
finally:
root = logging.getLogger()
for handler in root.handlers[:]:
if isinstance(handler, TextualLogHandler):
root.removeHandler(handler)
scheduler.stop()
meshtastic_service.disconnect()