Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. pages.phonecalls = {
  3.   init: function(pageInstance)
  4.   {
  5.     pageInstance.pagePhoneCallsElement = null;
  6.     pageInstance.lastPhoneCallTimestamp = 0;
  7.  
  8.     /* Function to update values of sliders based on new server data */
  9.     function handleServerData(jsonData)
  10.     {
  11.       var list = [];
  12.  
  13.       /* Loop through all */
  14.       jQuery.each(jsonData.results, function(timestamp, data)
  15.       {
  16.         var data = JSON.parse(data);
  17.         var found = false;
  18.  
  19.         jQuery.each(pageInstance.aliases, function(n, alias)
  20.         {
  21.           if (data.module === alias)
  22.           {
  23.             found = true;
  24.             return false; // break
  25.           }
  26.         });
  27.  
  28.         if (!found)
  29.         {
  30.           return true; // continue
  31.         }
  32.  
  33.         if (!data.timestamp)
  34.         {
  35.           data.timestamp = timestamp; // Workaround for older lists
  36.         }
  37.  
  38.         var d = new Date(data.timestamp * 1000);
  39.         data.time = d.toLocaleString();
  40.  
  41.         if (data.direction === "in" && pageInstance.inText)
  42.         {
  43.           data.directionText = pageInstance.inText;
  44.         }
  45.         else if (data.direction === "out" && pageInstance.outText)
  46.         {
  47.           data.directionText = pageInstance.outText;
  48.         }
  49.         else
  50.         {
  51.           data.directionText = data.direction;
  52.         }
  53.  
  54.         list.push(data);
  55.       });
  56.  
  57.       list.sort(function(a, b)
  58.       {
  59.         return b.timestamp - a.timestamp;
  60.       });
  61.  
  62.       if (list.length > 0 && pageInstance.lastPhoneCallTimestamp === list[0].timestamp)
  63.       {
  64.         return; // Do not update if no change
  65.       }
  66.  
  67.       pageInstance.pageContentElement.empty();
  68.       pageInstance.pagePhoneCallsElement = $("#page-phonecalls-template").tmpl().appendTo(pageInstance.pageContentElement);
  69.  
  70.       pageInstance.lastPhoneCallTimestamp = list[0].timestamp;
  71.  
  72.       var count = 0;
  73.  
  74.       jQuery.each(list, function(n, item)
  75.       {
  76.         if (count >= pageInstance.limit)
  77.         {
  78.           return false;
  79.         }
  80.        
  81.         $("#page-phonecalls-item-template").tmpl(item).appendTo(pageInstance.pageContentElement.find("ul"));
  82.  
  83.         count++;
  84.       });
  85.  
  86.       pageInstance.pagePhoneCallsElement.trigger("create");
  87.  
  88.       var timestamp = getServerTimestamp() / 1000;
  89.  
  90.       if (list.length > 0 && list[0].direction == "in" && list[0].timestamp + 10 > timestamp)
  91.       {
  92.         /* Pause polling timer */
  93.         clearInterval(pageInstance.pollTimer);
  94.         pageInstance.pollTimer = null;
  95.  
  96.         openDialog("Event", pageInstance.inText + " " + list[0].number + ".<br/>" + list[0].names.toString(), 15000, function()
  97.         {
  98.           pageInstance.pollTimer = setInterval(requestServerData, 4000);
  99.         });
  100.       }
  101.     }
  102.  
  103.    
  104.     /* Function to request new values from the server */
  105.     function requestServerData()
  106.     {
  107.       sendCommand("Storage_ListParametersRaw PhoneCalls", handleServerData);
  108.     }
  109.    
  110.    
  111.     /* Request the initial value from the server */
  112.     requestServerData();
  113.    
  114.    
  115.     /* Set timer for continious polling of the current values */
  116.     pageInstance.pollTimer = setInterval(requestServerData, 4000);
  117.   }
  118. };
  119.