-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetbuilder.js
81 lines (75 loc) · 2.34 KB
/
assetbuilder.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const chalk = require('chalk');
const axios = require('axios');
function makeTopic(type, source) {
switch (type) {
case 'presence':
// TODO: Find a better way to differentiate between a device and a venue
if (source.length < 25) {
return '/presence/stream/' + source;
} else {
return '/stream/' + source + '/presence'
}
case 'health':
case 'accelerometer':
case 'sensor':
case 'button':
case 'all':
return '/stream/' + source + '/' + type;
case 'telemetry':
return type + '/' + source
default:
console.error(chalk.red('Unknown stream type'));
process.exit(1);
}
}
function makeURL(env) {
switch (env) {
case 'production':
return 'mqtts://mqtt.kontakt.io:8083';
case 'accept':
return 'mqtts://acceptmqtt.kontakt.io:8083';
case 'test':
return 'mqtts://testmqtt.kontakt.io:8083';
default:
console.error(chalk.red('☢︎ Unknown environment ☢︎'));
process.exit(1);
}
}
function makeApiURL(env) {
switch (env) {
case 'production':
return 'https://api.kontakt.io';
case 'accept':
return 'https://acceptapi.kontakt.io';
case 'test':
return 'https://testapi.kontakt.io';
default:
console.error(chalk.red('☢︎ Unknown environment ☢︎'));
process.exit(1);
}
}
function getCompanyKey(env, apiKey) {
let url = makeApiURL(env);
let headers = {
'Accept': 'application/vnd.com.kontakt+json; version=10',
'Api-Key': apiKey
};
return axios.get(`${url}/manager/me`, { headers }).then(response => {
let companyId = response.data.companyId;
console.log(`${chalk.green('✔︎')} Fetched companyId ${companyId}`);
return companyId
}).catch(() => {
return ""
})
}
function getMissingCompanyId(missingData) {
if (missingData.type === 'telemetry' && (!missingData.source || missingData.source === '')) {
return getCompanyKey(missingData.env, missingData.apikey);
} else
return Promise.resolve(missingData.source);
}
module.exports = {
getMissingCompanyId,
makeTopic: makeTopic,
makeURL: makeURL
}