Subversion Repositories HomeAutomation

Rev

Rev 2154 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. pages.irdevicestate = {
  2.   init: function(pageInstance)
  3.   {
  4.     /* List to keep referenses to all the different switch elements */
  5.     pageInstance.pageSwitchElements = {};
  6.  
  7.     this.pageshow = function(arg) {
  8.       /* Request the initial value from the server */
  9.       requestServerData();
  10.       /* Set timer for continious polling of the current values */
  11.       pageInstance.pollTimer = setInterval(requestServerData, 4000);
  12.     }
  13.    
  14.     this.pagehide = function(arg) {
  15.       clearInterval(pageInstance.pollTimer);
  16.     }
  17.  
  18.     /* Function to update values of switches based on new server data */
  19.     function handleServerData(jsonData)
  20.     {
  21.       /* Loop through all results */
  22.       jQuery.each(jsonData, function(device, data)
  23.       {
  24.         /* Only do something if we know the device and have a value */
  25.         if (pageInstance.pageSwitchElements[device] && data.state  && data.timestamp)
  26.         {
  27.           /* Enable the switch if it was disabled by default */
  28.           pageInstance.pageSwitchElements[device].find("select").slider("enable");
  29.           /* Update the slider with the new value */
  30.           pageInstance.pageSwitchElements[device].find("select").val(data.state).slider("refresh");
  31.          
  32.           var date = new Date(data.timestamp*1000);
  33.           //pageInstance.pageSwitchElements[device].find(":jqmData(id=page-irdevice-timestamp-"+device+")").text(date.toDateString()+", "+date.toLocaleTimeString());
  34.           pageInstance.pageSwitchElements[device].find(":jqmData(id=page-irdevice-timestamp-"+device+")").text(jQuery.timeago(date));
  35.         }
  36.       });
  37.     }
  38.    
  39.    
  40.     /* Function to request new values from the server */
  41.     function requestServerData()
  42.     {
  43.       sendCommand("JSON.stringify(IrDeviceState_GetDataNative('" + pageInstance.devices.join("','") + "'));", handleServerData);
  44.     }
  45.    
  46.    
  47.     /* Loop through all the devices and create switches */
  48.     jQuery.each(pageInstance.devices, function(n, device)
  49.     {
  50.       var name = device;
  51.  
  52.       if (aliasNames[device])
  53.       {
  54.         name = aliasNames[device];
  55.       }
  56.      
  57.       /* Create the slider HTML from the template and add it to the DOM */
  58.       pageInstance.pageSwitchElements[device] = $("#page-irdevicestate-template").tmpl({ device: device, name: name }).appendTo(pageInstance.pageContentElement).trigger("create");
  59.      
  60.       /* Before we have any value it should be disabled */
  61.       pageInstance.pageSwitchElements[device].find("select").slider("disable");
  62.     });
  63.   }
  64. };
  65.  
  66.