14 Commits

Author SHA1 Message Date
lejubila
37a6c58b5a Corretto path dei comandi usati per genereare l'id di installazione, venvia generato un identificativo errato 2017-06-16 23:06:07 +02:00
lejubila
a34c1cd856 Modificato changelog per versione 0.4.1 2017-06-14 17:40:31 +02:00
lejubila
02cde84392 Corretto problema sull'invio identificativo installazione per uso statistico 2017-06-14 17:38:47 +02:00
lejubila
f58b2ba7fb Variato changelog per la versione 0.4.0 2017-06-14 01:19:43 +02:00
lejubila
2b13ba0cf0 aggiunto redirect standard error della chiamata per collezionare le statistiche di utilizzo 2017-06-13 01:00:06 +02:00
lejubila
e288244803 aggiunto unlock in caso timeout della funzione lock 2017-06-10 23:51:28 +02:00
lejubila
5a28d129f5 aggiunto sistema di invio identificativo installazione per uso statistico 2017-06-09 01:59:04 +02:00
lejubila
ced5ba83e8 aggiunto tempo di attesa random su funzione lock per impedire sovrapposizioni concorrenti 2017-05-30 23:51:39 +02:00
lejubila
44529c661f aggiunto supporto per l'inserimento di un cron disabiltato di schedulazione apertura/chiusura zona via api socket server 2017-05-28 16:42:13 +02:00
lejubila
357b976765 Aggiunto possibiltà di inserire una schedulazione di apertura/chiusura in modalità disabilitata 2017-05-23 23:34:43 +02:00
lejubila
3aad235dda aggiunto supporto per autenticazione su socket server 2017-05-21 12:42:26 +02:00
lejubila
6447ec5326 Aggiunto gestione lock/unnlock per prevenire chiamate concorrenti su apertura/chiusura delle elettrovalvole 2017-05-19 23:52:36 +02:00
lejubila
e5bfe3e8ca Corretto numero di versione 2017-05-14 10:51:00 +02:00
lejubila
9743198315 Aggiunto supporto sperimentale per elettrovalcole mono-stabili 2017-05-13 12:14:02 +02:00
3 changed files with 270 additions and 47 deletions

View File

@@ -1,3 +1,21 @@
## 0.4.2 - 16/06/2017
Fix another problem on generate installation identifier to sendo for statistic
## 0.4.1 - 14/06/2017
Fix problem on send identifier installation for statistic
## 0.4.0 - 14/06/2017
Add credentials support to socket server (define TCPSERVER_USER and TCPSERVER_PWD in your config file)
Add management lock/unlock for prevent concurrente call to open/close solenoid
Added the ability to enter an open / close schedule in disabled mode
Add send statistic information to remote server
During the initialization function, information on the last rain is no longer removed
## 0.3.1 - 13/05/2017
Add experimental support for monostable solenoid valve:
- define in your config file the variable EV_MONOSTABLE and assign value 1
- if the solenoid valves close instead of opening and vice versa, reverse the values of the RELE_GPIO_CLOSE and RELE_GPIO_OPEN variables in your configuration file
## 0.3.0 - 07/05/2017
Add command "open_in" for scheduling on the fly the opens/close a solenoid
Add command "del_cron_open_in" for delete scheduling the fly the opens/close a solenoid

View File

@@ -44,6 +44,13 @@ SED="/bin/sed"
# Percorso readlink
READLINK="/bin/readlink"
# Percorso stat
STAT="/usr/bin/stat"
# Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili,
# se impostato a 0 il sistema gestirà elettrovalvole bisstabili
EV_MONOSTABLE=0
# Id gpio usati per simulare il doppio deviatore con cui eseguire l'alimentazione alle elettrovalvole
SUPPLY_GPIO_1=2
SUPPLY_GPIO_2=3
@@ -107,3 +114,10 @@ NOT_IRRIGATE_IF_RAIN_SENSOR=86400
TCPSERVER_IP="127.0.0.1"
TCPSERVER_PORT="8084"
# Utente e password che i clients devono utilizzare per stabilire una connessione tramite socket server
TCPSERVER_USER=""
TCPSERVER_PWD=""
# Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo
NO_SEND_IDENTIFIER=0

View File

@@ -10,23 +10,27 @@
#
function initialize {
lock
log_write "Run initialize"
# Imposta l'alimentazione con voltaggio negativo e setta i gpio in scrittura
$GPIO -g write $SUPPLY_GPIO_1 0
$GPIO -g write $SUPPLY_GPIO_2 0
$GPIO -g mode $SUPPLY_GPIO_1 out
$GPIO -g mode $SUPPLY_GPIO_2 out
# Imposta l'alimentazione con voltaggio negativo e setta i gpio in scrittura per le elettrovalvole bistabili
if [ "$EV_MONOSTABLE" != "1" ]; then
$GPIO -g write $SUPPLY_GPIO_1 0
$GPIO -g write $SUPPLY_GPIO_2 0
$GPIO -g mode $SUPPLY_GPIO_1 out
$GPIO -g mode $SUPPLY_GPIO_2 out
fi
# Elimina tutti gli stati delle elettrovalvole preesistenti
rm -f "$STATUS_DIR"/*
rm -f "$STATUS_DIR"/ev*
# Inizializza i gpio delle elettrovalvole e ne chiude l'alimentazione
for i in $(seq $EV_TOTAL)
do
g=EV"$i"_GPIO
$GPIO -g write ${!g} RELE_GPIO_OPEN # chiude l'alimentazione all'elettrovalvole
$GPIO -g mode ${!g} out # setta il gpio nella modalita di scrittura
$GPIO -g mode ${!g} out # setta il gpio nella modalita di scrittura
ev_set_state $i 0
done
@@ -48,8 +52,13 @@ function initialize {
log_write "End initialize"
unlock
}
#
# Elimina i file contenente i messaggi da inserire nel json status
#
function reset_messages {
rm -f "$LAST_INFO_FILE.$!"
rm -f "$LAST_WARNING_FILE.$!"
@@ -62,7 +71,7 @@ function reset_messages {
# $2 se specificata la string "force" apre l'elettrovalvola anche se c'é pioggia
#
function ev_open {
cron_del open_in $1 > /dev/null 2>&1
if [ ! "$2" = "force" ]; then
@@ -97,18 +106,30 @@ function ev_open {
state=2
fi
log_write "Solenoid '$1' open"
message_write "success" "Solenoid open"
supply_positive
# Dall'alias dell'elettrovalvola recupero il numero e dal numero recupero gpio da usare
ev_alias2number $1
EVNUM=$?
ev_number2gpio $EVNUM
g=$?
$GPIO -g write $g $RELE_GPIO_CLOSE
sleep 1
$GPIO -g write $g $RELE_GPIO_OPEN
lock
# Gestisce l'apertura dell'elettrovalvola in base alla tipologia (monostabile / bistabile)
if [ "$EV_MONOSTABLE" == "1" ]; then
$GPIO -g write $g $RELE_GPIO_CLOSE
else
supply_positive
$GPIO -g write $g $RELE_GPIO_CLOSE
sleep 1
$GPIO -g write $g $RELE_GPIO_OPEN
fi
ev_set_state $EVNUM $state
unlock
log_write "Solenoid '$1' open"
message_write "success" "Solenoid open"
}
#
@@ -177,19 +198,32 @@ function ev_open_in {
# $1 alias elettrovalvola
#
function ev_close {
log_write "Solenoid '$1' close"
message_write "success" "Solenoid close"
supply_negative
#$GPIO_alias2number $1
# Dall'alias dell'elettrovalvola recupero il numero e dal numero recupero gpio da usare
ev_alias2number $1
EVNUM=$?
ev_number2gpio $EVNUM
g=$?
$GPIO -g write $g $RELE_GPIO_CLOSE
sleep 1
$GPIO -g write $g $RELE_GPIO_OPEN
lock
# Gestisce l'apertura dell'elettrovalvola in base alla tipologia (monostabile / bistabile)
if [ "$EV_MONOSTABLE" == "1" ]; then
$GPIO -g write $g $RELE_GPIO_OPEN
else
supply_negative
$GPIO -g write $g $RELE_GPIO_CLOSE
sleep 1
$GPIO -g write $g $RELE_GPIO_OPEN
fi
ev_set_state $EVNUM 0
unlock
log_write "Solenoid '$1' close"
message_write "success" "Solenoid close"
cron_del open_in_stop $1 > /dev/null 2>&1
}
@@ -455,6 +489,10 @@ function close_all_for_rain {
}
#
# Chiude tutte le elettrovalvole
# $1 indica se forzare la chiusura anche per le elettrovalvole con stato di inattività
#
function close_all {
for i in $(seq $EV_TOTAL)
@@ -472,6 +510,9 @@ function close_all {
}
#
# Stampa la lista degli alias delle elettrovalvole
#
function list_alias {
for i in $(seq $EV_TOTAL)
@@ -484,7 +525,10 @@ function list_alias {
}
#
# Stampa un json contanente lo status della centralina
# $1 .. $6 parametri opzionali
# - get_cron: aggiunge i dati relativi ai crontab delle scehdulazioni di apertura/chisura delle elettrovalvole
# - get_cron_open_in: aggiunge i dati relativi ai crontab degli avvii ritardati eseguiti con il comando open_in
#
function json_status {
local json=""
@@ -676,6 +720,7 @@ function cron_add {
local CRON_ARG=$7
local CRON_ARG2=$8
local CRON_COMMAND=""
local CRON_DISABLED=""
local PATH_SCRIPT=`$READLINK -f "$DIR_SCRIPT/$NAME_SCRIPT"`
local TMP_CRON_FILE2="$TMP_CRON_FILE-2"
@@ -768,6 +813,9 @@ function cron_add {
open)
CRON_COMMAND="$PATH_SCRIPT open $CRON_ARG"
if [ "$CRON_ARG2" == "disabled" ]; then
CRON_DISABLED="#"
fi
;;
open_in)
@@ -780,6 +828,9 @@ function cron_add {
close)
CRON_COMMAND="$PATH_SCRIPT close $CRON_ARG"
if [ "$CRON_ARG2" == "disabled" ]; then
CRON_DISABLED="#"
fi
;;
*)
@@ -804,7 +855,7 @@ function cron_add {
if [ "$NEW_CRON" -eq "0" ]; then
echo "$PREVIOUS_CONTENT" >> "$TMP_CRON_FILE2"
fi
echo "$CRON_M $CRON_H $CRON_DOM $CRON_MON $CRON_DOW $CRON_COMMAND" >> "$TMP_CRON_FILE2"
echo "$CRON_DISABLED$CRON_M $CRON_H $CRON_DOM $CRON_MON $CRON_DOW $CRON_COMMAND" >> "$TMP_CRON_FILE2"
echo "# END cron $CRON_TYPE $CRON_ARG" >> "$TMP_CRON_FILE2"
$CRONTAB "$TMP_CRON_FILE2"
@@ -864,6 +915,9 @@ function cron_get {
}
#
# Imposta il cron di inizializzazione della centralina
#
function set_cron_init {
cron_del "init" 2> /dev/null
@@ -871,12 +925,18 @@ function set_cron_init {
}
#
# Elimina il cron di inizializzazione della centralina
#
function del_cron_init {
cron_del "init"
}
#
# Imposta il cron per l'avvio del socket server
#
function set_cron_start_socket_server {
cron_del "start_socket_server" 2> /dev/null
@@ -884,41 +944,62 @@ function set_cron_start_socket_server {
}
#
# Elimina il cron per l'avvio del socket server
#
function del_cron_start_socket_server {
cron_del "start_socket_server"
}
#
# Imposta il cron che esegue il controllo di presenza pioggia tramite sensore
#
function set_cron_check_rain_sensor {
cron_del "check_rain_sensor" 2> /dev/null
cron_add "check_rain_sensor"
}
#
# Elimina il cron che esegue il controllo di presenza pioggia tramite sensore
#
function del_cron_check_rain_sensor {
cron_del "check_rain_sensor"
}
#
# Imposta il cron che esegue il controllo di presenza pioggia tramite servizio online
#
function set_cron_check_rain_online {
cron_del "check_rain_online" 2> /dev/null
cron_add "check_rain_online"
}
#
# Elimina il cron che esegue il controllo di presenza pioggia tramite servizio online
#
function del_cron_check_rain_online {
cron_del "check_rain_online"
}
#
# Imposta il cron che gestisce la chiusura delle elettrovalvole in caso di pioggia
#
function set_cron_close_all_for_rain {
cron_del "close_all_for_rain" 2> /dev/null
cron_add "close_all_for_rain"
}
#
# Elimina il cron che gestisce la chiusura delle elettrovalvole in caso di pioggia
#
function del_cron_close_all_for_rain {
cron_del "close_all_for_rain"
@@ -933,6 +1014,7 @@ function del_cron_close_all_for_rain {
# $4 giorno del mese cron
# $5 mese cron
# $6 giorno della settimana cron
# $7 disabled
#
function add_cron_open {
@@ -943,7 +1025,7 @@ function add_cron_open {
return 1
fi
cron_add "open" "$2" "$3" "$4" "$5" "$6" "$1"
cron_add "open" "$2" "$3" "$4" "$5" "$6" "$1" "$7"
}
@@ -1024,6 +1106,7 @@ function get_cron_close {
# $4 giorno del mese cron
# $5 mese cron
# $6 giorno della settimana cron
# $7 disabled
#
function add_cron_close {
@@ -1034,7 +1117,7 @@ function add_cron_close {
return 1
fi
cron_add "close" "$2" "$3" "$4" "$5" "$6" "$1"
cron_add "close" "$2" "$3" "$4" "$5" "$6" "$1" "$7"
}
@@ -1055,8 +1138,9 @@ function del_cron_close {
}
#
# Mostra il i parametri dello script
#
function show_usage {
echo -e "piGarden v. $VERSION.$SUB_VERSION.$RELEASE_VERSION"
echo -e ""
@@ -1087,11 +1171,11 @@ function show_usage {
echo -e "\t$NAME_SCRIPT set_cron_close_all_for_rain set crontab for close all solenoid when raining"
echo -e "\t$NAME_SCRIPT del_cron_close_all_for_rain remove crontab for close all solenoid when raining"
echo -e "\t$NAME_SCRIPT add_cron_open alias m h dom mon dow add crontab for open a solenoid"
echo -e "\t$NAME_SCRIPT add_cron_open alias m h dom mon dow [disbled] add crontab for open a solenoid"
echo -e "\t$NAME_SCRIPT del_cron_open alias remove all crontab for open a solenoid"
echo -e "\t$NAME_SCRIPT get_cron_open alias get all crontab for open a solenoid"
echo -e "\t$NAME_SCRIPT del_cron_open_in alias remove all crontab for open_in a solenoid"
echo -e "\t$NAME_SCRIPT add_cron_close alias m h dom mon dow add crontab for close a solenoid"
echo -e "\t$NAME_SCRIPT add_cron_close alias m h dom mon dow [disabled] add crontab for close a solenoid"
echo -e "\t$NAME_SCRIPT del_cron_close alias remove all crontab for close a solenoid"
echo -e "\t$NAME_SCRIPT get_cron_close alias get all crontab for close a solenoid"
echo -e "\n"
@@ -1099,24 +1183,20 @@ function show_usage {
echo -e "\t$NAME_SCRIPT debug2 [parameter]|[parameter]|..] Run debug code 2"
}
#
# Avvia il socket server
#
function start_socket_server {
rm -f "$TCPSERVER_PID_FILE"
echo $TCPSERVER_PID_SCRIPT > "$TCPSERVER_PID_FILE"
$TCPSERVER -v -RHl0 $TCPSERVER_IP $TCPSERVER_PORT $0 socket_server_command
#if [ $? -eq 0 ]; then
# echo $TCPSERVER_PID_SCRIPT > "$TCPSERVER_PID_FILE"
# trap stop_socket_server EXIT
# log_write "start socket server ";
# return 0
#else
# log_write "start socket server failed";
# return 1
#fi
}
#
# Ferma il socket server
#
function stop_socket_server {
if [ ! -f "$TCPSERVER_PID_FILE" ]; then
@@ -1132,11 +1212,29 @@ function stop_socket_server {
}
#
# Esegue un comando ricevuto dal socket server
#
function socket_server_command {
RUN_FROM_TCPSERVER=1
local line=""
if [ ! -z "$TCPSERVER_USER" ] && [ ! -z "$TCPSERVER_PWD" ]; then
local user=""
local password=""
read -t 3 user
read -t 3 password
user=$(echo "$user" | $TR -d '[\r\n]')
password=$(echo "$password" | $TR -d '[\r\n]')
if [ "$user" != "$TCPSERVER_USER" ] || [ "$password" != "$TCPSERVER_PWD" ]; then
log_write "socket connection from: $TCPREMOTEIP - Bad socket server credentials - user:$user"
json_error 0 "Bad socket server credentials"
return
fi
fi
read line
line=$(echo "$line " | $TR -d '[\r\n]')
arg1=$(echo "$line " | $CUT -d ' ' -f1)
@@ -1146,8 +1244,9 @@ function socket_server_command {
arg5=$(echo "$line " | $CUT -d ' ' -f5)
arg6=$(echo "$line " | $CUT -d ' ' -f6)
arg7=$(echo "$line " | $CUT -d ' ' -f7)
arg8=$(echo "$line " | $CUT -d ' ' -f8)
log_write "socket connection from: $TCPREMOTEIP - command: $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7"
log_write "socket connection from: $TCPREMOTEIP - command: $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7 $arg8"
reset_messages &> /dev/null
@@ -1252,10 +1351,10 @@ function socket_server_command {
;;
add_cron_open)
add_cron_open)
local vret=""
vret=`add_cron_open "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7"`
vret=`add_cron_open "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" $arg8`
if [[ ! -z $vret ]]; then
json_error 0 "Cron set failed"
@@ -1270,7 +1369,7 @@ function socket_server_command {
add_cron_close)
local vret=""
vret=`add_cron_close "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7"`
vret=`add_cron_close "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" $arg8`
if [[ ! -z $vret ]]; then
json_error 0 "Cron set failed"
@@ -1292,6 +1391,11 @@ function socket_server_command {
}
#
# Mostra un json per una risposta di errore
# $1 codice errore
# $2 messaggio di errore
#
json_error()
{
echo "{\"error\":{\"code\":$1,\"description\":\"$2\"}}"
@@ -1309,17 +1413,90 @@ list_descendants ()
echo "$children"
}
#
# Gestisce l'apertura di un lock
#
function lock {
sleep 0.$((100 * $RANDOM / 32767)) | sed 's/..$/.&/'
local max_time=10
local current_time=$(($1 + 1))
local lock_content=`cat "$LOCK_FILE" 2> /dev/null`
if [ -z $lock_content ]; then
lock_content="0"
fi
if [ "$lock_content" -eq "1" ]; then
if [ "$current_time" -gt "$max_time" ]; then
log_write "Maximum locked time reached"
sleep $max_time
unlock
exit 1
fi
log_write "Sleep 1 second for locked state"
sleep 1
lock $current_time
return
fi
echo "1" > "$LOCK_FILE"
}
#
# Chidue un lock
#
function unlock {
echo "0" > "$LOCK_FILE"
}
#
# Invia l'identificativo univoco ad uso statistico di utilizzo
#
function send_identifier {
if [ "$NO_SEND_IDENTIFIER" == "1" ]; then
return
fi
local FILE_ID="/tmp/pigarden.id"
if [ -f "$FILE_ID" ]; then
# Se il file non è più vecchio di un giorno esce
local max_age_file=86400
local time_file=`$STAT -c %Y "$FILE_ID"`
local age_file=$((`date +"%s"` - $time_file ))
#log_write "age_file=$age_file - max_age_file=$max_age_file"
if [ "$age_file" -lt "$max_age_file" ]; then
#log_write "Id troppo giovane ($age_file) esce e non esegue l'invio"
return
fi
fi
local ID=`/sbin/ifconfig | $GREP --color=never -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | /usr/bin/head -1 | /usr/bin/md5sum | $CUT -d" " -f 1`
if [ -z "$ID" ]; then
return;
fi
echo "$ID" > "$FILE_ID"
log_write "Send installation identifier to collect usage"
$CURL https://www.lejubila.net/statistic/collect_usage/piGarden/$ID/$VERSION/$SUB_VERSION/$RELEASE_VERSION > /dev/null 2>&1
}
function debug1 {
. "$DIR_SCRIPT/debug/debug1.sh"
}
function debug2 {
. "$DIR_SCRIPT/debug/debug1.sh"
. "$DIR_SCRIPT/debug/debug2.sh"
}
VERSION=0
SUB_VERSION=3
RELEASE_VERSION=0
SUB_VERSION=4
RELEASE_VERSION=2
DIR_SCRIPT=`dirname $0`
NAME_SCRIPT=${0##*/}
@@ -1332,6 +1509,7 @@ TCPSERVER_PID_FILE="$TMP_PATH/piGardenTcpServer.pid"
TCPSERVER_PID_SCRIPT=$$
RUN_FROM_TCPSERVER=0
TMP_CRON_FILE="$TMP_PATH/pigarden.user.cron.$$"
LOCK_FILE="$TMP_PATH/piGarden.lock"
if [ -f $CONFIG_ETC ]; then
. $CONFIG_ETC
@@ -1344,6 +1522,19 @@ LAST_INFO_FILE="$STATUS_DIR/last_info"
LAST_WARNING_FILE="$STATUS_DIR/last_worning"
LAST_SUCCESS_FILE="$STATUS_DIR/last_success"
# Elimina il file di lock se più vecchio di 11 secondi
if [ -f "$LOCK_FILE" ]; then
max_age_lock_file=11
time_lock_file=`$STAT -c %Y "$LOCK_FILE"`
age_lock_file=$((`date +"%s"` - $time_lock_file ))
if [ "$age_lock_file" -gt "$max_age_lock_file" ]; then
rm -f "$age_lock_file"
fi
fi
send_identifier &
case "$1" in
init)
initialize
@@ -1471,7 +1662,7 @@ case "$1" in
;;
add_cron_open)
add_cron_open "$2" "$3" "$4" "$5" "$6" "$7"
add_cron_open "$2" "$3" "$4" "$5" "$6" "$7" "$8"
;;
del_cron_open)
@@ -1487,7 +1678,7 @@ case "$1" in
;;
add_cron_close)
add_cron_close "$2" "$3" "$4" "$5" "$6" "$7"
add_cron_close "$2" "$3" "$4" "$5" "$6" "$7" "$8"
;;
del_cron_close)