2 Commits

8 changed files with 149 additions and 9 deletions

View File

@@ -1,3 +1,10 @@
# 0.6.3 - 10/08/2021
- Add command last_rain_sensor_timestamp, last_rain_online_timestamp, reset_last_rain_sensor_timestamp, reset_last_rain_online_timestamp
- Add socket server api for reset_last_rain_sensor_timestamp, reset_last_rain_online_timestamp
# 0.6.2 - 24/04/2021
- Update rainsensorqty driver to version 0.2.5c
# 0.6.1 - 06/09/2020
- Add support for send log to piGardenWeb

View File

@@ -0,0 +1 @@
echo 1 > /tmp/tick ; sleep 0.9 ; echo 0 > /tmp/tick

View File

@@ -60,6 +60,46 @@ en_echo() # enhanched echo - check verbose variable
[[ $RAINSENSORQTY_verbose = yes ]] && echo "$(d) $*"
}
check_incomplete_loop()
{
[[ ! -f $RAINSENSORQTY_HISTORYRAW ]] && return 1
[[ ! -f $RAINSENSORQTY_HISTORY ]] && touch $RAINSENSORQTY_HISTORY
> $RAINSENSORQTY_HISTORYTMP
if lastrainevent=$( rainevents 1 ) ; then
: # done ok
else
echo "WARNING: rainevents function had errors"
return 1
fi
set -- ${lastrainevent//:/ }
local started=$1
local before=$2
local counter=$3
wrongevent=$(awk -F ":" '$1=="'$started'" && $2!="'$before'" {print $0}' $RAINSENSORQTY_HISTORY)
if [[ -n $wrongevent ]] ; then
echo "ERROR: wrong last rain event found: $wrongevent , right one should be: $lastrainevent"
return 2
fi
if grep -q ^${lastrainevent}$ $RAINSENSORQTY_HISTORY ; then
: # already present
return 0
else
: # missing and fixed
if [[ $1 == tmp ]] ; then
echo $lastrainevent > $RAINSENSORQTY_HISTORYTMP
else
echo $lastrainevent >> $RAINSENSORQTY_HISTORY
fi
return 1
fi
}
#next function is not used anymore
rain_history()
{
[[ ! -f $RAINSENSORQTY_HISTORYRAW ]] && return 1
@@ -134,7 +174,7 @@ rainevents()
newloop=no
fi
if (( sequence == 1 )) ; then
# [[ $skiplast=true ]] && { skilast=false ; continue ; }
# [[ $skiplast=true ]] && { skiplast=false ; continue ; }
echo $time:$endtime:$endsequence
newloop=yes
(( event +=1 ))

View File

@@ -4,8 +4,8 @@
# Author: androtto
# file "drv_rainsensorqty_monitor.sh"
# monitor script
# Version: 0.2.5b
# Data: 12/May/2020
# Version: 0.2.5c
# Data: 08/Dec/2020
resetcounter()
{
@@ -14,6 +14,19 @@ resetcounter()
echo "SIGUSR1 received after last PULSE - counter resetted"
}
# DEBUG FUNCTION:
testloop()
{
touch /tmp/tick
while true
do
if [[ $( < /tmp/tick ) = "1" ]] ; then
break
fi
sleep 1
done
}
###############
# MAIN #
###############
@@ -51,30 +64,41 @@ fi
echo $$ > $RAINSENSORQTY_MONPID
drv_rainsensorqty_writelog $f "NORMAL - $$ pid monitor process started - see $RAINSENSORQTY_MONPID"
echo ""
en_echo "---- NEW RUN ----"
# check if monitor script was killed before writing a complete rain event
check_incomplete_loop
case $? in
2) echo "\$RAINSENSORQTY_HISTORY - wrong entry found - no fix possible" ;;
1) echo "\$RAINSENSORQTY_HISTORY - fixed incomplete loop" ;;
0) echo "\$RAINSENSORQTY_HISTORY - no incomplete loop needed to be fixed" ;;
esac
# init variables
MMEACH="$RAINSENSORQTY_MMEACH"
(( counter=0 ))
rain_history
before="-1"
echo ""
en_echo "---- NEW RUN ----"
en_echo "WAITING FOR $RAINSENSORQTY_PULSE PULSE"
# loop forever
while true
do
before=`date +%s`
sleep $RAINSENSOR_DEBOUNCE
#DEBUG: testloop #DEBUG
$GPIO -g wfi $gpio_port $RAINSENSORQTY_PULSE
now=`date +%s`
(( elapsed = now - before ))
if (( elapsed >= RAINSENSORQTY_SECSBETWEENRAINEVENT )) ; then
last_event="$started:$before:$counter"
(( counter=0 ))
drv_rainsensorqty_writelog $f "first drops after $elapsed seconds since last rain ( greater than $RAINSENSORQTY_SECSBETWEENRAINEVENT )- new cycle - waiting for $( $JQ -n "$RAINSENSORQTY_LOOPSFORSETRAINING * $MMEACH" ) mm of rain" &
(( before > 0 )) && echo $last_event >> $RAINSENSORQTY_HISTORY
en_echo "---- NEW CYCLE ----"
rain_history &
fi
(( counter+=1 ))
(( counter == 1 )) && (( started = now ))
echo "$now:$counter" >> ${RAINSENSORQTY_HISTORYRAW} &
MMWATER=$( $JQ -n "$counter*$MMEACH" )
en_echo $( printf "%s PULSE #%d RECEIVED (%.2f mm)" $RAINSENSORQTY_PULSE $counter $MMWATER )
@@ -85,4 +109,5 @@ do
else
drv_rainsensorqty_writelog $f "now is $text" &
fi
(( before = now ))
done

View File

@@ -120,4 +120,38 @@ function close_all_for_rain {
}
#
# Mostra il timestamp dell'ultima pioggia rilevato dal sensore
#
function last_rain_sensor_timestamp {
cat "$STATUS_DIR/last_rain_sensor" 2> /dev/null
}
#
# Mostra il timestamp dell'ultima pioggia rilevato dal servizio online
#
function last_rain_online_timestamp {
cat "$STATUS_DIR/last_rain_online" 2> /dev/null
}
#
# Resetta il timestamp dell'ultima pioggia rilevato dal sensore
#
function reset_last_rain_sensor_timestamp {
trigger_event "reset_last_rain_sensor_timestamp_before" ""
rm "$STATUS_DIR/last_rain_sensor" 2> /dev/null
trigger_event "reset_last_rain_sensor_timestamp_after" ""
log_write "rain" "info" "reset_last_rain_sensor_timestamp"
}
#
# Resetta mostra il timestamp dell'ultima pioggia rilevato dal servizio online
#
function reset_last_rain_online_timestamp {
trigger_event "reset_last_rain_online_timestamp_before" ""
rm "$STATUS_DIR/last_rain_online" 2> /dev/null
trigger_event "reset_last_rain_online_timestamp_before" ""
log_write "rain" "info" "reset_last_rain_online_timestamp"
}

View File

@@ -240,6 +240,19 @@ function socket_server_command {
nohup $PATH_SCRIPT poweroff > /dev/null 2>&1 &
;;
reset_last_rain_sensor_timestamp)
reset_last_rain_sensor_timestamp
message_write "success" "Timestamp of last sensor rain successfull reset"
json_status
;;
reset_last_rain_online_timestamp)
reset_last_rain_online_timestamp
message_write "success" "Timestamp of last online rain successfull reset"
json_status
;;
*)
json_error 0 "invalid command"
;;

View File

@@ -727,6 +727,10 @@ function show_usage {
echo -e "\t$NAME_SCRIPT list_alias view list of aliases solenoid"
echo -e "\t$NAME_SCRIPT ev_status alias show status solenoid"
echo -e "\t$NAME_SCRIPT ev_status_all show status solenoids"
echo -e "\t$NAME_SCRIPT last_rain_sensor_timestamp show timestamp of last rain sensor"
echo -e "\t$NAME_SCRIPT last_rain_online_timestamp show timestamp of last rain online"
echo -e "\t$NAME_SCRIPT reset_last_rain_sensor_timestamp show timestamp of last rain sensor"
echo -e "\t$NAME_SCRIPT reset_last_rain_online_timestamp show timestamp of last rain online"
echo -e "\t$NAME_SCRIPT json_status [get_cron|get_cron_open_in|get_schedule] show status in json format"
echo -e "\t$NAME_SCRIPT mqtt_status send status in json format to mqtt broker"
echo -e "\t$NAME_SCRIPT check_rain_online check rain from http://api.wunderground.com/"
@@ -1004,7 +1008,7 @@ function debug2 {
VERSION=0
SUB_VERSION=6
RELEASE_VERSION=1
RELEASE_VERSION=3
DIR_SCRIPT=`dirname $0`
NAME_SCRIPT=${0##*/}
@@ -1119,6 +1123,22 @@ case "$1" in
mqtt_status $2
;;
last_rain_sensor_timestamp)
last_rain_sensor_timestamp
;;
last_rain_online_timestamp)
last_rain_online_timestamp
;;
reset_last_rain_sensor_timestamp)
reset_last_rain_sensor_timestamp
;;
reset_last_rain_online_timestamp)
reset_last_rain_online_timestamp
;;
check_rain_online)
check_rain_online
;;