Projekt

Obecné

Profil

Stáhnout (1.79 KB) Statistiky
| Větev: | Tag: | Revize:
1
import logging
2
import logging.config
3
import argparse
4
from os.path import exists
5
from threading import Thread
6
from tendo import singleton
7
from sys import exit
8

    
9
from config_manager import Config
10
from usb_detector.detector import register_listener, usb_detector_run, usb_detector_set_config
11
from usb_detector.event_listener import usb_connected_callback, usb_disconnected_callback
12
from usb_detector.api_client import api_client_run, api_client_set_config
13

    
14

    
15
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
    else:
21
        print(f"Cannot find logger configuration \"{app_config.logger_config_file}\"! Please specify valid a path or define a new one.")
22
        exit(1)
23

    
24

    
25
if __name__ == "__main__":
26
    try:
27
        app_instance = singleton.SingleInstance()
28
    except singleton.SingleInstanceException:
29
        exit(1)
30

    
31
    arg_parser = argparse.ArgumentParser(description="ZF USB License Detector")
32
    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
    init_logging(config)
37

    
38
    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

    
44
    api_thread = Thread(target=api_client_run)
45
    api_thread.setDaemon(True)
46

    
47
    logging.info("Starting USB detector.")
48
    usb_detector_thread.start()
49

    
50
    logging.info("Starting API communication manager.")
51
    api_thread.start()
52

    
53
    usb_detector_thread.join()
54
    api_thread.join()
(2-2/2)