From f509f01b85b420327de12c2e01eb5d9ba61d813e Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Tue, 14 Jan 2014 01:53:56 +0000 Subject: [PATCH] mswitch abstract --- raspberrypi_gpio_relay_licht/mswitch.cgi | 30 +++++++++--- raspberrypi_gpio_relay_licht/switch.cgi | 73 ++++-------------------------- 2 files changed, 31 insertions(+), 72 deletions(-) diff --git a/raspberrypi_gpio_relay_licht/mswitch.cgi b/raspberrypi_gpio_relay_licht/mswitch.cgi index adf1d19..08d1bcd 100755 --- a/raspberrypi_gpio_relay_licht/mswitch.cgi +++ b/raspberrypi_gpio_relay_licht/mswitch.cgi @@ -1,14 +1,28 @@ #!/bin/zsh -VALID_ONOFF_IDS=(4 17 18 21 22 23) +VALID_ONOFF_IDS=(ceiling1 ceiling2 ceiling3 ceiling4 ceiling5 ceiling6) +VALID_GPIO_IDS=(4 23 18 17 22 21) +local -A IDGPIOMAP +local -A GPIOIDMAP +IDGPIOMAP[ceiling1]=4 +IDGPIOMAP[ceiling2]=23 +IDGPIOMAP[ceiling3]=18 +IDGPIOMAP[ceiling4]=17 +IDGPIOMAP[ceiling5]=22 +IDGPIOMAP[ceiling6]=21 GPIOPATH=/sys/class/gpio/gpio + +for k v in ${(kv)IDGPIOMAP}; do + GPIOIDMAP[$v]=$k +done + local -A GPIOS for QUERY in `echo $QUERY_STRING | tr '&' ' '`; do for VALIDID in $VALID_ONOFF_IDS; do if [ "$QUERY" = "$VALIDID=1" ]; then - GPIOS[$VALIDID]=1 + GPIOS[$IDGPIOMAP[$VALIDID]]=1 elif [ "$QUERY" = "$VALIDID=0" ]; then - GPIOS[$VALIDID]=0 + GPIOS[$IDGPIOMAP[$VALIDID]]=0 fi done if [ "$QUERY" = "mobile=1" ]; then @@ -21,7 +35,8 @@ done print_gpio_state() { - GPIOVALUE=$(cat "${GPIOPATH}${1}/value") + GPIO=${IDGPIOMAP[$1]} + GPIOVALUE=$(cat "${GPIOPATH}${GPIO}/value") if [[ $GPIOVALUE == "0" ]]; then echo -n "true" else @@ -30,7 +45,8 @@ print_gpio_state() { } gpio_is_on() { - GPIOVALUE=$(cat "${GPIOPATH}${1}/value") + GPIO=${IDGPIOMAP[$1]} + GPIOVALUE=$(cat "${GPIOPATH}${GPIO}/value") [ "$GPIOVALUE" = "0" ] } @@ -39,10 +55,10 @@ echo "" local -a GPIOSTATES for CHECKID in $VALID_ONOFF_IDS; do - VAL=$GPIOS[$CHECKID] + VAL=$GPIOS[$IDGPIOMAP[$CHECKID]] if [[ $VAL == 1 || $VAL == 0 ]]; then [[ $VAL == 1 ]] && VAL=0 || VAL=1 - echo "$VAL" > "${GPIOPATH}${CHECKID}/value" + echo "$VAL" > "${GPIOPATH}${IDGPIOMAP[$CHECKID]}/value" fi GPIOSTATES+=(\"${CHECKID}\":"$(print_gpio_state $CHECKID)") done diff --git a/raspberrypi_gpio_relay_licht/switch.cgi b/raspberrypi_gpio_relay_licht/switch.cgi index 167c64b..ef75238 100755 --- a/raspberrypi_gpio_relay_licht/switch.cgi +++ b/raspberrypi_gpio_relay_licht/switch.cgi @@ -20,48 +20,15 @@ for QUERY in `echo $QUERY_STRING | tr '&' ' '`; do done done - -GPIOPATH=/sys/class/gpio/gpio -VALID_ONOFF_IDS="4 23 18 17 22 21" +VALID_ONOFF_IDS="ceiling1 ceiling2 ceiling3 ceiling4 ceiling5 ceiling6" VALID_SEND_IDS="" -print_gpio_state() { - GPIOVALUE=$(cat "${GPIOPATH}${1}/value") - if [ "$GPIOVALUE" = "0" ]; then - echo "ON" - else - echo "Off" - fi -} - -gpio_is_on() { - GPIOVALUE=$(cat "${GPIOPATH}${1}/value") - [ "$GPIOVALUE" = "0" ] -} - -if [ "$POWER" = "1" -o "$POWER" = "0" ]; then - for CHECKID in $VALID_ONOFF_IDS ; do - if [ "$CHECKID" = "$ID" ]; then - [ $POWER = 1 ] && POWER=0 || POWER=1 - echo "$POWER" > "${GPIOPATH}${ID}/value" - echo "Content-type: text/html" - echo "" - echo "" - echo "" - echo "Realraum Relay Switch" - echo '' - echo "" - exit 0 - fi - done -fi - -DESC_23="Decke Leinwand (S)" -DESC_4="Decke E-Labor (SSW)" -DESC_18="Decke Eingang (W)" -DESC_17="Decke Durchgang (O)" -DESC_22="Decke Auslage (N)" -DESC_21="Decke Küche (NNO)" +DESC_ceiling1="Decke Leinwand (S)" +DESC_ceiling2 ="Decke E-Labor (SSW)" +DESC_ceiling3 ="Decke Eingang (W)" +DESC_ceiling4 ="Decke Durchgang (O)" +DESC_ceiling5 ="Decke Auslage (N)" +DESC_ceiling6 ="Decke Küche (NNO)" echo "Content-type: text/html" echo "" @@ -105,16 +72,8 @@ echo 'function sendMultiButton( str ) { }' echo 'setInterval("updateButtons(\"/cgi-bin/mswitch.cgi\");", 30*1000);' +echo 'updateButtons("/cgi-bin/mswitch.cgi");' -echo 'function sendButton( onoff, btn )' -echo '{' -echo ' var req = new XMLHttpRequest();' -echo ' url = "/cgi-bin/switch.cgi?power="+onoff+"&id="+btn;' -echo ' req.open("GET", url ,false);' -echo ' //google chrome workaround' -echo ' req.setRequestHeader("googlechromefix","");' -echo ' req.send(null);' -echo '}' echo '' echo '