update and display button/light state
authorBernhard Tittelbach <xro@realraum.at>
Tue, 22 Oct 2013 23:49:35 +0000 (23:49 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Tue, 22 Oct 2013 23:49:35 +0000 (23:49 +0000)
router_gpio_relay_licht/mswitch.cgi
router_gpio_relay_licht/switch.cgi

index 94176ce..c389c38 100755 (executable)
@@ -46,11 +46,11 @@ for CHECKID in $VALID_ONOFF_IDS; do
   if [ $VAL == 1 -o $VAL == 0 ]; then
     echo "$VAL" > "${GPIOPATH}${CHECKID}/value"
   fi
-  echo -n "$CHECKID:"
+  echo -n "\"$CHECKID\":"
   print_gpio_state $CHECKID
   echo ","
 done
-echo "-1:false}"
+echo "\"-1\":false}"
 #      echo "<html>"
 #      echo "<head>"
 #      echo "<title>Realraum Relay Switch</title>"
index 926b941..01dce98 100755 (executable)
@@ -24,6 +24,7 @@ done
 GPIOPATH=/sys/class/gpio/gpio
 #VALID_ONOFF_IDS="7 18 20 29"
 VALID_ONOFF_IDS="7 18 20"
+VALID_SEND_IDS=""
 
 print_gpio_state() {
   GPIOVALUE=$(cat "${GPIOPATH}${1}/value")
@@ -40,7 +41,7 @@ gpio_is_on() {
 }
 
 if [ "$POWER" == "1" -o "$POWER" == "0" ]; then
-  for CHECKID in $VALID_ONOFF_IDS ; do 
+  for CHECKID in $VALID_ONOFF_IDS ; do
     if [ "$CHECKID" == "$ID" ]; then
       echo "$POWER" > "${GPIOPATH}${ID}/value"
       echo "Content-type: text/html"
@@ -66,10 +67,43 @@ echo "<html>"
 echo "<head>"
 echo "<title>Realraum Relay Power</title>"
 echo '<script type="text/javascript">'
-echo 'function updateButtons(data)
-{
 
+echo 'function callbackUpdateButtons(req) {
+  if (req.status != 200) {
+    return;
+  }
+  var data = JSON.parse(req.responseText);
+  for (var keyid in data) {
+    on_btn = document.getElementById("onbtn_"+keyid);
+    off_btn = document.getElementById("offbtn_"+keyid);
+    if (on_btn && off_btn)
+    {
+      on_btn.className = "onbutton";
+      off_btn.className = "offbutton";
+      if (data[keyid])
+      { on_btn.className += " enableborder"; }
+      else
+      { off_btn.className += " enableborder"; }
+    }
+  }
 }'
+
+echo 'function updateButtons(uri) {
+  var req = new XMLHttpRequest;
+  req.overrideMimeType("application/json");
+  req.open("GET", uri, true);
+  req.onload  = function() {callbackUpdateButtons(req)};
+  req.setRequestHeader("googlechromefix","");
+  req.send(null);
+}'
+
+echo 'function sendMultiButton( str ) {
+ url = "/cgi-bin/mswitch.cgi?"+str;
+  updateButtons(url);
+}'
+
+echo 'setInterval("updateButtons(\"/cgi-bin/mswitch.cgi\");", 30*1000);'
+
 echo 'function sendButton( onoff, btn )'
 echo '{'
 echo ' var req = new XMLHttpRequest();'
@@ -91,7 +125,7 @@ echo '    border:1px solid black;'
 #echo '    height: 32px;'
 echo '    padding:0;'
 echo '}'
-  
+
 echo 'div.switchnameleft {'
 echo '    width:12em; display:inline-block; vertical-align:middle; margin-left:3px;'
 echo '}'
@@ -148,14 +182,14 @@ for DISPID in $VALID_ONOFF_IDS; do
   NAME="$(eval echo -n \$DESC_$DISPID)"
   [ -z "$NAME" ] && NAME=$DISPID
 
-echo "<div class=\"switchbox\">"
+  echo "<div class=\"switchbox\">"
   echo "<span class=\"alignbuttonsleft\">"
   if gpio_is_on $DISPID; then
-  echo " <button id=\"onbtn_$DISPID\" class=\"onbutton enableborder\" onClick='sendButton(\"1\",\"$DISPID\");'>On</button>"
-  echo " <button id=\"offbtn_$DISPID\" class=\"offbutton\" onClick='sendButton(\"0\",\"$DISPID\");'>Off</button>"
+  echo " <button id=\"onbtn_$DISPID\" class=\"onbutton enableborder\" onClick='sendMultiButton(\"$DISPID=1\");'>On</button>"
+  echo " <button id=\"offbtn_$DISPID\" class=\"offbutton\" onClick='sendMultiButton(\"$DISPID=0\");'>Off</button>"
   else
-  echo " <button id=\"onbtn_$DISPID\" class=\"onbutton\" onClick='sendButton(\"1\",\"$DISPID\");'>On</button>"
-  echo " <button id=\"offbtn_$DISPID\" class=\"offbutton enableborder\" onClick='sendButton(\"0\",\"$DISPID\");'>Off</button>"
+  echo " <button id=\"onbtn_$DISPID\" class=\"onbutton\" onClick='sendMultiButton(\"$DISPID=1\");'>On</button>"
+  echo " <button id=\"offbtn_$DISPID\" class=\"offbutton enableborder\" onClick='sendMultiButton(\"$DISPID=0\");'>Off</button>"
   fi
   echo "</span>"
   echo -n "<div class=\"switchnameright\">$NAME</div>"
@@ -163,15 +197,29 @@ echo "<div class=\"switchbox\">"
 #  print_gpio_state $DISPID
 #  echo ")</div>"
   echo "</div>"
-  
+
   if [ "$NOFLOAT" == "1" ]; then
     echo "<br/>"
-  fi 
+  fi
 done
 
+echo "<div class=\"switchbox\">"
+echo "<span class=\"alignbuttonsleft\">"
+echo -n " <button class=\"onbutton\" onClick='sendMultiButton(\""
+for DISPID in $VALID_ONOFF_IDS; do echo -n "$DISPID=1&"; done
+echo "\");'>On</button>"
+echo -n " <button class=\"offbutton\" onClick='sendMultiButton(\""
+for DISPID in $VALID_ONOFF_IDS; do echo -n "$DISPID=0&"; done
+echo "\");'>Off</button>"
+echo "</span>"
+echo -n "<div class=\"switchnameright\">Alle</div>"
+echo "</div>"
+if [ "$NOFLOAT" == "1" ]; then
+  echo "<br/>"
+fi
 echo "</div>"
 
-if [ "$MOBILE" != "1" ]; then                                                             
+if [ "$MOBILE" != "1" -a -n "$VALID_SEND_IDS" ]; then
 
 echo "<div style=\"float:left; border:1px solid black; margin-right:2ex; margin-bottom:2ex;\">"
 
@@ -181,40 +229,16 @@ 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>"
-
- if [ "$NOFLOAT" == "1" ]; then
-    echo "<div style=\"float:left; border:1px solid black;\">"
-    tail -n+107 /www/ymhremote.html | head -n 5
-    echo "</div>"
- fi 
 
-echo "<div style=\"float:left; border:1px solid black; margin-top:5px;\">"
-for DISPID in $VALID_BANSHEE_IDS $VALID_CAM_MOTOR_IDS; do
-  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" ]; then
+  if [ "$NOFLOAT" == "1" -a $((ITEMCOUNT % 2 )) -ne 1 ]; then
     echo "<br/>"
-  fi 
+  fi
 
 done
 echo "</div>"