Skip to content

Instantly share code, notes, and snippets.

@bartowl
Created February 22, 2021 14:18
Show Gist options
  • Save bartowl/c0efb60f66ddbe859d3ecc18e14d43a6 to your computer and use it in GitHub Desktop.
Save bartowl/c0efb60f66ddbe859d3ecc18e14d43a6 to your computer and use it in GitHub Desktop.
eLicznik Taurona - skrypt ładujący dane z licznika do InfluxDB, wspiera więcej miejsc poboru energii
#!/bin/bash
data=${1:-$(date +%d.%m.%Y --date yesterday)}
# run with date on format dd.mm.yyyy as argument, without arguments takes yesterday
# skip run if already done, to be started multiple times per day due to elicznik api instability
[ $(ls -l /tmp/fetch_elicznik*"$data" 2>/dev/null | wc -l) -eq 2 ] && exit
login="xxxxx"
password="xxxxx"
influxurl="http://xxxxxx:8086"
cookiejar=$(mktemp /tmp/elicznik_cookieXXXX)
touch $cookiejar
curl -sL -b $cookiejar -c $cookiejar -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36" -H "cache-control: no-cache" https://logowanie.tauron-dystrybucja.pl/login &> /dev/null
curl -sL -b $cookiejar -c $cookiejar -o /dev/null -d "username=${login}&password=${password}&service=https://elicznik.tauron-dystrybucja.pl" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36" -H "cache-control: no-cache" https://logowanie.tauron-dystrybucja.pl/login
#curl -L -b $cookiejar -c $cookiejar -d "username=${login}&password=${password}&service=https://elicznik.tauron-dystrybucja.pl" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36" -H "cache-control: no-cache" https://logowanie.tauron-dystrybucja.pl/login &> /dev/null
output=$(mktemp /tmp/elicznik_out_XXXX)
chart=$(mktemp /tmp/elicznik_chart_XXXX)
for meterid in xxxx_xxxx_xxxx yyyy_yyyy_yyyy; do
[ $meterid == 'xxxx_xxxx_xxxx' ] && meterdesc=lokalizacja1
[ $meterid == 'yyyy_yyyy_yyyy' ] && meterdesc=lokalizacja2
# check if already deliered correct and full data
[ -e "/tmp/fetch_elicznik_${meterdesc}_$data" ] && continue
#[ -z "$1" ] && rm "/tmp/fetch_elicznik_${meterdesc}_"* 2>/dev/null # remove previous day
curl -o /dev/null -sfL -b $cookiejar -c $cookiejar -d "smart=$meterid" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36" -H "cache-control: no-cache" "https://elicznik.tauron-dystrybucja.pl/" || \
curl -o /dev/null -sL -b $cookiejar -c $cookiejar -d "smart=$meterid" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36" -H "cache-control: no-cache" "https://elicznik.tauron-dystrybucja.pl/"
curl -sL -b $cookiejar -c $cookiejar -d "dane[smartNr]=$meterid&dane[chartDay]=$data&dane[checkOZE]=on&dane[paramType]=day&dane[paramArea]=&dane[chartType]=2" -H "Content-type: application/x-www-form-urlencoded" -H "cache-control: no-cache" https://elicznik.tauron-dystrybucja.pl/index/charts > $chart
# cat $chart
[ $(cat $chart | wc -c) -eq 0 ] && echo "$meterdesc got empty result @ $data" && continue
[ "$(jq .ok $chart)" != "1" ] && echo "data missing for $meterdesc @ $data" && continue
jq -r '[.dane.chart[]]|map(["elicznik,meter_id='$meterid',lokalizacja='$meterdesc' pobor="+.EC+" "+((.Date + "T" + (.Hour|tonumber -1 | tostring) +":00:00Z")|fromdate-1|tostring)])[]|join(", ")' $chart >> $output
jq -r '[.dane.OZE[]]|map(["elicznik,meter_id='$meterid',lokalizacja='$meterdesc' produkcja="+.EC+" "+((.Date + "T" + (.Hour|tonumber -1 | tostring) +":00:00Z")|fromdate-1|tostring)])[]|join(", ")' $chart >> $output
# cat $output
curl -f -X POST --data-binary @$output "$influx_url/write?db=FHEM&precision=s" 2> /dev/null
[ $? -eq 0 ] && [ "$(jq -r '(.ok|tostring) + (.isFull|tostring) + (.isFullOther|tostring)' $chart)" == "1truetrue" ] && touch "/tmp/fetch_elicznik_${meterdesc}_$data" && echo "received and uploaded complete data for $meterdesc @ $data"
done
curl -L -b $cookiejar -c $cookiejar -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36" -H "cache-control: no-cache" https://elicznik.tauron-dystrybucja.pl/wyloguj &> /dev/null
rm $cookiejar $output $chart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment