nodemecu/raspberry/registrador

81 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

2022-03-05 18:13:50 +00:00
#!/bin/bash
2022-03-17 19:26:50 +00:00
#set -x
2022-03-05 18:13:50 +00:00
source /etc/nodemecu.conf
2022-03-15 17:56:47 +00:00
source /opt/nodemecu/envs
2022-03-11 02:00:35 +00:00
source $funciones
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
while :;do
2022-03-11 17:10:48 +00:00
# archivo a procesar
file=$(get_stack)
2022-03-17 22:39:06 +00:00
# si no hay nada que procesar reinicia el bucle
test -z $file && funcion_espera && continue
2022-03-17 22:39:06 +00:00
# si no hay servidor definido se reinicia el bucle
test -z $servidor && echo "No se configuró nigun servidor de entrega." && funcion_espera && continue
2022-03-17 22:39:06 +00:00
# si no hay internet reinica el bucle
if ! funcion_verificar_internet; then
2022-03-22 17:25:35 +00:00
err_time=$(date +%Y-%m-%d-%H:%M:%S)
echo "$err_time - Parece no haber internet." | tee -a $log
2022-03-17 22:39:06 +00:00
funcion_espera
continue
fi
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
# si el fichero 'lock' existe (se esta creando un nuevo registro) reinicia el bucle
2022-03-17 22:39:06 +00:00
test -f $lock && continue
2022-03-05 18:13:50 +00:00
2022-03-17 22:39:06 +00:00
# validación del fichero
if ! funcion_validador_json "$(cat $registros/$file)";then
mv $registros/$file $registros_corruptos
continue
fi
2022-03-05 18:13:50 +00:00
2022-03-17 22:39:06 +00:00
# firmado de regisro
2022-03-17 19:26:50 +00:00
firma=$(sign_file $registros/$file)
2022-03-05 18:13:50 +00:00
2022-03-17 22:39:06 +00:00
# UUID del registro a enviar
uuid_registro=$(jq -r '."transaction_uuid"' $registros/$file)
2022-03-05 18:13:50 +00:00
2022-03-17 22:39:06 +00:00
# envio de registro
transaccion=$(curl -v -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-17 19:26:50 +00:00
2022-03-17 22:39:06 +00:00
# respuestas del servidor
respuesta_servidor="$(echo $transaccion | cut -d '~' -f 1)"
error_servidor="$(echo $transaccion | cut -d '~' -f 2)"
2022-03-17 19:26:50 +00:00
2022-03-17 22:39:06 +00:00
if [ ${#respuesta_servidor} -eq 36 ] && [ "$uuid_registro" == "$respuesta_servidor" ]; then
echo -e "$respuesta_servidor: \e[92mOK\e[0m"
2022-03-17 19:26:50 +00:00
mv $registros/$file $registros/$file.sig $historicos
2022-03-05 18:13:50 +00:00
else
2022-03-17 22:39:06 +00:00
2022-03-11 02:00:35 +00:00
err_time=$(date +%Y-%m-%d-%H:%M:%S)
2022-03-17 22:39:06 +00:00
srv_msg="$(echo $respuesta_servidor | grep -o '<body.*>.*</body>')"
2022-03-22 17:25:35 +00:00
echo -e "$err_time - $uuid_registro: \e[91mFAIL\e[0m"
2022-03-17 22:39:06 +00:00
echo "$err_time - $uuid_registro - Error del servidor: $error_servidor" >> $log
echo "$err_time - $uuid_registro - Mensaje del servidor:" >> $log
echo "$srv_msg" >> $log
if [ -s $curl_err ];then
echo "$err_time - $uuid_registro - Salida de curl:" >> $log
cat $curl_err >> $log
fi
2022-03-17 19:26:50 +00:00
2022-03-11 02:00:35 +00:00
rm $curl_err
2022-03-22 17:25:35 +00:00
#funcion_espera
2022-03-05 18:13:50 +00:00
fi
2022-03-11 02:00:35 +00:00
2022-03-22 17:25:35 +00:00
#exit
2022-03-11 02:00:35 +00:00
done
2022-03-05 18:13:50 +00:00
2022-03-17 22:39:06 +00:00
######
# se testea que el servidor responda
#test_url="$(curl -s -X POST -o /dev/null -w "%{http_code}" $servidor)"
#if [ $test_url -eq 404 ];then
# echo "$(date +%Y-%m-%d-%H:%M:%S) - $url - $test_url" >> /opt/nodemecu/errors.log && sleep 300 && continue
#fi