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

add remoteConnect endpoint for device

This commit is contained in:
Vishal Banthia 2015-12-07 22:26:11 +09:00
parent 232163d290
commit 96494247bb
2 changed files with 202 additions and 1 deletions

View file

@ -13,6 +13,9 @@ module.exports = {
, getCurrentUserDevices: getCurrentUserDevices
, addDeviceToUser: addDeviceToUser
, deleteDeviceFromUser: deleteDeviceFromUser
, getUserDeviceBySerial: getUserDeviceBySerial
, connectDeviceBySerial: connectDeviceBySerial
, disconnectDeviceBySerial: disconnectDeviceBySerial
}
function getCurrentUser(req, res) {
@ -52,7 +55,7 @@ function addDeviceToUser(req, res) {
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
if(device.ready && !device.using) {
if(device.present && device.ready && !device.using && !device.owner) {
var requirements = {
'serial': {
@ -140,3 +143,129 @@ function deleteDeviceFromUser(req, res) {
}
})
}
function getUserDeviceBySerial(req, res) {
var serial = req.swagger.params.serial.value
var fields = req.swagger.params.fields.value
dbapi.loadDevice(serial)
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
if (device.owner && device.owner === req.user.email) {
if(fields) {
device = _.pick(device, fields.split(','))
}
res.json({
success: true
, device: device
})
}
else {
res.json(404, {
success: false
, description: 'device is not owned by you'
})
}
}
else {
res.json(404, {
success: false
})
}
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
res.json(500, {
success: false
})
})
}
function connectDeviceBySerial(req, res) {
var serial = req.swagger.params.serial.value
dbapi.loadDevice(serial)
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
if (device.present && device.ready && device.using && device.owner.email == req.user.email) {
req.options.push.send([
device.channel
, wireutil.envelope(
new wire.ConnectStartMessage()
)
])
res.json(202, {
success: true
, description: 'Device Connect request is accepted'
})
}
else {
res.json(500, {
success: false
, description: 'Device is not owned by you or is not available'
})
}
}
else {
res.json(404, {
success: false
, description: 'Bad device serial'
})
}
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
res.json(500, {
success: false
})
})
}
function disconnectDeviceBySerial(req, res) {
var serial = req.swagger.params.serial.value
dbapi.loadDevice(serial)
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
if (device.present && device.ready && device.using && device.owner.email == req.user.email) {
req.options.push.send([
device.channel
, wireutil.envelope(
new wire.ConnectStopMessage()
)
])
res.json(202, {
success: true
, description: 'Device Disonnect request is accepted'
})
}
else {
res.json(500, {
success: false
, description: 'Device is not owned by you or is not available'
})
}
}
else {
res.json(404, {
success: false
, description: 'Bad device serial'
})
}
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
res.json(500, {
success: false
})
})
}