#!/usr/bin/env bash # The MIT License (MIT) # # reset-to-default-antennas-when-no-users-online # # Copyright (c) 2017 Kari Karvonen # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # 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. # Specify default antennas. This can be single antenna on comma separated # list many antennas # DEFAULT_ANTENNAS=1,2,7 DEFAULT_ANTENNAS=2 # do no change below this line if [ "x`which curl`" == "x" ]; then echo "curl not found. Please install curl." echo "sudo apt-get install curl" exit 1 else CURLEXEC=`which curl` fi if [ "x`which jq`" == "x" ]; then echo "jq not found. Please install jq." echo "sudo apt-get install jq" exit 1 else JQEXEC=`which jq` fi ANTSWITCHEXEC=/root/extensions/ant_switch/frontend/ant-switch-frontend ADMINJSON=/root/kiwi.config/admin.json KIWIJSON=/root/kiwi.config/kiwi.json if ! [[ -x "$ANTSWITCHEXEC" ]]; then echo "$0: Antenna switch script $ANTSWITCHEXEC not found or it is not executable. Exit." exit 1 fi if ! [[ -r "$ADMINJSON" ]]; then echo "$0: $ADMINJSON not found or it is not readable. Exit." exit 1 fi CURLOPTS=" --silent --connect-timeout 1" PORT=`$JQEXEC .port $ADMINJSON` STATUSURL="http://127.0.0.1:$PORT/status" FORBIDSWITCH=`$JQEXEC .ant_switch.denyswitching $KIWIJSON` if [ "$FORBIDSWITCH" != "0" ]; then echo "Antenna switching denied by configuration, not switching." exit 1 fi THUNDERSTORM=`$JQEXEC .ant_switch.thunderstorm $KIWIJSON` if [ "$THUNDERSTORM" == "1" ]; then echo "Antenna switching denied by thunderstorm mode, not switching." exit 1 fi STATUS=`$CURLEXEC $STATUSURL $CURLOPTS` if [ "x$STATUS" == "x" ]; then echo "$0: Status url $STATUSURL could not be retriewed. Exit." exit 1 fi ONLINEUSERS=`echo $STATUS | sed -e "s/.* users=\([0-9]\) .*/\1/"` re='^[0-9]+$' if ! [[ $ONLINEUSERS =~ $re ]] ; then echo "$0: Cannot parse users number from $STATUSURL. Exit." exit 1 fi if [ "$ONLINEUSERS" == "0" ]; then # No users online, switch to default antennas selected=`$ANTSWITCHEXEC s | sed -e 's/^Selected antennas: //'` if [ "$selected" != "$DEFAULT_ANTENNAS" ]; then ANTENNAS=`echo $DEFAULT_ANTENNAS | tr ',' ' '` $ANTSWITCHEXEC g for antenna in $ANTENNAS; do $ANTSWITCHEXEC +$antenna done fi fi