diff --git a/lib/db/api.js b/lib/db/api.js index 81892bdb..25939522 100644 --- a/lib/db/api.js +++ b/lib/db/api.js @@ -58,6 +58,7 @@ dbapi.saveDevice = function(serial, device) { , provider: device.provider , owner: null , status: device.status + , ready: false , statusChangedAt: r.now() , createdAt: r.now() , lastHeartbeatAt: r.now() @@ -89,6 +90,7 @@ dbapi.unsetDeviceOwner = function(serial, owner) { dbapi.setDeviceAbsent = function(serial) { return db.run(r.table('devices').get(serial).update({ present: false + , ready: false , lastHeartbeatAt: null })) } @@ -101,7 +103,8 @@ dbapi.setDeviceChannel = function(serial, channel) { dbapi.saveDeviceIdentity = function(serial, identity) { return db.run(r.table('devices').get(serial).update({ - platform: identity.platform + ready: true + , platform: identity.platform , manufacturer: identity.manufacturer , operator: identity.operator , model: identity.model diff --git a/lib/roles/app.js b/lib/roles/app.js index 8dc24053..97fd73c3 100644 --- a/lib/roles/app.js +++ b/lib/roles/app.js @@ -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) diff --git a/lib/util/datautil.js b/lib/util/datautil.js index 88229857..10ecb67b 100644 --- a/lib/util/datautil.js +++ b/lib/util/datautil.js @@ -50,6 +50,11 @@ module.exports.applyData = function(device) { return device } +module.exports.applyOwner = function(device, user) { + device.isOwnedByUser = !!device.owner && device.owner.email === user.email + return device +} + module.exports.middleware = function() { return express.static(pathutil.root('node_modules/stf-devices-db/data/small')) } diff --git a/res/app/components/stf/device/device-service.js b/res/app/components/stf/device/device-service.js index 811edc12..196634c1 100644 --- a/res/app/components/stf/device/device-service.js +++ b/res/app/components/stf/device/device-service.js @@ -17,17 +17,27 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) { } } + function sync(data) { + // usable IF device is physically present AND device is online AND + // preparations are ready AND the device has no owner or we are the + // owner + data.usable = data.present && data.status === 3 && data.ready && + (!data.owner || data.isOwnedByUser) + } + function get(data) { return devices[devicesBySerial[data.serial]] } function insert(data) { devicesBySerial[data.serial] = devices.push(data) - 1 + sync(data) notify() } - function modify(oldData, newData) { - _.assign(oldData, newData) + function modify(data, newData) { + _.assign(data, newData) + sync(data) notify() } diff --git a/res/app/device-list/device-list.jade b/res/app/device-list/device-list.jade index 365fbee3..8e8e3c69 100644 --- a/res/app/device-list/device-list.jade +++ b/res/app/device-list/device-list.jade @@ -91,7 +91,7 @@ div ul.device-list li(ng-repeat='device in tracker.devices track by device.serial') - span {{ device.serial }} {{ device.name || device.model }} {{ device.present ? 'present' : 'absent' }} {{ device.owner.email }} + span {{ device.serial }} {{ device.name || device.model }} {{ device.present ? 'present' : 'absent' }} {{ device.usable ? 'usable' : 'unusable' }} {{ device.isOwnedByUser ? 'owned by me' : '' }} {{ device.owner.email }} a(href='#!/devices/{{ device.serial }}') Linky button(ng-click="invite(device)") invite button(ng-click="kick(device)") kick