#!/bin/bash # # ## Debugging parameters: #TODAY="$1" ##TODAY="20161204" #CURRENT_YEAR="2017" # normal parameters: DATEFORMAT="%Y%m%d" TODAY=`date +$DATEFORMAT` CURRENT_YEAR=`date +%Y` CHRISTMAS="${CURRENT_YEAR}1224" TREE_KINGS="${CURRENT_YEAR}0106" # Zuordnung Adventskerze <-> GPIO Pin: CANDLE_ADDRESS[0]="17" CANDLE_ADDRESS[1]="18" CANDLE_ADDRESS[2]="27" CANDLE_ADDRESS[3]="22" # Zufallszahl zuruecksetzten rand="" # Verwendete Kerzen zuruesetzten CANDLE_TO_USE[0]="" CANDLE_TO_USE[1]="" CANDLE_TO_USE[2]="" CANDLE_TO_USE[3]="" # Debugging ja/nein DEBUG="" function debug() { MESSAGE=$1 echo "$MESSAGE" } function random_candles() { if [ -n "$DEBUG" ]; then debug "Entering randomization of candle usage:"; fi COUNTER=0 while [ $COUNTER -lt 4 ]; do rand=`echo $(($RANDOM % 4))` if [ "${CANDLE_TO_USE[$rand]}" = "" ]; then CANDLE_TO_USE[$rand]=$COUNTER if [ -n "$DEBUG" ]; then debug "$COUNTER. Candle to use: ${CANDLE_ADDRESS[$rand]} "; fi COUNTER=$((COUNTER + 1)) fi done } function switch_gpio () { i=$1 case $2 in on) state=0 ;; off) state=1 ;; *) esac if [ -n "$DEBUG" ]; then debug "Switch candle ${CANDLE_ADDRESS[$i]} $state"; fi gpio -g mode ${CANDLE_ADDRESS[$i]} out sleep .1 gpio -g write ${CANDLE_ADDRESS[$i]} $state sleep .1 } function preset_candles () { if [ -n "$DEBUG" ]; then debug "Initial state: all candles off!"; fi for i in $(seq 0 3); do switch_gpio $i off done } function get_advent_days () { X=`date -d "$CURRENT_YEAR-12-24 - 3 weeks" +%w` ADVENT[0]=`date -d "$CURRENT_YEAR-12-24 - 3 weeks -$X day" +$DATEFORMAT` X=`date -d "$CURRENT_YEAR-12-24 - 2 weeks" +%w` ADVENT[1]=`date -d "$CURRENT_YEAR-12-24 - 2 weeks -$X day" +$DATEFORMAT` X=`date -d "$CURRENT_YEAR-12-24 - 1 weeks" +%w` ADVENT[2]=`date -d "$CURRENT_YEAR-12-24 - 1 weeks -$X day" +$DATEFORMAT` X=`date -d "$CURRENT_YEAR-12-24 - 0 weeks" +%w` ADVENT[3]=`date -d "$CURRENT_YEAR-12-24 - 0 weeks -$X day" +$DATEFORMAT` } function print_advent_days () { if [ -n "$DEBUG" ]; then debug "1. Advent in $CURRENT_YEAR ist der: ${ADVENT[0]}"; fi if [ -n "$DEBUG" ]; then debug "2. Advent in $CURRENT_YEAR ist der: ${ADVENT[1]}"; fi if [ -n "$DEBUG" ]; then debug "3. Advent in $CURRENT_YEAR ist der: ${ADVENT[2]}"; fi if [ -n "$DEBUG" ]; then debug "4. Advent in $CURRENT_YEAR ist der: ${ADVENT[3]}"; fi } function switch_candles () { if [ $TODAY -le $TREE_KINGS ] ; then if [ -n "$DEBUG" ]; then debug "Weihnachten ist vorbei aber die Heiligen drei Koenige sind auch noch nicht..."; fi if [ -n "$DEBUG" ]; then debug "Trotzdem alle Kerzen an! ;-)"; fi for i in $(seq 0 3); do switch_gpio ${CANDLE_TO_USE[$i]} on done elif [ $TODAY -lt ${ADVENT[0]} ] ; then if [ -n "$DEBUG" ]; then debug "noch keine Adventszeit :-("; fi elif [ $TODAY -ge ${ADVENT[0]} ] && [ $TODAY -lt ${ADVENT[1]} ] ; then if [ -n "$DEBUG" ]; then debug "erste Adventswoche :-)"; fi for i in $(seq 0 0); do switch_gpio ${CANDLE_TO_USE[$i]} on done elif [ $TODAY -ge ${ADVENT[1]} ] && [ $TODAY -lt ${ADVENT[2]} ] ; then if [ -n "$DEBUG" ]; then debug "zweite Adventswoche :-)"; fi for i in $(seq 0 1); do switch_gpio ${CANDLE_TO_USE[$i]} on done elif [ $TODAY -ge ${ADVENT[2]} ] && [ $TODAY -lt ${ADVENT[3]} ] ; then if [ -n "$DEBUG" ]; then debug "dritte Adventswoche :-)"; fi for i in $(seq 0 2); do switch_gpio ${CANDLE_TO_USE[$i]} on done elif [ $TODAY -ge ${ADVENT[3]} ] && [ $TODAY -le $CHRISTMAS ] ; then if [ -n "$DEBUG" ]; then debug "vierte Adventswoche :-)"; fi for i in $(seq 0 3); do switch_gpio ${CANDLE_TO_USE[$i]} on done elif [ $TODAY -gt $CHRISTMAS ] ; then if [ -n "$DEBUG" ]; then debug "Weihnachten ist vorbei :-("; fi if [ -n "$DEBUG" ]; then debug "Trotzdem alle Kerzen an! ;-)"; fi for i in $(seq 0 3); do switch_gpio ${CANDLE_TO_USE[$i]} on done fi } ###### Main # # Check, if debugging is enabled: if [ "$1" = "debug" ]; then DEBUG="true" fi if [ -n "$DEBUG" ]; then debug "Today: $TODAY"; fi if [ -n "$DEBUG" ]; then debug "Year: $CURRENT_YEAR"; fi if [ -n "$DEBUG" ]; then debug "Three Kings day: $TREE_KINGS"; fi if [ -n "$DEBUG" ]; then debug "Christmas: $CHRISTMAS"; fi #if [ $TODAY -lt ${ADVENT[0]} ] ; then preset_candles get_advent_days print_advent_days random_candles switch_candles #echo "Zufallszahl: $rand"