use sensors shmcache
[svn42.git] / raspberry-kiosk / kiosk.js
index b090f24..7315033 100644 (file)
@@ -7,14 +7,21 @@ function writeGooglePlusEvents(data, elem)
     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 == "photo")
+        if ( attach[a].objectType == "album")
         {
-          noteimg = attach[a].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")
+        {
+          noteimgs.push(attach[a].image.url);
         }
         else if (attach[a].objectType == "event")
         {
@@ -23,11 +30,16 @@ function writeGooglePlusEvents(data, elem)
       }
     }
     ghtml += '<div class="gpluspost">'
-    ghtml += '<img class="gplusactor" src="'+item.actor.image.url+'"/><p class="gplustimestamp">'+item.updated+'</p>';
+    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)
+    if (noteimgs.length>0)
     {
-      ghtml += '<p class="gplusimg"><img class="gplusimg" src="'+noteimg+'"/></p>';
+      ghtml += '<table class="gplusimg" cellspacing="0"><tr>';
+      for (var ni=0; ni<noteimgs.length; ni++)
+      {
+        ghtml += '<td><img class="gplusimg" src="'+noteimgs[ni]+'"/></td>';
+      }
+      ghtml += '</tr></table>';
     }
     ghtml += '</div>';
   }
@@ -37,9 +49,9 @@ function writeGooglePlusEvents(data, elem)
 
 function loadGooglePlusEvents()
 {
-  var gplusuri = "https://www.googleapis.com/plus/v1/people/113737596421797426873/activities/public?maxResults=3&key="+gplusapikey;
+  var gpak = "AIzaSyD9xBFM-KWwSYBgZ8VzftJ5wYYvurOxEHg";
+  var gplusuri = "https://www.googleapis.com/plus/v1/people/113737596421797426873/activities/public?maxResults=4&key="+gpak;
   var gpluscontainer=document.getElementById("gplusevents");
-  gpluscontainer.innerHTML="Plus loading ...";
   $.getJSON(gplusuri, function(data){
     writeGooglePlusEvents(data, gpluscontainer);
   });
@@ -64,9 +76,10 @@ function writeCalendar(data, elem)
         {
           stime=stime.substring(0,stime.length-3);
         }
-        when = data[s].start +", "+stime+" Uhr";
+        if (stime.length <= 2) { stime+="h"; }
+        when = data[s].start.substring(8,10)+"."+data[s].start.substring(5,7) +", "+stime;
       }
-      calhtml += '<li class="level1"><span class="r3red">'+when+'</span> - '+data[s].title+'</li>'+"\n";
+      calhtml += '<li class="level1">'+when+' - <span class="r3red">'+data[s].title+'</span></li>'+"\n";
     }
     elem.innerHTML='<ul>'+calhtml+'</ul>';
 }
@@ -74,7 +87,6 @@ function loadCalendar()
 {
   //old URI: //grical.realraum.at/s/?query=!realraum&limit=9&view=json
   var calcontainer=document.getElementById("grical_upcoming");
-  calcontainer.innerHTML="Calendar loading ...<br/>please wait a second or two";
   $.getJSON('/shmcache/grical_realraum.json', function(data){
     writeCalendar(data, calcontainer);
   });
@@ -104,7 +116,7 @@ function writeAnwesenheitStatus(data)
     {
       $.each( data.sensors[s], function(stype, std){
         $.each( std, function(swhere, svalue){
-           sensorstd+='<td style="background-color:white; height:42px; text-align:center; vertical-align:middle; display:table-cell;"><b>'+stype+'</b><br/>'+swhere+': '+svalue+'</td>';
+           sensorstd+='<td class="sensorstatus"><b>'+stype+'</b><br/>'+swhere+': '+svalue+'</td>';
         });
       });
     }
@@ -126,18 +138,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()
 {
@@ -146,10 +146,10 @@ $(document).ready(function()
   updateAnwesenheitStatus();
   loadCalendar();
   loadGooglePlusEvents();
-  setInterval("updateAnwesenheitStatus()", 10000);
-  setInterval("loadCalendar()", 100000);
-  setInterval("updateSensors()", 50000);
-  setInterval("loadGooglePlusEvents()", 3600*1000);
+  setInterval("updateAnwesenheitStatus()", 10*1000);
+  setInterval("loadCalendar()", 123*1000);
+  setInterval("updateSensors()",145*1000);
+  setInterval("loadGooglePlusEvents()", 1207*1000);
 });
 
 function updateDateClock(now)
@@ -177,7 +177,7 @@ function highlightEntry(idx, color, value)
   }
 }
 
-
+var seconds = 0;
 function clock(now)
 {
   var now = new Date(new Date().valueOf() + 300);
@@ -188,26 +188,48 @@ 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)
+    {
+        return false;
+    }
+    if (typeof img.naturalWidth == "undefined" || (typeof img.naturalWidth == "number" && img.naturalWidth == 0))
+    {
+        return false;
+    }
+    return true;
+}
+
+var img_orig_src = {};
 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(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);
+      }
     }
 }