Rev 2154 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 2154 | Rev 2196 | ||
|---|---|---|---|
| Line 2... | Line 2... | ||
| 2 | init: function(pageInstance) |
2 | init: function(pageInstance) |
| 3 | { |
3 | { |
| 4 | /* List to keep referenses to all the different switch elements */ |
4 | /* List to keep referenses to all the different switch elements */ |
| 5 | pageInstance.pageSwitchElements = {}; |
5 | pageInstance.pageSwitchElements = {}; |
| 6 | 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 | } |
|
| 7 | 17 | ||
| 8 | /* Function to update values of switches based on new server data */ |
18 | /* Function to update values of switches based on new server data */ |
| 9 | function handleServerData(jsonData) |
19 | function handleServerData(jsonData) |
| 10 | { |
20 | { |
| 11 | /* Loop through all results */ |
21 | /* Loop through all results */ |
| 12 | jQuery.each(jsonData, function(device, data) |
22 | jQuery.each(jsonData, function(device, data) |
| 13 | { |
23 | { |
| 14 | /* Only do something if we know the device and have a value */ |
24 | /* Only do something if we know the device and have a value */ |
| 15 | if (pageInstance.pageSwitchElements[device] && data.state && data.timestamp) |
25 | if (pageInstance.pageSwitchElements[device] && data.state && data.timestamp) |
| 16 | { |
26 | { |
| 17 | /* Enable the switch if it was disabled by default */ |
27 | /* Enable the switch if it was disabled by default */ |
| 18 | pageInstance.pageSwitchElements[device].find("select").slider("enable"); |
28 | pageInstance.pageSwitchElements[device].find("select").slider("enable"); |
| Line 20... | Line 30... | ||
| 20 | pageInstance.pageSwitchElements[device].find("select").val(data.state).slider("refresh"); |
30 | pageInstance.pageSwitchElements[device].find("select").val(data.state).slider("refresh"); |
| 21 | 31 | ||
| 22 | var date = new Date(data.timestamp*1000); |
32 | var date = new Date(data.timestamp*1000); |
| 23 | //pageInstance.pageSwitchElements[device].find(":jqmData(id=page-irdevice-timestamp-"+device+")").text(date.toDateString()+", "+date.toLocaleTimeString()); |
33 | //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)); |
34 | pageInstance.pageSwitchElements[device].find(":jqmData(id=page-irdevice-timestamp-"+device+")").text(jQuery.timeago(date)); |
| 25 | } |
35 | } |
| 26 | }); |
36 | }); |
| 27 | } |
37 | } |
| 28 | 38 | ||
| 29 | 39 | ||
| 30 | /* Function to request new values from the server */ |
40 | /* Function to request new values from the server */ |
| 31 | function requestServerData() |
41 | function requestServerData() |
| Line 34... | Line 44... | ||
| 34 | } |
44 | } |
| 35 | 45 | ||
| 36 | 46 | ||
| 37 | /* Loop through all the devices and create switches */ |
47 | /* Loop through all the devices and create switches */ |
| 38 | jQuery.each(pageInstance.devices, function(n, device) |
48 | jQuery.each(pageInstance.devices, function(n, device) |
| 39 | { |
49 | { |
| 40 | var name = device; |
50 | var name = device; |
| 41 | 51 | ||
| 42 | if (aliasNames[device]) |
52 | if (aliasNames[device]) |
| 43 | { |
53 | { |
| 44 | name = aliasNames[device]; |
54 | name = aliasNames[device]; |
| 45 | } |
55 | } |
| 46 | 56 | ||
| 47 | /* Create the slider HTML from the template and add it to the DOM */ |
57 | /* 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"); |
58 | pageInstance.pageSwitchElements[device] = $("#page-irdevicestate-template").tmpl({ device: device, name: name }).appendTo(pageInstance.pageContentElement).trigger("create"); |
| 49 | 59 | ||
| 50 | /* Before we have any value it should be disabled */ |
60 | /* Before we have any value it should be disabled */ |
| 51 | pageInstance.pageSwitchElements[device].find("select").slider("disable"); |
61 | pageInstance.pageSwitchElements[device].find("select").slider("disable"); |
| 52 | }); |
62 | }); |
| 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 | } |
63 | } |
| 62 | }; |
64 | }; |
| 63 | 65 | ||