mirror of
https://github.com/openstf/stf
synced 2025-10-04 02:09:32 +02:00
Better device present/absent handling by separating it from the status event.
This commit is contained in:
parent
f432dc9fb9
commit
2086d7d439
9 changed files with 147 additions and 39 deletions
|
@ -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({
|
|
||||||
status: status.status
|
|
||||||
, provider: status.provider
|
|
||||||
, statusChangedAt: r.now()
|
|
||||||
}))
|
|
||||||
.then(function(stats) {
|
|
||||||
if (stats.skipped) {
|
|
||||||
return db.run(r.table('devices').insert({
|
return db.run(r.table('devices').insert({
|
||||||
serial: serial
|
serial: serial
|
||||||
, provider: status.provider
|
, present: true
|
||||||
, status: status.status
|
, provider: device.provider
|
||||||
|
, status: device.status
|
||||||
, statusChangedAt: r.now()
|
, statusChangedAt: r.now()
|
||||||
, createdAt: r.now()
|
, createdAt: r.now()
|
||||||
|
}
|
||||||
|
, {
|
||||||
|
upsert: true
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
return stats
|
|
||||||
|
dbapi.saveDeviceStatus = function(serial, status) {
|
||||||
|
return dbapi.ensureDeviceSaved(serial)
|
||||||
|
.then(function() {
|
||||||
|
return db.run(r.table('devices').get(serial).update({
|
||||||
|
status: status
|
||||||
|
, statusChangedAt: r.now()
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -432,9 +432,15 @@ module.exports = function(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function gracefullyExit() {
|
function gracefullyExit() {
|
||||||
|
if (isGrouped()) {
|
||||||
|
leaveGroup()
|
||||||
|
Promise.delay(500).then(gracefullyExit)
|
||||||
|
}
|
||||||
|
else {
|
||||||
log.info('Bye')
|
log.info('Bye')
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
process.on('SIGINT', function() {
|
process.on('SIGINT', function() {
|
||||||
gracefullyExit()
|
gracefullyExit()
|
||||||
|
|
|
@ -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])
|
||||||
})
|
})
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue