fix typoes

now including global problems (like "aircon on but doesnt need to be") in  airconproblem[] list
more human readable problem descriptions (to help with telegram notifications)
don't send telegram notifications when in dry run mode
don't send telegram notifications when killfile is active
add some telegram debugging to log file
This commit is contained in:
Rob Pearce 2023-12-09 08:47:07 +11:00
parent f81bf65ae8
commit 58999c9f5c
1 changed files with 100 additions and 41 deletions

141
aircon.sh
View File

@ -542,7 +542,7 @@ function generate_actions() { # populates global: nairconcommands & airconcmd
zaction[$x]="${zaction[$x]/set_myzone/}"
nomyzone="$nomyzone ${zname[$x]} "
[[ $db -eq 1 ]] && info "zaction[${zname[$z]} is '${zaction[$x]}'"
[[ $db -eq 1 ]] && info "nomyzone is '$nomyzone}'"
[[ $db -eq 1 ]] && info "nomyzone is '$nomyzone'"
fi
fi
done
@ -745,8 +745,19 @@ function add_aircon_command() {
airconproblem[$nairconcommands]="Aircon is not needed at this time."
else
airconcmdzone[$nairconcommands]="${zname[$idx]}"
airconproblem[$nairconcommands]="${zname[$idx]} is ${zproblem[$idx]/_/ }"
if [[ ${zproblem[$idx]} == "n/a" ]]; then
# global problem
if [[ -n $globprob ]]; then
airconproblem[$nairconcommands]="$(describe_globprob)"
else
airconproblem[$nairconcommands]="Unknown"
fi
else
# zone-specific problem
airconproblem[$nairconcommands]="${zname[$idx]} is ${zproblem[$idx]/_/ }"
fi
fi
if [[ $otheridx -eq -1 ]]; then
airconcmdotherzone[$nairconcommands]=""
else
@ -1654,27 +1665,50 @@ function show_aircon_status() {
}
function show_analysis() {
# generate textual description of global problem
function describe_globprob() {
[[ -z $globprob ]] && return 1;
if [[ $globprob == too_* ]]; then
echo "$limit or more zones are ${globprob/_/ }"
elif [[ $globprob == "not_needed" ]]; then
echo "The system is on but all zones at optimal temperature"
else
echo "_unknown_globprob_:$globprob"
fi
return 0
}
function show_analysis() { # returns 1 if all good and nothing to show
local count x ostr
echo -e "${UNDERLINE}Analysis:${PLAIN}"
local amode="normal"
if [[ $1 == "-t" ]]; then
mode="telegram"
shift
fi
if [[ $mode != "telegram" ]]; then
echo -e "${UNDERLINE}Analysis:${PLAIN}"
fi
count=0
for x in ${!zproblem[@]}; do
znowners=$(wc -w <<< "${zowner[$x]}" | bc)
zowners=$(sed -r 's/^ +//;s/ +$//;s/ +/, /g' <<< "${zowner[$x]}")
if [[ ${zproblem[$x]} != "n/a" ]]; then
if [[ ${zproblem[$x]} == "owner_not_home" ]]; then
if [[ ${zowner[$x]} == *\ * ]]; then
if [[ $znowners -gt 1 ]]; then
ostr="owners are"
else
ostr="owner is"
fi
echo "- ${zname[$x]} open but $ostr not detected (${zowner[$x]})."
echo "- The zone '${zname[$x]}' is open but its $ostr not home (${zowners})"
elif [[ ${zproblem[$x]} =~ force_ ]]; then
what=${zproblem[$x]##*_}
[[ $what == "close" ]] && what="${what}d"
echo "- ${zname[$x]} is forced $what"
echo "- The zone '${zname[$x]}' should be $what at this time"
else
if [[ ${zownerhome[$x]} -ne 1 ]]; then
if [[ ${zowner[$x]} == *\ * ]]; then
append1=", but (${zowner[$x]}) aren't online"
if [[ $znowners -gt 1 ]]; then
append1=", but none of its owners ($zowners) are online"
else
append1=", but ${zowner[$x]} isn't online"
fi
@ -1686,19 +1720,22 @@ function show_analysis() {
else
append2=""
fi
echo "- ${zname[$x]} is ${zproblem[$x]/_/ }${append1}${append2}"
echo "- The zone '${zname[$x]}' is ${zproblem[$x]/_/ }${append1}${append2}"
fi
count=$((count + 1))
fi
done
if [[ $globprob == too_* ]]; then
echo "- $limit or more zones are ${globprob/_/ }"
count=$((count + 1))
elif [[ $globprob == "not_needed" ]]; then
echo "- System is on but all zones at optimal temperature"
count=$((count + 1))
if [[ -n $globprob ]]; then
echo "- $(describe_globprob)"
fi
[[ $count -eq 0 ]] && echo -e "${GREEN}All is good!${PLAIN}"
if [[ $count -eq 0 && -z $globprob ]]; then
if [[ $mode != "telegram" ]]; then
echo -e "${GREEN}All is good!${PLAIN}"
fi
fi
[[ $count -eq 0 && -z $globprob ]] && return 1
return 0;
}
function killfile_active() { # returns true if killfile swill stop us
@ -1787,16 +1824,18 @@ function combine_commands() {
return 0
}
# note: this function is no longer used
function reword_problem() { #1="problem text"
local orig="$1" new=""
if [[ $orig == *" is too "* ]]; then
new=$(sed -E 's/^([A-Za-z]+) is too ([A-Za-z]+)/the zone "\1" was too \2/' <<<"$orig")
new=$(sed -r 's/^([A-Za-z]+) is too ([A-Za-z]+)/the zone "\1" was too \2/' <<<"$orig")
elif [[ $orig == *" is owner not_home "* ]]; then
new=$(sed -E 's/^([A-Za-z]+) is .*/the owner of the zone "\1" was not at home/' <<<"$orig")
new=$(sed -r 's/^([A-Za-z]+) is .*/the owner of the zone "\1" was not at home/' <<<"$orig")
elif [[ $orig == *" is force close"* ]]; then
new=$(sed -E 's/^([A-Za-z]+) is .*/the zone "\1" should be closed at this time/' <<<"$orig")
new=$(sed -r 's/^([A-Za-z]+) is .*/the zone "\1" should be closed at this time/' <<<"$orig")
elif [[ $orig == *" is n/a"* ]]; then
new=$(sed -E 's/^([A-Za-z]+) is .*/the zone "\1" should be the "myzone" at this time/' <<<"$orig")
## wrong! n/a means there is a globprob
new=$(sed -r 's/^([A-Za-z]+) is .*/the zone "\1" should be the "myzone" at this time/' <<<"$orig")
else
new="$orig"
fi
@ -1808,6 +1847,12 @@ function telegram_send() { #1=msg
local res msg="$1"
#action "sending to telegram: curl -s --data \"text=$msg\" --data \"chat_id=$TELEGRAM_CHAT\" --data \"parse_mode=markdown\" 'https://api.telegram.org/bot'$TELEGRAM_TOKEN'/sendMessage' "
res=$(curl -s --data "text=$msg" --data "chat_id=$TELEGRAM_CHAT" --data "parse_mode=markdown" 'https://api.telegram.org/bot'$TELEGRAM_TOKEN'/sendMessage' 2>&1)
if [[ $cronmode -eq 1 ]]; then
echo "telegram_send: text=[$msg]"
echo "telegram_send: curl=curl -s --data \"text=$msg\" --data \"chat_id=$TELEGRAM_CHAT\" --data \"parse_mode=markdown\" \'https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage\'"
echo "telegram_send: res=[$res]"
fi
#action "telegram result: $res"
}
@ -1836,26 +1881,6 @@ function run_commands() {
fi
fi
if [[ $USETELEGRAM -eq 1 ]]; then
prevprob=""
botwords=""
for x in ${!airconcmd[@]}; do
thisprob=${airconproblem[$x]}
thisfix=$(sed 's/&/%26/g' <<<"${airconcomment[$x]}")
if [[ $thisprob != $prevprob ]]; then
# send our existing buffer
[[ -n $botwords ]] && telegram_send "$botwords"
botwords=""
txt=$(reword_problem "$thisprob")
botwords=$(printf 'I noticed that _%s_, so I did this:%%0a' "$txt")
fixnum=1
prevprob="$thisprob"
fi
botwords="$botwords$(printf ' %d. %s%%0a' $fixnum "$thisfix")"
fixnum=$((fixnum + 1))
done
[[ -n $botwords ]] && telegram_send "$botwords"
fi
}
@ -1925,7 +1950,37 @@ function influx_init() { # $1 == dbname
return 0
}
function show_telegram_output() {
local botwords="" atext fixnum=1 thisprob thisfix
# get analysis text in telegram mode
atext=$(show_analysis -t)
[[ $? -ne 0 ]] && return 1;
# replace newlines
atext="$(sed ':begin;$!N;s/\n/%0a/;tbegin' <<<"$atext")"
# replace ampersands
atext=$(sed 's/&/%26/g' <<<"${atext}")
if [[ $? -eq 0 ]]; then
botwords="I noticed that:%0a"
botwords="${botwords}${atext}%0a%0a"
botwords="${botwords}I'm going to do this to fix it:%0a"
fixnum=1
for x in ${!airconcmd[@]}; do
thisprob=${airconproblem[$x]}
thisfix=$(sed 's/&/%26/g' <<<"${airconcomment[$x]}")
if [[ $cronmode -eq 1 ]]; then
echo "telegram_debug: thisprob=[$thisprob]"
echo "telegram_debug: airconcomment[$x]=[${airconcomment[$x]}]"
echo "telegram_debug: thisfix=[$thisfix]"
fi
botwords="$botwords$(printf ' %d. %s%%0a' $fixnum "$thisfix")"
fixnum=$((fixnum + 1))
done
fi
[[ -n $botwords && $fixnum -gt 1 ]] && telegram_send "$botwords"
}
p_now=$(date +%s)
p_last=$p_now
@ -2297,5 +2352,9 @@ if [[ $DOIT -eq 1 ]]; then
if ! killfile_active; then
run_commands
[[ $cronmode -ne 1 ]] && echo
if [[ $USETELEGRAM -eq 1 ]]; then
show_telegram_output
fi
fi
fi