|
6 | 6 | </head> |
7 | 7 | <body style="font-family:Verdana;"> |
8 | 8 |
|
9 | | -<table id="myTable" border="1"></table> |
| 9 | +<table><tr> |
| 10 | +<td> |
| 11 | +<table id="myTablePower" border="1"><tr> |
| 12 | +<td id="myTablePowerTopicsensors/power/0">Total power</td> |
| 13 | +<td id="myTablePowerPowersensors/power/0"></td> |
| 14 | +</tr> |
| 15 | +</table> |
| 16 | +</td> |
| 17 | +<td> |
| 18 | +<table id="myTableTemp" border="1"></table> |
| 19 | +</td> |
| 20 | +</tr></table> |
10 | 21 | <canvas id="mycanvas" width="500" height="100"></canvas> |
11 | 22 | <P> |
12 | 23 | <div id="time"></div> |
|
18 | 29 | ElementExists = function(id) { |
19 | 30 | return !!document.getElementById(id); |
20 | 31 | }; |
| 32 | + BeginsWith = function(needle, haystack){ |
| 33 | + return (haystack.substr(0, needle.length) == needle); |
| 34 | + } |
21 | 35 |
|
22 | 36 |
|
23 | 37 | var smoothie = new SmoothieChart({millisPerPixel: 500}); |
|
26 | 40 | smoothie.addTimeSeries(line1); |
27 | 41 |
|
28 | 42 | var socket = io.connect("http://192.168.1.103:8500"); |
29 | | - socket.on('data', function(data){ |
30 | | - // check if the table cells exists, if not create it and then set value |
31 | | - if (!ElementExists ("myTablePower" + data.topic)) { |
32 | | - var table=document.getElementById("myTable"); |
33 | | - var row=table.insertRow(0); |
34 | | - var cell=row.insertCell(0); |
35 | | - cell.id = "myTablePower" + data.topic; |
36 | | - var cell=row.insertCell(0); |
37 | | - cell.id = "myTableTopic" + data.topic; |
38 | | - } |
39 | | - document.getElementById("myTablePower" + data.topic).innerHTML= data.value; |
40 | | - document.getElementById("myTableTopic" + data.topic).innerHTML= data.topic; |
| 43 | + socket.on('data', function(data) { |
| 44 | + |
| 45 | + var topictag = "sensors/power/"; |
| 46 | + if (data.topic.substring(0,topictag.length) == topictag) { |
| 47 | + console.log("Power match - " + data.topic + " " + typeof(data.topic)); |
| 48 | + // check if the table cells exists, if not create it and then set value |
| 49 | + if (!ElementExists ("myTablePowerPower" + data.topic)) { |
| 50 | + var table=document.getElementById("myTablePower"); |
| 51 | + var row=table.insertRow(1); |
| 52 | + var cell=row.insertCell(0); |
| 53 | + cell.id = "myTablePowerPower" + data.topic; |
| 54 | + document.getElementById("myTablePowerPower" + data.topic).style.textAlign="right"; |
| 55 | + var cell=row.insertCell(0); |
| 56 | + cell.id = "myTablePowerTopic" + data.topic; |
| 57 | + document.getElementById("myTablePowerTopic" + data.topic).innerHTML= data.topic; |
| 58 | + } |
| 59 | + document.getElementById("myTablePowerPower" + data.topic).innerHTML= data.value; |
| 60 | + |
| 61 | + // if data topic is 0 it is the overall power consumption, so update the graph |
| 62 | + if (data.topic == "sensors/power/0") { |
| 63 | + line1.append (new Date().getTime(), data.value); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + var topictag = "sensors/temperature/"; |
| 68 | + if (data.topic.substring(0,topictag.length) == topictag) { |
| 69 | + console.log("Temperature match - " + data.topic + " " + typeof(data.topic)); |
| 70 | + // check if the table cells exists, if not create it and then set value |
| 71 | + if (!ElementExists ("myTableTempTemp" + data.topic)) { |
| 72 | + var table=document.getElementById("myTableTemp"); |
| 73 | + var row=table.insertRow(0); |
| 74 | + var cell=row.insertCell(0); |
| 75 | + cell.id = "myTableTempTemp" + data.topic; |
| 76 | + document.getElementById("myTableTempTemp" + data.topic).style.textAlign="right"; |
| 77 | + var cell=row.insertCell(0); |
| 78 | + cell.id = "myTableTempTopic" + data.topic; |
| 79 | + document.getElementById("myTableTempTopic" + data.topic).innerHTML= data.topic; |
| 80 | + } |
| 81 | + document.getElementById("myTableTempTemp" + data.topic).innerHTML= data.value; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
41 | 87 |
|
42 | 88 | // print the time the refresh happened |
43 | 89 | var dt = new Date(); |
44 | 90 | document.getElementById("time").innerHTML= dt.toLocaleTimeString(); |
45 | 91 |
|
46 | | - // if data topic is 0 it is the over power consumption |
47 | | - if (data.topic == "sensors/power/0") { |
48 | | - line1.append (new Date().getTime(), data.value); |
49 | | - } |
50 | 92 | }); |
51 | 93 | </script> |
52 | 94 | </body> |
|
0 commit comments