~martijnbraam/pyatem

ref: a8d69070992bbf2806c4bff29e5f3b04f81c72e4 pyatem/openswitcher_proxy/__main__.py -rw-r--r-- 2.7 KiB
a8d69070Martijn Braam openswitcher: improve reconnect logging 8 months ago
                                                                                
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
import argparse
import time

import toml
import logging

from openswitcher_proxy.error import RecoverableError
from openswitcher_proxy.frontend_httpapi import HttpApiFrontendThread
from openswitcher_proxy.frontend_status import StatusFrontendThread
from openswitcher_proxy.frontend_tcp import TcpFrontendThread
from openswitcher_proxy.frontend_mqtt import MqttFrontendThread
from openswitcher_proxy.hardware import HardwareThread

logging.basicConfig(
    format="%(asctime)s [%(levelname)-8s %(threadName)-15s] %(message)s",
    level=logging.INFO,
    datefmt='%Y-%m-%d %H:%M:%S',
)

threads = []
nthreads = {}


def run(config_path):
    config = toml.load(config_path)
    logging.info('Loading config file ' + config_path)
    if 'hardware' in config:
        nthreads['hardware'] = {}
        for hardware in config['hardware']:
            logging.info(f'  hardware: {hardware["id"]} ({hardware["label"]})')
            t = HardwareThread(hardware)
            t.daemon = True
            threads.append(t)
            nthreads['hardware'][hardware['id']] = t
            t.start()

    if 'frontend' in config:
        nthreads['frontend'] = {}
        for frontend in config['frontend']:
            if 'host' in frontend:
                connection = frontend['host']
            elif 'bind' in frontend:
                connection = frontend['bind']
            else:
                connection = 'n/a'
                logging.error(f'  Frontend is missing bind or host option')
            logging.info(f'  frontend: {frontend["type"]} ({connection})')
            try:
                if frontend['type'] == 'status':
                    t = StatusFrontendThread(frontend, nthreads)
                elif frontend['type'] == 'http-api':
                    t = HttpApiFrontendThread(frontend, nthreads)
                elif frontend['type'] == 'tcp':
                    t = TcpFrontendThread(frontend, nthreads)
                elif frontend['type'] == 'mqtt':
                    t = MqttFrontendThread(frontend, nthreads)
                else:
                    logging.error(f'  Unknown frontend type "{frontend["type"]}"')
                    continue
                t.daemon = True
                threads.append(t)
                nthreads['frontend'][t.name] = t
                t.start()
            except RecoverableError as e:
                logging.error(f'  Could not initialize the "{frontend["type"]}" frontend. {e}')

    while True:
        time.sleep(1)


def main():
    parser = argparse.ArgumentParser("OpenSwitcher proxy")
    parser.add_argument("--config", help="Config file to use", default="/etc/openswitcher/proxy.toml")
    args = parser.parse_args()

    run(args.config)


if __name__ == '__main__':
    main()