Spostato diverse parti dello script in file di inclusione, completato driver di esempio
This commit is contained in:
491
include/cron.include.sh
Normal file
491
include/cron.include.sh
Normal file
@@ -0,0 +1,491 @@
|
||||
#!/bin/bash
|
||||
|
||||
#
|
||||
# Elimina una tipoliga di schedulazione dal crontab dell'utente
|
||||
# $1 tipologia del crontab
|
||||
# $2 argomento della tipologia
|
||||
#
|
||||
function cron_del {
|
||||
|
||||
local CRON_TYPE=$1
|
||||
local CRON_ARG=$2
|
||||
|
||||
if [ -z "$CRON_TYPE" ]; then
|
||||
echo "Cron type is empty" >&2
|
||||
log_write "Cron type is empty"
|
||||
return 1
|
||||
fi
|
||||
|
||||
$CRONTAB -l > "$TMP_CRON_FILE"
|
||||
local START=`$GREP -n "# START cron $CRON_TYPE $CRON_ARG" "$TMP_CRON_FILE"| $CUT -d : -f 1`
|
||||
local END=`$GREP -n "# END cron $CRON_TYPE $CRON_ARG" "$TMP_CRON_FILE"| $CUT -d : -f 1`
|
||||
local re='^[0-9]+$'
|
||||
|
||||
if ! [[ "$START" =~ $re ]] && ! [[ "$END" =~ $re ]] ; then
|
||||
echo "$1 $2 cron is not present" >&2
|
||||
return
|
||||
fi
|
||||
if ! [[ $START =~ $re ]] ; then
|
||||
echo "Cron start don't find" >&2
|
||||
log_write "Cron start don't find"
|
||||
return 1
|
||||
fi
|
||||
if ! [[ $END =~ $re ]] ; then
|
||||
echo "Cron end cron don't find" >&2
|
||||
log_write "Cron end cron don't find"
|
||||
return 1
|
||||
fi
|
||||
if [ "$START" -gt "$END" ]; then
|
||||
echo "Wrong position for start and end in cron" >&2
|
||||
log_write "Wrong position for start and end in cron"
|
||||
return 1
|
||||
fi
|
||||
|
||||
|
||||
$SED "$START,${END}d" "$TMP_CRON_FILE" | $SED '$!N; /^\(.*\)\n\1$/!P; D' | $CRONTAB -
|
||||
#$CRONTAB "$TMP_CRON_FILE"
|
||||
rm "$TMP_CRON_FILE"
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Aggiunge una schedulazione nel crontab dell'utente
|
||||
# $1 tipologia del crontab
|
||||
# $2 minuto
|
||||
# $3 ora
|
||||
# $4 giorno del mese
|
||||
# $5 mese
|
||||
# $6 giorno della settimana
|
||||
# $7 argomento della tipologia
|
||||
# $8 secondo argomento della tipologia
|
||||
#
|
||||
function cron_add {
|
||||
|
||||
local CRON_TYPE=$1
|
||||
local CRON_M=$2
|
||||
local CRON_H=$3
|
||||
local CRON_DOM=$4
|
||||
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"
|
||||
|
||||
if [ -z "$CRON_TYPE" ]; then
|
||||
echo "Cron type is empty" >&2
|
||||
log_write "Cron type is empty"
|
||||
return 1
|
||||
fi
|
||||
|
||||
$CRONTAB -l > "$TMP_CRON_FILE"
|
||||
local START=`$GREP -n "# START cron $CRON_TYPE $CRON_ARG" "$TMP_CRON_FILE"| $CUT -d : -f 1`
|
||||
local END=`$GREP -n "# END cron $CRON_TYPE $CRON_ARG" "$TMP_CRON_FILE"| $CUT -d : -f 1`
|
||||
local re='^[0-9]+$'
|
||||
|
||||
local NEW_CRON=0
|
||||
local PREVIUS_CONTENT=""
|
||||
|
||||
if ! [[ $START =~ $re ]] && ! [[ $END =~ $re ]] ; then
|
||||
NEW_CRON=1
|
||||
else
|
||||
if ! [[ $START =~ $re ]] ; then
|
||||
echo "Cron start don't find" >&2
|
||||
log_write "Cron start don't find"
|
||||
return 1
|
||||
fi
|
||||
if ! [[ $END =~ $re ]] ; then
|
||||
echo "Cron end cron don't find" >&2
|
||||
log_write "Cron end cron don't find"
|
||||
return 1
|
||||
fi
|
||||
START=$(($START + 1))
|
||||
END=$(($END - 1))
|
||||
|
||||
if [ "$START" -gt "$END" ]; then
|
||||
echo "Wrong position for start and end in cron" >&2
|
||||
log_write "Wrong position for start and end in cron"
|
||||
return 1
|
||||
fi
|
||||
|
||||
PREVIOUS_CONTENT=`$SED -n "$START,${END}p" "$TMP_CRON_FILE"`
|
||||
|
||||
fi
|
||||
|
||||
case "$CRON_TYPE" in
|
||||
|
||||
init)
|
||||
CRON_M="@reboot"
|
||||
CRON_H=""
|
||||
CRON_DOM=""
|
||||
CRON_MON=""
|
||||
CRON_DOW=""
|
||||
CRON_COMMAND="$PATH_SCRIPT init"
|
||||
;;
|
||||
|
||||
start_socket_server)
|
||||
CRON_M="@reboot"
|
||||
CRON_H=""
|
||||
CRON_DOM=""
|
||||
CRON_MON=""
|
||||
CRON_DOW=""
|
||||
CRON_COMMAND="$PATH_SCRIPT start_socket_server force"
|
||||
;;
|
||||
|
||||
check_rain_online)
|
||||
CRON_M="*/3"
|
||||
CRON_H="*"
|
||||
CRON_DOM="*"
|
||||
CRON_MON="*"
|
||||
CRON_DOW="*"
|
||||
CRON_COMMAND="$PATH_SCRIPT check_rain_online 2> /tmp/check_rain_online.err"
|
||||
;;
|
||||
|
||||
check_rain_sensor)
|
||||
CRON_M="*"
|
||||
CRON_H="*"
|
||||
CRON_DOM="*"
|
||||
CRON_MON="*"
|
||||
CRON_DOW="*"
|
||||
CRON_COMMAND="$PATH_SCRIPT check_rain_sensor 2> /tmp/check_rain_sensor.err"
|
||||
;;
|
||||
|
||||
close_all_for_rain)
|
||||
CRON_M="*/5"
|
||||
CRON_H="*"
|
||||
CRON_DOM="*"
|
||||
CRON_MON="*"
|
||||
CRON_DOW="*"
|
||||
CRON_COMMAND="$PATH_SCRIPT close_all_for_rain 2> /tmp/close_all_for_rain.err 1> /dev/null"
|
||||
;;
|
||||
|
||||
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
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "Wrong cron type: $CRON_TYPE"
|
||||
log_write "Wrong cron type: $CRON_TYPE"
|
||||
;;
|
||||
|
||||
esac
|
||||
|
||||
if [ "$NEW_CRON" -eq "0" ]; then
|
||||
START=$(($START - 1))
|
||||
END=$(($END + 1))
|
||||
$SED "$START,${END}d" "$TMP_CRON_FILE" > "$TMP_CRON_FILE2"
|
||||
else
|
||||
cat "$TMP_CRON_FILE" > "$TMP_CRON_FILE2"
|
||||
fi
|
||||
|
||||
if [ "$NEW_CRON" -eq "1" ]; then
|
||||
echo "" >> "$TMP_CRON_FILE2"
|
||||
fi
|
||||
echo "# START cron $CRON_TYPE $CRON_ARG" >> "$TMP_CRON_FILE2"
|
||||
if [ "$NEW_CRON" -eq "0" ]; then
|
||||
echo "$PREVIOUS_CONTENT" >> "$TMP_CRON_FILE2"
|
||||
fi
|
||||
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"
|
||||
rm "$TMP_CRON_FILE" "$TMP_CRON_FILE2"
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Legge una tipoliga di schedulazione dal crontab dell'utente
|
||||
# $1 tipologia del crontab
|
||||
# $2 argomento della tipologia
|
||||
#
|
||||
function cron_get {
|
||||
|
||||
local CRON_TYPE=$1
|
||||
local CRON_ARG=$2
|
||||
|
||||
if [ -z "$CRON_TYPE" ]; then
|
||||
echo "Cron type is empty" >&2
|
||||
log_write "Cron type is empty"
|
||||
return 1
|
||||
fi
|
||||
|
||||
$CRONTAB -l > "$TMP_CRON_FILE"
|
||||
local START=`$GREP -n "# START cron $CRON_TYPE $CRON_ARG" "$TMP_CRON_FILE"| $CUT -d : -f 1`
|
||||
local END=`$GREP -n "# END cron $CRON_TYPE $CRON_ARG" "$TMP_CRON_FILE"| $CUT -d : -f 1`
|
||||
local re='^[0-9]+$'
|
||||
|
||||
local PREVIUS_CONTENT=""
|
||||
|
||||
if ! [[ $START =~ $re ]] && ! [[ $END =~ $re ]] ; then
|
||||
PREVIUS_CONTENT=""
|
||||
else
|
||||
if ! [[ $START =~ $re ]] ; then
|
||||
echo "Cron start don't find" >&2
|
||||
log_write "Cron start don't find"
|
||||
return 1
|
||||
fi
|
||||
if ! [[ $END =~ $re ]] ; then
|
||||
echo "Cron end cron don't find" >&2
|
||||
log_write "Cron end cron don't find"
|
||||
return 1
|
||||
fi
|
||||
START=$(($START + 1))
|
||||
END=$(($END - 1))
|
||||
|
||||
if [ "$START" -gt "$END" ]; then
|
||||
echo "Wrong position for start and end in cron" >&2
|
||||
log_write "Wrong position for start and end in cron"
|
||||
return 1
|
||||
fi
|
||||
|
||||
PREVIOUS_CONTENT=`$SED -n "$START,${END}p" "$TMP_CRON_FILE"`
|
||||
fi
|
||||
|
||||
echo "$PREVIOUS_CONTENT"
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Imposta il cron di inizializzazione della centralina
|
||||
#
|
||||
function set_cron_init {
|
||||
|
||||
cron_del "init" 2> /dev/null
|
||||
cron_add "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
|
||||
cron_add "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"
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Aggiunge una schedulazione cron per aprire una elettrovalvola
|
||||
# $1 alias elettrovalvola
|
||||
# $2 minuto cron
|
||||
# $3 ora cron
|
||||
# $4 giorno del mese cron
|
||||
# $5 mese cron
|
||||
# $6 giorno della settimana cron
|
||||
# $7 disabled
|
||||
#
|
||||
function add_cron_open {
|
||||
|
||||
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_add "open" "$2" "$3" "$4" "$5" "$6" "$1" "$7"
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Cancella tutte le schedulazioni cron per aprire una elettrovalvola
|
||||
# $1 alias elettrovalvola
|
||||
#
|
||||
function del_cron_open {
|
||||
|
||||
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" $1
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Legge tutte le schedulazioni cron per aprire una elettrovalvola
|
||||
# $1 alias elettrovalvola
|
||||
#
|
||||
function get_cron_open {
|
||||
|
||||
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_get "open" $1
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# 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
|
||||
#
|
||||
function get_cron_close {
|
||||
|
||||
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_get "close" $1
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Aggiunge una schedulazione cron per chiudere una elettrovalvola
|
||||
# $1 alias elettrovalvola
|
||||
# $2 minuto cron
|
||||
# $3 ora cron
|
||||
# $4 giorno del mese cron
|
||||
# $5 mese cron
|
||||
# $6 giorno della settimana cron
|
||||
# $7 disabled
|
||||
#
|
||||
function add_cron_close {
|
||||
|
||||
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_add "close" "$2" "$3" "$4" "$5" "$6" "$1" "$7"
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Cancella tutte le schedulazioni cron per chiudere una elettrovalvola
|
||||
# $1 alias elettrovalvola
|
||||
#
|
||||
function del_cron_close {
|
||||
|
||||
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 "close" $1
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
declare -A drv_avalible
|
||||
#declare -A drv_avalible
|
||||
declare -a list_drv
|
||||
|
||||
function setup_drv {
|
||||
|
||||
declare -a list_drv
|
||||
#declare -a list_drv
|
||||
list_drv=()
|
||||
|
||||
# Inizializza i driver per le elettrovalvole
|
||||
@@ -18,16 +19,25 @@ function setup_drv {
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
# Inizializza i driver per gli altri gpio
|
||||
for gpio in "$SUPPLY_GPIO_1" "$SUPPLY_GPIO_2" "$RAIN_GPIO"
|
||||
do
|
||||
if [[ "$gpio" == drv:* ]]; then
|
||||
local drv=`echo $gpio | $CUT -d':' -f2,2`
|
||||
if [[ ! " ${list_drv[@]} " =~ " ${drv} " ]]; then
|
||||
list_drv+=("$drv")
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
local file_drv
|
||||
for drv in "${list_drv[@]}"
|
||||
do
|
||||
for callback in init releopenclose status
|
||||
for callback in init rele supply rainsensor
|
||||
do
|
||||
file_drv="$DIR_SCRIPT/drv/$drv/$callback.include.sh"
|
||||
if [ -f "$file_drv" ]; then
|
||||
drv_avalible[$drv]="${drv_avalible[$drv]}#$callback#"
|
||||
#drv_avalible[$drv]="${drv_avalible[$drv]}#$callback#"
|
||||
#echo ${drv_avalible[$drv]}
|
||||
. "$file_drv"
|
||||
fi
|
||||
@@ -35,3 +45,249 @@ function setup_drv {
|
||||
done
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Restituisce in output il nome del driver callback function da richiamare per una specifica funzione
|
||||
#
|
||||
# $1 nome della funzione per il quale si vuore recuperare la callback
|
||||
# $2 idetificativo del driver
|
||||
function get_driver_callback {
|
||||
local fnc="$1"
|
||||
local idx="$2"
|
||||
local ret=""
|
||||
|
||||
if [[ "$idx" == drv:* ]]; then
|
||||
local drv=`echo $idx | $CUT -d':' -f2,2`
|
||||
if [[ ! " ${list_drv[@]} " =~ " ${drv} " ]]; then
|
||||
ret="drvnotfound"
|
||||
else
|
||||
ret="drv_${drv}_${fnc}"
|
||||
fi
|
||||
fi
|
||||
echo "$ret"
|
||||
}
|
||||
|
||||
#
|
||||
# Inizializza un relè e lo porta nello stato aperto
|
||||
#
|
||||
# $1 identificativo relè da inizializzare
|
||||
#
|
||||
function drv_rele_init {
|
||||
local idx="$1"
|
||||
local fnc=`get_driver_callback "rele_init" "$idx"`
|
||||
|
||||
# Nessun driver definito, esegue la chiusura del relè tramite gpio del raspberry
|
||||
if [ -z "$fnc" ]; then
|
||||
$GPIO -g write $idx RELE_GPIO_OPEN # chiude l'alimentazione all'elettrovalvole
|
||||
$GPIO -g mode $idx out # setta il gpio nella modalita di scrittura
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx"
|
||||
message_write "warning" "Driver not found: $idx"
|
||||
else
|
||||
$fnc "$idx"
|
||||
fi
|
||||
}
|
||||
|
||||
#
|
||||
# Chiude un relè
|
||||
#
|
||||
# $1 identificativo relè da chiudere
|
||||
#
|
||||
function drv_rele_close {
|
||||
local idx="$1"
|
||||
local fnc=`get_driver_callback "rele_close" "$idx"`
|
||||
|
||||
# Nessun driver definito, esegue la chiusura del relè tramite gpio del raspberry
|
||||
if [ -z "$fnc" ]; then
|
||||
$GPIO -g write $idx $RELE_GPIO_CLOSE
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx"
|
||||
message_write "warning" "Driver not found: $idx"
|
||||
else
|
||||
$fnc "$idx"
|
||||
fi
|
||||
}
|
||||
|
||||
#
|
||||
# Apre un relè
|
||||
#
|
||||
# $1 identificativo relè da aprire
|
||||
#
|
||||
function drv_rele_open {
|
||||
local idx="$1"
|
||||
local fnc=`get_driver_callback "rele_open" "$idx"`
|
||||
|
||||
# Nessun driver definito, esegue la chiusura del relè tramite gpio del raspberry
|
||||
if [ -z "$fnc" ]; then
|
||||
$GPIO -g write $idx $RELE_GPIO_OPEN
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx"
|
||||
message_write "warning" "Driver not found: $idx"
|
||||
else
|
||||
$fnc "$idx"
|
||||
fi
|
||||
}
|
||||
|
||||
#
|
||||
# Inizializza i rele che gestiscono l'alimentazione per le valvole bistabili
|
||||
#
|
||||
# $1 identificativo relè 1
|
||||
# $2 identificativo relè 2
|
||||
#
|
||||
function drv_supply_bistable_init {
|
||||
local idx1=$1
|
||||
local idx2=$2
|
||||
local fnc1=`get_driver_callback "supply_bistable_init" "$idx1"`
|
||||
local fnc2=`get_driver_callback "supply_bistable_init" "$idx2"`
|
||||
|
||||
# Nessun driver definito, esegue l'operazione tramite gpio del raspberry
|
||||
if [ -z "$fnc1" ]; then
|
||||
$GPIO -g write $idx1 0
|
||||
$GPIO -g mode $idx1 out
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc1" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx1"
|
||||
message_write "warning" "Driver not found: $idx1"
|
||||
return
|
||||
else
|
||||
$fnc1 "$idx1"
|
||||
fi
|
||||
|
||||
# Nessun driver definito, esegue l'operazione tramite gpio del raspberry
|
||||
if [ -z "$fnc2" ]; then
|
||||
$GPIO -g write $idx2 0
|
||||
$GPIO -g mode $idx2 out
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc2" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx2"
|
||||
message_write "warning" "Driver not found: $idx2"
|
||||
else
|
||||
$fnc2 "$idx2"
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Imposta la tensine positiva per le elettrovalvole bistabili
|
||||
#
|
||||
# $1 identificativo rele 1
|
||||
# $2 identificativo rele 2
|
||||
#
|
||||
function drv_supply_positive {
|
||||
local idx1=$1
|
||||
local idx2=$2
|
||||
local fnc1=`get_driver_callback "supply_positive" "$idx1"`
|
||||
local fnc2=`get_driver_callback "supply_positive" "$idx2"`
|
||||
|
||||
# Nessun driver definito, esegue l'operazione tramite gpio del raspberry
|
||||
if [ -z "$fnc1" ]; then
|
||||
$GPIO -g write $idx1 $SUPPLY_GPIO_POS
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc1" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx1"
|
||||
message_write "warning" "Driver not found: $idx1"
|
||||
return
|
||||
else
|
||||
$fnc1 "$idx1"
|
||||
fi
|
||||
|
||||
# Nessun driver definito, esegue l'operazione tramite gpio del raspberry
|
||||
if [ -z "$fnc2" ]; then
|
||||
$GPIO -g write $idx2 $SUPPLY_GPIO_POS
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc2" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx2"
|
||||
message_write "warning" "Driver not found: $idx2"
|
||||
else
|
||||
$fnc2 "$idx2"
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Imposta la tensine neagativa per le elettrovalvole bistabili
|
||||
#
|
||||
# $1 identificativo rele 1
|
||||
# $2 identificativo rele 2
|
||||
#
|
||||
function drv_supply_negative {
|
||||
local idx1=$1
|
||||
local idx2=$2
|
||||
local fnc1=`get_driver_callback "supply_negative" "$idx1"`
|
||||
local fnc2=`get_driver_callback "supply_negative" "$idx2"`
|
||||
|
||||
# Nessun driver definito, esegue l'operazione tramite gpio del raspberry
|
||||
if [ -z "$fnc1" ]; then
|
||||
$GPIO -g write $idx1 $SUPPLY_GPIO_NEG
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc1" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx1"
|
||||
message_write "warning" "Driver not found: $idx1"
|
||||
return
|
||||
else
|
||||
$fnc1 "$idx1"
|
||||
fi
|
||||
|
||||
# Nessun driver definito, esegue l'operazione tramite gpio del raspberry
|
||||
if [ -z "$fnc2" ]; then
|
||||
$GPIO -g write $idx2 $SUPPLY_GPIO_NEG
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc2" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx2"
|
||||
message_write "warning" "Driver not found: $idx2"
|
||||
else
|
||||
$fnc2 "$idx2"
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Inizializza il sensore della pioggia
|
||||
#
|
||||
# $1 identificativo gpio sensore pioggia
|
||||
#
|
||||
function drv_rain_sensor_init {
|
||||
local idx="$1"
|
||||
local fnc=`get_driver_callback "rain_sensor_init" "$idx"`
|
||||
local vret=""
|
||||
|
||||
# Nessun driver definito, esegue la lettura del sensore tramite gpio del raspberry
|
||||
if [ -z "$fnc" ]; then
|
||||
$GPIO -g mode $idx in
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx"
|
||||
message_write "warning" "Driver not found: $idx"
|
||||
else
|
||||
$fnc "$idx"
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Legge lo stato del sensore della pioggia
|
||||
#
|
||||
# $1 identificativo gpio sensore pioggia
|
||||
#
|
||||
function drv_rain_sensor_get {
|
||||
local idx="$1"
|
||||
local fnc=`get_driver_callback "rain_sensor_get" "$idx"`
|
||||
local vret=""
|
||||
|
||||
# Nessun driver definito, esegue la lettura del sensore tramite gpio del raspberry
|
||||
if [ -z "$fnc" ]; then
|
||||
val=`$GPIO -g read $idx`
|
||||
# Il driver definito non è stato trovato
|
||||
elif [ "$fnc" == "drvnotfound" ]; then
|
||||
log_write "Driver not found: $idx"
|
||||
message_write "warning" "Driver not found: $idx"
|
||||
else
|
||||
vret=`$fnc "$idx"`
|
||||
fi
|
||||
|
||||
echo "$vret"
|
||||
|
||||
}
|
||||
|
||||
211
include/socket.include.sh
Normal file
211
include/socket.include.sh
Normal file
@@ -0,0 +1,211 @@
|
||||
#!/bin/bash
|
||||
|
||||
#
|
||||
# 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
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# Ferma il socket server
|
||||
#
|
||||
function stop_socket_server {
|
||||
|
||||
if [ ! -f "$TCPSERVER_PID_FILE" ]; then
|
||||
echo "Daemon is not running"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log_write "stop socket server"
|
||||
|
||||
kill -9 $(list_descendants `cat "$TCPSERVER_PID_FILE"`) 2> /dev/null
|
||||
kill -9 `cat "$TCPSERVER_PID_FILE"` 2> /dev/null
|
||||
rm -f "$TCPSERVER_PID_FILE"
|
||||
|
||||
}
|
||||
|
||||
#
|
||||
# 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)
|
||||
arg2=$(echo "$line " | $CUT -d ' ' -f2)
|
||||
arg3=$(echo "$line " | $CUT -d ' ' -f3)
|
||||
arg4=$(echo "$line " | $CUT -d ' ' -f4)
|
||||
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 $arg8"
|
||||
|
||||
reset_messages &> /dev/null
|
||||
|
||||
case "$arg1" in
|
||||
status)
|
||||
json_status $arg2 $arg3 $arg4 $arg5 $arg6 $arg7
|
||||
;;
|
||||
|
||||
open)
|
||||
if [ "empty$arg2" == "empty" ]; then
|
||||
json_error 0 "Alias solenoid not specified"
|
||||
else
|
||||
ev_open $arg2 $arg3 &> /dev/null
|
||||
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 "get_cron_open_in"
|
||||
fi
|
||||
;;
|
||||
|
||||
set_general_cron)
|
||||
local vret=""
|
||||
for i in $arg2 $arg3 $arg4 $arg5 $arg6 $arg7
|
||||
do
|
||||
if [ $i = "set_cron_init" ]; then
|
||||
vret="$(vret)`set_cron_init`"
|
||||
elif [ $i = "set_cron_start_socket_server" ]; then
|
||||
vret="$(vret)`set_cron_start_socket_server`"
|
||||
elif [ $i = "set_cron_check_rain_sensor" ]; then
|
||||
vret="$(vret)`set_cron_check_rain_sensor`"
|
||||
elif [ $i = "set_cron_check_rain_online" ]; then
|
||||
vret="$(vret)`set_cron_check_rain_online`"
|
||||
elif [ $i = "set_cron_close_all_for_rain" ]; then
|
||||
vret="$(vret)`set_cron_close_all_for_rain`"
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ ! -z $vret ]]; then
|
||||
json_error 0 "Cron set failed"
|
||||
log_write "Cron set failed: $vret"
|
||||
else
|
||||
message_write "success" "Cron set successfull"
|
||||
json_status
|
||||
fi
|
||||
|
||||
;;
|
||||
|
||||
del_cron_open)
|
||||
local vret=""
|
||||
|
||||
vret=`del_cron_open $arg2`
|
||||
|
||||
if [[ ! -z $vret ]]; then
|
||||
json_error 0 "Cron set failed"
|
||||
log_write "Cron del failed: $vret"
|
||||
else
|
||||
message_write "success" "Cron set successfull"
|
||||
json_status
|
||||
fi
|
||||
|
||||
;;
|
||||
|
||||
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=""
|
||||
|
||||
vret=`del_cron_close $arg2`
|
||||
|
||||
if [[ ! -z $vret ]]; then
|
||||
json_error 0 "Cron set failed"
|
||||
log_write "Cron set failed: $vret"
|
||||
else
|
||||
message_write "success" "Cron set successfull"
|
||||
json_status
|
||||
fi
|
||||
|
||||
;;
|
||||
|
||||
add_cron_open)
|
||||
local vret=""
|
||||
|
||||
vret=`add_cron_open "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" $arg8`
|
||||
|
||||
if [[ ! -z $vret ]]; then
|
||||
json_error 0 "Cron set failed"
|
||||
log_write "Cron set failed: $vret"
|
||||
else
|
||||
message_write "success" "Cron set successfull"
|
||||
json_status
|
||||
fi
|
||||
|
||||
;;
|
||||
|
||||
add_cron_close)
|
||||
local vret=""
|
||||
|
||||
vret=`add_cron_close "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" $arg8`
|
||||
|
||||
if [[ ! -z $vret ]]; then
|
||||
json_error 0 "Cron set failed"
|
||||
log_write "Cron set failed: $vret"
|
||||
else
|
||||
message_write "success" "Cron set successfull"
|
||||
json_status
|
||||
fi
|
||||
|
||||
;;
|
||||
|
||||
*)
|
||||
json_error 0 "invalid command"
|
||||
;;
|
||||
|
||||
esac
|
||||
|
||||
reset_messages &> /dev/null
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user