Spostato diverse parti dello script in file di inclusione, completato driver di esempio

This commit is contained in:
lejubila
2017-08-15 12:35:28 +02:00
parent 426ccab0d2
commit 317781e217
8 changed files with 1074 additions and 736 deletions

View File

@@ -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"
}