Skip to content

Commit 23f9b40

Browse files
committed
Added new sensors display page to meet Ian's exacting user interface design requirements
1 parent a646783 commit 23f9b40

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

homemon-server/homemon-server.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var page_power = require('./routes/page_power');
4444
var page_powerbar = require('./routes/page_powerbar');
4545
var page_pushmessage = require('./routes/page_pushmessage');
4646
var page_sensors = require('./routes/page_sensors');
47+
var page_sensors2 = require('./routes/page_sensors2');
4748
var page_snmp = require('./routes/page_snmp');
4849

4950

@@ -56,6 +57,7 @@ app.get('/power', page_power.page);
5657
app.get('/powerbar', page_powerbar.page);
5758
app.get('/pushmessage', page_pushmessage.page);
5859
app.get('/sensors', page_sensors.page);
60+
app.get('/sensors2', page_sensors2.page);
5961
app.get('/snmp', page_snmp.page);
6062

6163

@@ -148,6 +150,15 @@ mqttclient.on('connect', function() {
148150
if (names[topic] != undefined) name = names[topic].name;
149151
io.sockets.in("sensors").emit('data', { topic: topic, value: messageString, name: name });
150152
}
153+
154+
if (topic.beginsWith("sensors/co/") || topic.beginsWith("sensors/co2/")
155+
|| topic.beginsWith("sensors/temperature/") || topic.beginsWith("sensors/humidity/")
156+
|| topic.beginsWith("sensors/no2") || topic.beginsWith("sensors/pressure")) {
157+
var name = null;
158+
if (names[topic] != undefined) name = names[topic].name;
159+
io.sockets.in("sensors2").emit('data', { topic: topic, value: messageString, name: name });
160+
}
161+
151162

152163
if (topic.beginsWith("sensors/iosbattery")) {
153164
var name = null;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
ElementExists = function(id) {
2+
return !!document.getElementById(id);
3+
};
4+
5+
// function to if a string starts with nother one
6+
String.prototype.beginsWith = function (string) {
7+
return(this.indexOf(string) === 0);
8+
};
9+
10+
11+
var myvalues = [];
12+
var updates = 0;
13+
14+
15+
var socket = io.connect("http://homemon.trease.eu:8500");
16+
socket.emit("subscribe", { room: "sensors2" });
17+
socket.on('data', function(data) {
18+
// console.log("Message received " + data.topic + " of " + data.value);
19+
20+
21+
// check the target topic exisits & if not create a target table entry
22+
if (!ElementExists (data.topic)) {
23+
var table=document.getElementById("myTable").getElementsByTagName('tbody')[0];
24+
25+
// var row=table.insertRow(count);
26+
device = data.topic.substring(data.topic.lastIndexOf('/')+1);
27+
console.log("creating new row for " + device);
28+
29+
var row=table.insertRow(table.rows.length);
30+
31+
var cell=row.insertCell(0);
32+
cell.id = "sensors/co2/" + device;
33+
34+
var cell=row.insertCell(0);
35+
cell.id = "sensors/co/" + device;
36+
37+
var cell=row.insertCell(0);
38+
cell.id = "sensors/no2/" + device;
39+
40+
var cell=row.insertCell(0);
41+
cell.id = "sensors/humidity/" + device;
42+
43+
var cell=row.insertCell(0);
44+
cell.id = "sensors/pressure/" + device;
45+
46+
var cell=row.insertCell(0);
47+
cell.id = "sensors/temperature/" + device;
48+
49+
var cell=row.insertCell(0);
50+
cell.id = device + "name";
51+
52+
53+
if(data.name != undefined) { // label cell based on emitted friendly name
54+
document.getElementById(device + "name").innerHTML = data.name;
55+
} else { // label ce;; based on topic name
56+
document.getElementById(device + "name").innerHTML = data.topic.slice(data.topic.lastIndexOf('/')+1);
57+
}
58+
}
59+
60+
if (ElementExists (data.topic)) {
61+
document.getElementById(data.topic).style.textAlign="center";
62+
document.getElementById(data.topic).innerHTML= data.value;
63+
}
64+
65+
// print the time the refresh happened
66+
var dt = new Date();
67+
document.getElementById("time").innerHTML= dt.toLocaleTimeString();
68+
document.getElementById("updates").innerHTML= ++updates;
69+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.page = function(req, res){
2+
res.render('sensors2', { title: 'Sensors 2', user: req.user });
3+
};

homemon-server/views/header.jade

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ div.navbar.navbar-inverse.navbar-fixed-top
1010
ul.nav.navbar-nav
1111
li(class=(title === 'Sensors' ? 'active' : ''))
1212
a(href="sensors") Sensors
13+
li(class=(title === 'Sensors 2' ? 'active' : ''))
14+
a(href="sensors2") Sensors 2
1315
li(class=(title === 'Power' ? 'active' : ''))
1416
a(href="power") Power
1517
li(class=(title === 'Power Bar' ? 'active' : ''))

homemon-server/views/sensors2.jade

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
extends layout
2+
3+
block content
4+
script(src="/socket.io/socket.io.js")
5+
script(src="/javascripts/sensors2.js")
6+
7+
8+
div.container
9+
div.row.well
10+
div.col-md-12
11+
h4.text-center Sensors
12+
| <table id="myTable" class="table table-striped table-condensed table-bordered sortable-theme-bootstrap" data-sortable>
13+
| <thead><tr>
14+
| <th style="14%; text-align: Left">Topic</th>
15+
| <th style="14%; text-align: center">Temperature</th>
16+
| <th style="14%; text-align: center">Pressure</th>
17+
| <th style="14%; text-align: center">Humidity</th>
18+
| <th style="14%; text-align: center">NO2</th>
19+
| <th style="14%; text-align: center">NO</th>
20+
| <th style="14%; text-align: center">CO2</th>
21+
| </tr></thead>
22+
| <tbody></tbody>
23+
| </table>
24+
25+
26+
div.row
27+
div.col-md-6
28+
#time
29+
div.col-md-6.text-right
30+
#updates
31+
32+
33+
34+

0 commit comments

Comments
 (0)