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

Better device present/absent handling by separating it from the status event.

This commit is contained in:
Simo Kinnunen 2014-02-03 19:42:20 +09:00
parent f432dc9fb9
commit 2086d7d439
9 changed files with 147 additions and 39 deletions

View file

@ -41,26 +41,36 @@ dbapi.saveDeviceLog = function(serial, entry) {
})) }))
} }
dbapi.saveDeviceStatus = function(serial, status) { dbapi.saveDevice = function(serial, device) {
return db.run(r.table('devices').get(serial).update({ return db.run(r.table('devices').insert({
status: status.status serial: serial
, provider: status.provider , present: true
, provider: device.provider
, status: device.status
, statusChangedAt: r.now() , statusChangedAt: r.now()
, createdAt: r.now()
}
, {
upsert: true
})) }))
.then(function(stats) { }
if (stats.skipped) {
return db.run(r.table('devices').insert({ dbapi.saveDeviceStatus = function(serial, status) {
serial: serial return dbapi.ensureDeviceSaved(serial)
, provider: status.provider .then(function() {
, status: status.status return db.run(r.table('devices').get(serial).update({
, statusChangedAt: r.now() status: status
, createdAt: r.now() , statusChangedAt: r.now()
})) }))
}
return stats
}) })
} }
dbapi.setDeviceAbsent = function(serial) {
return db.run(r.table('devices').get(serial).update({
present: false
}))
}
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 platform: identity.platform

View file

@ -178,6 +178,15 @@ module.exports = function(options) {
.on(wire.LeaveGroupMessage, function(channel, message) { .on(wire.LeaveGroupMessage, function(channel, message) {
socket.emit('group.leave', message) socket.emit('group.leave', message)
}) })
.on(wire.DevicePresentMessage, function(channel, message) {
socket.emit('device.present', message)
})
.on(wire.DeviceAbsentMessage, function(channel, message) {
socket.emit('device.absent', message)
})
.on(wire.DeviceStatusMessage, function(channel, message) {
socket.emit('device.status', message)
})
.handler() .handler()
// Global messages // Global messages

View file

@ -432,8 +432,14 @@ module.exports = function(options) {
} }
function gracefullyExit() { function gracefullyExit() {
log.info('Bye') if (isGrouped()) {
process.exit(0) leaveGroup()
Promise.delay(500).then(gracefullyExit)
}
else {
log.info('Bye')
process.exit(0)
}
} }
process.on('SIGINT', function() { process.on('SIGINT', function() {

View file

@ -32,6 +32,18 @@ module.exports = function(options) {
}) })
devDealer.on('message', wirerouter() devDealer.on('message', wirerouter()
.on(wire.DevicePresentMessage, function(channel, message, data) {
dbapi.saveDevice(message.serial, message)
appDealer.send([channel, data])
})
.on(wire.DeviceAbsentMessage, function(channel, message, data) {
dbapi.setDeviceAbsent(message.serial)
appDealer.send([channel, data])
})
.on(wire.DeviceStatusMessage, function(channel, message, data) {
dbapi.saveDeviceStatus(message.serial, message.status)
appDealer.send([channel, data])
})
.on(wire.JoinGroupMessage, function(channel, message, data) { .on(wire.JoinGroupMessage, function(channel, message, data) {
appDealer.send([channel, data]) appDealer.send([channel, data])
}) })
@ -51,9 +63,6 @@ module.exports = function(options) {
.on(wire.DeviceIdentityMessage, function(channel, message) { .on(wire.DeviceIdentityMessage, function(channel, message) {
dbapi.saveDeviceIdentity(message.serial, message) dbapi.saveDeviceIdentity(message.serial, message)
}) })
.on(wire.DeviceStatusMessage, function(channel, message) {
dbapi.saveDeviceStatus(message.serial, message)
})
.on(wire.ShellCommandDataMessage, function(channel, message, data) { .on(wire.ShellCommandDataMessage, function(channel, message, data) {
appDealer.send([channel, data]) appDealer.send([channel, data])
}) })

View file

@ -60,18 +60,36 @@ module.exports = function(options) {
tracker.on('add', function(device) { tracker.on('add', function(device) {
lists.all.push(device.id) lists.all.push(device.id)
pushDeviceStatus(device, device.type) push.send([
wireutil.global
, wireutil.envelope(new wire.DevicePresentMessage(
device.id
, options.name
, wireutil.toDeviceStatus(device.type)
))
])
maybeConnect(device) maybeConnect(device)
}) })
tracker.on('change', function(device) { tracker.on('change', function(device) {
pushDeviceStatus(device, device.type) push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceStatusMessage(
device.id
, wireutil.toDeviceStatus(device.type)
))
])
maybeConnect(device) || maybeDisconnect(device) maybeConnect(device) || maybeDisconnect(device)
}) })
tracker.on('remove', function(device) { tracker.on('remove', function(device) {
_.pull(lists.all, device.id) _.pull(lists.all, device.id)
pushDeviceStatus(device, 'absent') push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceAbsentMessage(
device.id
))
])
maybeDisconnect(device) maybeDisconnect(device)
}) })
@ -321,6 +339,12 @@ module.exports = function(options) {
Array.prototype.push.apply(options.ports, worker.ports) Array.prototype.push.apply(options.ports, worker.ports)
_.pull(lists.ready, id) _.pull(lists.ready, id)
_.pull(lists.waiting, id) _.pull(lists.waiting, id)
push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceAbsentMessage(
id
))
])
delayedTotals() delayedTotals()
return worker return worker
} }

View file

@ -14,7 +14,6 @@ var wireutil = {
, emulator: 'ONLINE' , emulator: 'ONLINE'
, unauthorized: 'UNAUTHORIZED' , unauthorized: 'UNAUTHORIZED'
, offline: 'OFFLINE' , offline: 'OFFLINE'
, absent: 'ABSENT'
}[type]] }[type]]
} }
, envelope: function(message) { , envelope: function(message) {
@ -106,13 +105,6 @@ var wireutil = {
}) })
)) ))
} }
, makeDeviceStatusMessage: function(serial, type, provider) {
return wireutil.envelope(new wire.DeviceStatusMessage(
serial
, wireutil.toDeviceStatus(type)
, provider
))
}
, makeProbeMessage: function() { , makeProbeMessage: function() {
return wireutil.envelope(new wire.ProbeMessage()) return wireutil.envelope(new wire.ProbeMessage())
} }

View file

@ -15,6 +15,8 @@ enum MessageType {
ShellCommandFailMessage = 12; ShellCommandFailMessage = 12;
DeviceIdentityMessage = 13; DeviceIdentityMessage = 13;
DeviceLogMessage = 14; DeviceLogMessage = 14;
DevicePresentMessage = 16;
DeviceAbsentMessage = 17;
} }
message Envelope { message Envelope {
@ -36,6 +38,16 @@ message DeviceLogMessage {
// Introductions // Introductions
message DevicePresentMessage {
required string serial = 1;
required string provider = 2;
required DeviceStatus status = 4;
}
message DeviceAbsentMessage {
required string serial = 1;
}
message DevicePokeMessage { message DevicePokeMessage {
required string serial = 1; required string serial = 1;
required string channel = 2; required string channel = 2;
@ -45,16 +57,14 @@ message ProbeMessage {
} }
enum DeviceStatus { enum DeviceStatus {
ABSENT = 1; OFFLINE = 1;
OFFLINE = 2; UNAUTHORIZED = 2;
UNAUTHORIZED = 3; ONLINE = 3;
ONLINE = 4;
} }
message DeviceStatusMessage { message DeviceStatusMessage {
required string serial = 1; required string serial = 1;
required DeviceStatus status = 2; required DeviceStatus status = 2;
required string provider = 3;
} }
message DeviceDisplayMessage { message DeviceDisplayMessage {

View file

@ -1,13 +1,56 @@
define(['./module', 'oboe'], function(mod, oboe) { define(['./module', 'oboe'], function(mod, oboe) {
function DevicesServiceFactory($rootScope, socketService) { function DevicesServiceFactory($rootScope, socket) {
var deviceService = { var deviceService = {
devices: [] devices: []
, devicesBySerial: {}
} }
function get(data) {
return deviceService.devices[deviceService.devicesBySerial[data.serial]]
}
function insert(data, alter) {
deviceService.devicesBySerial[data.serial] =
deviceService.devices.push(data) - 1
_.assign(data, alter)
$rootScope.$digest()
}
function modify(data, properties) {
if (data) {
_.assign(data, properties)
$rootScope.$digest()
}
}
function remove(data) {
var index = deviceService.devicesBySerial[data.serial]
if (index >= 0) {
deviceService.devices.splice(index, 1)
delete deviceService.devicesBySerial[data.serial]
$rootScope.$digest()
}
}
socket.on('device.present', function(data) {
remove(data)
insert(data, {
present: true
})
})
socket.on('device.status', function(data) {
modify(get(data), data)
})
socket.on('device.absent', function(data) {
remove(data)
})
oboe('/api/v1/devices') oboe('/api/v1/devices')
.node('devices[*]', function(device) { .node('devices[*]', function(device) {
deviceService.devices.push(device) // We want to skip other arguments
$rootScope.$digest() insert(device)
}) })
return deviceService return deviceService

View file

@ -16,6 +16,11 @@ define(['./module', 'lodash'], function(mod, _) {
$rootScope.$digest() $rootScope.$digest()
}) })
socket.on('device.absent', function(data) {
_.pull(groupService.members, data.serial)
$rootScope.$digest()
})
groupService.invite = function(requirements) { groupService.invite = function(requirements) {
socket.emit('group.invite', requirements) socket.emit('group.invite', requirements)
} }