<commit_before>#!/usr/bin/env bash

source $(dirname $0)/oref0-bash-common-functions.sh || (echo "ERROR: Failed to run oref0-bash-common-functions.sh. Is oref0 correctly installed?"; exit 1)

usage "$@" <<EOT
Usage: $self
Do all the things oref0 does once per minute, based on config files. This
should run from cron, in the myopenaps directory. Effects include trying to
get a network connection, killing crashed processes, syncing data, setting temp
basals, giving SMBs, and everything else that oref0 does. Writes to various
different log files, should (mostly) not write to stdout.
EOT

assert_cwd_contains_ini

CGM="$(get_pref_string .cgm)"
directory="$PWD"
CGM_LOOPDIR="$(get_pref_string .cgm_loop_path)"
BT_PEB="$(get_pref_string .bt_peb "")"
BT_MAC="$(get_pref_string .bt_mac "")"
PUSHOVER_TOKEN="$(get_pref_string .pushover_token "")"
PUSHOVER_USER="$(get_pref_string .pushover_user "")"

function is_process_running_named ()
{
    if ps aux |grep -v grep |grep -q "$1"; then
        return 0
    else
        return 1
    fi
}
function is_bash_process_running_named ()
{
    if ps aux | grep -v grep | grep bash | grep -q "$1"; then
        return 0
    else
        return 1
    fi
}

if ! is_bash_process_running_named "oref0-online $BT_MAC"; then
    oref0-online "$BT_MAC" 2>&1 >> /var/log/openaps/network.log &
fi

sudo wpa_cli -i wlan0 scan &

(
    killall -g --older-than 30m openaps
    killall-g oref0-pump-loop 1800
    killall -g --older-than 30m openaps-report
    killall-g oref0-g4-loop 600
) &

# kill pump-loop after 5 minutes of not writing to pump-loop.log
find /var/log/openaps/pump-loop.log -mmin +5 | grep pump && (
    echo No updates to pump-loop.log in 5m - killing processes
    killall -g --older-than 5m openaps
    killall-g oref0-pump-loop 300
    killall -g --older-than 5m openaps-report
) | tee -a /var/log/openaps/pump-loop.log | adddate openaps.pump-loop | uncolor |tee -a /var/log/openaps/openaps-date.log &

# if the rig doesn't recover after that, reboot:
oref0-radio-reboot &

if [[ ${CGM,,} =~ "g4-go" ]]; then
        cd $CGM_LOOPDIR
        if ! is_bash_process_running_named oref0-g4-loop; then
            oref0-g4-loop | tee -a /var/log/openaps/cgm-loop.log | adddate openaps.cgm-loop | uncolor |tee -a /var/log/openaps/openaps-date.log &
        fi<commit_msg>Kill ns-loop if it lasts for more than 10 minutes. (#1340)<commit_after>#!/usr/bin/env bash

source $(dirname $0)/oref0-bash-common-functions.sh || (echo "ERROR: Failed to run oref0-bash-common-functions.sh. Is oref0 correctly installed?"; exit 1)

usage "$@" <<EOT
Usage: $self
Do all the things oref0 does once per minute, based on config files. This
should run from cron, in the myopenaps directory. Effects include trying to
get a network connection, killing crashed processes, syncing data, setting temp
basals, giving SMBs, and everything else that oref0 does. Writes to various
different log files, should (mostly) not write to stdout.
EOT

assert_cwd_contains_ini

CGM="$(get_pref_string .cgm)"
directory="$PWD"
CGM_LOOPDIR="$(get_pref_string .cgm_loop_path)"
BT_PEB="$(get_pref_string .bt_peb "")"
BT_MAC="$(get_pref_string .bt_mac "")"
PUSHOVER_TOKEN="$(get_pref_string .pushover_token "")"
PUSHOVER_USER="$(get_pref_string .pushover_user "")"

function is_process_running_named ()
{
    if ps aux |grep -v grep |grep -q "$1"; then
        return 0
    else
        return 1
    fi
}
function is_bash_process_running_named ()
{
    if ps aux | grep -v grep | grep bash | grep -q "$1"; then
        return 0
    else
        return 1
    fi
}

if ! is_bash_process_running_named "oref0-online $BT_MAC"; then
    oref0-online "$BT_MAC" 2>&1 >> /var/log/openaps/network.log &
fi

sudo wpa_cli -i wlan0 scan &

(
    killall -g --older-than 30m openaps
    killall-g oref0-pump-loop 1800
    killall -g --older-than 30m openaps-report
    killall-g oref0-g4-loop 600
    killall-g oref0-ns-loop 600
) &

# kill pump-loop after 5 minutes of not writing to pump-loop.log
find /var/log/openaps/pump-loop.log -mmin +5 | grep pump && (
    echo No updates to pump-loop.log in 5m - killing processes
    killall -g --older-than 5m openaps
    killall-g oref0-pump-loop 300
    killall -g --older-than 5m openaps-report
) | tee -a /var/log/openaps/pump-loop.log | adddate openaps.pump-loop | uncolor |tee -a /var/log/openaps/openaps-date.log &

# if the rig doesn't recover after that, reboot:
oref0-radio-reboot &

if [[ ${CGM,,} =~ "g4-go" ]]; then
        cd $CGM_LOOPDIR
        if ! is_bash_process_running_named oref0-g4-loop; then
            oref0-g4-loop | tee -a /var/log/openaps/cgm-loop.log | adddate openaps.cgm-loop | uncolor |tee -a /var/log/openaps/openaps-date.log &
        fi
{'fraction_non_alphanumeric': 0.11849649607135272, 'fraction_numerical': 0.022085368443406243, 'mean_word_length': 3.317140238313474, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1659845', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1687, 'n_words': 492}