#!/bin/bash
#
# homematic.sh
#
# Retrieve sensor information from homematic
#
# 20130306 MV: Added help function
# 20121205 MV: Implement caching and better selection on channel/data id
# 20121204 MV: Initial version
#

URL="http://homematic/config/xmlapi/statelist.cgi"
CACHE=/tmp/homematic.cache
CONF=/etc/homematic.cfg

get_data()
{
   ID="$1"
   if test "$ID" = ""
   then
      echo "No ID available"
   fi
   cat $CACHE | gawk -v ID=$ID '
   BEGIN { start=0; }
   {
       nchan = split($0, channels, "<channel ");
       for (chanc = 1; chanc <= nchan; chanc++)
       {
          channel=channels[chanc];
          gsub(/>.*/,"",channel);
          gsub(/.*ise_id=/,"",channel);
          gsub(/'\''/,"",channel);
          if (channel == ID)
          {
             ntags = split(channels[chanc], tags, "<");
             for (tagc = 1; tagc <= ntags; tagc++)
             {
                if (index(tags[tagc], "datapoint") == 1)
                {
                   nfields = split(tags[tagc], fields, " ");
                   disp=0;
                   for (fieldc = 1; fieldc <= nfields; fieldc++)
                   {
                      split(fields[fieldc], subf, "=");
                      name=subf[1];
                      value=subf[2];
                      gsub(/'\''/, "", value);
                      if (name == "type" && value != "") 
                      {
                         if (start != 0)
                            printf(" ", value);
                         start=1;
                         printf("%s:", value);
                         disp=1;
                      }
                      if ((name == "value") && (disp == 1))
                      {
                         printf("%s", value);
                      }
                   }
                }
             }
          }
       }
   }'
}

get_config()
{
   context="$1"
   name="$2"

   if test "$context" = "" -o "$name" = ""
   then
      echo "Missing config options"
   fi

   found=0
   cat $CONF | while read line
   do
      test $found -eq 1 && ( echo "$line" | grep -i "^$name" > /dev/null ) && echo ${line/* /}
      test "$line" != "${line/[/}" && if test "$line" = "[$context]"
      then 
         found=1
      else
         found=0
      fi
   done
}

usage()
{
   echo "Usage: $0 [command]..."
   echo "Get Homematic CCU information for cacti."
   echo
   echo "  update                   Get new information from the CCU and cache to file."
   echo "  get <category> <name>    Get information for device name in the category and"
   echo "                           output in cacti parsable format. Information is"
   echo "                           described in the config file."
   echo
   echo "The configuration file ($CONF) describes categories"
   echo "and and devices."
   exit 1
}

main()
{
   case "$1" in
   "update")
      wget -q -O $CACHE.tmp $URL
      mv $CACHE.tmp $CACHE
   ;;
   "get")
      shift
      id=$( get_config $1 $2 )
      if test "$id" != ""
      then
         get_data $id
         echo
      else
         echo "Not found"
      fi
   ;;
   *)
      usage
   ;;
   esac
}

main $*
