Skip to content

Commit

Permalink
Merge pull request #1 from jamesbarnett91/websockets
Browse files Browse the repository at this point in the history
Switch from http polling to websockets
  • Loading branch information
jamesbarnett91 authored Apr 8, 2018
2 parents 0842361 + 5dd469a commit 531964e
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 419 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ If you hit this issue you can try disabling the VirtualBox adapter in `Control P
- [x] Show historical data
- [x] Build dists
- [x] Docker image
- [ ] Support switching between multiple plugs (currently only works for the fist plug discovered)
- [x] Support switching between multiple plugs
- [x] Switch to websockets
- [ ] Rescan for devices on the fly
- [ ] Add daily cost metrics
- [ ] Configurable poll rates
- [ ] Switch to websockets



31 changes: 19 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

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

var app = express();
const indexRouter = require('./routes/index');
const wsRouter = require('./routes/ws');

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

app.use('/', indexRouter);
app.use('/energy-usage', energyUsageRouter);
app.use('/power-state', powerStateRouter);
app.use('/ws', wsRouter);

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

module.exports = app;
app.listen(process.env.PORT || '3000');

module.exports.getWsClientCount = function() {
return expressWs.getWss().clients.size;
};

module.exports.getWsClients = function() {
return expressWs.getWss().clients;
};
90 changes: 0 additions & 90 deletions bin/www

This file was deleted.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "James Barnett"
},
"scripts": {
"start": "node ./bin/www",
"start": "node ./app.js",
"dist": "pkg . --out-path ./dist"
},
"dependencies": {
Expand All @@ -19,13 +19,14 @@
"http-errors": "~1.6.2",
"morgan": "~1.9.0",
"tplink-smarthome-api": "0.22.0",
"moment": "2.22.0"
"moment": "2.22.0",
"express-ws": "3.0.0"
},
"devDependencies": {
"pkg": "4.3.1"
},
"bin" : {
"tplink-monitor": "./bin/www"
"tplink-monitor": "./app.js"
},
"pkg": {
"scripts": "node_modules/hbs/lib/hbs.js",
Expand Down
Loading

0 comments on commit 531964e

Please sign in to comment.