From d40394bd628f4e5a09fe9836e2d22d1e9a87fb16 Mon Sep 17 00:00:00 2001 From: Der_Leopold Date: Sat, 9 Jan 2021 14:29:13 +0100 Subject: [PATCH] small refactor --- readSensor | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/readSensor b/readSensor index 8282a22..485ad5d 100755 --- a/readSensor +++ b/readSensor @@ -13,7 +13,7 @@ RETRY=3 # Number of seconds to wait between retries -DELAY=3 +DELAY=2 # Specify the temperature range where you expect the sensors will be operating # This is simply used as a safeguard to detect implausible sensor readings @@ -36,7 +36,7 @@ declare -A SENSORS=( [0]=LuftTemp [1]=WasserTemp [2]=BodenTemp ) # Location where the readings should be stored OUTFILE="output" - +RESULT="" # # no configuration/changes should be necessary below this line @@ -44,7 +44,6 @@ OUTFILE="output" cd "$(dirname "$0")"; touch "$OUTFILE" 2> /dev/null -echo "Timestamp="`date +%s` > "$OUTFILE" if [ ! -w $OUTFILE ]; then echo "Error: output file '$OUTFILE' not writeable" @@ -64,13 +63,19 @@ function check_output () { read_sensor check_output else - echo "${SENSORS[$SENSORID]}=Error" > "$OUTFILE" + RESULT="$RESULT${SENSORS[$SENSORID]}=Error\n" fi fi } -function write_results () { - echo "${SENSORS[$SENSORID]}=${VALUE}" >> "$OUTFILE" +function store_result () { + RESULT="$RESULT${SENSORS[$SENSORID]}=${VALUE}\n" +} + +function write_to_file () { + echo "Timestamp="`date +%s` > "$OUTFILE" + + echo -e "$RESULT" >> "$OUTFILE" } for SENSORID in "${!SENSORS[@]}" @@ -78,6 +83,7 @@ do CNT=0 read_sensor check_output - write_results + store_result sleep $DELAY done +write_to_file