Compare commits

...

20 commits

Author SHA1 Message Date
f d2f30d8fe7 si no hay archivos que enviar esperar al próximo intervalo 2022-03-03 15:13:39 -03:00
f 9fef227f5e get_stack solo trae el último archivo de la cola
las funciones hacen una sola cosa
2022-03-03 15:09:52 -03:00
f b8d45a0adf no crear los directorios si ya existen 2022-03-03 15:09:06 -03:00
f 2feee6f70b mover los archivos fallados a otro directorio
de lo contrario detienen el envío porque el stack siempre devuelve el fallado
2022-03-03 15:08:32 -03:00
f 4411d7ee0c probar la conexión sólo cuando hay un archivo para enviar 2022-03-03 15:06:00 -03:00
f 6105e5948e el id de transaccion es el mismo del archivo 2022-03-03 14:53:01 -03:00
f 623dccb301 solo el estado 200 indica que está todo bien 2022-03-03 14:49:39 -03:00
f 0b77d81c2b detener el loop si se lo pedimos amablemente 2022-03-03 14:47:52 -03:00
f f148b14b92 no declarar variables que se usan una vez 2022-03-03 14:37:09 -03:00
f a921e1fd5d usar el archivo de error configurado
además no hace falta borrarlo
2022-03-03 14:35:39 -03:00
f 676b3628bb usar el log configurado 2022-03-03 14:33:24 -03:00
f 46541ea34d fracciones gringas 2022-03-03 14:31:55 -03:00
f 303e35b075 chequear si hay un lock primero 2022-03-03 14:29:58 -03:00
f 8df59231a5 poder configurar el linter de json 2022-03-03 14:28:11 -03:00
f ee4dc72a0a no hardcodear el directorio de instalación 2022-03-03 14:25:27 -03:00
f 0f0a743123 no expandir directorios vacíos 2022-03-03 14:25:16 -03:00
f 3bd1b4cf48 cambia local 2022-03-03 14:18:32 -03:00
f 253992520b no hace falta salir 2022-03-03 14:17:10 -03:00
f c29f1dc07e crear el directorio de archivos corruptos 2022-03-03 14:16:39 -03:00
f dd48575a1b no hardcodear el directorio de instalación 2022-03-03 14:11:32 -03:00
5 changed files with 44 additions and 47 deletions

View file

@ -42,12 +42,12 @@ counter=$(cat $install_dir/counter)
uuid="$(uuidgen)"
timestamp="$(date +%s)"
json="{\"transaction_uuid\":\"$uuid\",\"controller_id\":\"$name\",\"timestamp\":\"$timestamp\",\"error_code\":\"10\",\"coordinates\":{\"lat\":1,\"lng\":1},\"battery_status\":\"98\",\"sample\":\"0\",\"storage\":\"uso del almacenamiento\",\"arduinos\":[$sensores]}"
touch /opt/nodemecu/lock
file=$(date +%Y%m%d%H%M%S)-$(uuidgen)
touch $install_dir/lock
file=$(date +%Y%m%d%H%M%S)-$uuid
for ((i=1 ; i <=3 ; i++));do
if echo $json | jsonlint-php 2> /dev/null;then
echo $json > /opt/nodemecu/stack/$file && rm /opt/nodemecu/lock && echo $((counter+1)) > $install_dir/counter && break
if echo $json | $json_linter >/dev/null 2>&1 ;then
echo $json > $install_dir/stack/$file && rm $install_dir/lock && echo $((counter+1)) > $install_dir/counter && break
else
echo "$(date +%Y-%m-%d-%H:%M:%S) - No se pudo obtener información." >> /opt/nodemecu/errors.log && sleep 1
echo "$(date +%Y-%m-%d-%H:%M:%S) - No se pudo obtener información." >> $log && sleep 1
fi
done

View file

@ -1,10 +1,3 @@
get_stack () {
if [[ ! -z $1 && $1 == "wc" ]];then
ls /opt/nodemecu/stack | wc -l
else
ls /opt/nodemecu/stack | tail -1
fi
ls $install_dir/stack | tail -1
}

View file

@ -1,7 +1,7 @@
#!/bin/bash
files="data_gen.sh monitor.sh register.sh uninstall.sh monitor.web bin functions counter"
config=nodemecu.conf.sample
source $config
source $config
echo -e "1. Instalando dependencias"
@ -10,18 +10,20 @@ sudo pip install pyserial
echo ""
# Estructura de directorios
echo -e "2. Creando directorios y copiando archivos"
# Estructura de directorios
echo -e "2. Creando directorios y copiando archivos"
sudo mkdir -vp $install_dir/archives/historical
sudo mkdir -vp $install_dir/archives/logs
sudo mkdir $historical
sudo mkdir $stack
echo ""
sudo cp -vr $files $install_dir
sudo mkdir -vp $install_dir/archives/logs
sudo mkdir -vp $historical
sudo mkdir -vp $stack
sudo mkdir -vp $corrupt
sudo mkdir -vp $failed
echo ""
sudo cp -vr $files $install_dir
sudo cp -v $config /etc/nodemecu.conf
sudo ln -s $install_dir/bin/nodemecu /usr/bin/nodemecu
sudo touch $log
sudo ln -s $install_dir/bin/nodemecu /usr/bin/nodemecu
sudo touch $log
echo ""
echo "3. Configurando systemd"

View file

@ -2,6 +2,7 @@ url=https://ectomobile.sutty.nl/transactions
name=
interval=
mode=test
json_linter=jsonlint-php
# AJUSTES GENERALES
@ -14,3 +15,4 @@ stack=$install_dir/stack
historical=$install_dir/historical
log=$install_dir/errors.log
corrupt=$install_dir/corrupt
failed=$install_dir/failed

View file

@ -1,47 +1,47 @@
#!/bin/bash
#set -e
source /etc/nodemecu.conf
source /opt/nodemecu/functions
source $install_dir/functions
# No expandir directorios vacíos
shopt -s nullglob
# Revisando stack
for filename in /opt/nodemecu/stack/*;do
test "$filename" = "/opt/nodemecu/stack/\*" || break
jsonlint-php $filename > /dev/null || mv -v $filename $corrupt
for filename in $install_dir/stack/*;do
$json_linter $filename >/dev/null 2>&1 || mv -v $filename $corrupt
done
while :;do
[ -f $install_dir/stop ] && break
[ -f $install_dir/lock ] && continue
file=$(get_stack)
[ -z $file ] && sleep ${interval:-1}m && continue
ping -c 1 fsf.org > /dev/null 2>&1 || continue
test_url="$(curl -s -X POST -o /dev/null -w "%{http_code}" $url)"
if [ $test_url -eq 404 ];then
echo "$(date +%Y-%m-%d-%H:%M:%S) - $url - $test_url" >> /opt/nodemecu/errors.log && sleep 300 && continue
if [ $test_url -ne 200 ];then
echo "$(date +%Y-%m-%d-%H:%M:%S) - $url - $test_url" >> $log && sleep 300 && continue
fi
[ -f $install_dir/lock ] && continue
file=$(get_stack)
[ -z $file ] && continue
local_transaction_uuid=$(jq -r '."transaction_uuid"' $stack/$file)
remote_transaction=$(curl -s --connect-timeout 0,9 --show-error -w "~%{http_code}" -X POST -H "Content-Type: application/json" -d @$stack/$file $url 2> $curl_err)
file="$stack/$file"
local_transaction_uuid="$(echo "$file" | cut -d - -f 2-)"
remote_transaction=$(curl -s --connect-timeout 1 --show-error -w "~%{http_code}" -X POST -H "Content-Type: application/json" -d @$file $url 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
if [ $local_transaction_uuid == $remote_response ]; then
mv $stack/$file $historical
echo -e "$remote_response: \e[92mOK\e[0m"
fi
if [ "$local_transaction_uuid" = "$remote_response" ]; then
mv $file $historical
echo -e "$remote_response: \e[92mOK\e[0m"
else
mv $file $failed
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
[ -s "$curl_err" ] && curl_msg="- $(<$curl_err)"
echo $(date +%Y-%m-%d-%H:%M:%S) - $local_transaction_uuid - server status: $server_error $srv_msg $curl_msg >> $log
fi
done
exit
done