Skip to content

Commit

Permalink
Bugfixes (#1411)
Browse files Browse the repository at this point in the history
* Add config settings for low-battery shutdown thresholds

Adds a pair of config settings for low-battery shutdown thresholds.
One config setting is for Edison (denominated in millivolts), the
other is for Pi (given as percentage). This config setting is needed
for USB-powered Pi rigs, where the battery level indicator just
returns a random number, which might occasionally be zero.

* If unable to update with git pull, prompt before aborting

This is mainly for convenience during development; a checkout in
some weird branch doesn't necessarily return success when you
"git pull", but that doesn't necessarily mean you want to abort
oref0-runagain.sh.

* Fix bug that would clobber sshd_config

Depending on the initial contents of sshd_config, this would sometimes
try to add a line to it, but instead completely overwrite it to contain
only that line.

* Extend timeout on JS syntax check unit tests

On Pi Zero hardware, these tests would sometimes time out because
starting the nodejs interpreter is slow (even with a fixed nodejs
interpreter). Extend the timeout to 4s, from the default of 2s, so they
pass.

* Clean up require() usage in IOB unit test, making it pass without timeout on Pi Zero

* Add bash-unit-test-temp to gitignore

* Check for bad (super slow) RPi nodejs versions, install nvm version if found

* Tweak log messages about battery level
  • Loading branch information
jimrandomh authored Dec 11, 2021
1 parent 1954758 commit 1ec3528
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 66 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ bin/__pycache__
package-lock.json

*.pyc

bash-unit-test-temp

2 changes: 1 addition & 1 deletion bin/openaps-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ apt-get -o Acquire::ForceIPv4=true update && apt-get -o Acquire::ForceIPv4=true
apt-get -o Acquire::ForceIPv4=true update && apt-get -o Acquire::ForceIPv4=true install -y sudo strace tcpdump screen acpid vim python-pip locate ntpdate ntp
#check if edison user exists before trying to add it to groups

grep "PermitRootLogin yes" /etc/ssh/sshd_config || echo "PermitRootLogin yes" > /etc/ssh/sshd_config
grep "PermitRootLogin yes" /etc/ssh/sshd_config || echo "PermitRootLogin yes" >>/etc/ssh/sshd_config

if getent passwd edison > /dev/null; then
echo "Adding edison to sudo users"
Expand Down
16 changes: 14 additions & 2 deletions bin/oref0-cron-every-15min.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@ assert_cwd_contains_ini

# proper shutdown once the EdisonVoltage very low (< 3050mV; 2950 is dead)
if is_edison; then
sudo ~/src/EdisonVoltage/voltage json batteryVoltage battery | jq .batteryVoltage | awk '{if ($1<=3050)system("sudo shutdown -h now")}' &
BATTERY_VOLTAGE="$(sudo ~/src/EdisonVoltage/voltage json batteryVoltage battery | jq .batteryVoltage)"
echo "Battery voltage is $BATTERY_VOLTAGE."
BATTERY_CUTOFF=$(get_pref_float .edison_battery_shutdown_voltage 3050)
if (( "$BATTERY_VOLTAGE" <= "$BATTERY_CUTOFF" )); then
echo "Critically low battery! Shutting down."
sudo shutdown -h now
fi
fi

# proper shutdown of pi rigs once the battery level is below 2 % (should be more than enough to shut down on a standard 18600 ~2Ah cell)
if is_pi; then
sudo ~/src/openaps-menu/scripts/getvoltage.sh | tee ~/myopenaps/monitor/edison-battery.json | jq .battery | awk '{if ($1<2)system("sudo shutdown -h now")}' &
BATTERY_PERCENT="$(sudo ~/src/openaps-menu/scripts/getvoltage.sh | tee ~/myopenaps/monitor/edison-battery.json | jq .battery)"
BATTERY_CUTOFF=$(get_pref_float .pi_battery_shutdown_percent 2)
echo "Battery level is $BATTERY_PERCENT percent"
if (( "$BATTERY_PERCENT" < "$BATTERY_CUTOFF" )); then
echo "Critically low battery! Shutting down."
sudo shutdown -h now
fi
fi

# temporarily disable hotspot for 1m every 15m to allow it to try to connect via wifi again
Expand Down
56 changes: 47 additions & 9 deletions bin/oref0-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,47 @@ function move_mmtune () {
fi
}

function install_or_upgrade_nodejs () {
# install/upgrade to latest node 8 if neither node 8 nor node 10+ LTS are installed
if ! nodejs --version | grep -e 'v8\.' -e 'v1[02468]\.' >/dev/null; then
echo Installing node 8
# Use nodesource setup script to add nodesource repository to sources.list.d
sudo bash -c "curl -sL https://deb.nodesource.com/setup_8.x | bash -" || die "Couldn't setup node 8"
# Install nodejs and npm from nodesource
sudo apt-get install -y nodejs=8.* || die "Couldn't install nodejs"
fi

# Check that the nodejs you have installed is not broken. In particular, we're
# checking for a problem with nodejs binaries that are present in the apt-get
# repo for RaspiOS builds from mid-2021 and earlier, where the node interpreter
# works, but has a 10x slower startup than expected (~30s on Pi Zero W
# hardware, as opposed to ~3s using a statically-linked binary of the same
# binary sourced from nvm).
sudo apt-get install -y time
NODE_EXECUTION_TIME="$(\time --format %e node -e 'true' 2>&1)"
if [ 1 -eq "$(echo "$NODE_EXECUTION_TIME > 10" |bc)" ]; then
echo "Your installed nodejs ($(node --version)) is very slow to start (took ${NODE_EXECUTION_TIME}s)"
echo "This is a known problem with certain versions of Raspberry Pi OS."

if prompt_yn "Install a new nodejs version using nvm?" Y; then
echo "Installing nvm and using it to replace the system-provided nodejs"

# Download nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Run nvm, adding its aliases to this shell
source ~/.nvm/nvm.sh
# Use nvm to install nodejs
nvm install 10.24.1
# Symlink node into /usr/local/bin, where it will shadow /usr/bin/node
ln -s ~/.nvm/versions/node/v10.24.1/bin/node /usr/local/bin/node

NEW_NODE_EXECUTION_TIME="$(\time --format %e node -e 'true' 2>&1)"
echo "New nodejs took ${NEW_NODE_EXECUTION_TIME}s to start"
fi
else
echo "Your installed nodejs version is OK."
fi
}

if ! validate_cgm "${CGM}"; then
DIR="" # to force a Usage prompt
Expand Down Expand Up @@ -689,14 +730,7 @@ if prompt_yn "" N; then
echo Running apt-get autoclean
sudo apt-get autoclean

# install/upgrade to latest node 8 if neither node 8 nor node 10+ LTS are installed
if ! nodejs --version | grep -e 'v8\.' -e 'v1[02468]\.' ; then
echo Installing node 8
# Use nodesource setup script to add nodesource repository to sources.list.d
sudo bash -c "curl -sL https://deb.nodesource.com/setup_8.x | bash -" || die "Couldn't setup node 8"
# Install nodejs and npm from nodesource
sudo apt-get install -y nodejs=8.* || die "Couldn't install nodejs"
fi
install_or_upgrade_nodejs

# Attempting to remove git to make install --nogit by default for existing users
echo Removing any existing git in $directory/.git
Expand Down Expand Up @@ -742,7 +776,11 @@ if prompt_yn "" N; then
mkdir -p $HOME/src/
if [ -d "$HOME/src/oref0/" ]; then
echo "$HOME/src/oref0/ already exists; pulling latest"
(cd $HOME/src/oref0 && git fetch && git pull) || die "Couldn't pull latest oref0"
(cd $HOME/src/oref0 && git fetch && git pull) || (
if ! prompt_yn "Couldn't pull latest oref0. Continue anyways?"; then
die "Failed to update oref0."
fi
)
else
echo -n "Cloning oref0: "
(cd $HOME/src && git clone git://github.com/openaps/oref0.git) || die "Couldn't clone oref0"
Expand Down
6 changes: 5 additions & 1 deletion lib/profile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ function defaults ( ) {
//, maxRaw: 200 // highest raw/noisy CGM value considered safe to use for looping
, calc_glucose_noise: false
, target_bg: false // set to an integer value in mg/dL to override pump min_bg
};
, edison_battery_shutdown_voltage: 3050
, pi_battery_shutdown_percent: 2
}
}

function displayedDefaults () {
Expand All @@ -87,6 +89,8 @@ function displayedDefaults () {
profile.enableUAM = allDefaults.enableUAM;
profile.curve = allDefaults.curve;
profile.offline_hotspot = allDefaults.offline_hotspot;
profile.edison_battery_shutdown_voltage = allDefaults.edison_battery_shutdown_voltage;
profile.pi_battery_shutdown_percent = allDefaults.pi_battery_shutdown_percent;

console.error(profile);
return profile
Expand Down
3 changes: 2 additions & 1 deletion tests/check-syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ describe("Syntax checks", function() {
var type = getFileFormat(file);
if(type !== "unknown") {
it(file, function() {
this.timeout(4000);
checkFile(file, type);
});
}
});
});
});
Loading

0 comments on commit 1ec3528

Please sign in to comment.