Beispiel #1
0
def run_pi4home(argv):
    args = parse_args(argv)
    CORE.dashboard = args.dashboard

    setup_log(args.verbose)
    if args.command in PRE_CONFIG_ACTIONS:
        try:
            return PRE_CONFIG_ACTIONS[args.command](args)
        except pi4homeError as e:
            _LOGGER.error(e)
            return 1

    CORE.config_path = args.configuration

    config = read_config(args.verbose)
    if config is None:
        return 1
    CORE.config = config

    if args.command in POST_CONFIG_ACTIONS:
        try:
            return POST_CONFIG_ACTIONS[args.command](args, config)
        except pi4homeError as e:
            _LOGGER.error(e)
            return 1
    safe_print(u"Unknown command {}".format(args.command))
    return 1
Beispiel #2
0
def run_miniterm(config, port):
    import serial
    if CONF_LOGGER not in config:
        _LOGGER.info("Logger is not enabled. Not starting UART logs.")
        return
    baud_rate = config['logger'][CONF_BAUD_RATE]
    if baud_rate == 0:
        _LOGGER.info(
            "UART logging is disabled (baud_rate=0). Not starting UART logs.")
    _LOGGER.info("Starting log output from %s with baud rate %s", port,
                 baud_rate)

    backtrace_state = False
    with serial.Serial(port, baudrate=baud_rate) as ser:
        while True:
            try:
                raw = ser.readline()
            except serial.SerialException:
                _LOGGER.error("Serial port closed!")
                return
            if IS_PY2:
                line = raw.replace('\r', '').replace('\n', '')
            else:
                line = raw.replace(b'\r', b'').replace(b'\n', b'').decode(
                    'utf8', 'backslashreplace')
            time = datetime.now().time().strftime('[%H:%M:%S]')
            message = time + line
            safe_print(message)

            backtrace_state = platformio_api.process_stacktrace(
                config, line, backtrace_state=backtrace_state)
Beispiel #3
0
def update_pi4home_core_repo():
    if CONF_REPOSITORY not in CORE.pi4home_core_version:
        return

    if CONF_BRANCH not in CORE.pi4home_core_version:
        # Git commit hash or tag cannot be updated
        return

    pi4home_core_path = CORE.relative_piolibdeps_path('pi4home-core')

    rc, _, _ = run_system_command('git', '-C', pi4home_core_path, '--help')
    if rc != 0:
        # git not installed or repo not downloaded yet
        return
    rc, _, _ = run_system_command('git', '-C', pi4home_core_path, 'diff-index',
                                  '--quiet', 'HEAD', '--')
    if rc != 0:
        # local changes, cannot update
        _LOGGER.warning(
            "Local changes in pi4home-core copy from git. Will not auto-update."
        )
        return
    _LOGGER.info("Updating pi4home-core copy from git (%s)", pi4home_core_path)
    rc, stdout, _ = run_system_command('git', '-c', 'color.ui=always', '-C',
                                       pi4home_core_path, 'pull', '--stat')
    if rc != 0:
        _LOGGER.warning("Couldn't auto-update local git copy of pi4home-core.")
        return
    if IS_PY3:
        stdout = stdout.decode('utf-8', 'backslashreplace')
    safe_print(stdout.strip())
Beispiel #4
0
def choose_prompt(options):
    if not options:
        raise ValueError

    if len(options) == 1:
        return options[0][1]

    safe_print(u"Found multiple options, please choose one:")
    for i, (desc, _) in enumerate(options):
        safe_print(u"  [{}] {}".format(i + 1, desc))

    while True:
        opt = safe_input('(number): ')
        if opt in options:
            opt = options.index(opt)
            break
        try:
            opt = int(opt)
            if opt < 1 or opt > len(options):
                raise ValueError
            break
        except ValueError:
            safe_print(color('red', u"Invalid option: '{}'".format(opt)))
    return options[opt - 1][1]
Beispiel #5
0
def command_version(args):
    safe_print(u"Version: {}".format(const.__version__))
    return 0
Beispiel #6
0
def command_config(args, config):
    _LOGGER.info("Configuration is valid!")
    if not args.verbose:
        config = strip_default_ids(config)
    safe_print(yaml_util.dump(config))
    return 0