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-11 17:10:48 +00:00
|
|
|
ls -I "*.sign" $registros | wc -l
|
2022-03-03 22:13:00 +00:00
|
|
|
else
|
2022-03-11 17:10:48 +00:00
|
|
|
ls -I "*.sign" $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-14 19:32:57 +00:00
|
|
|
test -f "$_file.sig" || ssh-keygen -Y sign -f "$private_key" -n file "$_file" 2>&1 >/dev/null
|
2022-03-11 17:10:48 +00:00
|
|
|
|
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
|
|
|
|
arduinos=( 0x01 0x02 )
|
|
|
|
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-03-11 00:57:16 +00:00
|
|
|
lecturas="$lecturas id:${lectura_arduino[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"
|
|
|
|
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() {
|
|
|
|
|
|
|
|
if [ "$1" = "iniciar" ]; then
|
|
|
|
|
|
|
|
if [ -z $intervalo ]; then
|
|
|
|
echo "Asegurese de haber establecido 'intervalo' de regitro y envio de datos."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ -z $nombre ]; then
|
|
|
|
echo "Asegurese de haber establecido el 'nombre' de dispositivo."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|