2022-03-03 22:13:00 +00:00
|
|
|
#!/bin/bash
|
2022-03-05 20:59:15 +00:00
|
|
|
#set -x
|
2022-03-03 22:13:00 +00:00
|
|
|
get_stack () {
|
|
|
|
if [[ ! -z $1 && $1 == "wc" ]];then
|
2022-03-17 19:26:50 +00:00
|
|
|
ls -I "*.sig" $registros | wc -l
|
2022-03-03 22:13:00 +00:00
|
|
|
else
|
2022-03-17 19:26:50 +00:00
|
|
|
ls -I "*.sig" $registros | tail -1
|
2022-03-03 22:13:00 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-03-11 17:10:48 +00:00
|
|
|
# Generar una llave privada ECDSA si no existe
|
|
|
|
generate_private_key () {
|
|
|
|
test -f "$private_key" && return 1
|
|
|
|
|
2022-03-14 19:32:57 +00:00
|
|
|
ssh-keygen -t ecdsa -f "$private_key" -N "" -C "$@"
|
2022-03-11 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Firmar el archivo usando la llave privada.
|
|
|
|
#
|
|
|
|
# Uso: sign_file archivo.json
|
2022-03-14 19:32:57 +00:00
|
|
|
# Devuelve: La firma
|
2022-03-11 17:10:48 +00:00
|
|
|
sign_file () {
|
|
|
|
local _file="$1"
|
|
|
|
|
|
|
|
test ! -f "$_file" && return 1
|
2022-03-17 22:39:06 +00:00
|
|
|
test -f "$_file.sig" || ssh-keygen -Y sign -f "$private_key" -n file "$_file" >/dev/null 2>&1
|
2022-03-14 19:32:57 +00:00
|
|
|
cat "${_file}.sig" | grep -v SIGNATURE | tr -d "\n"
|
2022-03-11 17:10:48 +00:00
|
|
|
}
|
2022-03-11 15:31:18 +00:00
|
|
|
|
|
|
|
|
2022-03-03 22:13:00 +00:00
|
|
|
funcion_datos_simulados() {
|
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
lecturas="id:dummy_plug-01 tp:temp vl:$(( $RANDOM % 20 + 15 )) un:C er:99 AD_SENSOR tp:hum vl:$(( $RANDOM % 50 + 35 )) un:percent er:99 AD_ARDUINO id:dummy_plug-02 tp:temp vl:$(( $RANDOM % 20 + 15 )) un:C er:99 AD_SENSOR tp:hum vl:$(( $RANDOM % 50 + 35 )) un:percent er:99 AD_ARDUINO id:dummy_plug-03 tp:temp vl:$(( $RANDOM % 20 + 15 )) un:C er:99 AD_SENSOR tp:hum vl:$(( $RANDOM % 50 + 35 )) un:percent er:99 AD_ARDUINO id:dummy_plug-04 tp:temp vl:$(( $RANDOM % 20 + 15 )) un:C er:99 AD_SENSOR tp:hum vl:$(( $RANDOM % 50 + 35 )) un:percent er:99 END"
|
|
|
|
|
2022-03-03 22:13:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:02:27 +00:00
|
|
|
funcion_datos_sensores() {
|
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
peticiones=1
|
2022-04-05 20:56:40 +00:00
|
|
|
arduinos=( 1 2 )
|
2022-03-11 00:57:16 +00:00
|
|
|
for arduino_id in ${arduinos[@]};do
|
|
|
|
lectura_arduino=($(timeout 3 $arduinos_py $arduino_id))
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
let "bloques = (${#lectura_arduino[@]} - 1) / 4"
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-04-05 20:56:40 +00:00
|
|
|
#lecturas="$lecturas id:${lectura_arduino[0]}"
|
|
|
|
lecturas="$lecturas id:${arduino_id[0]}"
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
indice=1
|
|
|
|
vueltas=1
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
while [ $vueltas -le $bloques ]; do
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
lecturas="$lecturas tp:${lectura_arduino[$indice]} vl:${lectura_arduino[$(expr $indice + 1 )]} un:${lectura_arduino[$(expr $indice + 2 )]} er:${lectura_arduino[$(expr $indice + 3 )]}"
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
if [ $vueltas -lt $bloques ]; then
|
|
|
|
lecturas="$lecturas AD_SENSOR"
|
|
|
|
fi
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
let "indice = $indice + 4 "
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
((vueltas++))
|
|
|
|
done
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
if [ $peticiones -lt ${#arduinos[@]} ];then
|
|
|
|
lecturas="$lecturas AD_ARDUINO"
|
|
|
|
fi
|
|
|
|
((peticiones++))
|
2022-03-08 19:02:27 +00:00
|
|
|
|
2022-03-11 00:57:16 +00:00
|
|
|
|
|
|
|
done
|
|
|
|
lecturas="$lecturas END"
|
|
|
|
}
|
2022-03-08 19:02:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-05 20:09:45 +00:00
|
|
|
funcion_configurar() {
|
|
|
|
local comando=$1
|
|
|
|
local parametro=$2
|
|
|
|
local parametros=$*
|
2022-03-05 20:59:15 +00:00
|
|
|
local url_regex='(https?|HTTPS?)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
|
|
|
|
local nuevo_modo='(test|sensores)'
|
|
|
|
|
|
|
|
case $comando in
|
2022-03-05 20:09:45 +00:00
|
|
|
nombre)
|
|
|
|
while :;do
|
2022-03-05 20:59:15 +00:00
|
|
|
nuevo_nombre=$(echo $parametros | sed 's/nombre //g' | sed 's/ /_/g')
|
2022-03-05 20:09:45 +00:00
|
|
|
|
2022-03-05 21:40:28 +00:00
|
|
|
if [[ "$nuevo_nombre" =~ ^[a-zA-Z0-9_]+$ ]];then
|
2022-03-05 20:59:15 +00:00
|
|
|
sed -i 's/nombre='$nombre'/nombre='$nuevo_nombre'/' $config
|
|
|
|
echo "nombre: $nuevo_nombre"
|
2022-03-05 20:09:45 +00:00
|
|
|
break
|
|
|
|
else
|
|
|
|
echo "El nombre solo puede contener carateres alfanumericos."
|
|
|
|
read -p "nombre: " parametro
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
|
|
|
|
servidor)
|
|
|
|
while :;do
|
|
|
|
if [[ $parametro =~ $url_regex ]]; then
|
2022-03-05 20:59:15 +00:00
|
|
|
sed -i 's}servidor='$servidor'}servidor='"$parametro"'}' $config
|
2022-03-05 20:09:45 +00:00
|
|
|
echo "Servidor $parametro"
|
|
|
|
break
|
|
|
|
else
|
|
|
|
echo "Intruzca una url válida."
|
|
|
|
read -p "url: " parametro
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
|
|
|
|
intervalo)
|
|
|
|
while :;do
|
|
|
|
if [[ $parametro -ge 1 && $parametro -le 60 ]]; then
|
|
|
|
sed -i 's/intervalo=[0-9]*/intervalo='$parametro'/' $config
|
|
|
|
echo "Intervalo seteado en $parametro"
|
2022-03-22 17:25:35 +00:00
|
|
|
echo "Para aplicar los cambios ejecute
|
|
|
|
sudo nodemecu captura dentener
|
|
|
|
sudo nodemecu captura iniciar"
|
2022-03-05 20:09:45 +00:00
|
|
|
break
|
|
|
|
else
|
|
|
|
echo "Establezca un valor entre 1 y 60."
|
|
|
|
read -p "Intervalo: " parametro
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
;;
|
2022-03-05 20:59:15 +00:00
|
|
|
modo)
|
|
|
|
while :;do
|
|
|
|
if [[ $parametro =~ $nuevo_modo ]]; then
|
|
|
|
sed -i 's/modo='$modo'/modo='$parametro'/' $config
|
|
|
|
break
|
|
|
|
else
|
|
|
|
echo "Los modos posibles son 'test' y 'sensores'"
|
|
|
|
read -p "modo: " parametro
|
2022-03-03 22:13:00 +00:00
|
|
|
|
2022-03-05 20:59:15 +00:00
|
|
|
fi
|
2022-03-03 22:13:00 +00:00
|
|
|
|
2022-03-05 20:59:15 +00:00
|
|
|
done
|
|
|
|
esac
|
|
|
|
}
|
2022-03-03 22:13:00 +00:00
|
|
|
|
2022-03-05 21:40:28 +00:00
|
|
|
|
|
|
|
funcion_captura() {
|
|
|
|
|
|
|
|
|
2022-03-22 17:25:35 +00:00
|
|
|
if [ "$1" = "iniciar" ]; then
|
2022-03-05 21:40:28 +00:00
|
|
|
|
|
|
|
echo "Iniciando la captura de datos en modo '$modo'"
|
|
|
|
echo ""
|
|
|
|
read -p "Desea continuar? Presione 'enter' para continuar o 'ctrl-c' para cancelar..."
|
|
|
|
echo "*/$intervalo * * * * $directorio_instalacion/generador_json" > /tmp/nodemecu.crontab
|
|
|
|
crontab -u root /tmp/nodemecu.crontab
|
|
|
|
echo ""
|
2022-03-11 19:48:45 +00:00
|
|
|
echo "Se tomarán datos cada $intervalo minutos."
|
2022-03-05 21:40:28 +00:00
|
|
|
echo ""
|
|
|
|
exit
|
|
|
|
elif [ "$1" = "detener" ]; then
|
|
|
|
crontab -u root -r
|
|
|
|
echo "Se detuvo la toma de datos."
|
|
|
|
echo ""
|
|
|
|
else
|
|
|
|
echo "Ingrese una orden válida: 'iniciar' o 'detener'."
|
|
|
|
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
|
2022-03-22 17:25:35 +00:00
|
|
|
### Validaciones
|
|
|
|
|
|
|
|
test_conf_nombre() {
|
|
|
|
|
|
|
|
if [ -z $nombre ]; then
|
|
|
|
echo "No se establecio un nombre para el dispositivo."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test_conf_servidor () {
|
|
|
|
|
|
|
|
if [ -z $servidor ];then
|
|
|
|
echo "No se establecio un servidor de entrega."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_conf_intervalo() {
|
|
|
|
|
|
|
|
if [ -z $intervalo ];then
|
|
|
|
echo "No se establecio un intervalo de captura."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_conf_key() {
|
|
|
|
|
|
|
|
if [ ! -f $private_key ];then
|
|
|
|
echo "No se creo juego de llaves."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
funcion_validador_json() {
|
|
|
|
|
|
|
|
validar="$@"
|
|
|
|
|
|
|
|
echo $validar | jsonlint-php > /dev/null 2>&1 || return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
funcion_verificar_internet() {
|
|
|
|
|
|
|
|
if host fsf.org > /dev/null 2>&1; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
funcion_espera() {
|
2022-03-22 17:25:35 +00:00
|
|
|
sleep 1m
|
|
|
|
#sleep ${intervalo:-1}m
|
2022-03-17 22:39:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
funcion_envio_registro() {
|
|
|
|
|
|
|
|
curl -s --show-error -w "~%{http_code}" \
|
|
|
|
-X POST -H "X-Signature: $(sign_file $registros/$file)" \
|
|
|
|
-H "Content-Type: application/json" -d @$registros/$file \
|
|
|
|
$servidor 2> $curl_err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-22 17:25:35 +00:00
|
|
|
funcion_monitor() {
|
|
|
|
local sb="\x1b["
|
|
|
|
local eb="\x1b[K\x1b[0m"
|
|
|
|
local st="\e["
|
|
|
|
local et="\e[0m"
|
|
|
|
|
|
|
|
while :;do
|
|
|
|
clear
|
|
|
|
echo -e "${sb}1;44;97m Nodemecu Monitor${eb}"
|
|
|
|
echo ""
|
|
|
|
echo -e "${st}1;94mNombre:${et} $nombre ${st}1;94mServidor:${et} $servidor ${st}1;94mIntervalo:${et} $intervalo"
|
|
|
|
echo ""
|
|
|
|
echo -e "${sb}1;44;97m Registros${eb}"
|
|
|
|
echo ""
|
|
|
|
echo -e "En cola: $(ls -I *.sig $registros| wc -l)"
|
|
|
|
echo -e "Enviados: $(ls -I *.sig $historicos | wc -l)"
|
|
|
|
echo -e "Registros corrupts: $(ls -I *.sig $registros_corruptos | wc -l)"
|
|
|
|
echo ""
|
|
|
|
echo -e "${sb}1;44;97m Ultimos mensajes${eb}"
|
|
|
|
echo ""
|
|
|
|
echo -e ${st}1m"Systemd unit 'nodemecu.service'${et}"
|
|
|
|
journalctl -n 3 -u nodemecu
|
|
|
|
echo ""
|
|
|
|
echo -e ${st}1m"Fichero '$log'${et}"
|
|
|
|
cat /var/log/nodemecu_errors.log | grep -v "Salida de curl" | grep -v '[*<>{}]' | tail -3
|
|
|
|
|
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
|
|
|
|
}
|
2022-03-17 22:39:06 +00:00
|
|
|
|