diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index 83d1d66b..8aa75927 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -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' ] diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index d0d53ea5..27517b26 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -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 , [ diff --git a/lib/resin/device/device.coffee b/lib/resin/device/device.coffee deleted file mode 100644 index 9d94205d..00000000 --- a/lib/resin/device/device.coffee +++ /dev/null @@ -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} 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) diff --git a/lib/resin/index.coffee b/lib/resin/index.coffee index 79e76866..11a17a27 100644 --- a/lib/resin/index.coffee +++ b/lib/resin/index.coffee @@ -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') diff --git a/lib/resin/device/device-data.json b/lib/resin/models/device-data.json similarity index 100% rename from lib/resin/device/device-data.json rename to lib/resin/models/device-data.json diff --git a/lib/resin/models/device.coffee b/lib/resin/models/device.coffee index 9ce0232d..084e6e61 100644 --- a/lib/resin/models/device.coffee +++ b/lib/resin/models/device.coffee @@ -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} 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) diff --git a/lib/resin/device/device.spec.coffee b/lib/resin/models/device.spec.coffee similarity index 86% rename from lib/resin/device/device.spec.coffee rename to lib/resin/models/device.spec.coffee index 958d926b..871eb9da 100644 --- a/lib/resin/device/device.spec.coffee +++ b/lib/resin/models/device.spec.coffee @@ -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)