nodemecu/data_gen.sh

54 lines
2.1 KiB
Bash
Raw Normal View History

2022-02-23 14:42:32 +00:00
#!/bin/bash
source /etc/nodemecu.conf
case $mode in
test)
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"
;;
esac
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),)"
;;
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
done
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)
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
else
echo "$(date +%Y-%m-%d-%H:%M:%S) - No se pudo obtener información." >> /opt/nodemecu/errors.log && sleep 1
fi
done