Mix device module with device model

This commit is contained in:
Juan Cruz Viotti 2014-12-22 15:02:52 -04:00
parent cda24c9e1b
commit a6b3ca1442
7 changed files with 54 additions and 58 deletions

View File

@ -16,14 +16,14 @@ exports.create = permissions.user (params, options) ->
if deviceType?
return callback(null, deviceType)
else
deviceTypes = resin.device.getSupportedDevices()
deviceTypes = resin.models.device.getSupportedDeviceTypes()
ui.widgets.select('Select a type', deviceTypes, callback)
(type, callback) ->
# TODO: Currently returns 'unknown'.
# Maybe we should break or handle better?
slugifiedType = resin.device.getDeviceSlug(type)
slugifiedType = resin.models.device.getDeviceSlug(type)
resin.models.application.create(params.name, slugifiedType, callback)
@ -34,7 +34,7 @@ exports.list = permissions.user ->
errors.handle(error) if error?
log.out ui.widgets.table.horizontal applications, (application) ->
application.device_type = resin.device.getDisplayName(application.device_type)
application.device_type = resin.models.device.getDisplayName(application.device_type)
application['Online Devices'] = _.where(application.device, is_online: 1).length
application['All Devices'] = application.device?.length or 0
delete application.git_repository
@ -47,7 +47,7 @@ exports.info = permissions.user (params) ->
errors.handle(error) if error?
log.out ui.widgets.table.vertical application, (application) ->
application.device_type = resin.device.getDisplayName(application.device_type)
application.device_type = resin.models.device.getDisplayName(application.device_type)
delete application.device
return application
, [ 'ID', 'Name', 'Device Type', 'Git Repository', 'Commit' ]

View File

@ -12,7 +12,7 @@ exports.list = permissions.user (params) ->
log.out ui.widgets.table.horizontal devices, (device) ->
device.application = device.application[0].app_name
device.device_type = resin.device.getDisplayName(device.device_type)
device.device_type = resin.models.device.getDisplayName(device.device_type)
delete device.note
delete device.supervisor_version
delete device.uuid
@ -25,7 +25,7 @@ exports.info = permissions.user (params) ->
errors.handle(error) if error?
log.out ui.widgets.table.vertical device, (device) ->
device.device_type = resin.device.getDisplayName(device.device_type)
device.device_type = resin.device.models.getDisplayName(device.device_type)
device.application = device.application[0].app_name
return device
, [

View File

@ -1,47 +0,0 @@
_ = require('lodash')
# TODO: This should be fetch from the server
DEVICES = require('./device-data.json')
# Get display name for a device
#
# For a list of supported devices, see getSupportedDevices()
#
# @param {String} device device name
# @return {String} device display name or 'Unknown'
#
# @example Get display name
# console.log resin.device.getDisplayName('raspberry-pi') # Raspberry Pi
# console.log resin.device.getDisplayName('rpi') # Raspberry Pi
#
exports.getDisplayName = (device) ->
if _.indexOf(exports.getSupportedDevices(), device) isnt -1
return device
for key, value of DEVICES
if _.indexOf(value.names, device) isnt -1
return key
return 'Unknown'
# Get device slug
#
# @param {String} device device name
# @return {String} device slug or 'unknown'
#
# @example Get device slug
# console.log resin.device.getDeviceSlug('Raspberry Pi') # raspberry-pi
#
exports.getDeviceSlug = (device) ->
displayName = exports.getDisplayName(device)
return DEVICES[displayName]?.slug or 'unknown'
# Get a list of supported devices
#
# @return {Array<String>} a list of all supported devices, by their display names
#
# @example Get all supported devices
# devices = resin.device.getSupportedDevices()
# console.log(devices)
#
exports.getSupportedDevices = ->
return _.keys(DEVICES)

View File

@ -4,6 +4,5 @@ module.exports =
errors: require('./errors/errors')
data: require('./data/data')
auth: require('./auth/auth')
device: require('./device/device')
vcs: require('./vcs/vcs')
settings: require('./settings')

View File

@ -3,6 +3,7 @@ _ = require('lodash-contrib')
errors = require('../errors/errors')
server = require('../server/server')
settings = require('../settings')
DEVICES = require('./device-data.json')
# Get all devices
#
@ -140,3 +141,46 @@ exports.rename = (id, name, callback) ->
.catch (error) ->
return callback(error)
# Get display name for a device
#
# For a list of supported devices, see getSupportedDeviceTypes()
#
# @param {String} device device name
# @return {String} device display name or 'Unknown'
#
# @example Get display name
# console.log resin.models.device.getDisplayName('raspberry-pi') # Raspberry Pi
# console.log resin.models.device.getDisplayName('rpi') # Raspberry Pi
#
exports.getDisplayName = (device) ->
if _.indexOf(exports.getSupportedDeviceTypes(), device) isnt -1
return device
for key, value of DEVICES
if _.indexOf(value.names, device) isnt -1
return key
return 'Unknown'
# Get device slug
#
# @param {String} device device name
# @return {String} device slug or 'unknown'
#
# @example Get device slug
# console.log resin.models.device.getDeviceSlug('Raspberry Pi') # raspberry-pi
#
exports.getDeviceSlug = (device) ->
displayName = exports.getDisplayName(device)
return DEVICES[displayName]?.slug or 'unknown'
# Get a list of supported device types
#
# @return {Array<String>} a list of all supported devices, by their display names
#
# @example Get all supported devices
# devices = resin.models.device.getSupportedDevicesTypes()
# console.log(devices)
#
exports.getSupportedDeviceTypes = ->
return _.keys(DEVICES)

View File

@ -29,7 +29,7 @@ describe 'Device:', ->
expect(device.getDisplayName(name)).to.equal('Unknown')
it 'should return the name itself if passing the display name', ->
for supportedDevice in device.getSupportedDevices()
for supportedDevice in device.getSupportedDeviceTypes()
displayName = device.getDisplayName(supportedDevice)
expect(displayName).to.equal(supportedDevice)
@ -48,12 +48,12 @@ describe 'Device:', ->
name = _.first(value.names)
expect(device.getDeviceSlug(name)).to.equal(value.slug)
describe '#getSupportedDevices()', ->
describe '#getSupportedDeviceTypes()', ->
it 'should return an array', ->
expect(device.getSupportedDevices()).to.be.an.instanceof(Array)
expect(device.getSupportedDeviceTypes()).to.be.an.instanceof(Array)
it 'should have every supported device', ->
supportedDevices = device.getSupportedDevices()
supportedDevices = device.getSupportedDeviceTypes()
for key, value in DEVICES
expect(supportedDevices.indexOf(key)).to.not.equal(-1)