balena-cli/lib/resin/models/device.coffee

119 lines
2.7 KiB
CoffeeScript
Raw Normal View History

2014-11-19 17:38:15 +00:00
canvas = require('./_canvas')
2014-12-08 12:48:19 +00:00
_ = require('lodash-contrib')
2014-11-26 16:38:02 +00:00
errors = require('../errors/errors')
2014-11-26 18:02:59 +00:00
server = require('../server/server')
settings = require('../settings')
2014-11-19 17:38:15 +00:00
2014-12-08 12:48:19 +00:00
# Get all devices
#
# @param {Function} callback callback(error, devices)
#
# @throw {NotAny} Will throw if no devices were found
#
# @example Get all devices
# resin.models.devices.getAll (error, devices) ->
# throw error if error?
# console.log(devices)
#
2014-11-28 16:46:24 +00:00
exports.getAll = (callback) ->
return canvas.get
resource: 'device'
options:
expand: 'application'
orderby: 'name asc'
.then (devices) ->
if _.isEmpty(devices)
return callback(new errors.NotAny('devices'))
return callback(null, devices)
.catch (error) ->
return callback(error)
2014-12-08 12:48:19 +00:00
# Get all devices by application
#
# @param {String, Number} applicationId application id
# @param {Function} callback callback(error, devices)
#
# @throw {NotAny} Will throw if no devices were found
#
# @example Get all devices by application
# resin.models.devices.getAllByApplication (error, devices) ->
# throw error if error?
# console.log(devices)
#
2014-11-28 16:46:24 +00:00
exports.getAllByApplication = (applicationId, callback) ->
2014-11-19 17:38:15 +00:00
return canvas.get
resource: 'device'
options:
filter:
application: applicationId
expand: 'application'
orderby: 'name asc'
2014-11-26 14:32:57 +00:00
.then (devices) ->
if _.isEmpty(devices)
2014-11-26 18:02:59 +00:00
return callback(new errors.NotAny('devices'))
2014-11-26 14:32:57 +00:00
2014-11-26 18:02:59 +00:00
return callback(null, devices)
2014-11-21 17:23:29 +00:00
2014-11-26 18:02:59 +00:00
.catch (error) ->
return callback(error)
2014-12-08 12:48:19 +00:00
# Get a single device
#
# @param {String, Number} id device id
# @param {Function} callback callback(error, device)
#
# @throw {NotFound} Will throw if device was not found
#
# @example Find device
# resin.models.device.get 51, (error, device) ->
# throw error if error?
# console.log(device)
#
2014-12-05 15:32:14 +00:00
exports.get = (deviceId, callback) ->
return canvas.get
resource: 'device'
id: deviceId
options:
expand: 'application'
.then (device) ->
if not device?
return callback(new errors.NotFound("device #{id}"))
return callback(null, device)
.catch (error) ->
return callback(error)
2014-12-08 12:48:19 +00:00
# Remove device
#
# @param {String, Number} id device id
# @param {Function} callback callback(error)
#
# @example Remove device
# resin.models.device.remove 51, (error) ->
# throw error if error?
#
2014-11-26 18:02:59 +00:00
exports.remove = (id, callback) ->
2014-11-21 17:23:29 +00:00
return canvas.delete
resource: 'device'
id: id
2014-11-26 18:02:59 +00:00
.then ->
return callback()
.catch (error) ->
return callback(error)
2014-12-08 12:48:19 +00:00
# Identify device
#
# @param {String} uuid device uuid
# @param {Function} callback callback(error)
#
# @example Identify device
# resin.models.device.identify '23c73a12e3527df55c60b9ce647640c1b7da1b32d71e6a21369ac0f00db828', (error) ->
# throw error if error?
#
2014-11-26 18:02:59 +00:00
exports.identify = (uuid, callback) ->
2014-12-08 12:48:19 +00:00
server.post(settings.get('urls.identify'), { uuid }, _.unary(callback))