2014-11-18 18:39:26 +00:00
|
|
|
_ = require('lodash')
|
2014-11-21 14:12:03 +00:00
|
|
|
async = require('async')
|
2014-11-19 12:51:42 +00:00
|
|
|
device = require('../device/device')
|
2014-11-19 14:42:18 +00:00
|
|
|
table = require('../table/table')
|
2014-11-24 18:08:58 +00:00
|
|
|
errors = require('../errors/errors')
|
2014-11-21 15:40:37 +00:00
|
|
|
log = require('../log/log')
|
2014-11-20 16:24:39 +00:00
|
|
|
server = require('../server/server')
|
2014-11-24 14:45:54 +00:00
|
|
|
widgets = require('../widgets/widgets')
|
2014-11-21 17:23:29 +00:00
|
|
|
patterns = require('../patterns/patterns')
|
2014-11-18 14:11:43 +00:00
|
|
|
applicationModel = require('../models/application')
|
2014-11-18 16:37:01 +00:00
|
|
|
authHooks = require('../hooks/auth')
|
2014-11-20 16:54:43 +00:00
|
|
|
config = require('../config')
|
2014-11-18 12:41:13 +00:00
|
|
|
|
2014-11-24 14:56:03 +00:00
|
|
|
exports.create = authHooks.failIfNotLoggedIn (name, program) ->
|
2014-11-24 14:45:54 +00:00
|
|
|
async.waterfall [
|
|
|
|
|
|
|
|
(callback) ->
|
2014-11-24 14:56:03 +00:00
|
|
|
deviceType = program.parent.type
|
|
|
|
|
|
|
|
if deviceType?
|
|
|
|
return callback(null, deviceType)
|
|
|
|
else
|
|
|
|
deviceTypes = device.getSupportedDevices()
|
|
|
|
widgets.select('Select a type', deviceTypes, callback)
|
2014-11-24 14:45:54 +00:00
|
|
|
|
|
|
|
(type, callback) ->
|
2014-11-24 14:56:03 +00:00
|
|
|
|
|
|
|
# TODO: Currently returns 'unknown'.
|
|
|
|
# Maybe we should break or handle better?
|
2014-11-24 14:45:54 +00:00
|
|
|
slugifiedType = device.getDeviceSlug(type)
|
2014-11-24 14:56:03 +00:00
|
|
|
|
2014-11-24 14:45:54 +00:00
|
|
|
applicationModel.create(name, slugifiedType).then ->
|
|
|
|
return callback()
|
|
|
|
.catch(callback)
|
|
|
|
|
2014-11-24 18:08:58 +00:00
|
|
|
], errors.handle
|
2014-11-24 14:45:54 +00:00
|
|
|
|
2014-11-18 16:37:01 +00:00
|
|
|
exports.list = authHooks.failIfNotLoggedIn ->
|
2014-11-18 14:11:43 +00:00
|
|
|
applicationModel.getAll().then (applications) ->
|
2014-11-18 18:39:26 +00:00
|
|
|
|
2014-11-21 15:40:37 +00:00
|
|
|
log.out table.horizontal applications, (application) ->
|
2014-11-19 14:42:18 +00:00
|
|
|
application.device_type = 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
|
|
|
|
delete application.device
|
|
|
|
return application
|
|
|
|
, [ 'ID', 'Name', 'Device Type', 'Online Devices', 'All Devices' ]
|
2014-11-19 13:23:40 +00:00
|
|
|
|
2014-11-24 18:08:58 +00:00
|
|
|
.catch(errors.handle)
|
2014-11-19 16:15:39 +00:00
|
|
|
|
2014-11-19 13:23:40 +00:00
|
|
|
exports.info = authHooks.failIfNotLoggedIn (id) ->
|
|
|
|
applicationModel.get(id).then (application) ->
|
|
|
|
|
2014-11-21 15:40:37 +00:00
|
|
|
log.out table.vertical application, (application) ->
|
2014-11-19 16:12:08 +00:00
|
|
|
application.device_type = device.getDisplayName(application.device_type)
|
|
|
|
delete application.device
|
|
|
|
return application
|
|
|
|
, [ 'ID', 'Name', 'Device Type', 'Git Repository', 'Commit' ]
|
2014-11-19 13:23:40 +00:00
|
|
|
|
2014-11-24 18:08:58 +00:00
|
|
|
.catch(errors.handle)
|
2014-11-20 16:24:39 +00:00
|
|
|
|
|
|
|
exports.restart = authHooks.failIfNotLoggedIn (id) ->
|
2014-11-20 16:54:43 +00:00
|
|
|
|
|
|
|
# TODO: Move this URL to config
|
2014-11-24 18:08:58 +00:00
|
|
|
server.post("/application/#{id}/restart", errors.handle)
|
2014-11-21 13:43:03 +00:00
|
|
|
|
2014-11-21 14:44:48 +00:00
|
|
|
exports.remove = authHooks.failIfNotLoggedIn (id, program) ->
|
2014-11-21 17:23:29 +00:00
|
|
|
patterns.remove 'application', program.parent.yes, (callback) ->
|
|
|
|
applicationModel.remove(id).then ->
|
|
|
|
return callback()
|
|
|
|
.catch(callback)
|
2014-11-24 18:08:58 +00:00
|
|
|
, errors.handle
|