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:
parent
f432dc9fb9
commit
2086d7d439
9 changed files with 147 additions and 39 deletions
|
@ -1,13 +1,56 @@
|
|||
define(['./module', 'oboe'], function(mod, oboe) {
|
||||
function DevicesServiceFactory($rootScope, socketService) {
|
||||
function DevicesServiceFactory($rootScope, socket) {
|
||||
var deviceService = {
|
||||
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')
|
||||
.node('devices[*]', function(device) {
|
||||
deviceService.devices.push(device)
|
||||
$rootScope.$digest()
|
||||
// We want to skip other arguments
|
||||
insert(device)
|
||||
})
|
||||
|
||||
return deviceService
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue