diff --git a/lib/units/api/controllers/user.js b/lib/units/api/controllers/user.js index 779b4a70..b0cb4df9 100644 --- a/lib/units/api/controllers/user.js +++ b/lib/units/api/controllers/user.js @@ -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 + }) + }) +} diff --git a/lib/units/api/swagger/api_v1.yaml b/lib/units/api/swagger/api_v1.yaml index bf7b380f..49ad2961 100644 --- a/lib/units/api/swagger/api_v1.yaml +++ b/lib/units/api/swagger/api_v1.yaml @@ -86,6 +86,34 @@ paths: - accessTokenAuth: [] /user/devices/{serial}: 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: summary: Release device from user description: The User Devices endpoint will request for device release from stf server. @@ -107,6 +135,50 @@ paths: $ref: "#/definitions/ErrorResponse" security: - 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: x-swagger-router-controller: token get: