This commit is contained in:
Sergio Pernas 2022-03-10 23:00:35 -03:00
parent 1d750f315e
commit a8477a9f98
2 changed files with 45 additions and 71 deletions

View file

@ -18,7 +18,6 @@ funcion_datos_simulados() {
funcion_datos_sensores() {
#local arduino_id=$1
peticiones=1
arduinos=( 0x01 0x02 )
for arduino_id in ${arduinos[@]};do
@ -36,9 +35,7 @@ funcion_datos_sensores() {
lecturas="$lecturas tp:${lectura_arduino[$indice]} vl:${lectura_arduino[$(expr $indice + 1 )]} un:${lectura_arduino[$(expr $indice + 2 )]} er:${lectura_arduino[$(expr $indice + 3 )]}"
if [ $vueltas -lt $bloques ]; then
lecturas="$lecturas AD_SENSOR"
fi
let "indice = $indice + 4 "

View file

@ -1,85 +1,62 @@
#!/bin/bash
#set -x
source /etc/nodemecu.conf
source funciones
conteo=$(cat $contador)
uuid="$(uuidgen)"
marca_de_tiempo="$(date +%s)"
case $modo in
test)
funcion_datos_simulados
;;
sensores)
funcion_datos_sensores
;;
esac
source $funciones
# ARMADO DE FICHERO JSON
# Revisando stack
for i in $lecturas;do
case $i in
id*)
sensores="$sensores { $(echo \"id\":\"$(echo $i | cut -d ":" -f 2)\", \"sensores\": [ )"
;;
ts*)
#sensores="$sensores { $(echo \"timestamp\": $(echo $i | cut -d ":" -f 2),)"
sensores="$sensores { $(echo \"timestamp\": $marca_de_tiempo,)"
;;
tp*)
sensores="$sensores $(echo \"type\": \"$(echo $i | cut -d ":" -f 2)\",)"
;;
vl*)
sensores="$sensores $(echo \"value\": $(echo $i | cut -d ":" -f 2),)"
;;
un*)
sensores="$sensores $(echo \"unit\": \"$(echo $i | cut -d ":" -f 2)\",)"
;;
er*)
sensores="$sensores $(echo \"error\": $(echo $i | cut -d ":" -f 2) })"
;;
AD_SENSOR)
sensores="$sensores,"
;;
AD_ARDUINO)
sensores="$sensores]},"
;;
END)
sensores="$sensores]}"
;;
esac
for registro_json in $registros/*;do
test "$registro_json" = "/opt/nodemecu/stack/\*" || break
jsonlint-php $registro_json > /dev/null || mv -v $registro_json $registros_corruptos
done
while :;do
# si no hay nada en el directorio registros reinicia el bucle
# espera el intervalo definido o 1 minuto.
file=$(get_stack)
[ -z $file ] && sleep ${intervalo:-1}m && continue
# si no respone el ping reinica el bucle
ping -c 1 fsf.org > /dev/null 2>&1 || continue
# si el fichero 'lock' existe (se esta creando un nuevo registro) reinicia el bucle
[ -f $lock ] && continue
json="{\"transaction_uuid\":\"$uuid\",\"controller_id\":\"$name\",\"timestamp\":\"$marca_de_tiempo\",\"error_code\":\"10\",\"coordinates\":{\"lat\":1,\"lng\":1},\"battery_status\":\"98\",\"sample\":\"0\",\"storage\":\"uso del almacenamiento\",\"arduinos\":[$sensores]}"
touch $lock
file=$(date +%Y%m%d%H%M%S -d @$marca_de_tiempo)-$uuid
# 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
if echo $json | jsonlint-php 2> /dev/null;then
echo $json > $directorio_instalacion/$file && rm $lock && echo $((counter+1)) > $contador
else
echo "$(date +%Y-%m-%d-%H:%M:%S -d @$marca_de_tiempo) - No se pudo obtener información." >> $log
fi
sing_file $registros/$file
firma="$(cat $registros/$file.sign)"
exit
local_transaction_uuid=$(jq -r '."transaction_uuid"' $registros/$file)
remote_transaction=$(curl -s --connect-timeout 0,9 --show-error -w "~%{http_code}" -X POST -H "X-Signature: $firma" -H "Content-Type: application/json" -d @$registros/$file $servidor 2> $curl_err)
remote_response="$(echo $remote_transaction | cut -d '~' -f 1)"
server_error="$(echo $remote_transaction | cut -d '~' -f 2)"
if [ ${#remote_response} -eq 36 ];then
# habia una razon para esto, ya va a saltar
for ((i=1 ; i <=3 ; i++));do
if echo $json | jsonlint-php 2> /dev/null;then
echo $json > $registros/$file && rm /opt/nodemecu/lock && echo $((counter+1)) > $contador && break
if [ $local_transaction_uuid == $remote_response ]; then
mv $registros/$file $historicos
echo -e "$remote_response: \e[92mOK\e[0m"
fi
else
echo "$(date +%Y-%m-%d-%H:%M:%S) - No se pudo obtener información." >> /opt/nodemecu/errors.log && sleep 1
echo -e "$local_transaction_uuid: \e[91mFAIL\e[0m"
# errores
unset srv_msg
unset curl_msg
err_time=$(date +%Y-%m-%d-%H:%M:%S)
[ ! -z "$remote_response" ] && srv_msg="- $( echo $remote_response | grep -o '<body.*>.*</body>')"
[ -s "$curl_err" ] && curl_msg="- $(</tmp/curl_err)"
echo $err_time - $local_transaction_uuid - server status: $server_error $srv_msg $curl_msg >> $log
rm $curl_err
fi
done
done
exit