<commit_before>#!/bin/bash

# main pump-loop
main() {
    prep
    until( \
        echo && echo Starting pump-loop at $(date): \
        && wait_for_bg \
        && wait_for_silence \
        && if_mdt_get_bg \
        && refresh_old_pumphistory_enact \
        && refresh_old_pumphistory_24h \
        && refresh_old_profile \
        && touch /tmp/pump_loop_enacted -r monitor/glucose.json \
        && ( refresh_temp_and_enact || ( smb_verify_status && refresh_temp_and_enact ) ) \
        && refresh_pumphistory_and_enact \
        && refresh_profile \
        && refresh_pumphistory_24h \
        && echo Completed pump-loop at $(date) \
        && touch /tmp/pump_loop_completed -r /tmp/pump_loop_enacted \
        && echo); do

            if grep -q "percent" monitor/temp_basal.json; then
                echo "Pssst! Your pump is set to % basal type. The pump won’t accept temporary basal rates in this mode. Change it to absolute u/hr, and temporary basal rates will then be able to be set."
            fi
            # On a random subset of failures, mmtune
            echo Error, retrying \
            && maybe_mmtune
            sleep 5
    done
}

# main supermicrobolus loop
smb_main() {
    prep
    if ! ( \
        prep
        # checking to see if the log reports out that it is on % basal type, which blocks remote temps being set
        echo && echo Starting supermicrobolus pump-loop at $(date) with $upto30s second wait_for_silence: \
        && wait_for_bg \
        && wait_for_silence $upto30s \
        && ( preflight || preflight ) \
        && if_mdt_get_bg \
        && refresh_old_pumphistory_24h \
        && refresh_old_pumphistory \
        && refresh_old_profile \
        && touch /tmp/pump_loop_enacted -r monitor/glucose.json \
        && refresh_smb_temp_and_enact \
        && ( smb_check_everything \
            && if (grep -q '"units":' enact/smb-suggested.json); then
                ( smb_bolus && \
                    touch /tmp/pump_loop_completed -r /tmp/pump_loop_enacted \
                ) \
                || ( smb_old_temp && ( \
                    echo "Falling back to normal pump-loop" \
                    && refresh_temp_and_enact \
                    && refresh_pumphistory_and_enact \
                    && refresh_profile \
                    && refresh_pumphistory_24h \
                    && echo Completed pump-loop at $(date) \
                    && echo \
                    ))
            fi
            ) \
            && ( refresh_profile 15; refresh_pumphistory_24h; true ) \
            && refresh_after_bolus_or_enact \
            && echo Completed supermicrobolus pump-loop at $(date): \
            && touch /tmp/pump_loop_completed -r /tmp/pump_loop_enacted \
            && echo \
    ); then
        echo -n "SMB pump-loop failed. "
        if grep -q "percent" monitor/temp_basal.json; then
            echo "Pssst! Your pump is set to % basal type. The pump won’t accept temporary basal rates in this mode. Change it to absolute u/hr, and temporary basal rates will then be able to be set."
        fi
        maybe_mmtune
        echo Unsuccessful supermicrobolus pump-loop at $(date)
    fi
}

function smb_reservoir_before {
    # Refresh reservoir.json and pumphistory.json
    gather \
    && cp monitor/reservoir.json monitor/lastreservoir.json \
    && openaps report invoke monitor/clock.json monitor/clock-zoned.json 2>&1 >/dev/null | tail -1 \
    && echo -n "Checking pump clock: " && (cat monitor/clock-zoned.json; echo) | tr -d '\n' \
    && echo -n " is within 1m of current time: " && date \
    && if (( $(bc <<< "$(date +%s -d $(cat monitor/clock-zoned.json | sed 's/"//g')) - $(date +%s)") < -60 )) || (( $(bc <<< "$(date +%s -d $(cat monitor/clock-zoned.json | sed 's/"//g')) - $(date +%s)") > 60 )); then
        echo Pump clock is more than 1m off: attempting to reset it
        oref0-set-device-clocks
       fi \
    && (( $(bc <<< "$(date +%s -d $(cat monitor/clock-zoned.json | sed 's/"//g')) - $(date +%s)") > -60 )) \
    && (( $(bc <<< "$(date +%s -d $(cat monitor/clock-zoned.json | sed 's/"//g')) - $(date +%s)") < 60 )) \
    && echo -n "and that pumphistory is less than 1m old.  " \
    && (find monitor/ -mmin -1 -size +5c | grep -q pumphistory)

}

# check if the temp was read more than 5m ago, or has been running more than 10m
function smb_old_temp {
    (find monitor/ -mmin +5 -size +5c | grep -q temp_basal && echo temp_basal.json more than 5m old) \
    || ( jq --exit-status "(.duration-1) % 30 < 20" monitor/temp_basal.json > /dev/null \
        && echo -n "Temp basal set more than 10m ago: " && jq .duration monitor/temp_basal.json
        )
}

# make sure everything is in the right condition to SMB
function smb_check_everything {
    # wait_for_silence and retry if first attempt fails
    smb_reservoir_before \
    && smb_enact_temp \
    && if (grep -q '"units":' enact/smb-suggested.json); then
        ( smb_verify_suggested || smb_suggest ) \
        && wait_for_silence 1 \
        && smb_verify_reservoir \
        && smb_verify_status \
        || ( echo Retrying SMB checks
            wait_for_silence 10
            smb_verify_status \
            && smb_reservoir_before \
            && smb_enact_temp \
            && ( smb_verify_suggested || smb_suggest ) \
            && smb_verify_reservoir \
            && smb_verify_status
            )
    else
        echo -n "No bolus needed (yet). "
    fi
}

function smb_suggest {
    rm -rf enact/smb-suggested.json
    ls enact/smb-suggested.json 2>/dev/null >/dev/null && die "enact/suggested.json present"
    # Run determine-basal
    echo -n Temp refresh && openaps report invoke monitor/temp_basal.json monitor/clock.json monitor/clock-zoned.json monitor/iob.json 2>&1 >/dev/null | tail -1 && echo ed \
    && openaps report invoke enact/smb-suggested.json 2>&1 >/dev/null \
    && cp -up enact/smb-suggested.json enact/suggested.json \
    && smb_verify_suggested
}
<commit_msg>randomize wait_for_silence before smb_verify_reservoir (#679)<commit_after>#!/bin/bash

# main pump-loop
main() {
    prep
    until( \
        echo && echo Starting pump-loop at $(date): \
        && wait_for_bg \
        && wait_for_silence \
        && if_mdt_get_bg \
        && refresh_old_pumphistory_enact \
        && refresh_old_pumphistory_24h \
        && refresh_old_profile \
        && touch /tmp/pump_loop_enacted -r monitor/glucose.json \
        && ( refresh_temp_and_enact || ( smb_verify_status && refresh_temp_and_enact ) ) \
        && refresh_pumphistory_and_enact \
        && refresh_profile \
        && refresh_pumphistory_24h \
        && echo Completed pump-loop at $(date) \
        && touch /tmp/pump_loop_completed -r /tmp/pump_loop_enacted \
        && echo); do

            if grep -q "percent" monitor/temp_basal.json; then
                echo "Pssst! Your pump is set to % basal type. The pump won’t accept temporary basal rates in this mode. Change it to absolute u/hr, and temporary basal rates will then be able to be set."
            fi
            # On a random subset of failures, mmtune
            echo Error, retrying \
            && maybe_mmtune
            sleep 5
    done
}

# main supermicrobolus loop
smb_main() {
    prep
    if ! ( \
        prep
        # checking to see if the log reports out that it is on % basal type, which blocks remote temps being set
        echo && echo Starting supermicrobolus pump-loop at $(date) with $upto30s second wait_for_silence: \
        && wait_for_bg \
        && wait_for_silence $upto30s \
        && ( preflight || preflight ) \
        && if_mdt_get_bg \
        && refresh_old_pumphistory_24h \
        && refresh_old_pumphistory \
        && refresh_old_profile \
        && touch /tmp/pump_loop_enacted -r monitor/glucose.json \
        && refresh_smb_temp_and_enact \
        && ( smb_check_everything \
            && if (grep -q '"units":' enact/smb-suggested.json); then
                ( smb_bolus && \
                    touch /tmp/pump_loop_completed -r /tmp/pump_loop_enacted \
                ) \
                || ( smb_old_temp && ( \
                    echo "Falling back to normal pump-loop" \
                    && refresh_temp_and_enact \
                    && refresh_pumphistory_and_enact \
                    && refresh_profile \
                    && refresh_pumphistory_24h \
                    && echo Completed pump-loop at $(date) \
                    && echo \
                    ))
            fi
            ) \
            && ( refresh_profile 15; refresh_pumphistory_24h; true ) \
            && refresh_after_bolus_or_enact \
            && echo Completed supermicrobolus pump-loop at $(date): \
            && touch /tmp/pump_loop_completed -r /tmp/pump_loop_enacted \
            && echo \
    ); then
        echo -n "SMB pump-loop failed. "
        if grep -q "percent" monitor/temp_basal.json; then
            echo "Pssst! Your pump is set to % basal type. The pump won’t accept temporary basal rates in this mode. Change it to absolute u/hr, and temporary basal rates will then be able to be set."
        fi
        maybe_mmtune
        echo Unsuccessful supermicrobolus pump-loop at $(date)
    fi
}

function smb_reservoir_before {
    # Refresh reservoir.json and pumphistory.json
    gather \
    && cp monitor/reservoir.json monitor/lastreservoir.json \
    && openaps report invoke monitor/clock.json monitor/clock-zoned.json 2>&1 >/dev/null | tail -1 \
    && echo -n "Checking pump clock: " && (cat monitor/clock-zoned.json; echo) | tr -d '\n' \
    && echo -n " is within 1m of current time: " && date \
    && if (( $(bc <<< "$(date +%s -d $(cat monitor/clock-zoned.json | sed 's/"//g')) - $(date +%s)") < -60 )) || (( $(bc <<< "$(date +%s -d $(cat monitor/clock-zoned.json | sed 's/"//g')) - $(date +%s)") > 60 )); then
        echo Pump clock is more than 1m off: attempting to reset it
        oref0-set-device-clocks
       fi \
    && (( $(bc <<< "$(date +%s -d $(cat monitor/clock-zoned.json | sed 's/"//g')) - $(date +%s)") > -60 )) \
    && (( $(bc <<< "$(date +%s -d $(cat monitor/clock-zoned.json | sed 's/"//g')) - $(date +%s)") < 60 )) \
    && echo -n "and that pumphistory is less than 1m old.  " \
    && (find monitor/ -mmin -1 -size +5c | grep -q pumphistory)

}

# check if the temp was read more than 5m ago, or has been running more than 10m
function smb_old_temp {
    (find monitor/ -mmin +5 -size +5c | grep -q temp_basal && echo temp_basal.json more than 5m old) \
    || ( jq --exit-status "(.duration-1) % 30 < 20" monitor/temp_basal.json > /dev/null \
        && echo -n "Temp basal set more than 10m ago: " && jq .duration monitor/temp_basal.json
        )
}

# make sure everything is in the right condition to SMB
function smb_check_everything {
    # wait_for_silence and retry if first attempt fails
    smb_reservoir_before \
    && smb_enact_temp \
    && if (grep -q '"units":' enact/smb-suggested.json); then
        ( smb_verify_suggested || smb_suggest ) \
        && echo -n "Listening for $upto10s s silence: " && wait_for_silence $upto10s \
        && smb_verify_reservoir \
        && smb_verify_status \
        || ( echo Retrying SMB checks
            wait_for_silence 10
            smb_verify_status \
            && smb_reservoir_before \
            && smb_enact_temp \
            && ( smb_verify_suggested || smb_suggest ) \
            && smb_verify_reservoir \
            && smb_verify_status
            )
    else
        echo -n "No bolus needed (yet). "
    fi
}

function smb_suggest {
    rm -rf enact/smb-suggested.json
    ls enact/smb-suggested.json 2>/dev/null >/dev/null && die "enact/suggested.json present"
    # Run determine-basal
    echo -n Temp refresh && openaps report invoke monitor/temp_basal.json monitor/clock.json monitor/clock-zoned.json monitor/iob.json 2>&1 >/dev/null | tail -1 && echo ed \
    && openaps report invoke enact/smb-suggested.json 2>&1 >/dev/null \
    && cp -up enact/smb-suggested.json enact/suggested.json \
    && smb_verify_suggested
}

{'fraction_non_alphanumeric': 0.11932522947159514, 'fraction_numerical': 0.009757711072521293, 'mean_word_length': 1.9824907521578299, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 25, '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': '6574762', 'n_tokens_mistral': 4109, 'n_tokens_neox': 3879, 'n_words': 1043}