main.py 3.27 KB
Newer Older
1 2 3 4
import argparse
import logging
from misc.settings import SETTINGS
from plugins.decorators import get_all_plugins
5
from specialized import plugin_telegram, plugin_picamera, plugin_motion_detector, plugin_buffered_recorder, \
6
    plugin_still, plugin_media_manager, plugin_status_led, plugin_pwmled
7
import plugin_ratcam
8 9
from plugins.processes_host import ProcessesHost
from misc.logging import ensure_logging_setup
10
from misc.signal import GracefulSignal
11 12 13 14 15 16 17 18


ensure_logging_setup()


def main(args):
    if args.token:
        SETTINGS.telegram.token = args.token
19
    if args.logfile is not None:
20
        logging.info('Switching over to logging to %s', args.logfile)
21 22 23 24
        ensure_logging_setup(logging.DEBUG if args.verbose else logging.INFO, reset=True, filename=args.logfile)
    else:
        ensure_logging_setup(logging.DEBUG if args.verbose else logging.INFO, reset=True)

25
    plugins = get_all_plugins()
26 27 28 29 30 31
    assert plugin_telegram.TELEGRAM_ROOT_PLUGIN_NAME in plugins
    assert plugin_picamera.PICAMERA_ROOT_PLUGIN_NAME in plugins
    assert plugin_media_manager.MEDIA_MANAGER_PLUGIN_NAME in plugins
    assert plugin_buffered_recorder.BUFFERED_RECORDER_PLUGIN_NAME in plugins
    assert plugin_motion_detector.MOTION_DETECTOR_PLUGIN_NAME in plugins
    assert plugin_still.STILL_PLUGIN_NAME in plugins
32
    assert plugin_ratcam.RATCAM_PLUGIN_NAME in plugins
33
    assert plugin_pwmled.PWMLED_PLUGIN_NAME in plugins
34
    assert plugin_status_led.STATUS_LED_PLUGIN_NAME in plugins
35
    if not args.camera:
36
        del plugins[plugin_picamera.PICAMERA_ROOT_PLUGIN_NAME]
37 38
    if not args.light:
        del plugins[plugin_pwmled.PWMLED_PLUGIN_NAME]
39 40
    if not args.status_led:
        del plugins[plugin_status_led.STATUS_LED_PLUGIN_NAME]
41
    logging.info('Running the following plugins: ' + ', '.join(plugins.keys()))
42 43 44 45 46
    # Ignore KeyboardInterrupt. If we don't do so, it will be raised also in the child processes. We do not have control
    # over the threads running in the child processes, so they will terminate, and here we get some network exception
    # because the socket is closed. We want instead to terminate gracefully.
    with GracefulSignal() as sigint:
        with ProcessesHost(plugins):
47
            logging.info('Ready.')
48 49
            sigint.wait()
            logging.info('Turning off...')
50 51 52 53 54


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--token', '-t', required=False, help='Telegram chat token.')
55
    parser.add_argument('--no-cam', '--no-camera', '-nc', required=False, dest='camera', default=True,
56
                        action='store_false', help='Skip initializing camera plugin.')
57 58
    parser.add_argument('--no-status', '--no-status-led', required=False, dest='status_led', default=True,
                        action='store_false', help='Skip initializing status LED plugin.')
59 60
    parser.add_argument('--no-light', '-nl', required=False, dest='light', default=True, action='store_false',
                        help='Skip PWM controlled led light.')
61 62
    parser.add_argument('--verbose', '-v', required=False, default=False, action='store_true',
                        help='Log up to debug level')
63 64
    parser.add_argument('--logfile', '--log', dest='logfile', required=False, default=None,
                        help='Log everything to a file.')
65
    main(parser.parse_args())