From 6b57864e5ced3e0acc06edf27354747e84ba2f23 Mon Sep 17 00:00:00 2001 From: tilman Date: Wed, 15 Apr 2020 19:50:34 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=9Edl5000sql.sh=E2=80=9C=20hinzuf=C3=BCge?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Erste Version - Proof of concept. Provisorien leben am längsten. --- dl5000sql.sh | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 dl5000sql.sh 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 <