diff --git a/include/cron.include.sh b/include/cron.include.sh index b74db2b..56a1cad 100644 --- a/include/cron.include.sh +++ b/include/cron.include.sh @@ -1,5 +1,3 @@ -#!/bin/bash - # # Elimina una tipoliga di schedulazione dal crontab dell'utente # $1 tipologia del crontab diff --git a/include/drv.include.sh b/include/drv.include.sh index 31ac3c1..32fefd2 100644 --- a/include/drv.include.sh +++ b/include/drv.include.sh @@ -1,4 +1,3 @@ -#declare -A drv_avalible declare -a list_drv function setup_drv { diff --git a/include/rain.include.sh b/include/rain.include.sh new file mode 100644 index 0000000..fecf234 --- /dev/null +++ b/include/rain.include.sh @@ -0,0 +1,99 @@ +# +# Controlla se se piove tramite http://api.wunderground.com/ +# +function check_rain_online { + # http://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary&MR=1 + $CURL http://api.wunderground.com/api/$WUNDERGROUND_KEY/conditions/q/$WUNDERGROUND_LOCATION.json > $TMP_PATH/check_rain_online.json + local weather=`cat $TMP_PATH/check_rain_online.json | $JQ -M ".current_observation.weather"` + local current_observation=`cat $TMP_PATH/check_rain_online.json | $JQ -M ".current_observation"` + local local_epoch=`cat $TMP_PATH/check_rain_online.json | $JQ -M -r ".current_observation.local_epoch"` + #echo $weather + #weather="[Light/Heavy] Drizzle" + if [ "$weather" = "null" ]; then + log_write "check_rain_online - failed read online data" + else + log_write "check_rain_online - weather=$weather, local_epoch=$local_epoch" + #if [[ "$weather" == *"Clear"* ]]; then + #if [[ "$weather" == *"Rain"* ]]; then + if [[ "$weather" == *"Rain"* ]] || + [[ "$weather" == *"Snow"* ]] || + [[ "$weather" == *"Hail"* ]] || + [[ "$weather" == *"Ice"* ]] || + [[ "$weather" == *"Thunderstorm"* ]] || + [[ "$weather" == *"Drizzle"* ]]; + then + #echo "ECCOMI!!!!!" + echo $local_epoch > "$STATUS_DIR/last_rain_online" + return $local_epoch + fi + echo "$current_observation" > "$STATUS_DIR/last_weather_online" + fi +} + +# +# Controlla se se piove tramite sensore +# +function check_rain_sensor { + + if [ -n "$RAIN_GPIO" ]; then + #local s=`$GPIO -g read $RAIN_GPIO` + local s=`drv_rain_sensor_get $RAIN_GPIO` + if [ "$s" = "$RAIN_GPIO_STATE" ]; then + local local_epoch=`date +%s` + echo $local_epoch > "$STATUS_DIR/last_rain_sensor" + log_write "check_rain_sensor - now it's raining ($local_epoch)" + return $local_epoch + else + log_write "check_rain_sensor - now is not raining" + fi + else + log_write "Rain sensor not present" + fi + +} + +# +# Chiude tutte le elettrovalvole se sta piovendo +# Eseguie il controllo in tempo reale sul sensore hardware e sui dati dell'ultima chiamata eseguita online +# +function close_all_for_rain { + + local close_all=0 + local now=`date +%s` + + if [[ "$NOT_IRRIGATE_IF_RAIN_ONLINE" -gt 0 && -f $STATUS_DIR/last_rain_online ]]; then + local last_rain=`cat $STATUS_DIR/last_rain_online` + local dif=0 + let "dif = now - last_rain" + if [ $dif -lt $NOT_IRRIGATE_IF_RAIN_ONLINE ]; then + close_all=1 + fi + fi + + if [[ "$NOT_IRRIGATE_IF_RAIN_SENSOR" -gt 0 && -f $STATUS_DIR/last_rain_sensor ]]; then + local last_rain=`cat $STATUS_DIR/last_rain_sensor` + local dif=0 + let "dif = now - last_rain" + if [ $dif -lt $NOT_IRRIGATE_IF_RAIN_SENSOR ]; then + close_all=1 + fi + fi + + if [ "$close_all" = "1" ]; then + for i in $(seq $EV_TOTAL) + do + local a=EV"$i"_ALIAS + local al=${!a} + ev_status $al + local state=$? + #echo "$al = $state" + if [ "$state" = "1" ]; then + ev_close $al + log_write "close_all_for_rain - Close solenoid '$al' for rain" + fi + done + fi + +} + + diff --git a/include/socket.include.sh b/include/socket.include.sh index b4817a0..80fcfcc 100644 --- a/include/socket.include.sh +++ b/include/socket.include.sh @@ -1,5 +1,3 @@ -#!/bin/bash - # # Avvia il socket server # diff --git a/piGarden.sh b/piGarden.sh index 6cedea9..9f1a0d7 100755 --- a/piGarden.sh +++ b/piGarden.sh @@ -408,104 +408,6 @@ function ev_status { return $state } -# -# Controlla se se piove tramite http://api.wunderground.com/ -# -function check_rain_online { - # http://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary&MR=1 - $CURL http://api.wunderground.com/api/$WUNDERGROUND_KEY/conditions/q/$WUNDERGROUND_LOCATION.json > $TMP_PATH/check_rain_online.json - local weather=`cat $TMP_PATH/check_rain_online.json | $JQ -M ".current_observation.weather"` - local current_observation=`cat $TMP_PATH/check_rain_online.json | $JQ -M ".current_observation"` - local local_epoch=`cat $TMP_PATH/check_rain_online.json | $JQ -M -r ".current_observation.local_epoch"` - #echo $weather - #weather="[Light/Heavy] Drizzle" - if [ "$weather" = "null" ]; then - log_write "check_rain_online - failed read online data" - else - log_write "check_rain_online - weather=$weather, local_epoch=$local_epoch" - #if [[ "$weather" == *"Clear"* ]]; then - #if [[ "$weather" == *"Rain"* ]]; then - if [[ "$weather" == *"Rain"* ]] || - [[ "$weather" == *"Snow"* ]] || - [[ "$weather" == *"Hail"* ]] || - [[ "$weather" == *"Ice"* ]] || - [[ "$weather" == *"Thunderstorm"* ]] || - [[ "$weather" == *"Drizzle"* ]]; - then - #echo "ECCOMI!!!!!" - echo $local_epoch > "$STATUS_DIR/last_rain_online" - return $local_epoch - fi - echo "$current_observation" > "$STATUS_DIR/last_weather_online" - fi -} - -# -# Controlla se se piove tramite sensore -# -function check_rain_sensor { - - if [ -n "$RAIN_GPIO" ]; then - #local s=`$GPIO -g read $RAIN_GPIO` - local s=`drv_rain_sensor_get $RAIN_GPIO` - if [ "$s" = "$RAIN_GPIO_STATE" ]; then - local local_epoch=`date +%s` - echo $local_epoch > "$STATUS_DIR/last_rain_sensor" - log_write "check_rain_sensor - now it's raining ($local_epoch)" - return $local_epoch - else - log_write "check_rain_sensor - now is not raining" - fi - else - log_write "Rain sensor not present" - fi - -} - -# -# Chiude tutte le elettrovalvole se sta piovendo -# Eseguie il controllo in tempo reale sul sensore hardware e sui dati dell'ultima chiamata eseguita online -# -function close_all_for_rain { - - local close_all=0 - local now=`date +%s` - - if [[ "$NOT_IRRIGATE_IF_RAIN_ONLINE" -gt 0 && -f $STATUS_DIR/last_rain_online ]]; then - local last_rain=`cat $STATUS_DIR/last_rain_online` - local dif=0 - let "dif = now - last_rain" - if [ $dif -lt $NOT_IRRIGATE_IF_RAIN_ONLINE ]; then - close_all=1 - fi - fi - - if [[ "$NOT_IRRIGATE_IF_RAIN_SENSOR" -gt 0 && -f $STATUS_DIR/last_rain_sensor ]]; then - local last_rain=`cat $STATUS_DIR/last_rain_sensor` - local dif=0 - let "dif = now - last_rain" - if [ $dif -lt $NOT_IRRIGATE_IF_RAIN_SENSOR ]; then - close_all=1 - fi - fi - - if [ "$close_all" = "1" ]; then - for i in $(seq $EV_TOTAL) - do - local a=EV"$i"_ALIAS - local al=${!a} - ev_status $al - local state=$? - #echo "$al = $state" - if [ "$state" = "1" ]; then - ev_close $al - log_write "close_all_for_rain - Close solenoid '$al' for rain" - fi - done - fi - -} - # # Chiude tutte le elettrovalvole # $1 indica se forzare la chiusura anche per le elettrovalvole con stato di inattività @@ -817,7 +719,7 @@ function debug2 { VERSION=0 SUB_VERSION=4 -RELEASE_VERSION=4 +RELEASE_VERSION=5 DIR_SCRIPT=`dirname $0` NAME_SCRIPT=${0##*/} @@ -842,6 +744,7 @@ fi . "$DIR_SCRIPT/include/drv.include.sh" . "$DIR_SCRIPT/include/cron.include.sh" . "$DIR_SCRIPT/include/socket.include.sh" +. "$DIR_SCRIPT/include/rain.include.sh" LAST_INFO_FILE="$STATUS_DIR/last_info" LAST_WARNING_FILE="$STATUS_DIR/last_worning"