Projekt

Obecné

Profil

Stáhnout (1.79 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
    if exists(app_config.logger_config_file):
17
        logging.config.fileConfig(fname=app_config.logger_config_file)
18
        api_client_set_config(app_config)
19
        usb_detector_set_config(app_config)
20 0b96f10c Pultak
    else:
21 474551ae silhavyj
        print(f"Cannot find logger configuration \"{app_config.logger_config_file}\"! Please specify valid a path or define a new one.")
22 0b96f10c Pultak
        exit(1)
23
24 bba28c53 silhavyj
25
if __name__ == "__main__":
26 fddc9b6d silhavyj
    try:
27
        app_instance = singleton.SingleInstance()
28
    except singleton.SingleInstanceException:
29
        exit(1)
30
31 9219c992 silhavyj
    arg_parser = argparse.ArgumentParser(description="ZF USB License Detector")
32 01b65346 silhavyj
    arg_parser.add_argument("-c", "--config", dest="config", required=True, help="Path to the configuration file")
33
    args = arg_parser.parse_args()
34
35
    config = Config(args.config)
36 474551ae silhavyj
    init_logging(config)
37 bba28c53 silhavyj
38 f7fb8759 silhavyj
    register_listener(callback=usb_connected_callback, connected=True)
39
    register_listener(callback=usb_disconnected_callback, connected=False)
40
41
    usb_detector_thread = Thread(target=usb_detector_run)
42
    usb_detector_thread.setDaemon(True)
43 76b68bb9 silhavyj
44
    api_thread = Thread(target=api_client_run)
45
    api_thread.setDaemon(True)
46
47 474551ae silhavyj
    logging.info("Starting USB detector.")
48 f7fb8759 silhavyj
    usb_detector_thread.start()
49 0b96f10c Pultak
50 474551ae silhavyj
    logging.info("Starting API communication manager.")
51 76b68bb9 silhavyj
    api_thread.start()
52 bba28c53 silhavyj
53 f7fb8759 silhavyj
    usb_detector_thread.join()
54 76b68bb9 silhavyj
    api_thread.join()