-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.py
70 lines (49 loc) · 1.74 KB
/
start.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
import argparse
import logging as logger
import signal
import sys
from xpsd import control
""" Main entry point for xplane-streamdeck by wortelus. """
formatter = logger.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
root = logger.getLogger()
root.setLevel(logger.INFO)
handler = logger.StreamHandler(sys.stdout)
handler.setLevel(logger.INFO)
handler.setFormatter(formatter)
root.addHandler(handler)
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description='Run xplane-streamdeck by wortelus.')
parser.add_argument('--debug', action='store_true', help='Enable debug output')
return parser.parse_args()
def signal_handler(ctl):
"""Handler for graceful shutdown on SIGINT"""
def handler(signum, frame):
logger.info("SIGINT received, shutting down gracefully...")
ctl.stop()
sys.exit(0)
return handler
def main():
args = parse_args()
# Set log level based on debug flag
if args.debug:
root.setLevel(logger.DEBUG)
handler.setLevel(logger.DEBUG)
logger.debug("Debugging is enabled.")
else:
root.setLevel(logger.INFO)
handler.setLevel(logger.INFO)
logger.info("Starting xplane-streamdeck by wortelus. This software is licensed under BSD 2-Clause License.")
logger.info("Copyright (c) 2022, Daniel Slavík All rights reserved.")
conf = control.load()
ctl = control.DeckControl(conf)
# Register signal handler for SIGINT
signal.signal(signal.SIGINT, signal_handler(ctl))
try:
control.run(ctl, conf.update_rate)
except Exception as e:
logger.error(f"An error occurred: {e}")
ctl.stop()
sys.exit(1)
if __name__ == "__main__":
main()