Skip to content

Commit

Permalink
Software - pijuice_sys: reimplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
betaboon committed Nov 16, 2020
1 parent 2c615ca commit 5c3589f
Show file tree
Hide file tree
Showing 15 changed files with 745 additions and 352 deletions.
13 changes: 13 additions & 0 deletions Software/Source/debian-base/pijuice-poweroff.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=PiJuice poweroff
Before=poweroff.target halt.target
DefaultDependencies=no

[Service]
Type=oneshot
User=pijuice
WorkingDirectory=/var/lib/pijuice/
ExecStart=/usr/bin/pijuice_sys poweroff

[Install]
WantedBy=poweroff.target halt.target
5 changes: 2 additions & 3 deletions Software/Source/debian-base/pijuice.service
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[Unit]
Description=PiJuice status service
Description=PiJuice system daemon
After=network.target

[Service]
Type=idle
User=pijuice
WorkingDirectory=/var/lib/pijuice/
ExecStart=/usr/bin/pijuice_sys.py
ExecStopPost=/usr/bin/pijuice_sys.py stop
ExecStart=/usr/bin/pijuice_sys daemon
Restart=always

[Install]
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions Software/Source/pijuice_sys/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3
from pijuice_sys.cli import main

if __name__ == "__main__":
main()
128 changes: 128 additions & 0 deletions Software/Source/pijuice_sys/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env python3
import argparse
import asyncio
import logging
import pathlib
import sys
import os

from pijuice_sys.daemon import (
PiJuiceSys,
PiJuiceSysInterfaceError,
PiJuiceSysConfigValidationError,
)


logging.basicConfig(level=logging.WARN)
logger = logging.getLogger(__package__)


def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="PiJuice System Daemon")
subparsers = parser.add_subparsers(
help="sub-command help", dest="command", required=True
)

parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="Increase verbosity level",
)
parser.add_argument(
"-B",
"--i2c-bus",
metavar="BUS",
type=int,
default=os.environ.get("PIJUICE_I2C_BUS", 1),
help="I2C Bus (default: %(default)s)",
)
parser.add_argument(
"-A",
"--i2c-address",
metavar="ADDRESS",
type=int,
default=os.environ.get("PIJUICE_I2C_ADDRESS", 0x14),
help="I2C Address (default: %(default)s)",
)
parser.add_argument(
"-C",
"--config-file",
metavar="FILE",
dest="config_file_path",
type=pathlib.Path,
default=os.environ.get(
"PIJUICE_CONFIG_FILE", "/var/lib/pijuice/pijuice_config.JSON"
),
help="Path to read Configuration file from (default: %(default)s)",
)

parser_daemon = subparsers.add_parser("daemon", help="run daemon")
parser_daemon.add_argument(
"--pid-file",
metavar="FILE",
dest="pid_file_path",
type=pathlib.Path,
default=os.environ.get("PIJUICE_PID_FILE", "/tmp/pijuice_sys.pid"),
help="Path to create pid-file at (default: %(default)s)",
)
parser_daemon.add_argument(
"--poll-interval",
metavar="INTERVAL",
type=float,
default=os.environ.get("PIJUICE_POLL_INTERVAL", 5.0),
help="Interval at which to poll status from pijuice (default: %(default)s)",
)
parser_daemon.add_argument(
"--button-poll-interval",
metavar="INTERVAL",
type=float,
default=os.environ.get("PIJUICE_BUTTON_POLL_INTERVAL", 1.0),
help="Interval at which to poll button-events from pijuice (default: %(default)s)",
)

parser_poweroff = subparsers.add_parser("poweroff", help="execute poweroff command")
return parser.parse_args()


async def run():
args = parse_args()
if args.verbose == 1:
logger.setLevel(logging.INFO)
elif args.verbose == 2:
logger.setLevel(logging.DEBUG)

try:
pijuice_sys = PiJuiceSys(
i2c_bus=args.i2c_bus,
i2c_address=args.i2c_address,
config_file_path=args.config_file_path,
)
except PiJuiceSysInterfaceError:
logger.error("failed to initialize PiJuice interface")
sys.exit(1)
except PiJuiceSysConfigValidationError as e:
logger.error(f"config validation failed: {e}")
sys.exit(1)

if args.command == "poweroff":
await pijuice_sys.execute_poweroff_command()
elif args.command == "daemon":
logger.debug("starting daemon")
await pijuice_sys.run_daemon(
pid_file_path=args.pid_file_path,
poll_interval=args.poll_interval,
button_poll_interval=args.button_poll_interval,
)


def main():
try:
asyncio.run(run())
except KeyboardInterrupt:
pass


if __name__ == "__main__":
main()
Loading

0 comments on commit 5c3589f

Please sign in to comment.