This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
getDevice.js
65 lines (52 loc) · 2.11 KB
/
getDevice.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
const ttn = require('ttn')
const fs = require('fs')
const { exec } = require('child_process')
const log = (logMessage) => {
process.stdout.write(logMessage + '\n')
}
if (process.env.appID === undefined || process.env.accessKey === undefined || process.env.deviceId === undefined) {
log('Provide \'appID\' & \'accessKey\' with the environment')
process.exit(1)
}
const appID = process.env.appID
const accessKey = process.env.accessKey
const deviceId = process.env.deviceId
const globalPath = __dirname + '/src/global.h'
const string2ByteArray = (string) => {
const byteArray = []
for (var i = 0; i < string.length; i++) {
byteArray.push(`0x${string[i].toUpperCase()}${string[i + 1].toUpperCase()}`)
i++
}
return byteArray.join(', ')
}
async function main() {
const application = await ttn.application(appID, accessKey)
const app = await application.get()
console.log(app)
const euis = await application.getEUIs()
const device = await application.device(deviceId)
console.log(device)
const devAddr = `static const u4_t DEVADDR = 0x${device.devAddr.toUpperCase()};\n`
const appSKey = `static const u1_t ARTKEY[16] = { ${string2ByteArray(device.appSKey)} };\n`
const nwkSKey = `static const u1_t DEVKEY[16] = { ${string2ByteArray(device.nwkSKey)} };\n`
const devEUI = `static const u1_t DEVEUI[8] = { ${string2ByteArray(device.devEui)} };\n`
const appvEUI = `static const u1_t APPEUI[8] = { ${string2ByteArray(device.appEui)} };\n`
const deviceIdString = `static const char DEVICEID[ ] = "${deviceId}";\n`
fs.writeFile(globalPath, `${devEUI}\n${appvEUI}\n${appSKey}\n${nwkSKey}\n${devAddr}\n${deviceIdString}`, { flag: 'w' }, function (err) {
if (err) throw err
console.log("It's saved!")
exec('platformio run -t upload', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
})
});
}
main().catch(function (err) {
console.error(err)
process.exit(1)
})