Skip to content

Commit 792e460

Browse files
committed
Finished for now! Time to modify into a new project.
1 parent 2283bd2 commit 792e460

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

mqtt/app.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ var app = express()
55
, io = require('socket.io').listen(server, {'log level': 1});
66
var fs = require('fs');
77

8+
// service settings file
9+
var config = require('./config.json');
810

911

10-
11-
var scriptStartDate = new Date();
12-
12+
var connectionCount = 0;
1313

1414
function timeSince(ts){
1515
now = new Date();
@@ -58,11 +58,13 @@ app.get('/hello.txt', function(req, res){
5858
});
5959
app.get('/app.css', function (req, res) {
6060
console.log('connection %j %s %s', req.connection.remoteAddress, req.method, req.url);
61-
res.sendfile(__dirname + '/app.css');
61+
res.sendfile(__dirname + '/app.css');
6262
});
6363
app.get('/stats', function(req, res){
64+
res.send(connectionCount + " clients connected");
6465
console.log('connection %j %s %s', req.connection.remoteAddress, req.method, req.url);
65-
res.send(timeSince(scriptStartDate));
66+
console.log('This process is pid ' + process.pid + " with an uptime of " + process.uptime());
67+
console.log('Running on ' + process.platform + ' (' + process.arch + ')');
6668
});
6769
app.get('/', function(req, res) {
6870
console.log('connection %j %s %s', req.connection.remoteAddress, req.method, req.url);
@@ -86,21 +88,27 @@ console.log('listening on port 8500');
8688

8789

8890
io.sockets.on('connection', function (socket) {
91+
connectionCount++;
92+
});
93+
io.sockets.on('disconnet', function (socket) {
94+
connectionCount--;
8995
});
9096

9197

9298
// subscribe to MQTT
9399
var mqtt = require('mqtt');
94-
var client = mqtt.createClient(1883, 'localhost', function(err, client) {
100+
var client = mqtt.createClient(1883, config.mqtt.host, function(err, client) {
95101
keepalive: 1000
96102
});
103+
console.log('connecting to mqtt on ' + config.mqtt.host + '(' + config.mqtt.port + ')');
97104
// global variables for tracking cumulative power usage
98105
var powercumulativeToday = 0;
99106
var powercumulativeHour = 0;
100107
var powerlasttime = new Date(); // UNIX time in ms
101108

102109
client.on('connect', function() {
103110
client.subscribe('sensors/+/+');
111+
console.log('subscribing to sensors/+/+ on ' + config.mqtt.host + '(' + config.mqtt.port + ')');
104112

105113
client.on('message', function(topic, message) {
106114
// console.log('topic: ' + topic + ' payload: ' + message);

mqtt/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mqtt":
3+
{
4+
"host": "127.0.0.1",
5+
"port": "1833"
6+
}
7+
}

0 commit comments

Comments
 (0)