diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2018-04-07 21:43:17 +0100 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2018-04-07 21:43:17 +0100 |
| commit | 1301d90e93af799a9054f133847ebf3cbda15f9d (patch) | |
| tree | 7c9e12431c63a7a52523d2cd1f15e144dcaf680f /routes/index.js | |
| parent | 24b4a39fce61dd9bd8ab7757f3bbda0636adc8c9 (diff) | |
| download | tplink-energy-monitor-1301d90e93af799a9054f133847ebf3cbda15f9d.tar.xz tplink-energy-monitor-1301d90e93af799a9054f133847ebf3cbda15f9d.zip | |
Add support for switching between multiple plugs
Diffstat (limited to 'routes/index.js')
| -rw-r--r-- | routes/index.js | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/routes/index.js b/routes/index.js index 4909ff8..12c62cd 100644 --- a/routes/index.js +++ b/routes/index.js @@ -5,11 +5,27 @@ const deviceManager = require('../services/device-manager'); router.get('/', function(req, res, next) { + let deviceId = sortDevices(deviceManager.getAllDevices())[0].deviceId; + + res.redirect('/' + deviceId); + +}); + +router.get('/:deviceId', function(req, res, next) { + + let deviceId = req.params.deviceId; + res.render('index', { - device: deviceManager.getDevice(), - devices: deviceManager.getAllDevices() + device: deviceManager.getDevice(deviceId), + devices: sortDevices(deviceManager.getAllDevices()) }); }); +function sortDevices(devices) { + return devices.slice().sort((a, b) => { + return a.alias.toLowerCase().localeCompare(b.alias.toLowerCase()) + }) +} + module.exports = router; |