diff --git a/bin/oref0-append-local-temptarget.sh b/bin/oref0-append-local-temptarget.sh new file mode 100755 index 000000000..fe78ba5d1 --- /dev/null +++ b/bin/oref0-append-local-temptarget.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +if [[ ! -z "$2" ]]; then + input=$(oref0-set-local-temptarget $@) +elif [[ ! -z "$1" ]]; then + input=$(cat $1) +else + input=$(cat /dev/stdin) +fi +#cat "${1:-/dev/stdin}" \ +echo $input \ + | tee /tmp/temptarget.json \ + && jq -s '[.[0]] + .[1]' /tmp/temptarget.json settings/local-temptargets.json \ + | tee settings/local-temptargets.json.new \ + && mv settings/local-temptargets.json.new settings/local-temptargets.json diff --git a/bin/oref0-dex-wait-until-expected.sh b/bin/oref0-dex-wait-until-expected.sh index 69e7b3593..c9ea49b2f 100755 --- a/bin/oref0-dex-wait-until-expected.sh +++ b/bin/oref0-dex-wait-until-expected.sh @@ -10,7 +10,9 @@ if (( $(bc <<< "$TIME_SINCE >= $OLD") )); then echo "CGM Data $TIME_SINCE mins ago is old (>=$OLD), not waiting" else WAIT_MINS=$(bc <<< "$OLD - $TIME_SINCE") - if (( $(bc <<< "$WAIT_MINS >= $MAX_WAIT") )); then + if (( $(bc <<< "$WAIT_MINS > 6") )); then + echo "Clock mismatch ($WAIT_MINS > 6); not waiting" + elif (( $(bc <<< "$WAIT_MINS >= $MAX_WAIT") )); then echo "CGM Data $TIME_SINCE mins ago is fresh (< $OLD), $WAIT_MINS mins > max wait ($MAX_WAIT mins) waiting for next attempt" exit 1 else diff --git a/bin/oref0-ns-loop.sh b/bin/oref0-ns-loop.sh index 8d82e125c..2c3f2a4f5 100755 --- a/bin/oref0-ns-loop.sh +++ b/bin/oref0-ns-loop.sh @@ -35,7 +35,7 @@ function overtemp { function highload { # check whether system load average is high - uptime | awk '$NF > 2' + uptime | awk '$NF > 2' | grep load } @@ -81,17 +81,33 @@ function find_valid_ns_glucose { } function ns_temptargets { - openaps report invoke settings/temptargets.json settings/profile.json >/dev/null + #openaps report invoke settings/temptargets.json settings/profile.json >/dev/null + nightscout ns $NIGHTSCOUT_HOST $API_SECRET temp_targets > settings/ns-temptargets.json.new + cat settings/ns-temptargets.json.new | jq .[0].duration | egrep -q [0-9] && mv settings/ns-temptargets.json.new settings/ns-temptargets.json # TODO: merge local-temptargets.json with ns-temptargets.json #openaps report invoke settings/ns-temptargets.json settings/profile.json - echo -n "Refreshed temptargets: " - cat settings/temptargets.json | jq -c -C '.[0] | { target: .targetBottom, duration: .duration }' + echo -n "Refreshed NS temptargets: " + cat settings/ns-temptargets.json | jq -c -C '.[0] | { target: .targetBottom, duration: .duration, start: .created_at }' + # delete any local-temptarget files last modified more than 24h ago + find settings/local-temptarget* -mmin +1440 -exec rm {} \; + echo -n "Merging local temptargets: " + cat settings/local-temptargets.json | jq -c -C '.[0] | { target: .targetBottom, duration: .duration, start: .created_at }' + jq -s '.[0] + .[1]|unique|sort_by(.created_at)|reverse' settings/ns-temptargets.json settings/local-temptargets.json > settings/temptargets.json + echo -n "Temptargets merged: " + cat settings/temptargets.json | jq -c -C '.[0] | { target: .targetBottom, duration: .duration, start: .created_at }' + oref0-get-profile settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json --model=settings/model.json --autotune settings/autotune.json | jq . > settings/profile.json.new || die "Couldn't refresh profile" + if cat settings/profile.json.new | jq . | grep -q basal; then + mv settings/profile.json.new settings/profile.json + else + die "Invalid profile.json.new after refresh" + fi } # openaps report invoke monitor/carbhistory.json; oref0-meal monitor/pumphistory-merged.json settings/profile.json monitor/clock-zoned.json monitor/glucose.json settings/basal_profile.json monitor/carbhistory.json > monitor/meal.json.new; grep -q COB monitor/meal.json.new && mv monitor/meal.json.new monitor/meal.json; exit 0 function ns_meal_carbs { #openaps report invoke monitor/carbhistory.json >/dev/null - nightscout ns $NIGHTSCOUT_HOST $API_SECRET carb_history > monitor/carbhistory.json + nightscout ns $NIGHTSCOUT_HOST $API_SECRET carb_history > monitor/carbhistory.json.new + cat monitor/carbhistory.json.new | jq .[0].carbs | egrep -q [0-9] && mv monitor/carbhistory.json.new monitor/carbhistory.json oref0-meal monitor/pumphistory-merged.json settings/profile.json monitor/clock-zoned.json monitor/glucose.json settings/basal_profile.json monitor/carbhistory.json > monitor/meal.json.new grep -q COB monitor/meal.json.new && mv monitor/meal.json.new monitor/meal.json echo -n "Refreshed carbhistory; COB: " diff --git a/bin/oref0-online.sh b/bin/oref0-online.sh index 8398c8bcb..5adb9de76 100755 --- a/bin/oref0-online.sh +++ b/bin/oref0-online.sh @@ -1,78 +1,251 @@ #!/bin/bash -echo; echo Starting oref0-online. -# if we are connected to wifi but don't have an IP, try to get one -if iwgetid -r wlan0 | egrep -q "[A-Za-z0-9_]+"; then - if ! ip route | grep default | grep -q wlan0; then - echo Attempting to renew wlan0 IP - sudo dhclient wlan0 + +main() { + MACs=$@ + HostAPDIP='10.29.29.1' + echo; echo Starting oref0-online at $(date). + # if we are connected to wifi but don't have an IP, try to get one + if iwgetid -r wlan0 | egrep -q "[A-Za-z0-9_]+"; then + if ! ip route | grep default | grep -q wlan0; then + if grep -q $(iwgetid -r wlan0) /tmp/bad_wifi; then + echo Not renewing wlan0 IP due to previous connectivity failure: + ls -la /tmp/bad_wifi + else + echo Attempting to renew wlan0 IP + sudo dhclient wlan0 + fi + fi + fi + if ifconfig | egrep -q "wlan0" >/dev/null; then + #if [[ $(ip -4 -o addr show dev wlan0 | awk '{split($4,a,"/");print a[1]}') = $(print_local_ip wlan0) ]]; then + print_wifi_name + echo -n "At $(date) my local wifi IP is: " + print_local_ip wlan0 + fi + if ifconfig | egrep -q "bnep0" >/dev/null; then + #if [[ $(ip -4 -o addr show dev bnep0 | awk '{split($4,a,"/");print a[1]}') = $(print_local_ip bnep0) ]]; then + print_bluetooth_name + #fi + echo -n "At $(date) my local Bluetooth IP is: " + print_local_ip bnep0 + else + echo "At $(date) my Bluetooth PAN is not connected" + fi + echo -n "At $(date) my public IP is: " + if check_ip; then + stop_hotspot + if has_ip wlan0 && has_ip bnep0; then + # if online via BT w/o a DHCP IP, cycle wifi + if print_local_ip wlan0 | grep $HostAPDIP || ! has_ip wlan0; then + ifdown wlan0; ifup wlan0 + fi + fi + # if online via wifi, disconnect BT + if has_ip wlan0 && ifconfig | egrep -q "bnep0" >/dev/null; then + bt_disconnect $MACs + #wifi_dhcp_renew + fi + else + echo + print_wifi_name + if ! has_ip wlan0; then + wifi_dhcp_renew + fi + if ! check_ip >/dev/null; then + bt_connect $MACs + fi + #print_wifi_name + if check_ip >/dev/null; then + # if we're online after activating bluetooth, shut down any local-access hotspot we're running + stop_hotspot + if ! print_local_ip wlan0 | egrep -q "[A-Za-z0-9_]+" >/dev/null; then + wifi_dhcp_renew + fi + else + # if we can't connect via BT, might as well try previously bad wifi networks again + rm /tmp/bad_wifi + # if we can't get online via wifi or bluetooth, start our own local-access hotspot + start_hotspot $@ + # don't disconnect bluetooth when starting local-only hotspot + fi + fi + echo Finished oref0-online at $(date). +} + +function print_bluetooth_name { + echo -n "At $(date) my Bluetooth is connected to " + grep Name /var/lib/bluetooth/*/*/info | awk -F = '{print $2}' + #echo ${MACs} +} + +function print_wifi_name { + SSID=$(iwgetid -r wlan0 | tr -d '\n') + if [[ ! -z $SSID ]]; then + echo "At $(date) my wifi network name is $SSID" + else + echo "At $(date) my wifi is not connected" fi -fi -echo -n "At $(date) my local IP is: " -ip -4 -o addr show dev wlan0 | awk '{split($4,a,"/");print a[1]}' -ip -4 -o addr show dev bnep0 | awk '{split($4,a,"/");print a[1]}' -echo -echo -n "At $(date), my wifi network name is " -iwgetid -r wlan0 | tr -d '\n' -echo -n ", and my public IP is: " -if curl --compressed -4 -s -m 15 checkip.amazonaws.com | awk -F , '{print $NF}' | egrep "^[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]$"; then - # if we are back on wifi (and have connectivity to checkip.amazonaws.com), shut down bluetooth - if ( ifconfig | grep -A1 wlan0 | grep -q "inet addr" ) && ( ifconfig | grep -A1 bnep0 | grep -q "inet addr" ); then - echo "Back online via wifi; disconnecting BT $MAC" - ifdown bnep0 - # loop over as many MACs as are provided as arguments - for MAC; do +} + +function print_local_ip { + LOCAL_IP=$(ip -4 -o addr show dev $1 | awk '{split($4,a,"/");print a[1]}') + if [[ -z $LOCAL_IP ]]; then + echo unassigned + else + echo $LOCAL_IP + fi +} + +function check_ip { + PUBLIC_IP=$(curl --compressed -4 -s -m 15 checkip.amazonaws.com | awk -F , '{print $NF}' | egrep "^[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]$") + if [[ -z $PUBLIC_IP ]]; then + echo not found + return 1 + else + echo $PUBLIC_IP + fi +} + +function has_ip { + ifconfig | grep -A1 $1 | grep -q "inet addr" +} + +function bt_connect { + # loop over as many MACs as are provided as arguments + for MAC; do + #echo -n "At $(date) my public IP is: " + if ! check_ip >/dev/null; then + echo; echo "No Internet access detected, attempting to connect BT to $MAC" + oref0-bluetoothup sudo bt-pan client $MAC -d - done - echo "and getting new wlan0 IP" + sudo bt-pan client $MAC + #echo "Attempt to get bnep0 IP :" + sudo dhclient bnep0 + if ifconfig | egrep -q "bnep0" >/dev/null; then + echo -n "Connected to Bluetooth with IP: " + print_local_ip bnep0 + fi + # if we couldn't reach the Internet over wifi, but (now) have a bnep0 IP, release the wifi IP/route + if has_ip wlan0 && has_ip bnep0 && ! grep -q $HostAPDIP /etc/network/interfaces; then + # release the wifi IP/route but *don't* renew it, in case it's not working + sudo dhclient wlan0 -r + iwgetid -r wlan0 >> /tmp/bad_wifi + fi + #echo + fi + done +} + +function bt_disconnect { + echo "Disconnecting BT $MAC" + ifdown bnep0 + # loop over as many MACs as are provided as arguments + for MAC; do + sudo bt-pan client $MAC -d + done +} + +function wifi_dhcp_renew { + if grep -q $(iwgetid -r wlan0) /tmp/bad_wifi; then + echo Not renewing wlan0 IP due to previous connectivity failure: + ls -la /tmp/bad_wifi + else + echo; echo "Getting new wlan0 IP" ps aux | grep -v grep | grep -q "dhclient wlan0" && sudo killall dhclient sudo dhclient wlan0 -r sudo dhclient wlan0 fi -else - echo - echo -n "At $(date), my wifi network name is " - iwgetid -r wlan0 | tr -d '\n' - echo -n ", and my public IP is: " - # loop over as many MACs as are provided as arguments - if ! curl --compressed -4 -s -m 15 checkip.amazonaws.com | awk -F , '{print $NF}' | egrep "^[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]$"; then - echo - for MAC; do - echo -n "At $(date) my public IP is: " - if ! curl --compressed -4 -s -m 15 checkip.amazonaws.com | awk -F , '{print $NF}' | egrep "^[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]$"; then - echo; echo -n "Error, connecting BT to $MAC" - oref0-bluetoothup - sudo bt-pan client $MAC -d - sudo bt-pan client $MAC - echo -n ", getting bnep0 IP" - sudo dhclient bnep0 - # if we couldn't reach the Internet over wifi, but we have a bnep0 IP, release the wifi IP/route - if ( ifconfig | grep -A1 wlan0 | grep -q "inet addr" ) && ( ifconfig | grep -A1 bnep0 | grep -q "inet addr" ); then - echo -n " and releasing wifi IP" - sudo dhclient wlan0 -r - echo - echo Sleeping for 2 minutes before trying wifi again - sleep 120 - fi - echo - fi - done - echo +} + +function stop_hotspot { + if grep -q $HostAPDIP /etc/network/interfaces || iwconfig wlan0 | grep Mode:Master; then + echo "Shutting down local-only hotspot" + echo "Attempting to stop hostapd" + /etc/init.d/hostapd stop + echo "Attempting to stop dnsmasq" + /etc/init.d/dnsmasq stop + echo "Activating client config" + ifdown wlan0 + cp /etc/network/interfaces.client /etc/network/interfaces + ifup wlan0 + echo "Renewing IP Address for wlan0" + dhclient_restart + else + echo -n "At $(date) my local hotspot is not running" + if ! cat preferences.json | jq -e .offline_hotspot >/dev/null; then + echo " (and not enabled in preferences.json)" + else + echo + fi fi - echo -n "At $(date), my wifi network name is " - iwgetid -r wlan0 | tr -d '\n' - echo -n ", and my public IP is: " - # if we still can't get online, try cycling networking as a last resort - if ! curl --compressed -4 -s -m 15 checkip.amazonaws.com | awk -F , '{print $NF}' | egrep "^[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]$"; then - echo; echo "Error, cycling networking " - sudo /etc/init.d/networking stop +} + +function stop_cycle { + stop_hotspot + echo "Cycling wlan0" + ifdown wlan0; ifup wlan0 +} + + +function start_hotspot { + echo + if ls /tmp/disable_hotspot; then + stop_cycle + elif ! ls preferences.json 2>/dev/null >/dev/null \ + || ! cat preferences.json | jq -e .offline_hotspot >/dev/null; then + echo "Offline hotspot not enabled in preferences.json" + stop_cycle + elif [[ -z $1 ]]; then + echo "No BT MAC provided: not activating local-only hotspot" + echo "Cycling wlan0" + ifdown wlan0; ifup wlan0 + elif grep -q $HostAPDIP /etc/network/interfaces \ + && ifconfig wlan0 | grep -q $HostAPDIP; then + echo "Local hotspot is running." + service hostapd status > /dev/null || service hostapd restart + service dnsmasq status > /dev/null || service dnsmasq restart + elif ! ls /etc/network/interfaces.ap 2>/dev/null >/dev/null; then + echo "Local-only hotspot not configured" + stop_cycle + else + echo "Unable to connect via wifi or Bluetooth; activating local-only hotspot" + echo "Killing wpa_supplicant" + #killall wpa_supplicant + wpa_cli terminate + echo "Shutting down wlan0" + ifdown wlan0 + echo "Activating AP config" + cp /etc/network/interfaces.ap /etc/network/interfaces + ifup wlan0 + echo "Attempting to start hostapd" + /etc/init.d/hostapd start + echo "Attempting to start dnsmasq" + service udhcpd stop + /etc/init.d/dnsmasq start + systemctl daemon-reload + #echo "Stopping networking" + #/etc/init.d/networking stop + #echo "Starting networking" + #/etc/init.d/networking start sleep 5 - sudo /etc/init.d/networking start - echo "and getting new wlan0 IP" - ps aux | grep -v grep | grep -q "dhclient wlan0" && sudo killall dhclient - sudo dhclient wlan0 -r - sudo dhclient wlan0 + echo "Setting IP Address for wlan0" + /sbin/ifconfig wlan0 $HostAPDIP netmask 255.255.255.0 up fi -fi -# restart avahi every minute to keep mDNS working properly -#/etc/init.d/avahi-daemon restart -echo Finished oref0-online. +} + +function dhclient_restart { + ps aux | grep -v grep | grep -q "dhclient wlan0" && sudo killall dhclient + sudo dhclient wlan0 -r + sudo dhclient wlan0 +} + +function restart_networking { + echo; echo "Error, cycling networking " + sudo /etc/init.d/networking stop + sleep 5 + sudo /etc/init.d/networking start + echo "and getting new wlan0 IP" + dhclient_restart +} + +main "$@" diff --git a/bin/oref0-pump-loop.sh b/bin/oref0-pump-loop.sh index 0364c018f..659490010 100755 --- a/bin/oref0-pump-loop.sh +++ b/bin/oref0-pump-loop.sh @@ -590,7 +590,7 @@ function refresh_profile { function wait_for_bg { if grep "MDT cgm" openaps.ini 2>&1 >/dev/null; then echo "MDT CGM configured; not waiting" - elif egrep -q "Waiting [01].[0-9]m to microbolus again." enact/smb-suggested.json; then + elif egrep -q "Waiting [0].[0-9]m to microbolus again." enact/smb-suggested.json; then echo "Retrying microbolus without waiting for new BG" else echo -n "Waiting up to 4 minutes for new BG: " diff --git a/bin/oref0-set-local-temptarget.js b/bin/oref0-set-local-temptarget.js new file mode 100755 index 000000000..2b9a837be --- /dev/null +++ b/bin/oref0-set-local-temptarget.js @@ -0,0 +1,50 @@ +#!/usr/bin/env node + +/* + Released under MIT license. See the accompanying LICENSE.txt file for + full terms and conditions + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +function usage ( ) { + console.log('usage: ', process.argv[1], ' [starttime]'); + console.log('example: ', process.argv[1], '110 60'); + console.log('example: ', process.argv[1], '120 30 2017-10-18:00:15:00.000Z'); +} + +if (!module.parent) { + var target = process.argv.slice(2, 3).pop(); + var duration = process.argv.slice(3, 4).pop(); + var start = process.argv.slice(4, 5).pop(); + + if ([null, '--help', '-h', 'help'].indexOf(target) > 0) { + usage( ); + process.exit(0) + } + if (!target || !duration) { + usage( ) + process.exit(1); + } + + var temptarget = {}; + temptarget.targetBottom = parseInt(target); + temptarget.targetTop = parseInt(target); + temptarget.duration = parseInt(duration); + if (start) { + temptarget.created_at = new Date(start); + } else { + temptarget.created_at = new Date(); + } + + + console.log(JSON.stringify(temptarget)); +} + diff --git a/bin/oref0-setup.sh b/bin/oref0-setup.sh index d8ef73cc6..85818f7f5 100755 --- a/bin/oref0-setup.sh +++ b/bin/oref0-setup.sh @@ -650,7 +650,31 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then else echo bluez v ${bluetoothdversion} already installed fi - fi + echo Installing prerequisites and configs for local-only hotspot + apt-get install -y hostapd dnsmasq || die "Couldn't install hostapd dnsmasq" + ls /etc/dnsmasq.conf.bak 2>/dev/null || mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak + cp $HOME/src/oref0/headless/dnsmasq.conf /etc/dnsmasq.conf || die "Couldn't copy dnsmasq.conf" + ls /etc/hostapd/hostapd.conf.bak 2>/dev/null || mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.bak + cp $HOME/src/oref0/headless/hostapd.conf /etc/hostapd/hostapd.conf || die "Couldn't copy hostapd.conf" + sed -i.bak -e "s|DAEMON_CONF=$|DAEMON_CONF=/etc/hostapd/hostapd.conf|g" /etc/init.d/hostapd + cp $HOME/src/oref0/headless/interfaces.ap /etc/network/ || die "Couldn't copy interfaces.ap" + cp /etc/network/interfaces /etc/network/interfaces.client || die "Couldn't copy interfaces.client" + #Stop automatic startup of hostapd & dnsmasq + update-rc.d -f hostapd remove + update-rc.d -f dnsmasq remove + # Edit /etc/hostapd/hostapd.conf for wifi using Hostname + sed -i.bak -e "s/ssid=OpenAPS/ssid=${HOSTNAME}/" /etc/hostapd/hostapd.conf + # Add Commands to /etc/rc.local + # Interrupt Kernel Messages + if ! grep -q 'sudo dmesg -n 1' /etc/rc.local; then + sed -i.bak -e '$ i sudo dmesg -n 1' /etc/rc.local + fi + # Add to /etc/rc.local to check if in hotspot mode and turn back to client mode during bootup + if ! grep -q 'cp /etc/network/interfaces.client /etc/network/interfaces' /etc/rc.local; then + sed -i.bak -e "$ i if [ -f /etc/network/interfaces.client ]; then\n\tif grep -q '#wpa-' /etc/network/interfaces; then\n\t\tsudo ifdown wlan0\n\t\tsudo cp /etc/network/interfaces.client /etc/network/interfaces\n\t\tsudo ifup wlan0\n\tfi\nfi" /etc/rc.local || die "Couldn't modify /etc/rc.local" + fi + fi + # add/configure devices if [[ ${CGM,,} =~ "g5" || ${CGM,,} =~ "g5-upload" ]]; then openaps use cgm config --G5 @@ -998,7 +1022,10 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then (crontab -l; crontab -l | grep -q "NIGHTSCOUT_HOST" || echo NIGHTSCOUT_HOST=$NIGHTSCOUT_HOST) | crontab - (crontab -l; crontab -l | grep -q "API_SECRET=" || echo API_SECRET=$API_HASHED_SECRET) | crontab - (crontab -l; crontab -l | grep -q "PATH=" || echo "PATH=$PATH" ) | crontab - - (crontab -l; crontab -l | grep -q "oref0-online $BT_MAC" || echo '* * * * * ps aux | grep -v grep | grep -q "oref0-online '$BT_MAC'" || oref0-online '$BT_MAC' 2>&1 >> /var/log/openaps/network.log' ) | crontab - + (crontab -l; crontab -l | grep -q "oref0-online $BT_MAC" || echo '* * * * * ps aux | grep -v grep | grep -q "oref0-online '$BT_MAC'" || cd '$directory' && oref0-online '$BT_MAC' 2>&1 >> /var/log/openaps/network.log' ) | crontab - + # temporarily disable hotspot for 1m every hour to allow it to try to connect via wifi again + (crontab -l; crontab -l | grep -q "touch /tmp/disable_hotspot" || echo '0 * * * * touch /tmp/disable_hotspot' ) | crontab - + (crontab -l; crontab -l | grep -q "rm /tmp/disable_hotspot" || echo '1 * * * * rm /tmp/disable_hotspot' ) | crontab - (crontab -l; crontab -l | grep -q "sudo wpa_cli scan" || echo '* * * * * sudo wpa_cli scan') | crontab - (crontab -l; crontab -l | grep -q "killall -g --older-than 30m oref0" || echo '* * * * * ( killall -g --older-than 30m openaps; killall -g --older-than 30m oref0-pump-loop; killall -g --older-than 30m openaps-report )') | crontab - # kill pump-loop after 5 minutes of not writing to pump-loop.log diff --git a/headless/hostapd.conf b/headless/hostapd.conf index abe3a0a18..c76aad1e3 100644 --- a/headless/hostapd.conf +++ b/headless/hostapd.conf @@ -19,4 +19,10 @@ hw_mode=g channel=6 macaddr_acl=0 ignore_broadcast_ssid=0 -wpa=0 +wpa=2 +wpa_passphrase=#OpenAPS +wpa_key_mgmt=WPA-PSK +wpa_pairwise=CCMP +wpa_group_rekey=86400 +ieee80211n=1 +wme_enabled=1 diff --git a/headless/install.sh b/headless/install.sh index 86cd453a8..d7ba33874 100755 --- a/headless/install.sh +++ b/headless/install.sh @@ -15,5 +15,6 @@ cp ./dnsmasq.conf /etc/dnsmasq.conf ls /etc/hostapd/hostapd.conf.bak || mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.bak cp ./hostapd.conf /etc/hostapd/hostapd.conf sed -i.bak -e "s|DAEMON_CONF=|DAEMON_CONF=/etc/hostapd/hostapd.conf|g" /etc/init.d/hostapd -cp interfaces.* /etc/network/ -ls $PWD/headless.sh && ( grep -q headless /etc/crontab || echo "* * * * * root $PWD/headless.sh" ) >> /etc/crontab +cp interfaces.ap /etc/network/ +cp /etc/network/interfaces /etc/network/interfaces.client +#ls $PWD/headless.sh && ( grep -q headless /etc/crontab || echo "* * * * * root $PWD/headless.sh" ) >> /etc/crontab diff --git a/lib/profile/index.js b/lib/profile/index.js index 54fa31797..4768dce55 100644 --- a/lib/profile/index.js +++ b/lib/profile/index.js @@ -42,7 +42,8 @@ function defaults ( ) { , curve: "rapid-acting" , useCustomPeakTime: false , insulinPeakTime: 75 - , carbsReqThreshold: 1 + , carbsReqThreshold: 1 // grams of carbsReq to trigger a pushover + , offline_hotspot: false // enabled an offline-only local wifi hotspot if no Internet available }; return profile; } @@ -64,6 +65,7 @@ function displayedDefaults () { profile.enableSMB_with_temptarget = allDefaults.enableSMB_with_temptarget; profile.enableUAM = allDefaults.enableUAM; profile.curve = allDefaults.curve; + profile.offline_hotspot = allDefaults.offline_hotspot; console.error(profile); return profile diff --git a/package.json b/package.json index 46c6f99bb..d2ce149bc 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "ns-upload": "./bin/ns-upload.sh", "ns-upload-entries": "./bin/ns-upload-entries.sh", "oref0": "./bin/oref0.sh", + "oref0-append-local-temptarget": "./bin/oref0-append-local-temptarget.sh", "oref0-autotune": "./bin/oref0-autotune.sh", "oref0-autotune-core": "./bin/oref0-autotune-core.js", "oref0-autotune-export-to-xlsx": "./bin/oref0-autotune-export-to-xlsx.py", @@ -72,6 +73,7 @@ "oref0-reset-usb": "bin/oref0-reset-usb.sh", "oref0-set-device-clocks": "./bin/oref0-set-device-clocks.sh", "oref0-set-system-clock": "./bin/oref0-set-system-clock.sh", + "oref0-set-local-temptarget": "./bin/oref0-set-local-temptarget.js", "oref0-setup": "./bin/oref0-setup.sh", "oref0-subg-ww-radio-parameters": "./bin/oref0-subg-ww-radio-parameters.sh", "oref0_subg_ww_radio_parameters.py": "./bin/oref0_subg_ww_radio_parameters.py", diff --git a/www/app.py b/www/app.py new file mode 100644 index 000000000..6d7b53341 --- /dev/null +++ b/www/app.py @@ -0,0 +1,39 @@ +import os +import socket + +from flask import Flask, render_template, url_for, json, jsonify +app = Flask(__name__) + +@app.route("/") +def index(): + myopenaps_dir = "/root/myopenaps/" + data=dict() + data['hostname']=socket.gethostname() + glucose = json.load(open(os.path.join(myopenaps_dir, "monitor/glucose.json"))) + data['glucose']=glucose[0] + # TODO: calculate delta properly when glucose[1] isn't 5m ago + delta=glucose[0]['glucose']-glucose[1]['glucose'] + tick="" + if delta >= 0: + tick += "+" + tick += str(delta) + data['tick']=tick + iob = json.load(open(os.path.join(myopenaps_dir, "monitor/iob.json"))) + data['iob'] = iob[0] + data['meal'] = json.load(open(os.path.join(myopenaps_dir, "monitor/meal.json"))) + data['suggested'] = json.load(open(os.path.join(myopenaps_dir, "enact/suggested.json"))) + data['enacted'] = json.load(open(os.path.join(myopenaps_dir, "enact/enacted.json"))) + data['temp_basal'] = json.load(open(os.path.join(myopenaps_dir, "monitor/temp_basal.json"))) + # print(data) + return render_template('index.html', data=data ) + # return "Hello World!" + +@app.route("/enacted") +def enacted(): + #SITE_ROOT = os.path.realpath(os.path.dirname(__file__)) + json_url = os.path.join("/root/myopenaps/enact/enacted.json") + data = json.load(open(json_url)) + return jsonify(data) + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0') diff --git a/www/app.pyc b/www/app.pyc new file mode 100644 index 000000000..89a332996 Binary files /dev/null and b/www/app.pyc differ diff --git a/www/static/style.css b/www/static/style.css new file mode 100644 index 000000000..95910686d --- /dev/null +++ b/www/static/style.css @@ -0,0 +1,4 @@ +body { + background: white; + color: black; +} diff --git a/www/templates/index.html b/www/templates/index.html new file mode 100644 index 000000000..a31eb6a64 --- /dev/null +++ b/www/templates/index.html @@ -0,0 +1,16 @@ + + + + + +

OpenAPS

+

Running on {{ data.hostname }}

+

BG: {{ data.glucose.glucose }} {{ data.tick }} {{ data.glucose.direction }} at {{ data.glucose.dateString }}

+

IOB: {{ data.iob.iob }}, COB: {{ data.meal.mealCOB }}

+

Eventual BG: {{ data.suggested.eventualBG }}

+

Current temp: {{ data.temp_basal.rate }} for {{ data.temp_basal.duration}}m

+

Enacted: {{ data.enacted.rate }} for {{ data.enacted.duration }} + SMB {{ data.enacted.units|default("0", true) }}U

+

Reason: {{ data.enacted.reason}} at {{ data.enacted.timestamp }}

+ + +