Skip to content

Commit 531964e

Browse files
Merge pull request #1 from jamesbarnett91/websockets
Switch from http polling to websockets
2 parents 0842361 + 5dd469a commit 531964e

File tree

14 files changed

+552
-419
lines changed

14 files changed

+552
-419
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ If you hit this issue you can try disabling the VirtualBox adapter in `Control P
5151
- [x] Show historical data
5252
- [x] Build dists
5353
- [x] Docker image
54-
- [ ] Support switching between multiple plugs (currently only works for the fist plug discovered)
54+
- [x] Support switching between multiple plugs
55+
- [x] Switch to websockets
5556
- [ ] Rescan for devices on the fly
5657
- [ ] Add daily cost metrics
57-
- [ ] Configurable poll rates
58-
- [ ] Switch to websockets
58+
5959

6060

app.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
var createError = require('http-errors');
2-
var express = require('express');
3-
var path = require('path');
4-
var cookieParser = require('cookie-parser');
5-
var logger = require('morgan');
1+
const createError = require('http-errors');
2+
const express = require('express');
3+
const path = require('path');
4+
const cookieParser = require('cookie-parser');
5+
const logger = require('morgan');
66

7-
var indexRouter = require('./routes/index');
8-
var energyUsageRouter = require('./routes/energy-usage');
9-
var powerStateRouter = require('./routes/power-state');
7+
const app = express();
8+
const expressWs = require('express-ws')(app);
109

11-
var app = express();
10+
const indexRouter = require('./routes/index');
11+
const wsRouter = require('./routes/ws');
1212

1313
// view engine setup
1414
app.set('views', path.join(__dirname, 'views'));
@@ -21,8 +21,7 @@ app.use(cookieParser());
2121
app.use(express.static(path.join(__dirname, 'public')));
2222

2323
app.use('/', indexRouter);
24-
app.use('/energy-usage', energyUsageRouter);
25-
app.use('/power-state', powerStateRouter);
24+
app.use('/ws', wsRouter);
2625

2726
// catch 404 and forward to error handler
2827
app.use(function(req, res, next) {
@@ -40,4 +39,12 @@ app.use(function(err, req, res, next) {
4039
res.render('error');
4140
});
4241

43-
module.exports = app;
42+
app.listen(process.env.PORT || '3000');
43+
44+
module.exports.getWsClientCount = function() {
45+
return expressWs.getWss().clients.size;
46+
};
47+
48+
module.exports.getWsClients = function() {
49+
return expressWs.getWss().clients;
50+
};

bin/www

Lines changed: 0 additions & 90 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"name": "James Barnett"
99
},
1010
"scripts": {
11-
"start": "node ./bin/www",
11+
"start": "node ./app.js",
1212
"dist": "pkg . --out-path ./dist"
1313
},
1414
"dependencies": {
@@ -19,13 +19,14 @@
1919
"http-errors": "~1.6.2",
2020
"morgan": "~1.9.0",
2121
"tplink-smarthome-api": "0.22.0",
22-
"moment": "2.22.0"
22+
"moment": "2.22.0",
23+
"express-ws": "3.0.0"
2324
},
2425
"devDependencies": {
2526
"pkg": "4.3.1"
2627
},
2728
"bin" : {
28-
"tplink-monitor": "./bin/www"
29+
"tplink-monitor": "./app.js"
2930
},
3031
"pkg": {
3132
"scripts": "node_modules/hbs/lib/hbs.js",

0 commit comments

Comments
 (0)