Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pijuice_sys re-implementation #583

Open
wants to merge 3 commits into
base: experimental
Choose a base branch
from

Conversation

betaboon
Copy link

Hello Pijuice-team,

since i had several problems integrating pijuice_sys on non-raspbian distributions i figured i could reimplement pijuice_sys and add several improvements on how things work.

i think this PR might help with multiple open issues and feature-requests as well.

this PR includes several things:

changes to setup.py

  • switching to setuptools allows explicit definition of dependencies
  • switching to setuptools allows the definition of entry_points to better separate implementation and the name of a command when installed
  • using of functools.partial allows better separation of settings that are common to pijuice-base and pijuice-gui and of settings that are different, making the file more readable and maintainable

pijuice_cmd

allows shell access to wakeup-on-charge, system-power-switch, power-off and led-blink.
it is used in the new system-function-scripts and can be used in user-function-scripts.

[pi@raspberry:~]# pijuice_cmd --help
usage: pijuice_cmd [-h] [-B BUS] [-A ADDRESS] [-q]
                   {wakeup-on-charge,system-power-switch,power-off,led-blink} ...

PiJuice Cmd

positional arguments:
  {wakeup-on-charge,system-power-switch,power-off,led-blink}
                        sub-command help
    wakeup-on-charge    configure wakeup-on-charge
    system-power-switch
                        configure system-power-switch
    power-off           configure power-off
    led-blink           run led-blink pattern

optional arguments:
  -h, --help            show this help message and exit
  -B BUS, --i2c-bus BUS
                        I2C Bus (default: 1)
  -A ADDRESS, --i2c-address ADDRESS
                        I2C Address (default: 20)
  -q, --quiet           Disable output

[pi@raspberry:~]# pijuice_cmd wakeup-on-charge --help
usage: pijuice_cmd wakeup-on-charge [-h] [-D] [-T TRIGGER_LEVEL]

optional arguments:
  -h, --help            show this help message and exit
  -D, --disabled        Whether to disable wakeup-on-charge
  -T TRIGGER_LEVEL, --trigger-level TRIGGER_LEVEL
                        Battery-charge to wakeup at (default: 100)

[pi@raspberry:~]# pijuice_cmd system-power-switch --help
usage: pijuice_cmd system-power-switch [-h] {0,500,2100}

positional arguments:
  {0,500,2100}  Current limit for VSYS pin (in mA)

optional arguments:
  -h, --help    show this help message and exit

[pi@raspberry:~]# pijuice_cmd power-off --help
usage: pijuice_cmd power-off [-h] [-D DELAY]

optional arguments:
  -h, --help            show this help message and exit
  -D DELAY, --delay DELAY
                        Delay before powering off (0 to 65535) (default: 0)

[pi@raspberry:~]# pijuice_cmd led-blink --help
usage: pijuice_cmd led-blink [-h] [-L LED] [-C COUNT] [--rgb1 R,G,B] [--period1 PERIOD] [--rgb2 R,G,B] [--period2 PERIOD]

optional arguments:
  -h, --help            show this help message and exit
  -L LED, --led LED     Led to blink (default: D2)
  -C COUNT, --count COUNT
                        Amount of repetitions (default: 1)
  --rgb1 R,G,B          RGB components of first blink (default: 150,0,0)
  --period1 PERIOD      Duration of first blink in milliseconds (default: 200)
  --rgb2 R,G,B          RGB components of second blink (default: 0,100,0)
  --period2 PERIOD      Duration of second blink in milliseconds (default: 200)

pijuice_sys-reimplementation

  • it uses asyncio to assist in making the codebase more readable and maintainable.
  • it separates the system_tasks into several co-routines.
  • it uses marshmallow to define proper schemas for the configuration file to allow validation.
  • it separates what is being executed on system-functions into shell-scripts.
  • it allows overriding which shell-script to run on system-functions
  • it does not rely on a HALT_FILE
  • it comes with an argument-parser
  • it comes with (debug-)logging
  • it falls back i using the daemon-user when running scripts that are now owned by a user that is part of the daemons group
[pi@raspberry:~]# pijuice_sys --help
usage: pijuice_sys [-h] [-v] [-B BUS] [-A ADDRESS] [-C FILE] {daemon,poweroff} ...

PiJuice System Daemon

positional arguments:
  {daemon,poweroff}     sub-command help
    daemon              run daemon
    poweroff            execute poweroff command

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         Increase verbosity level
  -B BUS, --i2c-bus BUS
                        I2C Bus (default: 1)
  -A ADDRESS, --i2c-address ADDRESS
                        I2C Address (default: 20)
  -C FILE, --config-file FILE
                        Path to read Configuration file from (default: /var/lib/pijuice/pijuice_config.JSON)

[pi@raspberry:~]# pijuice_sys daemon --help
usage: pijuice_sys daemon [-h] [--pid-file FILE] [--poll-interval INTERVAL] [--button-poll-interval INTERVAL]

optional arguments:
  -h, --help            show this help message and exit
  --pid-file FILE       Path to create pid-file at (default: /tmp/pijuice_sys.pid)
  --poll-interval INTERVAL
                        Interval at which to poll status from pijuice (default: 5.0)
  --button-poll-interval INTERVAL
                        Interval at which to poll button-events from pijuice (default: 1.0)

[pi@raspberry:~]# pijuice_sys poweroff --help
usage: pijuice_sys poweroff [-h]

optional arguments:
  -h, --help  show this help message and exit

systemd-service changes

the systemd-service is now seperated into pijuice and pijuice-poweroff.
pijuice-poweroff is started when the system is being shutdown.

general notes

i tried to make this a drop-in replacement as much as possible.
the only changes that are not backward compatible:

  • the commandline interface of pijuice_sys
  • the daemon falls back to running scripts as the daemon-user when the script is not owned by a user that is part of the daemons group
  • USER_FUNCX now MUST be the path of a file and cannot be a command itself.
  • SYS_FUNC_HALTand SYS_FUNC_HALT_POW_OFF now executes systemctl poweroff instead of halt as i think this is more appropriate

anyway, that's it.
let me know of you have any thoughts on this, any requests etc.

@shawaj
Copy link
Member

shawaj commented Nov 17, 2020

@betaboon thanks very much for your hard work on this code and supporting the project. Much appreciated!

Myself and @tvoverbeek and @ryanteck will take a closer look :-)

@shawaj
Copy link
Member

shawaj commented Jan 1, 2021

@betaboon I was wondering if perhaps it might be better to combine the functionality of pijuice_util.py and your pijuice_cmd into a single command line utility?

https://github.com/PiSupply/PiJuice/blob/master/Software/Source/Utilities/pijuice_util.py

As this is quite a major change, I'm thinking to perhaps merge your changes into a new experimental branch so that we can test and if necessary develop further before releasing it to master.

I guess we will need to develop / change some of the documentation which I guess we should do before merging to master branch.

The other thing I noticed is, was there a particular reason you used decimal numbering for the I2C address? I.e you have it 20 default instead of 0x14? I think having it as hex might be easier to understand perhaps.

Of the non backward compatible changes you mentioned, the only one that seems to potentially break people's existing setups is this one:

USER_FUNCX now MUST be the path of a file and cannot be a command itself.

Do you think there's a way to have it handle both commands and file paths? I would rather not break people's existing setups if it can be avoided.

Lastly, when installing this as a deb over an existing install does this require any tidying up etc?

@shawaj
Copy link
Member

shawaj commented Jan 1, 2021

FYI @mmilann also

@shawaj shawaj changed the base branch from master to experimental January 1, 2021 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants