Make use of resin-register-device

This commit is contained in:
Juan Cruz Viotti 2015-05-27 11:14:37 -04:00 committed by Pablo Carranza Vélez
parent 6c2467cca8
commit aead4c7531
2 changed files with 3 additions and 22 deletions

View File

@ -23,6 +23,7 @@
"pubnub": "~3.6.4",
"randomstring": "~1.0.3",
"request": "^2.51.0",
"resin-register-device": "^1.0.1",
"server-destroy": "^1.0.0",
"sqlite3": "~3.0.4",
"tty.js": "0.2.14-1",

View File

@ -2,30 +2,10 @@ Promise = require 'bluebird'
_ = require 'lodash'
knex = require './db'
utils = require './utils'
crypto = require 'crypto'
deviceRegister = require 'resin-register-device'
{ resinApi } = require './request'
fs = Promise.promisifyAll(require('fs'))
registerDevice = (apiKey, userId, applicationId, deviceType, uuid) ->
# I'd be nice if the UUID matched the output of a SHA-256 function, but although the length limit of the CN
# attribute in a X.509 certificate is 64 chars, a 32 byte UUID (64 chars in hex) doesn't pass the certificate
# validation in OpenVPN This either means that the RFC counts a final NULL byte as part of the CN or that the
# OpenVPN/OpenSSL implementation has a bug.
if not uuid?
uuid = crypto.pseudoRandomBytes(31).toString('hex')
resinApi.post(
resource: 'device'
body:
user: userId
application: applicationId
uuid: uuid
device_type: deviceType
customOptions:
apikey: apiKey
).then (data) ->
_.pick(data, 'id', 'uuid')
module.exports = ->
# Load config file
fs.readFileAsync('/boot/config.json', 'utf8')
@ -34,7 +14,7 @@ module.exports = ->
userConfig.deviceType ?= 'raspberry-pi'
if userConfig.uuid? and userConfig.registered_at?
return userConfig
registerDevice(userConfig.apiKey, userConfig.userId, userConfig.applicationId, userConfig.deviceType, userConfig.uuid)
deviceRegister.register(resinApi, userConfig)
.then (device) ->
userConfig.uuid = device.uuid
userConfig.registered_at = Date.now()