Subversion Repositories HomeAutomation

Rev

Rev 2144 | 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.  
  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.         }
  25.       });
  26.     }
  27.    
  28.    
  29.     /* Function to request new values from the server */
  30.     function requestServerData()
  31.     {
  32.       sendCommand("JSON.stringify(IrDeviceState_GetDataNative('" + pageInstance.devices.join("','") + "'));", handleServerData);
  33.     }
  34.    
  35.    
  36.     /* Loop through all the devices and create switches */
  37.     jQuery.each(pageInstance.devices, function(n, device)
  38.     {
  39.       var name = device;
  40.  
  41.       if (aliasNames[device])
  42.       {
  43.         name = aliasNames[device];
  44.       }
  45.      
  46.       /* Create the slider HTML from the template and add it to the DOM */
  47.       pageInstance.pageSwitchElements[device] = $("#page-irdevicestate-template").tmpl({ device: device, name: name }).appendTo(pageInstance.pageContentElement).trigger("create");
  48.      
  49.       /* Before we have any value it should be disabled */
  50.       pageInstance.pageSwitchElements[device].find("select").slider("disable");
  51.     });
  52.    
  53.    
  54.     /* Request the initial value from the server */
  55.     requestServerData();
  56.    
  57.    
  58.     /* Set timer for continious polling of the current values */
  59.     pageInstance.pollTimer = setInterval(requestServerData, 4000);
  60.   }
  61. };
  62.  
  63.