mirror of
https://github.com/openstf/stf
synced 2025-10-04 10:19:30 +02:00
add remoteConnect endpoint for device
This commit is contained in:
parent
232163d290
commit
96494247bb
2 changed files with 202 additions and 1 deletions
|
@ -13,6 +13,9 @@ module.exports = {
|
||||||
, getCurrentUserDevices: getCurrentUserDevices
|
, getCurrentUserDevices: getCurrentUserDevices
|
||||||
, addDeviceToUser: addDeviceToUser
|
, addDeviceToUser: addDeviceToUser
|
||||||
, deleteDeviceFromUser: deleteDeviceFromUser
|
, deleteDeviceFromUser: deleteDeviceFromUser
|
||||||
|
, getUserDeviceBySerial: getUserDeviceBySerial
|
||||||
|
, connectDeviceBySerial: connectDeviceBySerial
|
||||||
|
, disconnectDeviceBySerial: disconnectDeviceBySerial
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentUser(req, res) {
|
function getCurrentUser(req, res) {
|
||||||
|
@ -52,7 +55,7 @@ function addDeviceToUser(req, res) {
|
||||||
.then(function(device) {
|
.then(function(device) {
|
||||||
if (device) {
|
if (device) {
|
||||||
datautil.normalize(device, req.user)
|
datautil.normalize(device, req.user)
|
||||||
if(device.ready && !device.using) {
|
if(device.present && device.ready && !device.using && !device.owner) {
|
||||||
|
|
||||||
var requirements = {
|
var requirements = {
|
||||||
'serial': {
|
'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
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -86,6 +86,34 @@ paths:
|
||||||
- accessTokenAuth: []
|
- accessTokenAuth: []
|
||||||
/user/devices/{serial}:
|
/user/devices/{serial}:
|
||||||
x-swagger-router-controller: user
|
x-swagger-router-controller: user
|
||||||
|
get:
|
||||||
|
summary: Device Information
|
||||||
|
description: The device enpoint return information about a single device.
|
||||||
|
operationId: getUserDeviceBySerial
|
||||||
|
tags:
|
||||||
|
- user
|
||||||
|
parameters:
|
||||||
|
- name: serial
|
||||||
|
in: path
|
||||||
|
description: Device Serial
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
- name: fields
|
||||||
|
in: query
|
||||||
|
description: Fields query parameter takes a comma seperated list of fields. Only listed field will be return in response
|
||||||
|
required: false
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Device Information
|
||||||
|
schema:
|
||||||
|
$ref: "#/definitions/DeviceResponse"
|
||||||
|
default:
|
||||||
|
description: Unexpected Error
|
||||||
|
schema:
|
||||||
|
$ref: "#/definitions/ErrorResponse"
|
||||||
|
security:
|
||||||
|
- accessTokenAuth: []
|
||||||
delete:
|
delete:
|
||||||
summary: Release device from user
|
summary: Release device from user
|
||||||
description: The User Devices endpoint will request for device release from stf server.
|
description: The User Devices endpoint will request for device release from stf server.
|
||||||
|
@ -107,6 +135,50 @@ paths:
|
||||||
$ref: "#/definitions/ErrorResponse"
|
$ref: "#/definitions/ErrorResponse"
|
||||||
security:
|
security:
|
||||||
- accessTokenAuth: []
|
- accessTokenAuth: []
|
||||||
|
/user/devices/{serial}/remoteConnect:
|
||||||
|
x-swagger-router-controller: user
|
||||||
|
post:
|
||||||
|
summary: Remote Connect
|
||||||
|
description: The device connect endpoint will request stf server to connect remotely
|
||||||
|
operationId: connectDeviceBySerial
|
||||||
|
tags:
|
||||||
|
- user
|
||||||
|
parameters:
|
||||||
|
- name: serial
|
||||||
|
in: path
|
||||||
|
description: Device Serial
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
"202":
|
||||||
|
description: Device Connect Request Status
|
||||||
|
default:
|
||||||
|
description: Unexpected Error
|
||||||
|
schema:
|
||||||
|
$ref: "#/definitions/ErrorResponse"
|
||||||
|
security:
|
||||||
|
- accessTokenAuth: []
|
||||||
|
delete:
|
||||||
|
summary: Remote Disconnect
|
||||||
|
description: The device connect endpoint will request stf server to disconnect remotely
|
||||||
|
operationId: disconnectDeviceBySerial
|
||||||
|
tags:
|
||||||
|
- user
|
||||||
|
parameters:
|
||||||
|
- name: serial
|
||||||
|
in: path
|
||||||
|
description: Device Serial
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
"202":
|
||||||
|
description: Device Disconnect Request Status
|
||||||
|
default:
|
||||||
|
description: Unexpected Error
|
||||||
|
schema:
|
||||||
|
$ref: "#/definitions/ErrorResponse"
|
||||||
|
security:
|
||||||
|
- accessTokenAuth: []
|
||||||
/user/accessTokens:
|
/user/accessTokens:
|
||||||
x-swagger-router-controller: token
|
x-swagger-router-controller: token
|
||||||
get:
|
get:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue