Add convenience flags for knowing if a device is usable, ready, and owned by us.

This commit is contained in:
Simo Kinnunen
2014-03-18 15:06:02 +09:00
parent cc42b7c51f
commit 30b04ab2d2
5 changed files with 47 additions and 12 deletions

View File

@@ -139,7 +139,10 @@ module.exports = function(options) {
.then(function(cursor) {
return Promise.promisify(cursor.toArray, cursor)()
.then(function(list) {
list.forEach(datautil.applyData)
list.forEach(function(device) {
datautil.applyData(device)
datautil.applyOwner(device, req.user)
})
res.json({
success: true
, devices: list
@@ -158,9 +161,11 @@ module.exports = function(options) {
dbapi.loadDevice(req.params.serial)
.then(function(device) {
if (device) {
datautil.applyData(device)
datautil.applyOwner(device, req.user)
res.json({
success: true
, device: datautil.applyData(device)
, device: device
})
}
else {
@@ -258,22 +263,34 @@ module.exports = function(options) {
socket.emit('device.remove', {
serial: message.serial
, present: false
, ready: false
, lastHeartbeatAt: null
, isOwnedByUser: false
})
})
.on(wire.JoinGroupMessage, function(channel, message) {
socket.emit('device.change', message)
socket.emit('device.change', datautil.applyOwner({
serial: message.serial
, owner: message.owner
}
, user
))
})
.on(wire.LeaveGroupMessage, function(channel, message) {
socket.emit('device.change', {
serial: message.serial
, owner: null
})
socket.emit('device.change', datautil.applyOwner({
serial: message.serial
, owner: null
}
, user
))
})
.on(wire.DeviceStatusMessage, function(channel, message) {
socket.emit('device.change', message)
})
.on(wire.DeviceIdentityMessage, function(channel, message) {
socket.emit('device.change', datautil.applyData(message))
datautil.applyData(message)
message.ready = true
socket.emit('device.change', message)
})
.on(wire.TransactionProgressMessage, function(channel, message) {
socket.emit('tx.progress', channel.toString(), message)