diff --git a/dl5000sql.sh b/dl5000sql.sh new file mode 100644 index 0000000..181dd8b --- /dev/null +++ b/dl5000sql.sh @@ -0,0 +1,82 @@ +#!/bin/bash +# dl5000sql.sh reads data from the DL5000 weather station and writes it up to a MySQL/MariaDB. +# https://code.hw12.org/tilman/dl5000 +# +# Version 0.0.1 - 15.04.2020 "Proof of concept" +# +# License GPL3.0 or later +# https://code.hw12.org/tilman/dl5000/src/branch/master/LICENSE + +#Log file and temporary txt. +PFAD=$(dirname "$(readlink -e "$0")"); +FILE=$PFAD"/"$DATE."txt"; +LOG=$PFAD"/dl5000sql.log"; + +TMP0="dl5000.tmp"; +TMP1="dl5001.tmp"; +TMP2="dl5002.tmp"; + +#Database +DB_USER="dbuser"; +DB_PASS="dbpasswd"; +DB_HOST="dbhost"; +DB_NAME="dbname"; +DB_TAB="TempData"; + +#Get date and round to nearest 5min (for harmonized use with data from other scripts in pChart) +DATE_RAW=$(/bin/date +%s); +DATE=$(/bin/date -d @$DATE_RAW "+%Y-%m-%d %H:%M:%S"); +DATE_N5M=$(echo "($DATE_RAW % 300)"|bc); + +if [ $DATE_N5M -lt 150 ];then + DATE_N5M=$(echo "($DATE_RAW - ($DATE_RAW % 300))"|bc); +else + DATE_N5M=$(echo "($DATE_RAW - ($DATE_RAW % 300) + 300)"|bc); +fi +DATE_N5M=$(/bin/date -d @$DATE_N5M "+%Y-%m-%d %H:%M:%S"); + +readsensors(){ +cd /opt/raumklima + +. ./venv/bin/activate +python src/read_rs500_test.py > ${TMP0} +deactivate + +#Prüfe abgelegte Datei +TMP_FILE=$(wc -c ${TMP0} | awk '{print $1}') +if [ ${TMP_FILE} -le 12 ]; then + sleep 5; + readsensors; +fi + +} +#Start mit Datenabruf +readsensors; + +#Erste Zeile entfernen +sed -e '1d' ${TMP0} > ${TMP1}; +#Leerzeichen entfernen +tr -d ' ' < ${TMP1} > ${TMP2}; + +#Zeilenweise in Array übertragen +while read -r DATA; + do DATASETS+=(${DATA}); +done < dl5002.tmp; + +#Zeilen in Variablen nach Raum übertragen +for DATASETS in "${DATASETS[@]}" + do + readarray -td';' DATASET <<< "${DATASETS[@]}"; + + declare "S${DATASET[0]}_Temp"=${DATASET[1]}; + declare "S${DATASET[0]}_Humidity"=${DATASET[2]:0:2}; + +done + +#Send data to database +mysql -u $DB_USER -p$DB_PASS -h $DB_HOST -D $DB_NAME <