Add command line: sensor_status, sensor_status_all, sensor_status_set. Add api command: sensor_status_set.

This commit is contained in:
lejubila
2021-08-22 15:19:34 +02:00
parent 7242b1c2ab
commit c91128cbb4
5 changed files with 224 additions and 3 deletions

View File

@@ -1,3 +1,8 @@
# 0.6.4 - xx/xx/2021
- Add support for sensor mi flora
- Add command line: sensor_status, sensor_status_all, sensor_status_set
- Add api command: sensor_status_set
# 0.6.3 - 10/08/2021
- Add command last_rain_sensor_timestamp, last_rain_online_timestamp, reset_last_rain_sensor_timestamp, reset_last_rain_online_timestamp
- Add socket server api for reset_last_rain_sensor_timestamp, reset_last_rain_online_timestamp

View File

@@ -107,6 +107,8 @@ EV1_ALIAS="1" #
EV1_GPIO=17 # Physical 11 - wPi 0
#EV1_NORAIN=1 # Non interrompe l'irrigazione di questa zona in caso di pioggia
#EV1_MONOSTABLE=1 # L'elettrovalvola è monostabile
#EV1_SENSOR_ALIAS=Mi_Flora # Nome del sensore (definito in SENSORx_ALIAS) per stabilire l'umidità del terreno
#EV1_SENSOR_MOISTURE=50 # Percentule di umidità ottimale
EV2_ALIAS="2" #
EV2_GPIO=27 # Physical 13 - wPi 2
@@ -124,6 +126,16 @@ EV6_ALIAS="6" #
EV6_GPIO=24 # Physical 18 - wPi 5
# Numero di sensori
SENSOR_TOTAL=1
# Definizione sensori
SENSOR1_ALIAS=Mi_Flora
# Definisci il servizio online da utilizzare per il controllo delle condizioni meteo, puoi scegliere openweathermap oppure wunderground.
# Se non vuoi configurare nessun servizio imposta il vale "none"
WEATHER_SERVICE="openweathermap"

157
include/sensor.include.sh Normal file
View File

@@ -0,0 +1,157 @@
#
# Imposta lo stato di una elettrovalvola
# $1 numero del sensore
# $2 tipologia dello stato
# $3 stato da scrivere
#
function sensor_set_state {
echo "$3" > "$STATUS_DIR/sensor$1_$2"
}
#
# Legge lo stato di un sensore
# $1 numero del sensore
# $2 tipologia dello stato
#
function sensor_get_state {
if [ ! -f "$STATUS_DIR/sensor$1_$2" ]; then
sensor_set_state $1 $2 ""
fi
return `cat "$STATUS_DIR/sensor$1_$2"`
}
#
# Recupera il numero di un sensore in base all'alias
# $1 alias del sensore
#
function sensor_alias2number {
for i in $(seq $EV_TOTAL)
do
a=SENSOR"$i"_ALIAS
av=${!a}
if [ "$av" == "$1" ]; then
return $i
fi
done
log_write "general" "error" "ERROR sensor alias not found: $1"
message_write "warning" "Sensor alias not found"
mqtt_status
exit 1
}
#
# Verifica se un alias di un sensore esiste
# $1 alias dell'elettrovalvola
#
function sensor_alias_exists {
local vret='FALSE'
for i in $(seq $EV_TOTAL)
do
a=SENSOR"$i"_ALIAS
av=${!a}
if [ "$av" == "$1" ]; then
vret='TRUE'
fi
done
echo $vret
}
#
# Mostra lo stato di tutte le elettrovalvole
#
function sensor_status_all {
for i in $(seq $SENSOR_TOTAL)
do
a=SENSOR"$i"_ALIAS
av=${!a}
for t in $SENSOR_STATE_TYPE
do
sensor_get_state $i $t
echo -e "$av: $t $?"
done
done
}
#
# Mostra lo stato di un sensore
# $1 alias sensore
# $2 tipologia dello stato
#
function sensor_status {
sensor_alias2number $1
i=$?
if [ -z "$2" ]; then
for t in $SENSOR_STATE_TYPE
do
sensor_get_state $i $t
echo -e "$av: $t $?"
done
else
sensor_get_state $i $2
local state=$?
echo -e "$state"
return $state
fi
}
#
# Imposta lo stato di un sensore per alias
# $1 alias sensore
# $2 tipologia dello stato
# $3 stato da imopostare
#
function sensor_status_set {
sensor_alias2number $1
i=$?
sensor_set_state $i $2 $3
mqtt_status
}
#
# Stampa la lista degli alias dei sensori
#
function list_alias_sensor {
for i in $(seq $SENSOR_TOTAL)
do
local a=SENSOR"$i"_ALIAS
local al=${!a}
echo $al
done
}
#
# Stampa lo stato di tutti i sensori in formato json
#
function json_sensor_status_all {
local js=""
local js_item=""
local js_type=""
for i in $(seq $SENSOR_TOTAL)
do
a=SENSOR"$i"_ALIAS
av=${!a}
js_type=""
for t in $SENSOR_STATE_TYPE
do
sensor_get_state $i $t
js_type="$js_type \"$t\": \"$?\", "
done
js_type="${js_type::-2}"
js_item="$js_item \"$av\":{$js_type}, ";
done
if [[ ! -z $js_item ]]; then
js_item="${js_item::-2}"
fi
js="\"sensor\": {$js_item}"
echo $js
}

View File

@@ -252,6 +252,14 @@ function socket_server_command {
json_status
;;
sensor_status_set)
if [ "empty$arg2" == "empty" ]; then
json_error 0 "Alias sensor not specified"
else
sensor_status_set $arg2 $arg3 &> /dev/null
json_status
fi
;;
*)
json_error 0 "invalid command"

View File

@@ -684,8 +684,9 @@ function json_status {
fi
local json_cron_open_in="\"cron_open_in\":{$json_get_cron_open_in}"
local json_timestamp="\"timestamp\": $(date +%s)"
local json_sensor="$(json_sensor_status_all)"
json="{$json_version,$json_timestamp,$json_event,$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 $json_schedule}"
json="{$json_version,$json_timestamp,$json_event,$json,$json_sensor,$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 $json_schedule}"
echo "$json"
@@ -727,14 +728,25 @@ function show_usage {
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 "\n"
echo -e "\t$NAME_SCRIPT list_alias_sensor view list of aliases sensor"
echo -e "\t$NAME_SCRIPT sensor_status alias [type] show status sensor (type: $SENSOR_STATE_TYPE)"
echo -e "\t$NAME_SCRIPT sensor_status_set alias type value set status of sensor (type: $SENSOR_STATE_TYPE)"
echo -e "\t$NAME_SCRIPT sensor_status_all show status of all sensors"
echo -e "\n"
echo -e "\t$NAME_SCRIPT last_rain_sensor_timestamp show timestamp of last rain sensor"
echo -e "\t$NAME_SCRIPT last_rain_online_timestamp show timestamp of last rain online"
echo -e "\t$NAME_SCRIPT reset_last_rain_sensor_timestamp show timestamp of last rain sensor"
echo -e "\t$NAME_SCRIPT reset_last_rain_online_timestamp show timestamp of last rain online"
echo -e "\n"
echo -e "\t$NAME_SCRIPT json_status [get_cron|get_cron_open_in|get_schedule] show status in json format"
echo -e "\t$NAME_SCRIPT mqtt_status send status in json format to mqtt broker"
echo -e "\t$NAME_SCRIPT check_rain_online check rain from http://api.wunderground.com/"
echo -e "\n"
echo -e "\t$NAME_SCRIPT check_rain_online check rain from online api service"
echo -e "\t$NAME_SCRIPT check_rain_sensor check rain from hardware sensor"
echo -e "\n"
echo -e "\t$NAME_SCRIPT close_all_for_rain close all solenoid if it's raining"
echo -e "\t$NAME_SCRIPT close_all [force] close all solenoid"
echo -e "\n"
@@ -1008,7 +1020,7 @@ function debug2 {
VERSION=0
SUB_VERSION=6
RELEASE_VERSION=3
RELEASE_VERSION=4
DIR_SCRIPT=`dirname $0`
NAME_SCRIPT=${0##*/}
@@ -1034,6 +1046,7 @@ fi
. "$DIR_SCRIPT/include/cron.include.sh"
. "$DIR_SCRIPT/include/socket.include.sh"
. "$DIR_SCRIPT/include/rain.include.sh"
. "$DIR_SCRIPT/include/sensor.include.sh"
. "$DIR_SCRIPT/include/events.include.sh"
MESSAGE_INFO=""
@@ -1043,8 +1056,14 @@ MESSAGE_SUCCESS=""
CURRENT_EVENT=""
CURRENT_EVENT_ALIAS=""
SENSOR_STATE_TYPE="moisture temperature fertility illuminance"
PARENT_PID=0
if [ -z $SENSOR_TOTAL ]; then
SENSOR_TOTAL=0
fi
if [ -z $LOG_OUTPUT_DRV_FILE ]; then
LOG_OUTPUT_DRV_FILE="/dev/null"
fi
@@ -1115,6 +1134,26 @@ case "$1" in
ev_status_all
;;
list_alias_sensor)
list_alias_sensor
;;
sensor_status)
sensor_status $2 $3
;;
sensor_status_all)
sensor_status_all
;;
sensor_status_set)
sensor_status_set $2 $3 $4
;;
json_sensor_status_all)
json_sensor_status_all
;;
json_status)
json_status $2 $3 $4 $5 $6
;;