diff --git a/lib/db/api.js b/lib/db/api.js index aa32a61e..50c2f1dc 100644 --- a/lib/db/api.js +++ b/lib/db/api.js @@ -69,3 +69,11 @@ module.exports.saveDeviceIdentity = function(serial, identity) { , display: identity.display })) } + +module.exports.loadDevices = function() { + return db.run(r.table('devices')) +} + +module.exports.loadDevice = function(serial) { + return db.run(r.table('devices').get(serial)) +} diff --git a/lib/roles/app.js b/lib/roles/app.js index 1978ebaa..aa76c9eb 100644 --- a/lib/roles/app.js +++ b/lib/roles/app.js @@ -92,6 +92,36 @@ module.exports = function(options) { res.render('index') }) + app.get('/api/v1/devices', function(req, res) { + dbapi.loadDevices() + .then(function(cursor) { + return Promise.promisify(cursor.toArray, cursor)() + .then(function(list) { + res.json(list) + }) + }) + .catch(function(err) { + log.error('Failed to load device list: ', err.stack) + res.send(500) + }) + }) + + app.get('/api/v1/devices/:serial', function(req, res) { + dbapi.loadDevice(req.params.serial) + .then(function(device) { + if (device) { + res.json(device) + } + else { + res.send(404) + } + }) + .catch(function(err) { + log.error('Failed to load device "%s": ', req.params.serial, err.stack) + res.send(500) + }) + }) + io.set('authorization', (function() { var parse = Promise.promisify(express.cookieParser(options.secret)) return function(handshake, accept) {