witw/data/get_data.sh

58 lines
1.6 KiB
Bash
Raw Normal View History

2023-10-17 16:58:11 +02:00
#!/bin/bash
2023-10-19 23:04:42 +02:00
# ///////////////////////////////////////////////
# Whistling in the Wind
# Data Sonification with Supercollider
# Rob Canning 2023
# ///////////////////////////////////////////////
2023-10-17 16:58:11 +02:00
2023-10-19 23:04:42 +02:00
# Array of weather station MAC addresses
2023-10-17 16:58:11 +02:00
2023-10-19 23:04:42 +02:00
STATIONS=( 'D7:80:BE:2C:63:BD' 'F1:CC:C3:0B:7F:5C' )
# Request API token (JSON) and parse for access_token string with Python3
2023-10-17 16:58:11 +02:00
TOKEN=$(curl --insecure --data \
"username=data-api-user&password=jBD0CD1wGOKoTVtC&grant_type=password&client_id=phenode" \
https://phenode-link.com:8443/realms/grafana/protocol/openid-connect/token \
| python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])")
2023-10-19 23:04:42 +02:00
echo $TOKEN;
# pull down the data and clean it up with sed and awk
2023-10-17 16:58:11 +02:00
for data in "${STATIONS[@]}"
2023-10-19 23:04:42 +02:00
2023-10-17 16:58:11 +02:00
do
2023-10-19 23:04:42 +02:00
if [ -d $data ] ## if data dir exists then remove it
then echo "removing old data directories" ; rm -r $data
else echo "nothing to remove"
fi
2023-10-17 16:58:11 +02:00
echo "downloading data from device with MAC: $data"
2023-10-19 23:04:42 +02:00
# curl --insecure \
# -X POST https://phenode-link.com:2537/devices/$data/all-data/2023-04-10T16:52:21Z \
# -H \"Authorization: Bearer $TOKEN\" \
# --output $data.zip
2023-10-17 16:58:11 +02:00
2023-10-20 01:54:56 +02:00
unzip $data.zip && mv all-data $data &&
#rm $data.zip &&
cd $data
2023-10-17 16:58:11 +02:00
2023-10-19 23:04:42 +02:00
# backup unfiltered / raw data
cp sensor_data.csv sensor_data_ORIG.csv
2023-10-17 16:58:11 +02:00
# remove the null data and replace with 0
2023-10-19 23:04:42 +02:00
sed -i 's/^,/0,/; :a;s/,,/,0,/g;ta' sensor_data.csv
2023-10-17 16:58:11 +02:00
# clear the ugly rows from Growing Degree Days
2023-10-19 23:04:42 +02:00
awk -F, 'NR==1{print} NR>1 && $2=="0" {gsub(/[)(\]\[]/,"",$0);print}' sensor_data.csv > tmp && mv tmp sensor_data.csv
2023-10-17 16:58:11 +02:00
cd ../
2023-10-19 23:04:42 +02:00
# sensor_data.csv is the final file to be used by supercollider
# sensor_data_ORIG.csv is the backup original data
2023-10-17 16:58:11 +02:00
done