2014-11-25 17:50:50 +00:00
|
|
|
_ = require('lodash')
|
2014-11-24 18:55:05 +00:00
|
|
|
isOnline = require('is-online')
|
|
|
|
|
2014-11-25 17:50:50 +00:00
|
|
|
CONNECTION_PARAMETERS = [
|
|
|
|
'network'
|
2014-12-01 15:41:32 +00:00
|
|
|
'wifiSsid'
|
2014-11-25 17:50:50 +00:00
|
|
|
'wifiKey'
|
|
|
|
]
|
|
|
|
|
2014-11-24 18:55:05 +00:00
|
|
|
# A wrapper around isOnline in order
|
|
|
|
# to be able to stub it with Sinon
|
|
|
|
exports.isOnline = isOnline
|
2014-11-25 17:50:50 +00:00
|
|
|
|
|
|
|
validateEthernetConnectionParameters = (parameters = {}) ->
|
2014-12-01 15:41:32 +00:00
|
|
|
return if not parameters.wifiSsid? and not parameters.wifiKey?
|
2014-11-25 17:50:50 +00:00
|
|
|
return new Error('You can only use wifi options if network is wifi')
|
|
|
|
|
|
|
|
validateWifiConnectionParameters = (parameters = {}) ->
|
2014-12-01 15:41:32 +00:00
|
|
|
return if parameters.wifiSsid? and parameters.wifiKey?
|
|
|
|
return new Error('You have to provide an ssid and key if network is wifi')
|
2014-11-25 17:50:50 +00:00
|
|
|
|
|
|
|
exports.parseConnectionParameters = (parameters = {}, callback) ->
|
|
|
|
parameters = _.pick(parameters, CONNECTION_PARAMETERS)
|
2014-12-01 15:41:32 +00:00
|
|
|
parameters = _.omit parameters, (value) ->
|
|
|
|
return not value?
|
2014-11-25 17:50:50 +00:00
|
|
|
|
|
|
|
if parameters.network is 'ethernet'
|
|
|
|
error = validateEthernetConnectionParameters(parameters)
|
|
|
|
return callback(error, parameters)
|
|
|
|
|
|
|
|
else if parameters.network is 'wifi'
|
|
|
|
error = validateWifiConnectionParameters(parameters)
|
|
|
|
return callback(error, parameters)
|
|
|
|
|
|
|
|
return callback(new Error('Unknown network type'))
|