mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-20 01:03:30 +02:00
Switch to a more consistent naming convention for services and resource loading.
This commit is contained in:
86
res/app/scripts/services/DeviceService.js
Normal file
86
res/app/scripts/services/DeviceService.js
Normal file
@@ -0,0 +1,86 @@
|
||||
define(['./_module', 'oboe'], function(services, oboe) {
|
||||
function DeviceServiceFactory($rootScope, $http, 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)
|
||||
})
|
||||
|
||||
socket.on('device.identity', function(data) {
|
||||
modify(get(data), data)
|
||||
})
|
||||
|
||||
socket.on('group.join', function(data) {
|
||||
modify(get(data), data)
|
||||
})
|
||||
|
||||
socket.on('group.leave', function(data) {
|
||||
modify(get(data), {
|
||||
owner: null
|
||||
})
|
||||
})
|
||||
|
||||
oboe('/api/v1/devices')
|
||||
.node('devices[*]', function(device) {
|
||||
// We want to skip other arguments
|
||||
insert(device)
|
||||
})
|
||||
|
||||
deviceService.get = function(serial) {
|
||||
return $http.get('/api/v1/devices/' + serial)
|
||||
.then(function(response) {
|
||||
return response.data.device
|
||||
})
|
||||
}
|
||||
|
||||
return deviceService
|
||||
}
|
||||
|
||||
services.factory('DeviceService'
|
||||
, [ '$rootScope'
|
||||
, '$http'
|
||||
, 'SocketService'
|
||||
, DeviceServiceFactory
|
||||
])
|
||||
})
|
||||
Reference in New Issue
Block a user