16 Commits

Author SHA1 Message Date
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
lejubila
2889758b28 aggiunto data al changelog 2017-05-07 18:19:36 +02:00
lejubila
477f2b15a0 cambiato numero di versione, aggiunto informazioni cron_open_in alle chiamate json_status eseguite per l'apertura/chiusura delle zone dal socket server 2017-05-07 17:08:05 +02:00
lejubila
973bcf0b3d Aggiunto funzioni command 'open_in' e 'del_cron_open_in' per la gestione degli avvii ritardati. Aggiunto api al socket server per i comandi 'open_in' e 'delete_cron_open_in'. Per alcuni file temporanei cambiato path su /var/shm (tmpfs) per prevenire deterioramento della scheda sd. 2017-05-07 02:47:27 +02:00
lejubila
95199d2bb1 Add function open_in 2017-04-27 18:49:05 +02:00
lejubila
2fc1c04f3d Corretto problema che faceva chiudere le elttrovalvole in caso di pioggia anche se erano state aperte in modalità 'force' 2017-04-25 01:43:18 +02:00
4 changed files with 458 additions and 65 deletions

View File

@@ -1,3 +1,25 @@
## 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
Add api in socket server for command open_in and delete_cron_open_in
Fix minor bug on command "open"
Changed the path of some temporary files to prevent sd card faults
## 0.2.2 - 25/04/2017
Fix bug: if it's reining, the solenoid valves were also closed even if they were pushed open in "force" mode
## 0.2.1 - 22/04/2017
Add installation instructions in README.md file

View File

@@ -1,4 +1,4 @@
## piGarden
# piGarden
Bash script to manage an irrigation system built with a Raspberry Pi
@@ -10,7 +10,7 @@ Documentation of piGarden and build system irrigation with Raspberry Pi can be f
This script is open-sourced software under GNU GENERAL PUBLIC LICENSE Version 2
## Installagtion
## Installation
1) Installs the necessary packages on your terminal:

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,16 +10,20 @@
#
function initialize {
lock
log_write "Run initialize"
# Imposta l'alimentazione con voltaggio negativo e setta i gpio in scrittura
# 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)
@@ -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,6 +71,9 @@ 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
if [[ "$NOT_IRRIGATE_IF_RAIN_ONLINE" -gt 0 && -f $STATUS_DIR/last_rain_online ]]; then
local last_rain=`cat $STATUS_DIR/last_rain_online`
@@ -89,37 +101,130 @@ function ev_open {
fi
fi
log_write "Solenoid '$1' open"
message_write "success" "Solenoid open"
supply_positive
#gpio_alias2number $1
local state=1
if [ "$2" = "force" ]; then
state=2
fi
# Dall'alias dell'elettrovalvola recupero il numero e dal numero recupero gpio da usare
ev_alias2number $1
EVNUM=$?
ev_number2gpio $EVNUM
g=$?
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
ev_set_state $EVNUM 1
fi
ev_set_state $EVNUM $state
unlock
log_write "Solenoid '$1' open"
message_write "success" "Solenoid open"
}
#
# Commuta un elettrovalvola nello stato aperto
# $1 minute_start
# $2 minute_stop
# $3 alias elettrovalvola
# $4 se specificata la string "force" apre l'elettrovalvola anche se c'é pioggia
#
function ev_open_in {
local minute_start=$1
local minute_stop=$2
local alias=$3
local force=$4
re='^[0-9]+$'
if ! [[ $minute_start =~ $re ]] ; then
echo -e "Time start of irrigation is wrong or not specified"
message_write "warning" "Time start of irrigation is wrong or not specified"
return 1
fi
if ! [[ $minute_stop =~ $re ]] ; then
echo -e "Time stop of irrigation is wrong or not specified"
message_write "warning" "Time stop of irrigation is wrong or not specified"
return 1
fi
if [ $minute_stop -lt "1" ] ; then
echo -e "Time stop of irrigation is wrong"
message_write "warning" "Time stop of irrigation is wrong"
return 1
fi
if [ "empty$alias" == "empty" ]; then
echo -e "Alias solenoid not specified"
message_write "warning" "Alias solenoid not specified"
return 1
fi
gpio_alias2number $alias > /dev/null 2>&1
minute_start=$(($minute_start + 1))
minute_stop=$(($minute_start + $minute_stop))
local cron_start=`date -d "today + $minute_start minutes" +"%M %H %d %m %u"`
cron_del open_in $alias > /dev/null 2>&1
cron_del open_in_stop $alias > /dev/null 2>&1
if [ "$minute_start" -eq "1" ]; then
ev_open $alias $force
else
cron_add open_in $cron_start "$alias" "$force"
fi
local cron_stop=`date -d "today + $minute_stop minutes" +"%M %H %d %m %u"`
cron_add open_in_stop $cron_stop "$alias"
message_write "success" "Scheduled start successfully performed"
#echo $cron_start
#echo $cron_stop
}
#
# Commuta un elettrovalvola nello stato chiuso
# $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=$?
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
}
#
@@ -207,7 +312,7 @@ function gpio_alias2number {
done
log_write "ERROR solenoid alias not found: $1"
message_write "error" "Solenoid alias not found"
message_write "warning" "Solenoid alias not found"
exit 1
}
@@ -226,7 +331,7 @@ function ev_alias2number {
done
log_write "ERROR solenoid alias not found: $1"
message_write "error" "Solenoid alias not found"
message_write "warning" "Solenoid alias not found"
exit 1
}
@@ -292,10 +397,10 @@ function ev_status {
#
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/check_rain_online.json
local weather=`cat /tmp/check_rain_online.json | $JQ -M ".current_observation.weather"`
local current_observation=`cat /tmp/check_rain_online.json | $JQ -M ".current_observation"`
local local_epoch=`cat /tmp/check_rain_online.json | $JQ -M -r ".current_observation.local_epoch"`
$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
@@ -384,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)
@@ -393,7 +502,7 @@ function close_all {
ev_status $al
local state=$?
#echo "$al = $state"
if [[ "$state" = "1" || "$1" = "force" ]]; then
if [[ "$state" -gt "0" || "$1" = "force" ]]; then
ev_close $al
log_write "close_all - Close solenoid '$al' for rain"
fi
@@ -401,6 +510,9 @@ function close_all {
}
#
# Stampa la lista degli alias delle elettrovalvole
#
function list_alias {
for i in $(seq $EV_TOTAL)
@@ -413,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=""
@@ -426,6 +541,7 @@ function json_status {
local last_warning=""
local last_success=""
local with_get_cron="0"
local with_get_cron_open_in="0"
local vret=""
for i in $1 $2 $3 $4 $5 $6
@@ -433,6 +549,9 @@ function json_status {
if [ $i = "get_cron" ]; then
with_get_cron="1"
fi
if [ $i = "get_cron_open_in" ]; then
with_get_cron_open_in="1"
fi
done
for i in $(seq $EV_TOTAL)
@@ -497,7 +616,33 @@ function json_status {
fi
local json_cron="\"cron\":{$json_get_cron}"
json="{$json_version,$json,$json_last_weather_online,$json_error,$json_last_info,$json_last_warning,$json_last_success,$json_last_rain_online,$json_last_rain_sensor,$json_cron}"
local json_get_cron_open_in=""
if [ $with_get_cron_open_in = "1" ]; then
local values_open_in=""
local values_open_in_stop=""
for i in $(seq $EV_TOTAL)
do
local a=EV"$i"_ALIAS
local av=${!a}
local crn="$(cron_get "open_in" $av)"
crn=`echo "$crn" | sed ':a;N;$!ba;s/\n/%%/g'`
values_open_in="\"$av\": \"$crn\", $values_open_in"
local crn="$(cron_get "open_in_stop" $av)"
crn=`echo "$crn" | sed ':a;N;$!ba;s/\n/%%/g'`
values_open_in_stop="\"$av\": \"$crn\", $values_open_in_stop"
done
if [[ ! -z $values_open_in ]]; then
values_open_in="${values_open_in::-2}"
fi
if [[ ! -z $values_open_in_stop ]]; then
values_open_in_stop="${values_open_in_stop::-2}"
fi
json_get_cron_open_in="\"open_in\": {$values_open_in},\"open_in_stop\": {$values_open_in_stop}"
fi
local json_cron_open_in="\"cron_open_in\":{$json_get_cron_open_in}"
json="{$json_version,$json,$json_last_weather_online,$json_error,$json_last_info,$json_last_warning,$json_last_success,$json_last_rain_online,$json_last_rain_sensor,$json_cron,$json_cron_open_in}"
echo "$json"
@@ -547,7 +692,7 @@ function cron_del {
fi
$SED "$START,${END}d" "$TMP_CRON_FILE" | $CRONTAB -
$SED "$START,${END}d" "$TMP_CRON_FILE" | $SED '$!N; /^\(.*\)\n\1$/!P; D' | $CRONTAB -
#$CRONTAB "$TMP_CRON_FILE"
rm "$TMP_CRON_FILE"
@@ -562,6 +707,7 @@ function cron_del {
# $5 mese
# $6 giorno della settimana
# $7 argomento della tipologia
# $8 secondo argomento della tipologia
#
function cron_add {
@@ -572,7 +718,9 @@ function cron_add {
local CRON_MON=$5
local CRON_DOW=$6
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"
@@ -665,10 +813,24 @@ function cron_add {
open)
CRON_COMMAND="$PATH_SCRIPT open $CRON_ARG"
if [ "$CRON_ARG2" == "disabled" ]; then
CRON_DISABLED="#"
fi
;;
open_in)
CRON_COMMAND="$PATH_SCRIPT open $CRON_ARG $CRON_ARG2"
;;
open_in_stop)
CRON_COMMAND="$PATH_SCRIPT close $CRON_ARG"
;;
close)
CRON_COMMAND="$PATH_SCRIPT close $CRON_ARG"
if [ "$CRON_ARG2" == "disabled" ]; then
CRON_DISABLED="#"
fi
;;
*)
@@ -693,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"
@@ -753,6 +915,9 @@ function cron_get {
}
#
# Imposta il cron di inizializzazione della centralina
#
function set_cron_init {
cron_del "init" 2> /dev/null
@@ -760,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
@@ -773,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"
@@ -822,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 {
@@ -832,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"
}
@@ -870,6 +1063,24 @@ function get_cron_open {
}
#
# Cancella tutte le schedulazioni cron per aprire/chiudere una elettrovalvola in modo ritardato
# $1 alias elettrovalvola
#
function del_cron_open_in {
local exists=`alias_exists $1`
if [ "check $exists" = "check FALSE" ]; then
log_write "Alias $1 not found"
echo "Alias $1 not found"
return 1
fi
cron_del "open_in" $1
cron_del "open_in_stop" $1
}
#
# Legge tutte le schedulazioni cron per chiudere una elettrovalvola
# $1 alias elettrovalvola
@@ -895,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 {
@@ -905,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"
}
@@ -926,17 +1138,21 @@ function del_cron_close {
}
#
# Mostra il i parametri dello script
#
function show_usage {
echo -e "piGarden v. $VERSION.$SUB_VERSION.$RELEASE_VERSION"
echo -e ""
echo -e "Usage:"
echo -e "\t$NAME_SCRIPT init initialize supply and solenoid in closed state"
echo -e "\t$NAME_SCRIPT open alias [force] open a solenoid"
echo -e "\t$NAME_SCRIPT open_in minute_start minute_stop alias [force] open a solenoid in minute_start for minute_stop"
echo -e "\t$NAME_SCRIPT close alias close a solenoid"
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 json_status [get_cron] show status in json format"
echo -e "\t$NAME_SCRIPT json_status [get_cron|get_cron_open_in] show status in json format"
echo -e "\t$NAME_SCRIPT check_rain_online check rain from http://api.wunderground.com/"
echo -e "\t$NAME_SCRIPT check_rain_sensor check rain from hardware sensor"
echo -e "\t$NAME_SCRIPT close_all_for_rain close all solenoid if it's raining"
@@ -955,10 +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 add_cron_close alias m h dom mon dow add crontab for close 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 [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"
@@ -966,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
@@ -999,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)
@@ -1013,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
@@ -1028,16 +1260,21 @@ function socket_server_command {
json_error 0 "Alias solenoid not specified"
else
ev_open $arg2 $arg3 &> /dev/null
json_status
json_status "get_cron_open_in"
fi
;;
open_in)
ev_open_in $arg2 $arg3 $arg4 $arg5 &> /dev/null
json_status "get_cron_open_in"
;;
close)
if [ "empty$arg2" == "empty" ]; then
json_error 0 "Alias solenoid not specified"
else
ev_close $arg2 &> /dev/null
json_status
json_status "get_cron_open_in"
fi
;;
@@ -1075,7 +1312,7 @@ function socket_server_command {
if [[ ! -z $vret ]]; then
json_error 0 "Cron set failed"
log_write "Cron set failed: $vret"
log_write "Cron del failed: $vret"
else
message_write "success" "Cron set successfull"
json_status
@@ -1083,6 +1320,22 @@ function socket_server_command {
;;
del_cron_open_in)
local vret=""
vret=`del_cron_open_in $arg2`
if [[ ! -z $vret ]]; then
json_error 0 "Cron del failed"
log_write "Cron del failed: $vret"
else
message_write "success" "Scheduled start successfully deleted"
json_status "get_cron_open_in"
fi
;;
del_cron_close)
local vret=""
@@ -1101,7 +1354,7 @@ function socket_server_command {
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"
@@ -1116,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"
@@ -1138,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\"}}"
@@ -1155,25 +1413,100 @@ 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 è 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 ))
if [ "$age_file" -lt "$max_age_file" ]; then
return
fi
else
local ID=`ifconfig | $GREP --color=never -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | head -1 | md5sum | $CUT -d" " -f 1`
if [ -z "$ID" ]; then
return;
fi
echo "$ID" > "$FILE_ID"
fi
$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=2
SUB_VERSION=4
RELEASE_VERSION=0
DIR_SCRIPT=`dirname $0`
NAME_SCRIPT=${0##*/}
CONFIG_ETC="/etc/piGarden.conf"
TCPSERVER_PID_FILE="/tmp/piGardenTcpServer.pid"
TMP_PATH="/run/shm"
if [ ! -d "$TMP_PATH" ]; then
TMP_PATH="/tmp"
fi
TCPSERVER_PID_FILE="$TMP_PATH/piGardenTcpServer.pid"
TCPSERVER_PID_SCRIPT=$$
RUN_FROM_TCPSERVER=0
TMP_CRON_FILE="/tmp/pigarden.user.cron.$$"
TMP_CRON_FILE="$TMP_PATH/pigarden.user.cron.$$"
LOCK_FILE="$TMP_PATH/piGarden.lock"
if [ -f $CONFIG_ETC ]; then
. $CONFIG_ETC
@@ -1186,6 +1519,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
@@ -1194,10 +1540,15 @@ case "$1" in
open)
if [ "empty$2" == "empty" ]; then
echo -e "Alias solenoid not specified"
exit 1
fi
ev_open $2 $3
;;
open_in)
ev_open_in $2 $3 $4 $5
;;
close)
if [ "empty$2" == "empty" ]; then
echo -e "Alias solenoid not specified"
@@ -1308,19 +1659,23 @@ 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)
del_cron_open $2
;;
del_cron_open_in)
del_cron_open_in $2
;;
get_cron_open)
get_cron_open $2
;;
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)
@@ -1346,5 +1701,7 @@ case "$1" in
;;
esac
# Elimina eventuali file temporane utilizzati per la gestione dei cron
rm "$TMP_CRON_FILE" 2> /dev/null
rm "$TMP_CRON_FILE-2" 2> /dev/null