Arreglos varios #6
5 changed files with 44 additions and 47 deletions
10
data_gen.sh
10
data_gen.sh
|
@ -42,12 +42,12 @@ counter=$(cat $install_dir/counter)
|
||||||
uuid="$(uuidgen)"
|
uuid="$(uuidgen)"
|
||||||
timestamp="$(date +%s)"
|
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]}"
|
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
|
touch $install_dir/lock
|
||||||
file=$(date +%Y%m%d%H%M%S)-$(uuidgen)
|
file=$(date +%Y%m%d%H%M%S)-$uuid
|
||||||
for ((i=1 ; i <=3 ; i++));do
|
for ((i=1 ; i <=3 ; i++));do
|
||||||
if echo $json | jsonlint-php 2> /dev/null;then
|
if echo $json | $json_linter >/dev/null 2>&1 ;then
|
||||||
echo $json > /opt/nodemecu/stack/$file && rm /opt/nodemecu/lock && echo $((counter+1)) > $install_dir/counter && break
|
echo $json > $install_dir/stack/$file && rm $install_dir/lock && echo $((counter+1)) > $install_dir/counter && break
|
||||||
else
|
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
|
fi
|
||||||
done
|
done
|
||||||
|
|
|
@ -1,10 +1,3 @@
|
||||||
get_stack () {
|
get_stack () {
|
||||||
if [[ ! -z $1 && $1 == "wc" ]];then
|
ls $install_dir/stack | tail -1
|
||||||
ls /opt/nodemecu/stack | wc -l
|
|
||||||
else
|
|
||||||
ls /opt/nodemecu/stack | tail -1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,10 @@ echo -e "2. Creando directorios y copiando archivos"
|
||||||
|
|
||||||
sudo mkdir -vp $install_dir/archives/historical
|
sudo mkdir -vp $install_dir/archives/historical
|
||||||
sudo mkdir -vp $install_dir/archives/logs
|
sudo mkdir -vp $install_dir/archives/logs
|
||||||
sudo mkdir $historical
|
sudo mkdir -vp $historical
|
||||||
sudo mkdir $stack
|
sudo mkdir -vp $stack
|
||||||
|
sudo mkdir -vp $corrupt
|
||||||
|
sudo mkdir -vp $failed
|
||||||
echo ""
|
echo ""
|
||||||
sudo cp -vr $files $install_dir
|
sudo cp -vr $files $install_dir
|
||||||
sudo cp -v $config /etc/nodemecu.conf
|
sudo cp -v $config /etc/nodemecu.conf
|
||||||
|
|
|
@ -2,6 +2,7 @@ url=https://ectomobile.sutty.nl/transactions
|
||||||
name=
|
name=
|
||||||
interval=
|
interval=
|
||||||
mode=test
|
mode=test
|
||||||
|
json_linter=jsonlint-php
|
||||||
|
|
||||||
|
|
||||||
# AJUSTES GENERALES
|
# AJUSTES GENERALES
|
||||||
|
@ -14,3 +15,4 @@ stack=$install_dir/stack
|
||||||
historical=$install_dir/historical
|
historical=$install_dir/historical
|
||||||
log=$install_dir/errors.log
|
log=$install_dir/errors.log
|
||||||
corrupt=$install_dir/corrupt
|
corrupt=$install_dir/corrupt
|
||||||
|
failed=$install_dir/failed
|
||||||
|
|
46
register.sh
46
register.sh
|
@ -1,47 +1,47 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
#set -e
|
#set -e
|
||||||
source /etc/nodemecu.conf
|
source /etc/nodemecu.conf
|
||||||
source /opt/nodemecu/functions
|
source $install_dir/functions
|
||||||
|
|
||||||
|
# No expandir directorios vacíos
|
||||||
|
shopt -s nullglob
|
||||||
|
|
||||||
# Revisando stack
|
# Revisando stack
|
||||||
|
|
||||||
for filename in /opt/nodemecu/stack/*;do
|
for filename in $install_dir/stack/*;do
|
||||||
test "$filename" = "/opt/nodemecu/stack/\*" || break
|
$json_linter $filename >/dev/null 2>&1 || mv -v $filename $corrupt
|
||||||
jsonlint-php $filename > /dev/null || mv -v $filename $corrupt
|
|
||||||
done
|
done
|
||||||
|
|
||||||
while :;do
|
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
|
ping -c 1 fsf.org > /dev/null 2>&1 || continue
|
||||||
test_url="$(curl -s -X POST -o /dev/null -w "%{http_code}" $url)"
|
test_url="$(curl -s -X POST -o /dev/null -w "%{http_code}" $url)"
|
||||||
if [ $test_url -eq 404 ];then
|
if [ $test_url -ne 200 ];then
|
||||||
echo "$(date +%Y-%m-%d-%H:%M:%S) - $url - $test_url" >> /opt/nodemecu/errors.log && sleep 300 && continue
|
echo "$(date +%Y-%m-%d-%H:%M:%S) - $url - $test_url" >> $log && sleep 300 && continue
|
||||||
fi
|
fi
|
||||||
[ -f $install_dir/lock ] && continue
|
|
||||||
file=$(get_stack)
|
file="$stack/$file"
|
||||||
[ -z $file ] && continue
|
local_transaction_uuid="$(echo "$file" | cut -d - -f 2-)"
|
||||||
local_transaction_uuid=$(jq -r '."transaction_uuid"' $stack/$file)
|
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_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)
|
|
||||||
remote_response="$(echo $remote_transaction | cut -d '~' -f 1)"
|
remote_response="$(echo $remote_transaction | cut -d '~' -f 1)"
|
||||||
server_error="$(echo $remote_transaction | cut -d '~' -f 2)"
|
server_error="$(echo $remote_transaction | cut -d '~' -f 2)"
|
||||||
if [ ${#remote_response} -eq 36 ];then
|
|
||||||
|
|
||||||
if [ $local_transaction_uuid == $remote_response ]; then
|
if [ "$local_transaction_uuid" = "$remote_response" ]; then
|
||||||
mv $stack/$file $historical
|
mv $file $historical
|
||||||
echo -e "$remote_response: \e[92mOK\e[0m"
|
echo -e "$remote_response: \e[92mOK\e[0m"
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
|
mv $file $failed
|
||||||
echo -e "$local_transaction_uuid: \e[91mFAIL\e[0m"
|
echo -e "$local_transaction_uuid: \e[91mFAIL\e[0m"
|
||||||
# errores
|
# errores
|
||||||
unset srv_msg
|
unset srv_msg
|
||||||
unset curl_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>')"
|
[ ! -z "$remote_response" ] && srv_msg="- $( echo $remote_response | grep -o '<body.*>.*</body>')"
|
||||||
[ -s "$curl_err" ] && curl_msg="- $(</tmp/curl_err)"
|
[ -s "$curl_err" ] && curl_msg="- $(<$curl_err)"
|
||||||
echo $err_time - $local_transaction_uuid - server status: $server_error $srv_msg $curl_msg >> $log
|
echo $(date +%Y-%m-%d-%H:%M:%S) - $local_transaction_uuid - server status: $server_error $srv_msg $curl_msg >> $log
|
||||||
rm $curl_err
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
exit
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue