mswitch abstract
authorBernhard Tittelbach <xro@realraum.at>
Tue, 14 Jan 2014 01:53:56 +0000 (01:53 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Tue, 14 Jan 2014 01:53:56 +0000 (01:53 +0000)
raspberrypi_gpio_relay_licht/mswitch.cgi
raspberrypi_gpio_relay_licht/switch.cgi

index adf1d19..08d1bcd 100755 (executable)
@@ -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
index 167c64b..ef75238 100755 (executable)
@@ -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 "<html>"
-      echo "<head>"
-      echo "<title>Realraum Relay Switch</title>"
-      echo '<script type="text/javascript">window.location="/cgi-bin/switch.cgi";</script>'
-      echo "</head></html>"
-      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&uuml;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&uuml;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 '</script>'
 echo '<style>'
 echo 'div.switchbox {'
@@ -243,22 +202,6 @@ echo "<div style=\"float:left; border:1px solid black; margin-right:2ex; margin-
 
 ITEMCOUNT=0
 
-for DISPID in $VALID_SEND_IDS; do
-  ITEMCOUNT=$((ITEMCOUNT+1))
-  NAME="$(eval echo \$DESC_$DISPID)"
-  [ -z "$NAME" ] && NAME=$DISPID
-
-  echo "<div class=\"switchbox\">"
-  echo "<span class=\"alignbuttonsleft\">"
-  echo " <button class=\"sendbutton\" onClick='sendButton(\"on\",\"$DISPID\");'> </button>"
-  echo "</span>"
-  echo "<div class=\"switchnameright\">$NAME</div>"
-  echo "</div>"
-  if [ "$NOFLOAT" = "1" -a $((ITEMCOUNT % 2 )) -ne 1 ]; then
-    echo "<br/>"
-  fi
-
-done
 echo "</div>"
 fi
 echo "</div>"