Tweak method calculating "perfect" temperature so that we heat/cool until a little PAST the midpoint rather than before.

Modify device check to be faster and more accurate
More verbosity when not in cron mode
This commit is contained in:
Rob Pearce 2022-06-14 21:09:56 +10:00
parent 1030b88acb
commit e7fcc08091
1 changed files with 66 additions and 30 deletions

View File

@ -98,7 +98,7 @@ function get_midrange() { # outputs midpoint of temperature range for given zon
} }
# get_perfect_tolerance zone_idx # get_perfect_tolerance zone_idx
function get_perfect_tolerance() { # outputs the difference from midpoint inside which temperature is considered "perfect" function get_perfect_tolerance() { # outputs the difference from midpoint after which temperature is considered "perfect"
local min max local min max
min=${zwantmin[$idx]} min=${zwantmin[$idx]}
max=${zwantmax[$idx]} max=${zwantmax[$idx]}
@ -171,9 +171,9 @@ fi
midtol=$(get_perfect_tolerance $idx) midtol=$(get_perfect_tolerance $idx)
#echo "zone ${zname[$idx]} temp ${ztemp[$idx]} mid is $mid midtol is $midtol perfect is >= $(echo "$mid - $midtol" | bc)" #echo "zone ${zname[$idx]} temp ${ztemp[$idx]} mid is $mid midtol is $midtol perfect is >= $(echo "$mid - $midtol" | bc)"
if [[ $airconmode == "heat" ]]; then if [[ $airconmode == "heat" ]]; then
thisperfect=$(echo "${ztemp[$idx]} >= (${mid} - ${midtol})" | bc) thisperfect=$(echo "${ztemp[$idx]} >= (${mid} + ${midtol})" | bc)
elif [[ $airconmode == "cool" ]]; then elif [[ $airconmode == "cool" ]]; then
thisperfect=$(echo "${ztemp[$idx]} <= (${mid} + ${midtol})" | bc) thisperfect=$(echo "${ztemp[$idx]} <= (${mid} - ${midtol})" | bc)
fi fi
fi fi
fi fi
@ -376,6 +376,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd
local globaction mzmustmove poss nomyzone local globaction mzmustmove poss nomyzone
local db local db
db=0 db=0
[[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Determining actions... ${PLAIN}"
nairconcommands=0 nairconcommands=0
powerchange="" powerchange=""
@ -595,6 +596,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd
if [[ -n $globaction ]]; then if [[ -n $globaction ]]; then
gen_aircon_command -1 ${globaction} gen_aircon_command -1 ${globaction}
fi fi
echo -e "${GREEN}${BOLD}ok${PLAIN}"
} }
# gen_aircon_command zone_idx "command1 command2 etc" # gen_aircon_command zone_idx "command1 command2 etc"
@ -936,6 +938,7 @@ function load_config() {
local cond allconds x local cond allconds x
local db local db
parse_config || return 1 parse_config || return 1
[[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Loading config file... ${PLAIN}"
db=0 db=0
rv=0 rv=0
while read line; do while read line; do
@ -979,6 +982,13 @@ function load_config() {
fi fi
fi fi
done < "$CONFIGFILE" done < "$CONFIGFILE"
if [[ $cronmode -eq 0 && $logmode -eq 0 ]]; then
if [[ $rv -eq 0 ]]; then
echo -e "${GREEN}${BOLD}ok${PLAIN}"
else
echo -e "${RED}${BOLD}fail${PLAIN}"
fi
fi
return $rv return $rv
} }
@ -1165,6 +1175,7 @@ function parse_config() {
local config_human line_human errstr local config_human line_human errstr
local ign="" thisignored=0 oneof local ign="" thisignored=0 oneof
[[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Parsing config file... ${PLAIN}"
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
show=$1 show=$1
fi fi
@ -1194,7 +1205,6 @@ function parse_config() {
fi fi
fi fi
IFS=' ' read -ra tok <<< "$line" IFS=' ' read -ra tok <<< "$line"
if [[ ${tok[0]} == "temp" ]]; then if [[ ${tok[0]} == "temp" ]]; then
local min max local min max
@ -1266,6 +1276,7 @@ function parse_config() {
done < "$CONFIGFILE" done < "$CONFIGFILE"
if [[ $fileok -eq 1 ]]; then if [[ $fileok -eq 1 ]]; then
[[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${GREEN}${BOLD}ok${PLAIN}"
if [[ $show -eq 2 ]]; then if [[ $show -eq 2 ]]; then
echo -e "${config_human}" echo -e "${config_human}"
elif [[ $show -eq 1 ]]; then elif [[ $show -eq 1 ]]; then
@ -1273,6 +1284,7 @@ function parse_config() {
fi fi
rv=0 rv=0
else else
[[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${RED}${BOLD}fail${PLAIN}"
error "Configuration file is invalid:" error "Configuration file is invalid:"
echo -e "${RED}$errstr${plainc}" | sed -e 's/^/ /' echo -e "${RED}$errstr${plainc}" | sed -e 's/^/ /'
rv=1 rv=1
@ -1280,32 +1292,47 @@ function parse_config() {
return $rv return $rv
} }
# if we can ping any of the args, return ok # if we can ping ANY of the args, return ok
function canping() { function canping() {
local ip db host thisrv mac local n hname ip db host success=0 mac
local os local os
os=$(uname -s) os=$(uname -s)
db=0 db=0
# get ip and clear arp
n=0
for host in $*; do for host in $*; do
hname[$n]="$host"
if [[ $os == "Darwin" ]]; then if [[ $os == "Darwin" ]]; then
ip=$(dscacheutil -q host -a name $host | grep ^ip_address: | awk '{ print $NF}') ip[$n]=$(dscacheutil -q host -a name $host | grep ^ip_address: | awk '{ print $NF}')
else else
ip=$(getent hosts $host | grep -v :) ip[$n]=$(getent hosts $host | grep -v : | awk '{ print $1 }')
fi fi
ip=${ip%% *} if [[ -n ${ip[$n]} ]]; then
if [[ -z $ip ]]; then arp -d ${ip[$n]} >/dev/null 2>&1
thisrv=1
else
arp -d $ip >/dev/null 2>&1
#ping -c1 -w1 -n -q $ip >/dev/null 2>&1 &
arping -c10 -W0.1 -w3 -C1 -q ${ip} 2>/dev/null
sleep 0.3
mac=$(arp -n $ip)
mac=$(echo "$mac" | egrep -v "Host|xpired" | awk '{print $2}')
[[ $mac == *:* ]] && thisrv=0 || thisrv=1
fi fi
[[ $db -eq 1 ]] && info "canping() $host ($ip) is $thisrv - mac=$mac" done
if [[ $thisrv -eq 0 ]]; then
# send new arp req
for n in ${!ip[@]}; do
if [[ -n ${ip[$n]} ]]; then
arping -c10 -W0.1 -w4 -C1 -q ${ip[$n]} 2>/dev/null &
fi
done
# wait for arp replies or timeout
wait
sleep 0.4
# now check entries
for n in ${!ip[@]}; do
if [[ -n ${ip[$n]} ]]; then
mac=$(arp -n ${ip[$n]})
#mac=$(arp -n $ip | egrep -v "Host|xpired" | awk '{print $2}')
#[[ $mac == *:* ]] && thisrv=0
[[ $? -eq 0 ]] && success=1
fi
[[ $db -eq 1 ]] && info "canping() ${hname[$n]} (${ip[$n]}) is $success - mac=$mac"
if [[ $success -eq 1 ]]; then
return 0 return 0
fi fi
done done
@ -1878,10 +1905,10 @@ else
fi fi
# ping all hosts in background to populate arp table # ping all hosts in background to populate arp table
for x in ${devices}; do #for x in ${devices}; do
ping -c1 -w1 -n -q $ip >/dev/null 2>&1 & # ping -c1 -w1 -n -q $ip >/dev/null 2>&1 &
done #done
wait #wait
if [[ $robtest -eq 1 ]]; then if [[ $robtest -eq 1 ]]; then
for x in ${!zname[@]}; do for x in ${!zname[@]}; do
@ -1915,6 +1942,7 @@ if [[ $showwho -eq 1 ]]; then
echo echo
exit 0 exit 0
elif [[ $showwho -eq 2 ]]; then elif [[ $showwho -eq 2 ]]; then
[[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -en "${GREEN}${BOLD}>> ${PLAIN}${GREEN}Querying zone owners... ${PLAIN}"
# get max device hostname length # get max device hostname length
maxlen=1 maxlen=1
for x in ${ownerhost[@]}; do for x in ${ownerhost[@]}; do
@ -1925,23 +1953,31 @@ elif [[ $showwho -eq 2 ]]; then
HFORMAT="%-${maxlen}s${GREEN}%-16s${PLAIN}%s\n" HFORMAT="%-${maxlen}s${GREEN}%-16s${PLAIN}%s\n"
AFORMAT="%-${maxlen}s${RED}%-16s${PLAIN}%s\n" AFORMAT="%-${maxlen}s${RED}%-16s${PLAIN}%s\n"
alldevs=$(echo "${ownerhost[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ') alldevs=$(echo "${ownerhost[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')
for x in $alldevs; do
canping ${x} >/dev/null 2>&1
pingres[$x]=$?
done
[[ $cronmode -eq 0 && $logmode -eq 0 ]] && echo -e "${GREEN}${BOLD}ok${PLAIN}"
echo echo
printf "$TFORMAT" "Devices" "Availability" "Zones owned" printf "$TFORMAT" "Devices" "Availability" "Zones owned"
for x in $alldevs; do for x in $alldevs; do
if canping ${x}; then if [[ ${pingres[$x]} -eq 0 ]]; then
thisform="$HFORMAT" thisform="$HFORMAT"
str="Available" str="Available"
else else
thisform="$AFORMAT" thisform="$AFORMAT"
str="Unavailable" str="Unavailable"
fi fi
unset mydevs || declare -a mydevs unset mydevs || declare -a mydevs
for i in ${!ownerzone[@]}; do for i in ${!ownerzone[@]}; do
if [[ ${ownerhost[$i]} == *${x}* ]]; then if [[ ${ownerhost[$i]} == *${x}* ]]; then
mydevs+=("${ownerzone[$i]}") mydevs+=("${ownerzone[$i]}")
fi fi
done done
allmydevs=$(echo "${mydevs[@]}" | tr '\n' ' ') allmydevs=$(echo "${mydevs[@]}" | tr '\n' ' ')
printf "$thisform" "${x}" "$str" "${allmydevs}" printf "$thisform" "${x}" "$str" "${allmydevs}"
done done
echo echo