display state and switch multiple at once
[svn42.git] / router_gpio_relay_licht / mswitch.cgi
diff --git a/router_gpio_relay_licht/mswitch.cgi b/router_gpio_relay_licht/mswitch.cgi
new file mode 100755 (executable)
index 0000000..6d0d587
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+VALID_ONOFF_IDS="7 18 20"
+GPIOPATH=/sys/class/gpio/gpio
+
+for QUERY in `echo $QUERY_STRING | tr '&' ' '`; do
+  for VALIDID in $VALID_ONOFF_IDS; do
+    if [ "$QUERY" == "$VALIDID=1" ]; then
+      eval "GPIO_$VALIDID"=1
+    elif [ "$QUERY" == "$VALIDID=0" ]; then
+      eval "GPIO_$VALIDID=0"
+    elif [ "$QUERY" == "$VALIDID=q" ]; then
+      eval "GPIO_$VALIDID=q"
+    fi
+  done
+  if [ "$QUERY" == "mobile=1" ]; then
+    MOBILE='1'
+    NOFLOAT='1'
+  elif [ "$QUERY" == "nofloat=1" ]; then
+    NOFLOAT='1'
+  fi
+done
+
+
+print_gpio_state() {
+  GPIOVALUE=$(cat "${GPIOPATH}${1}/value")
+  if [ "$GPIOVALUE" = "1" ]; then
+    echo "1"
+  else
+    echo "0"
+  fi
+}
+
+gpio_is_on() {
+  GPIOVALUE=$(cat "${GPIOPATH}${1}/value")                                                                                                                                    
+  [ "$GPIOVALUE" = "1" ]
+}
+
+echo "Content-type: text/html"
+echo ""
+
+
+for CHECKID in $VALID_ONOFF_IDS; do
+  VAL=""
+  VAL="$(eval echo \$GPIO_$CHECKID)"
+  if [ $VAL == 1 -o $VAL == 0 ]; then
+    echo "$VAL" > "${GPIOPATH}${CHECKID}/value"
+  fi
+  echo -n "$CHECKID:"
+  print_gpio_state $CHECKID
+done
+
+#      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
+