mirror of
https://github.com/openstf/stf
synced 2025-10-04 02:09:32 +02:00
Add more advanced filtering to the device tracker. Make the group tracker work properly.
This commit is contained in:
parent
334490d878
commit
fa7437a445
3 changed files with 65 additions and 23 deletions
|
@ -34,7 +34,7 @@ dbapi.loadUser = function(email) {
|
||||||
|
|
||||||
dbapi.loadGroup = function(email) {
|
dbapi.loadGroup = function(email) {
|
||||||
return db.run(r.table('devices').getAll(email, {
|
return db.run(r.table('devices').getAll(email, {
|
||||||
index: 'ownerEmail'
|
index: 'owner'
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
var r = require('rethinkdb')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
users: {
|
users: {
|
||||||
primaryKey: 'email'
|
primaryKey: 'email'
|
||||||
|
@ -5,8 +7,8 @@ module.exports = {
|
||||||
, devices: {
|
, devices: {
|
||||||
primaryKey: 'serial'
|
primaryKey: 'serial'
|
||||||
, indexes: {
|
, indexes: {
|
||||||
ownerEmail: function(device) {
|
owner: function(device) {
|
||||||
return device('owner')('email')
|
return r.branch(device('present'), device('owner')('email'), r.literal())
|
||||||
}
|
}
|
||||||
, lastHeartbeatAt: null
|
, lastHeartbeatAt: null
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,34 +88,59 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scopedSocket.on('device.add', function (data) {
|
function fetch(data) {
|
||||||
|
deviceService.load(data.serial)
|
||||||
|
.then(changeListener)
|
||||||
|
.catch(function() {})
|
||||||
|
}
|
||||||
|
|
||||||
|
function addListener(data) {
|
||||||
var device = get(data)
|
var device = get(data)
|
||||||
if (device) {
|
if (device) {
|
||||||
modify(device, data)
|
modify(device, data)
|
||||||
}
|
}
|
||||||
else if (options.insertPresent) {
|
else if (options.filter(data)) {
|
||||||
insert(data)
|
insert(data)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
scopedSocket.on('device.remove', function (data) {
|
function removeListener(data) {
|
||||||
var device = get(data)
|
var device = get(data)
|
||||||
if (device) {
|
if (device) {
|
||||||
modify(device, data)
|
modify(device, data)
|
||||||
}
|
if (!options.filter(device)) {
|
||||||
})
|
|
||||||
|
|
||||||
scopedSocket.on('device.change', function (data) {
|
|
||||||
var device = get(data)
|
|
||||||
if (device) {
|
|
||||||
if (options.removeAbsent) {
|
|
||||||
remove(device)
|
remove(device)
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
modify(device, data)
|
else {
|
||||||
|
if (options.filter(data)) {
|
||||||
|
insert(data)
|
||||||
|
// We've only got partial data
|
||||||
|
fetch(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
function changeListener(data) {
|
||||||
|
var device = get(data)
|
||||||
|
if (device) {
|
||||||
|
modify(device, data)
|
||||||
|
if (!options.filter(device)) {
|
||||||
|
remove(device)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (options.filter(data)) {
|
||||||
|
insert(data)
|
||||||
|
// We've only got partial data
|
||||||
|
fetch(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scopedSocket.on('device.add', addListener)
|
||||||
|
scopedSocket.on('device.remove', removeListener)
|
||||||
|
scopedSocket.on('device.change', changeListener)
|
||||||
|
|
||||||
this.add = function(device) {
|
this.add = function(device) {
|
||||||
remove(device)
|
remove(device)
|
||||||
|
@ -127,7 +152,9 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) {
|
||||||
|
|
||||||
deviceService.trackAll = function ($scope) {
|
deviceService.trackAll = function ($scope) {
|
||||||
var tracker = new Tracker($scope, {
|
var tracker = new Tracker($scope, {
|
||||||
insertPresent: true
|
filter: function(device) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
oboe('/api/v1/devices')
|
oboe('/api/v1/devices')
|
||||||
|
@ -140,7 +167,9 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) {
|
||||||
|
|
||||||
deviceService.trackGroup = function ($scope) {
|
deviceService.trackGroup = function ($scope) {
|
||||||
var tracker = new Tracker($scope, {
|
var tracker = new Tracker($scope, {
|
||||||
removeAbsent: true
|
filter: function(device) {
|
||||||
|
return device.using
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
oboe('/api/v1/group')
|
oboe('/api/v1/group')
|
||||||
|
@ -151,15 +180,26 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) {
|
||||||
return tracker
|
return tracker
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceService.get = function (serial, $scope) {
|
deviceService.load = function(serial) {
|
||||||
var tracker = new Tracker($scope, {})
|
|
||||||
|
|
||||||
return $http.get('/api/v1/devices/' + serial)
|
return $http.get('/api/v1/devices/' + serial)
|
||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
tracker.add(response.data.device)
|
|
||||||
return response.data.device
|
return response.data.device
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deviceService.get = function (serial, $scope) {
|
||||||
|
var tracker = new Tracker($scope, {
|
||||||
|
filter: function(device) {
|
||||||
|
return device.serial === serial
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return deviceService.load(serial)
|
||||||
|
.then(function(device) {
|
||||||
|
tracker.add(device)
|
||||||
|
return device
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return deviceService
|
return deviceService
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue