-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraptor.js
39 lines (34 loc) · 1.08 KB
/
raptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const Raptor = require('raptor-sdk')
const l = module.exports
let r
l.client = () => r
l.initialize = (url, brokerUrl, credentials) => {
r = new Raptor({
url,
mqttUrl: brokerUrl,
username: credentials.username,
password: credentials.password
})
return r.Auth().login()
.then(() => {
return r.Admin().Token().list()
.then((tokens) => {
tokens = tokens ? tokens.getContent().filter((t) => t.name === credentials.tokenName) : []
if(tokens) {
return Promise.resolve(tokens[0])
}
return r.Admin().Token().create({
name: credentials.tokenName,
expires: 0,
})
})
})
.then((token) => {
credentials.token = token
const cfg = r.getConfig()
cfg.token = token.token
cfg.username = cfg.password = null
r.setConfig(cfg)
return r.Auth().login()
})
}