diff --git a/CHANGELOG.md b/CHANGELOG.md index 1acbd73..b4dfcd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.5.1 - xx/xx/2017 +- Added events managemets + # 0.5.0 - 12/10/2017 - Implemented driver subsystem for interfacing with other board - Added driver spb16ch for interfacing with "Smart Power Board 16 channel with RTC" diff --git a/conf/piGarden.conf.example b/conf/piGarden.conf.example index 71cfce8..8571c1c 100644 --- a/conf/piGarden.conf.example +++ b/conf/piGarden.conf.example @@ -8,6 +8,9 @@ LOG_OUTPUT_DRV_FILE="/tmp/piGarden.drv.log" # Status directory STATUS_DIR="/home/pi/piGarden/state" +# Event directory +EVENT_DIR="/home/pi/piGarden/events" + # Posizione gpio GPIO="/usr/local/bin/gpio" @@ -86,6 +89,7 @@ EV_TOTAL=6 # Definizione delle elettrovalvole EV1_ALIAS="1" # EV1_GPIO=17 # Physical 11 - wPi 0 +#EV1_NORAIN=1 # Non interrompe l'irrigazione di questa zona in caso di pioggia EV2_ALIAS="2" # EV2_GPIO=27 # Physical 13 - wPi 2 diff --git a/conf/piGarden.conf.spb16ch.example b/conf/piGarden.conf.spb16ch.example index a85b0de..d508dbf 100644 --- a/conf/piGarden.conf.spb16ch.example +++ b/conf/piGarden.conf.spb16ch.example @@ -8,6 +8,9 @@ LOG_FILE_MAX_SIZE=1048576 # 1MB # Status directory STATUS_DIR="/home/pi/piGarden/state" +# Event directory +EVENT_DIR="/home/pi/piGarden/events" + # Posizione gpio GPIO="/usr/local/bin/gpio" @@ -85,6 +88,7 @@ EV_TOTAL=128 # Definizione delle elettrovalvole EV1_ALIAS="Zona_1" # EV1_GPIO="drv:spb16ch:1" +#EV1_NORAIN=1 # Non interrompe l'irrigazione di questa zona in caso di pioggia EV2_ALIAS="Zona_2" # EV2_GPIO="drv:spb16ch:2" diff --git a/events/init_before/.gitignore b/events/init_before/.gitignore new file mode 100644 index 0000000..86d0cb2 --- /dev/null +++ b/events/init_before/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/events/scripts/.gitignore b/events/scripts/.gitignore new file mode 100644 index 0000000..ba05618 --- /dev/null +++ b/events/scripts/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +*custom* +# Except this file +!.gitignore diff --git a/events/scripts/sendmail.sh b/events/scripts/sendmail.sh new file mode 100755 index 0000000..a7eb757 --- /dev/null +++ b/events/scripts/sendmail.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Send mail on triggered event +# +# $1 = event +# $2 = cause +# $3 = time +# +# To send an email with this script you must install and configure ssmtp and mailutils: +# sudo apt-get install ssmtp mailutils +# +# edit the configuration file /etc/ssmtp/ssmtp.conf and insert the below lines: +# +# root=postmaster +# mailhub=smtp.gmail.com:587 +# hostname=guard +# FromLineOverride=YES +# AuthUser=your_mail@gmail.com +# AuthPass=your_password +# UseSTARTTLS=YES +# + +EVENT="$1" +CAUSE="$2" +TIME=$3 + +TO="mail@destination.com" +FROM="piGuardian@your_domain.com" +SUBJECT="piGuardian notification mail: $EVENT" + +echo -e "PiGuardian triggered new event\n\nEVENT: $EVENT\nCAUSE: $CAUSE\nTIME: $(/bin/date -d@$TIME)" | /usr/bin/mail -s "$SUBJECT" $TO -r $FROM -A /home/pi/piGuardian/log/piGuardian.log diff --git a/events/scripts/sendtelegram.sh b/events/scripts/sendtelegram.sh new file mode 100755 index 0000000..4328a27 --- /dev/null +++ b/events/scripts/sendtelegram.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# +# Send mail on triggered event +# +# $1 = event +# $2 = cause +# $3 = time +# +# To send an email with this script you must install telegram-cli @see http://tuxmaniacs.it/2015/01/installare-telegram-sul-raspberry-pi.html +# + +EVENT="$1" +CAUSE="$2" +TIME=$3 + +TG_CLI="/home/pi/tg/bin/telegram-cli" + +TO="Your_Destination_Contact" + +CMD="msg $TO \"piGuardian triggered new event\\n\\nEVENT: $EVENT\\nCAUSE: $CAUSE\\nTIME: $(/bin/date -d@$TIME)\"" +(sleep 1; echo "contact_list"; sleep 1; echo $CMD) | $TG_CLI -W diff --git a/include/rain.include.sh b/include/rain.include.sh index 946f88e..d95174a 100644 --- a/include/rain.include.sh +++ b/include/rain.include.sh @@ -2,11 +2,16 @@ # Controlla se se piove tramite http://api.wunderground.com/ # function check_rain_online { + + trigger_event "check_rain_online_before" "" + # 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"` + local current_state_rain_online="" + local last_state_rain_online=`cat "$STATUS_DIR/last_state_rain_online" 2> /dev/null` #echo $weather #weather="[Light/Heavy] Drizzle" if [ "$weather" = "null" ]; then @@ -22,12 +27,17 @@ function check_rain_online { [[ "$weather" == *"Thunderstorm"* ]] || [[ "$weather" == *"Drizzle"* ]]; then - #echo "ECCOMI!!!!!" + current_state_rain_online='rain' echo $local_epoch > "$STATUS_DIR/last_rain_online" - #return $local_epoch fi echo "$current_observation" > "$STATUS_DIR/last_weather_online" + if [ "$current_state_rain_online" != "$last_state_rain_online" ]; then + echo "$current_state_rain_online" > "$STATUS_DIR/last_state_rain_online" + trigger_event "check_rain_online_change" "$current_state_rain_online" + fi fi + + trigger_event "check_rain_online_after" "$current_state_rain_online" } # @@ -36,16 +46,24 @@ function check_rain_online { function check_rain_sensor { if [ -n "$RAIN_GPIO" ]; then - #local s=`$GPIO -g read $RAIN_GPIO` + trigger_event "check_rain_sensor_before" "" + local current_state_rain_sensor="" + local last_state_rain_sensor=`cat "$STATUS_DIR/last_state_rain_sensor" 2> /dev/null` local s=`drv_rain_sensor_get $RAIN_GPIO` if [ "$s" = "$RAIN_GPIO_STATE" ]; then + current_state_rain_sensor='rain' 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 + #return $local_epoch else log_write "check_rain_sensor - now is not raining" fi + if [ "$current_state_rain_sensor" != "$last_state_rain_sensor" ]; then + echo "$current_state_rain_sensor" > "$STATUS_DIR/last_state_rain_sensor" + trigger_event "check_rain_sensor_change" "$current_state_rain_sensor" + fi + trigger_event "check_rain_sensor_after" "$current_state_rain_sensor" else log_write "Rain sensor not present" fi diff --git a/piGarden.sh b/piGarden.sh index 8ed9e36..92175c5 100755 --- a/piGarden.sh +++ b/piGarden.sh @@ -14,6 +14,8 @@ function initialize { unlock + trigger_event "init_before" "" + # Inizializza i driver gpio for drv in "${list_drv[@]}" do @@ -60,6 +62,7 @@ function initialize { log_write "Rain sensor not present" fi + trigger_event "init_after" "" log_write "End initialize" } @@ -114,6 +117,12 @@ function ev_open { state=2 fi + trigger_event "ev_open_before" "$1" "$2" + if [ $? -eq 0 ]; then + log_write "Solenoid '$1' not open due to external event" + message_write 'warning' "Solenoid not open due to external event" + fi + # Dall'alias dell'elettrovalvola recupero il numero e dal numero recupero gpio da usare ev_alias2number $1 EVNUM=$? @@ -136,6 +145,8 @@ function ev_open { ev_set_state $EVNUM $state + trigger_event "ev_open_after" "$1" "$2" + unlock log_write "Solenoid '$1' open" @@ -214,6 +225,8 @@ function ev_close { EVNUM=$? g=`ev_number2gpio $EVNUM` + trigger_event "ev_close_before" "$1" + lock # Gestisce l'apertura dell'elettrovalvola in base alla tipologia (monostabile / bistabile) @@ -231,6 +244,8 @@ function ev_close { ev_set_state $EVNUM 0 + trigger_event "ev_close_after" "$1" + unlock log_write "Solenoid '$1' close" @@ -756,7 +771,7 @@ function debug2 { VERSION=0 SUB_VERSION=5 -RELEASE_VERSION=0 +RELEASE_VERSION=1 DIR_SCRIPT=`dirname $0` NAME_SCRIPT=${0##*/} @@ -791,6 +806,11 @@ if [ -z $LOG_OUTPUT_DRV_FILE ]; then $LOG_OUTPUT_DRV_FILE="/dev/null" fi +if [ -z "$EVENT_DIR" ]; then + EVENT_DIR="$DIR_SCRIPT/events" +fi + + # Elimina il file di lock se più vecchio di 11 secondi if [ -f "$LOCK_FILE" ]; then max_age_lock_file=11