1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 10:19:30 +02:00

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

@ -58,6 +58,7 @@ dbapi.saveDevice = function(serial, device) {
, provider: device.provider , provider: device.provider
, owner: null , owner: null
, status: device.status , status: device.status
, ready: false
, statusChangedAt: r.now() , statusChangedAt: r.now()
, createdAt: r.now() , createdAt: r.now()
, lastHeartbeatAt: r.now() , lastHeartbeatAt: r.now()
@ -89,6 +90,7 @@ dbapi.unsetDeviceOwner = function(serial, owner) {
dbapi.setDeviceAbsent = function(serial) { dbapi.setDeviceAbsent = function(serial) {
return db.run(r.table('devices').get(serial).update({ return db.run(r.table('devices').get(serial).update({
present: false present: false
, ready: false
, lastHeartbeatAt: null , lastHeartbeatAt: null
})) }))
} }
@ -101,7 +103,8 @@ dbapi.setDeviceChannel = function(serial, channel) {
dbapi.saveDeviceIdentity = function(serial, identity) { dbapi.saveDeviceIdentity = function(serial, identity) {
return db.run(r.table('devices').get(serial).update({ return db.run(r.table('devices').get(serial).update({
platform: identity.platform ready: true
, platform: identity.platform
, manufacturer: identity.manufacturer , manufacturer: identity.manufacturer
, operator: identity.operator , operator: identity.operator
, model: identity.model , model: identity.model

View file

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

View file

@ -50,6 +50,11 @@ module.exports.applyData = function(device) {
return device return device
} }
module.exports.applyOwner = function(device, user) {
device.isOwnedByUser = !!device.owner && device.owner.email === user.email
return device
}
module.exports.middleware = function() { module.exports.middleware = function() {
return express.static(pathutil.root('node_modules/stf-devices-db/data/small')) return express.static(pathutil.root('node_modules/stf-devices-db/data/small'))
} }

View file

@ -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) { function get(data) {
return devices[devicesBySerial[data.serial]] return devices[devicesBySerial[data.serial]]
} }
function insert(data) { function insert(data) {
devicesBySerial[data.serial] = devices.push(data) - 1 devicesBySerial[data.serial] = devices.push(data) - 1
sync(data)
notify() notify()
} }
function modify(oldData, newData) { function modify(data, newData) {
_.assign(oldData, newData) _.assign(data, newData)
sync(data)
notify() notify()
} }

View file

@ -91,7 +91,7 @@ div
ul.device-list ul.device-list
li(ng-repeat='device in tracker.devices track by device.serial') 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 a(href='#!/devices/{{ device.serial }}') Linky
button(ng-click="invite(device)") invite button(ng-click="invite(device)") invite
button(ng-click="kick(device)") kick button(ng-click="kick(device)") kick