Browse Source

Merge branch 'master' of ssh://git.fixme.ch:1337/fixme-nice-api

Rorist 7 years ago
parent
commit
c6551385f9
2 changed files with 127 additions and 78 deletions
  1. 121 76
      main.js
  2. 6 2
      trigger.html

+ 121 - 76
main.js

@@ -1,29 +1,58 @@
+/*jslint  browser:true*/
+/*global  jsonParse*/
+/*global  confirm*/
+
 var msgBlock   =  document.getElementById("msg-block");
 var loadBlock  =  document.getElementById("loading-block");
 var closeBlock =  document.getElementById("close-block");
 var openBlock  =  document.getElementById("open-block");
 
-function onPageLoad() {
-    toggleDiv(openBlock, 0);
-    toggleDiv(closeBlock, 0);
-    toggleDiv(msgBlock, 0);
-    updateSpaceInformation();
-    refresh_counter = setTimeout("onPageLoad()", 60 *1000);
-    checkHours(document.getElementById("hours"));
+function toggleDiv(div, show) {
+    "use strict";
+    if (show) {
+        div.style.visibility = "visible";
+        div.style.height = "auto";
+    } else {
+        div.style.visibility = "hidden";
+        div.style.height = "0px";
+    }
+}
+
+function displayError() {
+    "use strict";
+    msgBlock.style.background = "red";
+    msgBlock.innerHTML = "Error connecting to fixme server";
+}
+
+var baseUrl = "https://fixme.ch/cgi-bin/twitter.pl";
+function checkHours(hoursForm) {
+    "use strict";
+    var hoursOpen = hoursForm.value,
+        formParent = hoursForm.parentElement,
+        openButton = formParent.openbutton;
+    hoursOpen = Math.floor(hoursOpen);
+    //TODO throw error if parentElement is null
+    if (isNaN(hoursOpen) || hoursOpen < 1) {
+        openButton.disabled = true;
+    } else {
+        openButton.disabled = false;
+    }
 }
 
 function changeHour(inc) {
+    "use strict";
     var val = document.getElementById("hours");
     if (inc) {
-        val.value = (val.value-0) + 1;
-    } else if((val.value-0)>1) {
-        val.value = (val.value-0) - 1;
+        val.value = parseInt(val.value, 10) + 1;
+    } else if (val.value > 1) {
+        val.value = parseInt(val.value, 10) - 1;
     }
     checkHours(val);
 }
 
 var apiUrl = "https://fixme.ch/cgi-bin/spaceapi.py";
 function updateSpaceInformation() {
+    "use strict";
     toggleDiv(msgBlock, 0);
     toggleDiv(loadBlock, 1);
 
@@ -31,89 +60,71 @@ function updateSpaceInformation() {
 
     xhr.open("GET", apiUrl, true);
 
-    xhr.onreadystatechange = function() {
-
-      if ( xhr.readyState == 4) {
-        if (xhr.status == 200) {
-            try {
-                var parsed_text = jsonParse(xhr.responseText);
-                var isOpen = parsed_text.open;
-                var open_duration = parsed_text.duration;
-                var closing_time = new Date(Number(parsed_text.lastchange) * 1000);
-                closing_time.setHours(closing_time.getHours() + open_duration);
-            } catch (err) {
-                //json parsing failed or doesn't contain the correct element
-                alert(err);
-                displayError();
-                return;
-            }
-            if (isOpen) {
-                toggleDiv(openBlock, 0);
-                toggleDiv(closeBlock, 1);
-            } else {
-                toggleDiv(openBlock, 1);
-                toggleDiv(closeBlock, 0);
-                document.hoursform.hours.focus();
-            }
-            var diff_time = Number(closing_time.getTime()) - new Date().getTime();
-            if (diff_time > 0) {
-                update_date(new Date(diff_time));
+    xhr.onreadystatechange = function () {
+        if (xhr.readyState === 4) {
+            if (xhr.status === 200) {
+                try {
+                    var parsed_text = jsonParse(xhr.responseText),
+                        isOpen = parsed_text.open,
+                        open_duration = parsed_text.duration,
+                        closing_time = new Date(Number(parsed_text.lastchange) * 1000);
+                    closing_time.setHours(closing_time.getHours() + open_duration);
+                } catch (err) {
+                    /*json parsing failed or doesn't contain the correct element
+                    alert(err);*/
+                    displayError();
+                    return;
+                }
+                if (isOpen) {
+                    toggleDiv(openBlock, 0);
+                    toggleDiv(closeBlock, 1);
+                } else {
+                    toggleDiv(openBlock, 1);
+                    toggleDiv(closeBlock, 0);
+                    document.hoursform.hours.focus();
+                }
+                var diff_time = Number(closing_time.getTime()) - new Date().getTime();
+                if (diff_time > 0) {
+                    update_date(new Date(diff_time));
+                } else {
+                    update_date(new Date(0));
+                }
+                msgBlock.innerHTML = parsed_text.status;
+                toggleDiv(msgBlock, 1);
+                toggleDiv(loadBlock, 0);
             } else {
-                update_date(new Date(0));
+                displayError();
             }
-            msgBlock.innerHTML = parsed_text.status;
-            toggleDiv(msgBlock, 1);
-            toggleDiv(loadBlock, 0);
-        } else {
-            displayError();
-        }
       }
     };
     xhr.send(null);
 }
 
-function toggleDiv(div, show){
-    if (show) {
-        div.style.visibility = "visible";
-        div.style.height = "auto";
-    } else {
-        div.style.visibility = "hidden";
-        div.style.height = "0px";
-    }
-}
-
-function displayError() {
-    msgBlock.style.background = "red";
-    msgBlock.innerHTML = "Error connecting to fixme server";
+function onPageLoad() {
+    "use strict";
+    toggleDiv(openBlock, 0);
+    toggleDiv(closeBlock, 0);
+    toggleDiv(msgBlock, 0);
+    updateSpaceInformation();
+    setInterval(onPageLoad, 60 * 1000);
+    checkHours(document.getElementById("hours"));
 }
 
-var baseUrl = "https://fixme.ch/cgi-bin/twitter.pl";
-function checkHours(hoursForm) {
-    var hoursOpen = hoursForm.value;
-    var hoursOpen = Math.floor(hoursOpen);
-    var formParent = hoursForm.parentElement;
-    //TODO throw error if parentElement is null
-    var openButton = formParent.openbutton;
-    if (isNaN(hoursOpen) || hoursOpen < 1) {
-        openButton.disabled = true;
-    } else {
-        openButton.disabled = false;
-    }
-}
 
 function openSpace(hoursOpen) {
-    if(hoursOpen==undefined){
+    "use strict";
+    if (hoursOpen === undefined) {
         var hoursForm = document.hoursform.hours;
         hoursOpen = hoursForm.value;
     }
     var confirm_return = confirm("Are you sure you want to open the hackerspace ?");
     if (confirm_return) {
-        var requestUrl = baseUrl + "?do=custom&hours=" + hoursOpen;
-        var requestObject = new XMLHttpRequest();
+        var requestUrl = baseUrl + "?do=custom&hours=" + hoursOpen,
+            requestObject = new XMLHttpRequest();
         requestObject.open("GET", requestUrl, true);
-        requestObject.onreadystatechange = function() {
-            if ( requestObject.readyState == 4) {
-                if (requestObject.status == 200) {
+        requestObject.onreadystatechange = function () {
+            if (requestObject.readyState === 4) {
+                if (requestObject.status === 200) {
                     msgBlock.innerHTML = requestObject.responseText;
                 }
             }
@@ -123,11 +134,13 @@ function openSpace(hoursOpen) {
 }
 
 function openSpace2(){
+    "use strict";
     var hours = prompt("Hours:", 1);
     openSpace(hours);
 }
 
 function closeSpace() {
+    "use strict";
     var requestUrl = baseUrl + "?do=close";
     var requestObject = new XMLHttpRequest();
     var confirm_value = confirm("Are you sure you want to close the space ?");
@@ -146,6 +159,8 @@ function closeSpace() {
 }
 
 function update_date(date) {
+
+    "use strict";
     var hours = String(date.getHours() - 1);
     if (hours.length == 1) {
         hours = "0" + hours;
@@ -166,9 +181,39 @@ function update_date(date) {
 }
 
 function setTextForId(id, text) {
+    "use strict";
     if(document.all){
          document.getElementById(id).innerText = text;
     } else{
         document.getElementById(id).textContent = text;
     }
 }
+
+
+function switchTheLight(red, green, blue) {
+    "set strict";
+    var requestUrl = "http://led.fixme.ch/rgb/";
+    var requestObject = new XMLHttpRequest();
+    requestObject.open("POST", requestUrl, true);
+    requestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+    requestObject.send("red=" + red + "&green=" + green + "&blue=" + blue);
+}
+
+function switchTheLightOn() {
+    "set strict";
+  switchTheLight(255, 255, 255)
+}
+
+function switchTheLightOff() {
+    "set strict";
+  switchTheLight(0, 0, 0)
+}
+
+
+function switchTheLightToPolice() {
+  red = function() { switchTheLight(255, 0, 0) }
+  blue = function() { switchTheLight(0, 0, 255) }
+
+  window.setInterval(red, 100);
+  window.setTimeout(function() { window.setInterval(blue, 100)}, 50)
+}

+ 6 - 2
trigger.html

@@ -13,7 +13,7 @@
 
   <div class='central-block'>
       <div id="loading-block">Loading ...</div>
-      
+
       <div id="open-block" style="visibility:hidden;">
         <form name="hoursform" id="hoursform">
           <label for="hours">Hours : </label>
@@ -31,7 +31,7 @@
           class="button">
           <input type="button" onclick="openSpace2()" value="re-Open"
           class="button">
-        </form>
+          <br>
         The space is still open for :
         <div class="numbers">
           <span class="number" id="first-hour">2</span> <span class=
@@ -40,6 +40,10 @@
           "first-minute">2</span> <span class="number" id=
           "second-minute">2</span>
         </div>
+          <input type="button" onclick="switchTheLightOn()" value="Switch the light on" class="button">
+          <input type="button" onclick="switchTheLightOff()" value="Switch the light off" class="button">
+          <input type="button" onclick="switchTheLightToPolice()" value="Police!" class="button">
+        </form>
       </div>
 
       <div id="msg-block"></div>