nodemecu/funciones

137 lines
3.7 KiB
Text
Raw Normal View History

2022-03-03 22:13:00 +00:00
#!/bin/bash
#set -x
2022-03-03 22:13:00 +00:00
get_stack () {
if [[ ! -z $1 && $1 == "wc" ]];then
ls /opt/nodemecu/stack | wc -l
else
ls /opt/nodemecu/stack | tail -1
fi
}
funcion_datos_simulados() {
lecturas="id:dummy_plug-01 ts:$(date +%s) tp:temp vl:$(( $RANDOM % 20 + 15 )) un:C er:99 AD_SENSOR ts:$(date +%s) tp:hum vl:$(( $RANDOM % 50 + 35 )) un:percent er:99 AD_ARDUINO id:dummy_plug-02 ts:$(date +%s) tp:temp vl:$(( $RANDOM % 20 + 15 )) un:C er:99 AD_SENSOR ts:$(date +%s) tp:hum vl:$(( $RANDOM % 50 + 35 )) un:percent er:99 AD_ARDUINO id:dummy_plug-03 ts:$(date +%s) tp:temp vl:$(( $RANDOM % 20 + 15 )) un:C er:99 AD_SENSOR ts:$(date +%s) tp:hum vl:$(( $RANDOM % 50 + 35 )) un:percent er:99 AD_ARDUINO id:dummy_plug-04 ts:$(date +%s) tp:temp vl:$(( $RANDOM % 20 + 15 )) un:C er:99 AD_SENSOR ts:$(date +%s) tp:hum vl:$(( $RANDOM % 50 + 35 )) un:percent er:99 END"
}
2022-03-08 19:02:27 +00:00
funcion_datos_sensores() {
timeout 1 python $arduinos_py $i
local lectura_arduino=($(python arduino.py $id_arduino))
lecturas="id:${lectura_arduino[0]} ts:$(date +%s) tp:${lectura_arduino[1]} vl:${lectura_arduino[2]} un:${lectura_arduino[3]} er:${lectura_arduino[4]}"
}
2022-03-05 20:09:45 +00:00
funcion_configurar() {
local comando=$1
local parametro=$2
local parametros=$*
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
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
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
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
;;
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
fi
2022-03-03 22:13:00 +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 ""
echo "Se enviarán datos cada $intervalo minutos."
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
}