-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (83 loc) · 3.12 KB
/
main.py
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
import os
import yaml
import logging.config
import time
from core.printer_controller import PrinterController
from core.mqtt_handler import MQTTHandler
def setup_logging():
"""Setup logging configuration"""
config_path = os.path.join(
os.path.dirname(__file__),
'config/logging_config.yaml'
)
# Create logs directory if it doesn't exist
os.makedirs('logs', exist_ok=True)
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
logging.config.dictConfig(config)
def load_config():
"""Load configuration from yaml file"""
config_path = os.path.join(
os.path.dirname(__file__),
'config/printer_config.yaml'
)
with open(config_path, 'r') as f:
return yaml.safe_load(f)
class PrinterSystem:
"""Main printer system class"""
def __init__(self):
# Setup logging
setup_logging()
self.logger = logging.getLogger('printer')
# Load configuration
self.config = load_config()
self.logger.info("Configuration loaded successfully")
# Initialize components
self._init_components()
def _init_components(self):
"""Initialize system components"""
try:
# Initialize printer controller
self.printer = PrinterController(self.config)
if not self.printer.connect():
raise Exception("Failed to connect to printer")
# Initialize MQTT handler
self.mqtt_handler = MQTTHandler(self.config['mqtt'], self.printer)
self.mqtt_handler.connect()
except Exception as e:
self.logger.error(f"Failed to initialize components: {e}")
raise
def run(self):
"""Main system loop"""
self.logger.info("System initialized, entering main loop")
while True:
try:
# Check printer connection
if not self.printer.check_connection():
self.logger.error("Lost connection to printer")
continue
# Check if backup needed
if self.printer.db_manager.check_backup_needed():
self.printer.db_manager.create_backup()
self.printer.db_manager.cleanup_old_backups()
# Get printer status
status = self.printer.get_status()
# Publish status
self.mqtt_handler.publish_status(status)
# Sleep for a bit
time.sleep(5)
except KeyboardInterrupt:
self.logger.info("Shutting down...")
break
except Exception as e:
self.logger.error(f"Error in main loop: {e}")
time.sleep(5) # Wait before retrying
def main():
try:
system = PrinterSystem()
system.run()
except Exception as e:
logging.error(f"Fatal error: {e}")
raise
if __name__ == "__main__":
main()