Subversion Repositories HomeAutomation

Rev

Rev 2097 | Rev 2106 | 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.         var d = new Date(data.timestamp * 1000);
  34.         data.time = d.toLocaleString();
  35.  
  36.         if (data.direction === "in" && pageInstance.inText)
  37.         {
  38.           data.directionText = pageInstance.inText;
  39.         }
  40.         else if (data.direction === "out" && pageInstance.outText)
  41.         {
  42.           data.directionText = pageInstance.outText;
  43.         }
  44.         else
  45.         {
  46.           data.directionText = data.direction;
  47.         }
  48.  
  49.         list.push(data);
  50.       });
  51.  
  52.       list.sort(function(a, b)
  53.       {
  54.         return b.timestamp - a.timestamp;
  55.       });
  56.  
  57.       if (list.length > 0 && pageInstance.lastPhoneCallTimestamp === list[0].timestamp)
  58.       {
  59.         return; // Do not update if no change
  60.       }
  61.  
  62.       pageInstance.pageContentElement.empty();
  63.       pageInstance.pagePhoneCallsElement = $("#page-phonecalls-template").tmpl().appendTo(pageInstance.pageContentElement);
  64.  
  65.       pageInstance.lastPhoneCallTimestamp = list[0].timestamp;
  66.  
  67.       var count = 0;
  68.  
  69.       jQuery.each(list, function(n, item)
  70.       {
  71.         if (count >= pageInstance.limit)
  72.         {
  73.           return false;
  74.         }
  75.        
  76.         $("#page-phonecalls-item-template").tmpl(item).appendTo(pageInstance.pageContentElement.find("ul"));
  77.  
  78.         count++;
  79.       });
  80.  
  81.       pageInstance.pagePhoneCallsElement.trigger("create");
  82.  
  83.       var timestamp = getServerTimestamp() / 1000;
  84.  
  85.       if (list.length > 0 && list[0].direction == "in" && list[0].timestamp + 10 > timestamp)
  86.       {
  87.         /* Pause polling timer */
  88.         clearInterval(pageInstance.pollTimer);
  89.         pageInstance.pollTimer = null;
  90.  
  91.         openDialog("Event", pageInstance.inText + " " + list[0].number + ".<br/>" + list[0].names.toString(), 15000, function()
  92.         {
  93.           pageInstance.pollTimer = setInterval(requestServerData, 4000);
  94.         });
  95.       }
  96.     }
  97.  
  98.    
  99.     /* Function to request new values from the server */
  100.     function requestServerData()
  101.     {
  102.       sendCommand("Storage_ListParametersRaw PhoneCalls", handleServerData);
  103.     }
  104.    
  105.    
  106.     /* Request the initial value from the server */
  107.     requestServerData();
  108.    
  109.    
  110.     /* Set timer for continious polling of the current values */
  111.     pageInstance.pollTimer = setInterval(requestServerData, 4000);
  112.   }
  113. };
  114.