|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +import pathlib |
| 6 | +import sys |
| 7 | +import os |
| 8 | + |
| 9 | +from pijuice_sys.daemon import ( |
| 10 | + PiJuiceSys, |
| 11 | + PiJuiceSysInterfaceError, |
| 12 | + PiJuiceSysConfigValidationError, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +logging.basicConfig(level=logging.WARN) |
| 17 | +logger = logging.getLogger(__package__) |
| 18 | + |
| 19 | + |
| 20 | +def parse_args() -> argparse.Namespace: |
| 21 | + parser = argparse.ArgumentParser(description="PiJuice System Daemon") |
| 22 | + subparsers = parser.add_subparsers( |
| 23 | + help="sub-command help", dest="command", required=True |
| 24 | + ) |
| 25 | + |
| 26 | + parser.add_argument( |
| 27 | + "-v", |
| 28 | + "--verbose", |
| 29 | + action="count", |
| 30 | + default=0, |
| 31 | + help="Increase verbosity level", |
| 32 | + ) |
| 33 | + parser.add_argument( |
| 34 | + "-B", |
| 35 | + "--i2c-bus", |
| 36 | + metavar="BUS", |
| 37 | + type=int, |
| 38 | + default=os.environ.get("PIJUICE_I2C_BUS", 1), |
| 39 | + help="I2C Bus (default: %(default)s)", |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + "-A", |
| 43 | + "--i2c-address", |
| 44 | + metavar="ADDRESS", |
| 45 | + type=int, |
| 46 | + default=os.environ.get("PIJUICE_I2C_ADDRESS", 0x14), |
| 47 | + help="I2C Address (default: %(default)s)", |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + "-C", |
| 51 | + "--config-file", |
| 52 | + metavar="FILE", |
| 53 | + dest="config_file_path", |
| 54 | + type=pathlib.Path, |
| 55 | + default=os.environ.get( |
| 56 | + "PIJUICE_CONFIG_FILE", "/var/lib/pijuice/pijuice_config.JSON" |
| 57 | + ), |
| 58 | + help="Path to read Configuration file from (default: %(default)s)", |
| 59 | + ) |
| 60 | + |
| 61 | + parser_daemon = subparsers.add_parser("daemon", help="run daemon") |
| 62 | + parser_daemon.add_argument( |
| 63 | + "--pid-file", |
| 64 | + metavar="FILE", |
| 65 | + dest="pid_file_path", |
| 66 | + type=pathlib.Path, |
| 67 | + default=os.environ.get("PIJUICE_PID_FILE", "/tmp/pijuice_sys.pid"), |
| 68 | + help="Path to create pid-file at (default: %(default)s)", |
| 69 | + ) |
| 70 | + parser_daemon.add_argument( |
| 71 | + "--poll-interval", |
| 72 | + metavar="INTERVAL", |
| 73 | + type=float, |
| 74 | + default=os.environ.get("PIJUICE_POLL_INTERVAL", 5.0), |
| 75 | + help="Interval at which to poll status from pijuice (default: %(default)s)", |
| 76 | + ) |
| 77 | + parser_daemon.add_argument( |
| 78 | + "--button-poll-interval", |
| 79 | + metavar="INTERVAL", |
| 80 | + type=float, |
| 81 | + default=os.environ.get("PIJUICE_BUTTON_POLL_INTERVAL", 1.0), |
| 82 | + help="Interval at which to poll button-events from pijuice (default: %(default)s)", |
| 83 | + ) |
| 84 | + |
| 85 | + parser_poweroff = subparsers.add_parser("poweroff", help="execute poweroff command") |
| 86 | + return parser.parse_args() |
| 87 | + |
| 88 | + |
| 89 | +async def run(): |
| 90 | + args = parse_args() |
| 91 | + if args.verbose == 1: |
| 92 | + logger.setLevel(logging.INFO) |
| 93 | + elif args.verbose == 2: |
| 94 | + logger.setLevel(logging.DEBUG) |
| 95 | + |
| 96 | + try: |
| 97 | + pijuice_sys = PiJuiceSys( |
| 98 | + i2c_bus=args.i2c_bus, |
| 99 | + i2c_address=args.i2c_address, |
| 100 | + config_file_path=args.config_file_path, |
| 101 | + ) |
| 102 | + except PiJuiceSysInterfaceError: |
| 103 | + logger.error("failed to initialize PiJuice interface") |
| 104 | + sys.exit(1) |
| 105 | + except PiJuiceSysConfigValidationError as e: |
| 106 | + logger.error(f"config validation failed: {e}") |
| 107 | + sys.exit(1) |
| 108 | + |
| 109 | + if args.command == "poweroff": |
| 110 | + await pijuice_sys.execute_poweroff_command() |
| 111 | + elif args.command == "daemon": |
| 112 | + logger.debug("starting daemon") |
| 113 | + await pijuice_sys.run_daemon( |
| 114 | + pid_file_path=args.pid_file_path, |
| 115 | + poll_interval=args.poll_interval, |
| 116 | + button_poll_interval=args.button_poll_interval, |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +def main(): |
| 121 | + try: |
| 122 | + asyncio.run(run()) |
| 123 | + except KeyboardInterrupt: |
| 124 | + pass |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments