site nav‎‏‎‎‏‎

categories‎ > ‎code‎ > ‎

x10receiver

posted 14 Nov 2010 12:43 by Slippy Lane
#!/bin/bash

# x10receiver
# A looping BASH script to receive and process x10 RF Remote commands

# *** REQUIREMENTS ***
# CM12U (for UK users) or CM11x (where x is the appropriate letter code for your country's version of the CM11)
# A built and installed copy of heyu (available at http://www.heyu.org)
# A linux PC running BASH

# *** NOTES ***
# This is a quick'n'dirty script, badly coded but it works for me.
# You'll need to edit it for your system and heyu setup before it'll work properly

echo "$0 running..."
heyu stop
heyu start
lfile=$HOME/lfile.dat
housecode="X"
unitcode="0"
fn="None"
while true; do
  heyu logtail 2 > $lfile
  counter=1
  while read line
  do
    if [ $counter = 1 ]; then
      line1=$line
      counter=2
    elif [ $counter = 2 ]; then
      line2=$line
      counter=0
    fi
  done < "$lfile"
#  echo
#  echo "***BEGIN***"
#  echo $line1
#  echo $line2
#  echo "-${line1:16:14}-"
  if [ "${line2:16:9}" = "rcvi func" ]; then
    if [ "${line1:16:14}" = "rcvi addr unit" ]; then
      line1=${line1:15}
#      echo $line1
      heyu logmsg "Checked1"
      lineidx=`expr index "$line1" ':'`
      lineidx2=$(($lineidx+4))
      lineidx3=$(($lineidx+5))
      lineidx1a=`expr index "$line1" '^('`
      lineidx1=$(($lineidx1a - 2))
#      echo $lineidx $lineidx1 $lineidx1a $lineidx2 $lineidx3
      lasthousecode=$housecode
      lastunitcode=$unitcode
      housecode=${line1:$lineidx2:1}
      unitcode=${line1:$lineidx3:$(($lineidx1 - $lineidx3))}
#      echo "/${line2:16:9}/"
    fi
      line2=${line2:15}
      heyu logmsg "Checked2"
      lineidx4=`expr index "$line2" ':'`
      lineidx5=20
      lineidx6=$(($lineidx4-22))
      fn=${line2:$lineidx5:$lineidx6}
      echo
      echo "[FN: $fn]"
      echo "[HC: $housecode (prev: $lasthousecode)]"
      echo "[UC: $unitcode (prev: $lastunitcode)]"

#  read dummy

    if [ "$fn" = " On" ]; then
      fn="on"
    elif [ "$fn" = "Off" ]; then
      fn="off"
    elif [ "$fn" = "ght" ]; then
      fn="bright"
    elif [ "$fn" = "Dim" ]; then
      fn="dim"
    fi

    if [ "$housecode" = "A" ]; then
      if [ $unitcode = 9 ]; then
        if [ "$fn" = "on" ]; then
          heyu on A1
        elif [ "$fn" = "off" ]; then
         heyu off A1
        elif [ "$fn" = "bright" ]; then
          heyu off A1
          sleep 5
          heyu on A1
        elif [ "$fn" = "dim" ]; then
          heyu off A1
          sleep 60
          heyu on A1
        fi
      elif [ $unitcode = 12 ]; then
        if [ "$fn" = "on" ]; then
          zenity --info --text="9:      router\n9 Bright: short reset router\n9 Dim:      long reset router\n10:      hall light\n11:      living room light\n12 On:      This help\n12 Off:      gscripts\n13 On:      transmission\n13 Off:      terminal\n14 On:      calculator\n14 Off:      nautilus\n15 On:      chrome\n15 Off:      gedit\n16 On:      A1,2,3 on\n16 Off:      A1,2,3 off" &
        elif [ "$fn" = "off" ]; then
          gscripts &
        fi  
      elif [ $unitcode = 13 ]; then
        if [ "$fn" = "on" ]; then
          transmission &
        elif [ "$fn" = "off" ]; then
          gnome-terminal &
        fi  
      elif [ $unitcode = 14 ]; then
        if [ "$fn" = "on" ]; then
          gnome-calculator &
        elif [ "$fn" = "off" ]; then
          nautilus &
        fi  
      elif [ $unitcode = 15 ]; then
        if [ "$fn" = "on" ]; then
          google-chrome &
        elif [ "$fn" = "off" ]; then
          gedit &
        fi  
      elif [ $unitcode = 16 ]; then
        if [ "$fn" = "on" ]; then
          heyu $fn A1
          heyu $fn A2
          heyu $fn A3
        elif [ "$fn" = "off" ]; then
          heyu $fn A1
          heyu $fn A2
          heyu $fn A3
        elif [ "$fn" = "bright" ]; then
          heyu $fn A2 2
          heyu $fn A3 2
        elif [ "$fn" = "dim" ]; then
          heyu $fn A2 2
          heyu $fn A3 2
        fi  
      else
        if [ "$fn" = "on" ]; then
          heyu $fn $housecode$(($unitcode-8))
        elif [ "$fn" = "off" ]; then
          heyu $fn $housecode$(($unitcode-8))
        elif [ "$fn" = "bright" ]; then
          heyu $fn $housecode$(($unitcode-8)) 2
        elif [ "$fn" = "dim" ]; then
          heyu $fn $housecode$(($unitcode-8)) 2
        fi
      fi
    fi
  fi
done