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

Modify APIs from Aysnc -> Sync using timeout

This commit is contained in:
Vishal Banthia 2015-12-14 14:02:20 +09:00
parent d67d06a19f
commit a6266931ad
10 changed files with 354 additions and 239 deletions

View file

@ -49,24 +49,22 @@ function getDeviceBySerial(req, res) {
dbapi.loadDevice(serial)
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
if(fields) {
device = _.pick(device, fields.split(','))
}
res.json({
success: true
, device: device
})
}
else {
res.status(404).json({
if (!device) {
return res.status(404).json({
success: false
, description: 'Device not found'
})
}
datautil.normalize(device, req.user)
if(fields) {
device = _.pick(device, fields.split(','))
}
res.json({
success: true
, device: device
})
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)

View file

@ -1,13 +1,16 @@
var util = require('util')
var Promise = require('bluebird')
var _ = require('lodash')
var Promise = require('bluebird')
var uuid = require('node-uuid')
var dbapi = require('../../../db/api')
var logger = require('../../../util/logger')
var datautil = require('../../../util/datautil')
var deviceutil = require('../../../util/deviceutil')
var wire = require('../../../wire')
var wireutil = require('../../../wire/util')
var wirerouter = require('../../../wire/router')
var log = logger.createLogger('api:controllers:user')
@ -66,32 +69,29 @@ function getUserDeviceBySerial(req, res) {
dbapi.loadDevice(serial)
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
if (device.owner && device.owner.email === req.user.email) {
if(fields) {
device = _.pick(device, fields.split(','))
}
res.json({
success: true
, device: device
})
}
else {
res.status(401).json({
success: false
, description: 'Device is not owned by you'
})
}
}
else {
res.status(404).json({
if (!device) {
return res.status(404).json({
success: false
, description: 'Device not found'
})
}
datautil.normalize(device, req.user)
if (!deviceutil.isOwnedByUser(device, req.user)) {
return res.status(403).json({
success: false
, description: 'Device is not owned by you'
})
}
if(fields) {
device = _.pick(device, fields.split(','))
}
res.json({
success: true
, device: device
})
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
@ -107,55 +107,71 @@ function addUserDevice(req, res) {
dbapi.loadDevice(serial)
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
if(device.present && device.ready && !device.using && !device.owner) {
var requirements = {
'serial': {
'value': serial,
'match': 'exact'
}
}
req.options.push.send([
device.channel
, wireutil.envelope(
new wire.GroupMessage(
new wire.OwnerMessage(
req.user.email
, req.user.name
, req.user.group
)
, timeout
, wireutil.toDeviceRequirements(requirements)
)
)
])
res.status(202).json({
success: true
, description: 'Device Add request is accepted. Check if device is successfully added using pollingUrl'
, pollingUrl: util.format('%s://%s%s/user/devices/%s'
, req.protocol
, req.get('host')
, req.swagger.operation.api.basePath
, serial
)
})
} else {
res.status(401).json({
success: false
, description: 'Device is being used or not available'
})
}
} else {
res.status(404).json({
if (!device) {
return res.status(404).json({
success: false
, description: 'Bad device serial'
, description: 'Device not found'
})
}
datautil.normalize(device, req.user)
if (!deviceutil.isAddable(device, req.user)) {
return res.status(403).json({
success: false
, description: 'Device is being used or not available'
})
}
// Timer will be called if no JoinGroupMessage is received till 5 seconds
var responseTimer = setTimeout(function() {
req.options.channelRouter.removeListener(wireutil.global, messageListener)
return res.status(504).json({
success: false
, description: 'Device is not responding'
})
}, 5000)
var messageListener = wirerouter()
.on(wire.JoinGroupMessage, function(channel, message) {
if (message.serial === serial && message.owner.email === req.user.email) {
clearTimeout(responseTimer)
req.options.channelRouter.removeListener(wireutil.global, messageListener)
return res.json({
success: true
, description: 'Device successfully added'
})
}
})
.handler()
req.options.channelRouter.on(wireutil.global, messageListener)
req.options.push.send([
device.channel
, wireutil.envelope(
new wire.GroupMessage(
new wire.OwnerMessage(
req.user.email
, req.user.name
, req.user.group
)
, timeout
, wireutil.toDeviceRequirements({
'serial': {
'value': serial
, 'match': 'exact'
}
})
)
)
])
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
res.status(500).json({
success: false
})
})
}
@ -164,49 +180,66 @@ function deleteUserDeviceBySerial(req, res) {
dbapi.loadDevice(serial)
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
if(device.using && device.owner.email == req.user.email) {
var requirements = {
'serial': {
'value': serial,
'match': 'exact'
}
}
req.options.push.send([
device.channel
, wireutil.envelope(
new wire.UngroupMessage(
wireutil.toDeviceRequirements(requirements)
)
)
])
res.status(202).json({
success: true
, description: 'Device Release request is accepted. Check if device is successfully removed using pollingUrl'
, pollingUrl: util.format('%s://%s%s/user/devices/%s'
, req.protocol
, req.get('host')
, req.swagger.operation.api.basePath
, serial
)
})
} else {
res.status(401).json({
success: false
, description: 'You cannot kick this device'
})
}
} else {
res.status(404).json({
if (!device) {
return res.status(404).json({
success: false
, description: 'Bad device serial'
, description: 'Device not found'
})
}
datautil.normalize(device, req.user)
if (!deviceutil.isOwnedByUser(device, req.user)) {
return res.status(403).json({
success: false
, description: 'You cannot release this device. Not owned by you'
})
}
// Timer will be called if no JoinGroupMessage is received till 5 seconds
var responseTimer = setTimeout(function() {
req.options.channelRouter.removeListener(wireutil.global, messageListener)
return res.status(504).json({
success: false
, description: 'Device is not responding'
})
}, 5000)
var messageListener = wirerouter()
.on(wire.LeaveGroupMessage, function(channel, message) {
if (message.serial === serial && message.owner.email === req.user.email) {
clearTimeout(responseTimer)
req.options.channelRouter.removeListener(wireutil.global, messageListener)
return res.json({
success: true
, description: 'Device successfully removed'
})
}
})
.handler()
req.options.channelRouter.on(wireutil.global, messageListener)
req.options.push.send([
device.channel
, wireutil.envelope(
new wire.UngroupMessage(
wireutil.toDeviceRequirements({
'serial': {
'value': serial
, 'match': 'exact'
}
})
)
)
])
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
res.status(500).json({
success: false
})
})
}
@ -215,41 +248,58 @@ function remoteConnectUserDeviceBySerial(req, res) {
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.status(202).json({
success: true
, description: 'Device Connect request is accepted. Check if device is successfully connected using pollingUrl'
, pollingUrl: util.format('%s://%s%s/user/devices/%s'
, req.protocol
, req.get('host')
, req.swagger.operation.api.basePath
, serial
)
})
}
else {
res.status(401).json({
success: false
, description: 'Device is not owned by you or is not available'
})
}
}
else {
res.status(404).json({
if (!device) {
return res.status(404).json({
success: false
, description: 'Device not found'
})
}
datautil.normalize(device, req.user)
if (!deviceutil.isOwnedByUser(device, req.user)) {
return res.status(403).json({
success: false
, description: 'Device is not owned by you or is not available'
})
}
var responseChannel = 'txn_' + uuid.v4()
req.options.sub.subscribe(responseChannel)
// Timer will be called if no JoinGroupMessage is received till 5 seconds
var timer = setTimeout(function() {
req.options.channelRouter.removeListener(responseChannel, messageListener)
req.options.sub.unsubscribe(responseChannel)
return res.status(504).json({
success: false
, description: 'Device is not responding'
})
}, 5000)
var messageListener = wirerouter()
.on(wire.ConnectStartedMessage, function(channel, message) {
if (message.serial === serial) {
clearTimeout(timer)
req.options.sub.unsubscribe(responseChannel)
req.options.channelRouter.removeListener(responseChannel, messageListener)
return res.json({
success: true
, remoteConnectUrl: message.url
})
}
})
.handler()
req.options.channelRouter.on(responseChannel, messageListener)
req.options.push.send([
device.channel
, wireutil.transaction(
responseChannel
, new wire.ConnectStartMessage()
)
])
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
@ -264,41 +314,59 @@ function remoteDisconnectUserDeviceBySerial(req, res) {
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.status(202).json({
success: true
, description: 'Device Disonnect request is accepted. Check if device is successfully disconnected using pollingUrl'
, pollingUrl: util.format('%s://%s%s/user/devices/%s'
, req.protocol
, req.get('host')
, req.swagger.operation.api.basePath
, serial
)
})
}
else {
res.status(401).json({
success: false
, description: 'Device is not owned by you or is not available'
})
}
}
else {
res.status(404).json({
if (!device) {
return res.status(404).json({
success: false
, description: 'Device not found'
})
}
datautil.normalize(device, req.user)
if (!deviceutil.isOwnedByUser(device, req.user)) {
return res.status(403).json({
success: false
, description: 'Device is not owned by you or is not available'
})
}
var responseChannel = 'txn_' + uuid.v4()
req.options.sub.subscribe(responseChannel)
// Timer will be called if no JoinGroupMessage is received till 5 seconds
var timer = setTimeout(function() {
req.options.channelRouter.removeListener(responseChannel, messageListener)
req.options.sub.unsubscribe(responseChannel)
return res.status(504).json({
success: false
, description: 'Device is not responding'
})
}, 5000)
var messageListener = wirerouter()
.on(wire.ConnectStoppedMessage, function(channel, message) {
if (message.serial === serial) {
clearTimeout(timer)
req.options.sub.unsubscribe(responseChannel)
req.options.channelRouter.removeListener(responseChannel, messageListener)
return res.json({
success: true
, description: 'Device remote disconnected successfully'
})
}
})
.handler()
req.options.channelRouter.on(responseChannel, messageListener)
req.options.push.send([
device.channel
, wireutil.transaction(
responseChannel
, new wire.ConnectStopMessage()
)
])
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)