Subversion Repositories HomeAutomation

Rev

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