update kiosk sensor code for spaceapi 0.13
[svn42.git] / raspberry-kiosk / kiosk.js
index d453cda..0c90e4e 100644 (file)
@@ -1,24 +1,38 @@
+function min(a,b)
+{
+       if (a > b)
+               return b;
+       else
+               return a;
+}
+
 function writeGooglePlusEvents(data, elem)
 {
   var ghtml = "";
+  //var gplusimgwidth = parseInt($('<table class="gplusimg" />').css("width"));
+  var gplusimgwidth = 495;
+  var minimgwidth=78;
   for (var i=0; i< data.items.length; i++)
   {
     var item = data.items[i];
     var noteobj = item.object;
     var attach = noteobj.attachments;
     var notetxt = noteobj.content;
-    var noteimg = false;
+    var noteimgs = new Array();
     if (attach)
     {
       for (var a=0; a<attach.length; a++)
       {
         if ( attach[a].objectType == "album")
         {
-          noteimg = attach[a].thumbnails[0].image.url;
+          for (var t=0; t<attach[a].thumbnails.length; t++)
+          {
+            noteimgs.push(attach[a].thumbnails[t].image.url);
+          }
         }
         else if ( attach[a].objectType == "photo")
         {
-          noteimg = attach[a].image.url;
+          noteimgs.push(attach[a].image.url);
         }
         else if (attach[a].objectType == "event")
         {
@@ -29,9 +43,36 @@ function writeGooglePlusEvents(data, elem)
     ghtml += '<div class="gpluspost">'
     ghtml += '<img class="gplusactor" src="'+item.actor.image.url+'"/><p class="gplustimestamp">'+item.updated.substring(0,16).replace("T"," ")+'</p>';
     ghtml += '<p class="gplustxt">'+notetxt+'</p>';
-    if (noteimg)
-    {
-      ghtml += '<p class="gplusimg"><img class="gplusimg" src="'+noteimg+'"/></p>';
+    if (noteimgs.length>0)
+    { 
+      var bigimglimit;
+      ghtml += '<table class="gplusimg" cellspacing="0"><tr>';
+      if (noteimgs.length > 3)
+      {
+              var gplusimgmaxwidth = gplusimgwidth - (minimgwidth * ((noteimgs.length -1) / 3));
+             bigimglimit = "max-height:"+(minimgwidth*3)+"px; max-width:"+gplusimgmaxwidth+"px;"
+             ghtml += '<td><img class="gplusimg" style="'+bigimglimit+'" src="'+noteimgs[0]+'"/></td>';
+             for (var ni=1; ni<noteimgs.length; ni+=3)
+             {
+                 ghtml += '<td>';
+                 var niimax = min(noteimgs.length, ni+3);
+                 for (var nii=ni; nii<niimax; nii++)
+                 {
+                       ghtml += '<img class="gplusimg" style="max-width:'+minimgwidth+'px; max-height:'+minimgwidth+'px;" src="'+noteimgs[nii]+'"/><br/>';
+                 }
+                 ghtml += '</td>';
+             }
+      }
+      else
+      {
+             var gplusimgmaxwidth = gplusimgwidth / noteimgs.length;
+            bigimglimit = "max-width:"+gplusimgmaxwidth+"px;"
+             for (var ni=0; ni<noteimgs.length; ni++)
+            {
+               ghtml += '<td><img class="gplusimg" style="'+bigimglimit+'" src="'+noteimgs[ni]+'"/></td>';
+            }
+      }
+      ghtml += '</tr></table>';
     }
     ghtml += '</div>';
   }
@@ -49,6 +90,22 @@ function loadGooglePlusEvents()
   });
 }
 
+function weekday2str(dow)
+{
+    var weekday=new Array(7);
+    weekday[0]="So";
+    weekday[1]="Mo";
+    weekday[2]="Di";
+    weekday[3]="Mi";
+    weekday[4]="Do";
+    weekday[5]="Fr";
+    weekday[6]="Sa";
+    weekday[7]="So";
+    if (dow >=0 && dow <=7)
+        return weekday[dow];
+    else
+        return "";
+}
 
 function writeCalendar(data, elem)
 {
@@ -63,13 +120,20 @@ function writeCalendar(data, elem)
       }
       else
       {
+        var weekday = weekday2str((new Date(dt)).getDay());
         var stime = data[s].starttime;
+        var month = data[s].start.substring(5,7);
+        if (month[0] == '0')
+          month = month[1];
+        var dayofmonth = data[s].start.substring(8,10);
+          if (dayofmonth[0] == '0')
+          dayofmonth = dayofmonth[1];
         while (stime.substring(stime.length-3,stime.length) == ":00")
         {
           stime=stime.substring(0,stime.length-3);
         }
         if (stime.length <= 2) { stime+="h"; }
-        when = data[s].start.substring(8,10)+"."+data[s].start.substring(5,7) +", "+stime;
+        when = weekday + " " + dayofmonth+"."+month +", "+stime;
       }
       calhtml += '<li class="level1">'+when+' - <span class="r3red">'+data[s].title+'</span></li>'+"\n";
     }
@@ -104,20 +168,47 @@ function writeAnwesenheitStatus(data)
 
   if (data.sensors)
   {
-    for (var s=0; s<data.sensors.length;s++)
+    if (data.sensors.temperature)
     {
-      $.each( data.sensors[s], function(stype, std){
-        $.each( std, function(swhere, svalue){
-           sensorstd+='<td class="sensorstatus"><b>'+stype+'</b><br/>'+swhere+': '+svalue+'</td>';
-        });
+      $.each( data.sensors.temperature, function(s, sensorobj) {
+        sensorstd+='<td style="background-color:white; height:42px; text-align:center; vertical-align:middle; display:table-cell;"><b>Temperatur</b><br/>'+sensorobj.location+': '+sensorobj.value.toFixed(2)+sensorobj.unit+'</td>';
       });
     }
-  }
-  if (sensorstd != "")
-  {
-    sensorshtml='<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>'+sensorstd+'</tr></table>';
-    document.getElementById('sensor_status').innerHTML=sensorshtml;
-  }
+    if (data.sensors.ext_illumination)
+    {
+      $.each( data.sensors.ext_illumination, function(s, sensorobj) {
+        sensorstd+='<td style="background-color:white; height:42px; text-align:center; vertical-align:middle; display:table-cell;"><b>Licht</b><br/>'+sensorobj.location+': '+sensorobj.value+'</td>';
+      });
+    }
+    if (data.sensors.door_locked)
+    {
+      $.each( data.sensors.door_locked, function(s, sensorobj)  {
+        var lockstatus="Unlocked";
+        if (sensorobj.value) { lockstatus = "Locked"; }
+        sensorstd+='<td style="background-color:white; height:42px; text-align:center; vertical-align:middle; display:table-cell;"><b>Türschloß</b><br/>'+sensorobj.location+': '+lockstatus+'</td>';
+      });
+    }
+    if (data.sensors.ext_door_ajar)
+    {
+      $.each( data.sensors.ext_door_ajar, function(s, sensorobj)  {
+        var lockstatus="Shut";
+        if (sensorobj.value) { lockstatus = "Ajar"; }
+        sensorstd+='<td style="background-color:white; height:42px; text-align:center; vertical-align:middle; display:table-cell;"><b>Türkontakt</b><br/>'+sensorobj.location+': '+lockstatus+'</td>';
+      });
+    }
+    if (data.sensors.ext_dust)
+    {
+      $.each( data.sensors.ext_dust, function(s, sensorobj) {
+        sensorstd+='<td style="background-color:white; height:42px; text-align:center; vertical-align:middle; display:table-cell;"><b>Staub</b><br/>'+sensorobj.location+': '+sensorobj.value+sensorobj.unit+'</td>';
+      });
+
+    }    
+    if (sensorstd != "")
+    {
+      sensorshtml='<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>'+sensorstd+'</tr></table>';
+      document.getElementById('sensor_status').innerHTML=sensorshtml;
+    }
+  }  
 }
 
 function updateAnwesenheitStatus()
@@ -130,18 +221,6 @@ function updateAnwesenheitStatus()
  //req.send(null);
  var jqxhr = $.getJSON(url, writeAnwesenheitStatus);
 }
-var anwesenheit_timer = window.setInterval("updateAnwesenheitStatus()", 10000);
-
-function updateSensors()
-{
-  reloadImg(document.getElementById("tempsensor"));
-  reloadImg(document.getElementById("movementsensor"));
-  reloadImg(document.getElementById("lightsensor"));
-}
-
-var timer;
-var seconds = 0;
-var schedule = Array()
 
 $(document).ready(function()
 {
@@ -150,9 +229,9 @@ $(document).ready(function()
   updateAnwesenheitStatus();
   loadCalendar();
   loadGooglePlusEvents();
-  setInterval("updateAnwesenheitStatus()", 10000);
+  setInterval("updateAnwesenheitStatus()", 10*1000);
   setInterval("loadCalendar()", 123*1000);
-  setInterval("updateSensors()",125*1000);
+  setInterval("updateSensors()",145*1000);
   setInterval("loadGooglePlusEvents()", 1207*1000);
 });
 
@@ -181,7 +260,7 @@ function highlightEntry(idx, color, value)
   }
 }
 
-
+var seconds = 0;
 function clock(now)
 {
   var now = new Date(new Date().valueOf() + 300);
@@ -192,39 +271,56 @@ function clock(now)
   }
 }
 
-//function updateSchedule()
-//{
-//  $.ajax({type: "GET", url: "/export/schedules.php", data: "days=3&start=-1", dataType: "xml", error: showError, success: parseXml});
-//}
-
 function showError(XMLHttpRequest, textStatus, errorThrown)
 {
   alert("Error: " + textStatus);
 }
 
+function updateSensors()
+{
+  reloadImg(document.getElementById("tempsensor"));
+  reloadImg(document.getElementById("movementsensor"));
+  reloadImg(document.getElementById("lightsensor"));
+}
+
 function IsImageOk(img) {
-    if (!img.complete) {
+    if (!img.complete)
+    {
         return false;
     }
-    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
+    if (typeof img.naturalWidth == "undefined" || (typeof img.naturalWidth == "number" && img.naturalWidth == 0))
+    {
         return false;
     }
     return true;
 }
 
+var img_orig_src = {};
+function reloadImgAlt(element)
+{
+    if(element.complete)
+    {
+      var new_image = new Image();
+      //set up the new image
+      new_image.id = element.id;
+      new_image.className = element.className;
+      if (! img_orig_src[element.id])
+      {
+        img_orig_src[element.id] = element.src;
+      }
+      new_image.src = img_orig_src[element.id] + "?dt="+Math.floor(new Date().getTime() / 1000).toString();
+      if (IsImageOk(new_image))
+      {
+        element.parentNode.insertBefore(new_image,element);
+        element.parentNode.removeChild(element);
+      }
+    }
+}
 function reloadImg(element)
 {
-    //var image = document.getElementById("theText");
-    if(element.complete) {
-        var new_image = new Image();
-        //set up the new image
-        new_image.id = element.id;
-        new_image.className = element.className;
-        new_image.src = element.src;
-       if (IsImageOk(new_image))
-       {
-               element.parentNode.insertBefore(new_image,element);
-               element.parentNode.removeChild(element);
-       }
+    if (! img_orig_src[element.id])
+    {
+      img_orig_src[element.id] = element.src;
     }
+    element.src = img_orig_src[element.id] + "?dt="+Math.floor(new Date().getTime() / 1000).toString();
 }