Projekt

Obecné

Profil

Stáhnout (3.15 KB) Statistiky
| Větev: | Tag: | Revize:
1 bba28c53 silhavyj
import logging
2 0b96f10c Pultak
import logging.config
3 01b65346 silhavyj
import argparse
4 0b96f10c Pultak
from os.path import exists
5 f7fb8759 silhavyj
from threading import Thread
6 bba28c53 silhavyj
from tendo import singleton
7 fddc9b6d silhavyj
from sys import exit
8 bba28c53 silhavyj
9 474551ae silhavyj
from config_manager import Config
10
from usb_detector.detector import register_listener, usb_detector_run, usb_detector_set_config
11 f7fb8759 silhavyj
from usb_detector.event_listener import usb_connected_callback, usb_disconnected_callback
12 76bdccc4 silhavyj
from usb_detector.api_client import api_client_run, api_client_set_config
13 f7fb8759 silhavyj
14 e22c7a67 silhavyj
15 474551ae silhavyj
def init_logging(app_config: Config):
16 6b5427e6 silhavyj
    """Initializes logging, api client, and usb detector.
17
18
    The function checks whether the path to the logger configuration
19 675e5fb6 silhavyj
    file is valid or not. The path is defined in the logger section of the
20 6b5427e6 silhavyj
    main configuration file. It also calls api_client_set_config and
21 675e5fb6 silhavyj
    usb_detector_set_config to fully initialize the application.
22 6b5427e6 silhavyj
23
    :param app_config: instance of Config (config manager)
24
    """
25
    # If the logger configuration file exists.
26 474551ae silhavyj
    if exists(app_config.logger_config_file):
27 6b5427e6 silhavyj
        # Initialize logging according to the logger config file.
28 474551ae silhavyj
        logging.config.fileConfig(fname=app_config.logger_config_file)
29 6b5427e6 silhavyj
30
        # Initialize the rest of the application.
31 474551ae silhavyj
        api_client_set_config(app_config)
32
        usb_detector_set_config(app_config)
33 0b96f10c Pultak
    else:
34 6b5427e6 silhavyj
        # If the file does not exist, terminate the application.
35 474551ae silhavyj
        print(f"Cannot find logger configuration \"{app_config.logger_config_file}\"! Please specify valid a path or define a new one.")
36 0b96f10c Pultak
        exit(1)
37
38 bba28c53 silhavyj
39
if __name__ == "__main__":
40 6b5427e6 silhavyj
    """Main entry point of the application.
41
    
42 675e5fb6 silhavyj
    The application expects one parameter to be passed in -
43 6b5427e6 silhavyj
    the path to the configuration file. The user can print out help
44
    using the '-h' option.
45
    """
46 675e5fb6 silhavyj
    # Make sure that there is only one running instance (process) of this application.
47 fddc9b6d silhavyj
    try:
48
        app_instance = singleton.SingleInstance()
49
    except singleton.SingleInstanceException:
50
        exit(1)
51
52 6b5427e6 silhavyj
    # Parse the arguments passed in from the command line.
53 9219c992 silhavyj
    arg_parser = argparse.ArgumentParser(description="ZF USB License Detector")
54 01b65346 silhavyj
    arg_parser.add_argument("-c", "--config", dest="config", required=True, help="Path to the configuration file")
55
    args = arg_parser.parse_args()
56
57 6b5427e6 silhavyj
    # Read the configuration file and initialize the application (logging).
58 01b65346 silhavyj
    config = Config(args.config)
59 474551ae silhavyj
    init_logging(config)
60 bba28c53 silhavyj
61 6b5427e6 silhavyj
    # Register callbacks (connected/disconnected USB device).
62 f7fb8759 silhavyj
    register_listener(callback=usb_connected_callback, connected=True)
63
    register_listener(callback=usb_disconnected_callback, connected=False)
64
65 6b5427e6 silhavyj
    # Create a thread for the USB detector.
66 f7fb8759 silhavyj
    usb_detector_thread = Thread(target=usb_detector_run)
67
    usb_detector_thread.setDaemon(True)
68 76b68bb9 silhavyj
69 6b5427e6 silhavyj
    # Create a thread for resending failed payloads to the server.
70 76b68bb9 silhavyj
    api_thread = Thread(target=api_client_run)
71
    api_thread.setDaemon(True)
72
73 6b5427e6 silhavyj
    # Start the USB detector thread.
74 474551ae silhavyj
    logging.info("Starting USB detector.")
75 f7fb8759 silhavyj
    usb_detector_thread.start()
76 0b96f10c Pultak
77 6b5427e6 silhavyj
    # Start the API client thread.
78 474551ae silhavyj
    logging.info("Starting API communication manager.")
79 76b68bb9 silhavyj
    api_thread.start()
80 bba28c53 silhavyj
81 6b5427e6 silhavyj
    # The execution should never get here as both
82
    # threads are infinite loops.
83 f7fb8759 silhavyj
    usb_detector_thread.join()
84 76b68bb9 silhavyj
    api_thread.join()