-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
178 lines (163 loc) · 6.01 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import fetch from 'node-fetch'
import mqtt from 'async-mqtt'
import awsIot from 'aws-iot-device-sdk'
import Messenger from './lib/messenger.js'
// just for debugging with util.inspect, etc.
//import util from 'util'
// async wrapper for MQTT client
let localMqtt = null
// messenger with cloud provider (not async)
let cloudMsgr = null
/**
* Forces the supervisor to update environment variables.
*/
async function updateEnvironmentVars() {
const updateUrl = `${process.env.BALENA_SUPERVISOR_ADDRESS}/v1/update?apikey=${process.env.BALENA_SUPERVISOR_API_KEY}`
const updateResp = await fetch(updateUrl, {
method: 'POST',
body: '{ "force": true }',
headers: { 'Content-Type': 'application/json' }
})
console.log("Supervisor updated:", updateResp.status)
}
/**
* Provision provided device to cloud.
*
* @return {boolean} true if provisioning successful; otherwise false.
*/
async function provision(uuid) {
let url = process.env.PROVISION_URL
if (!url) {
throw 'PROVISION_URL environment variable not defined'
}
console.log("Provisioning with cloud provider")
const response = await fetch(url, {
method: 'POST',
body: `{ "uuid": "${uuid}", "balena_service": "${process.env.RESIN_SERVICE_NAME}" }`,
headers: {'Cache-Control': 'no-cache', 'Content-Type': 'application/json'}
})
const text = await response.text()
if (response.ok) {
// response.status >= 200 && response.status < 300
console.log(`Provisioned OK: ${response.status} ${text}`)
} else {
console.warn(`Provisioning failure: ${response.status} ${text}`)
// If device already provisioned, Supervisor may not have updated environment
// vars yet and thus tried to provision again. So force Supervisor to update
// and refresh environment variables. If successful, this service will
// not attempt to provision on the next invocation.
let alreadyExists = false
switch (process.env.CLOUD_PROVIDER) {
case 'AWS':
alreadyExists = (text == "thing already exists")
break
case 'AZURE':
alreadyExists = text.startsWith("DeviceAlreadyExistsError")
break
case 'GCP':
let respJson = {}
try {
respJson = JSON.parse(text)
} catch(e) {
// just use empty respJson
}
const alreadyExistsCode = 6
alreadyExists = (respJson.code && respJson.code == alreadyExistsCode)
break
default:
// not possible at this point
break
}
if (alreadyExists) {
console.warn(`Device already exists on ${process.env.CLOUD_PROVIDER}; updating environment vars`)
updateEnvironmentVars()
}
}
return response.ok
}
/**
* Connects and subscribes to local MQTT topic. Retries twice if can't connect.
*
* If success, 'localMqtt' is not null.
*/
async function connectLocal() {
if (!process.env.PRODUCER_TOPIC) {
process.env.PRODUCER_TOPIC = 'sensors'
}
let count = 0
const maxTries = 3
const delay = 5
do {
try {
count++
if (!localMqtt) {
localMqtt = await mqtt.connectAsync('mqtt://127.0.0.1')
console.log("Connected to mqtt://127.0.0.1")
}
await localMqtt.subscribe(process.env.PRODUCER_TOPIC, { qos: 1 })
console.log("Subscribed to topic:", process.env.PRODUCER_TOPIC)
break
} catch(e) {
console.warn("Cannot connect to local MQTT:", e)
if (count < maxTries) {
console.log(`Retry in ${delay} seconds`)
await new Promise(r => setTimeout(r, delay * 1000))
} else {
console.warn(`Retries exhausted`)
localMqtt = null // indicates connection failed
}
}
} while(count < maxTries)
}
/**
* Runs the relay. Wraps all execution in a try/catch block.
*
* Initializes CLOUD_PROVIDER variable to identify the provider based on the
* presence of provider specific variables. Alternatively, you may explicitly
* pre-define a CLOUD_PROVIDER variable.
*/
async function start() {
//console.log("env: " + JSON.stringify(process.env))
if (!process.env.CLOUD_PROVIDER) {
if (process.env.AWS_DATA_ENDPOINT) {
process.env.CLOUD_PROVIDER = 'AWS'
} else if (process.env.AZURE_HUB_HOST) {
process.env.CLOUD_PROVIDER = 'AZURE'
} else if (process.env.PROVISION_URL && process.env.PROVISION_URL.includes('cloudfunctions.net')) {
process.env.CLOUD_PROVIDER = 'GCP'
} else {
console.error("Can't determine cloud provider")
return
}
}
let cloudMsgr = Messenger.create(process.env.CLOUD_PROVIDER)
console.log(`Created cloud messenger: ${cloudMsgr}`)
try {
if (cloudMsgr.isUnregistered()) {
await provision(process.env.RESIN_DEVICE_UUID)
} else if (cloudMsgr.isRegistrationComplete()) {
await connectLocal()
if (localMqtt) {
if (!process.env.CLOUD_CONSUMER_TOPIC) {
process.env.CLOUD_CONSUMER_TOPIC = cloudMsgr.defaultConsumerTopic
}
const finalTopic = cloudMsgr.finalizeConsumerTopic(process.env.CLOUD_CONSUMER_TOPIC)
if (cloudMsgr.isSyncConnect()) {
cloudMsgr.connectSync()
} else {
await cloudMsgr.connect()
}
localMqtt.on('message', function (topic, message) {
cloudMsgr.publish(finalTopic, message)
})
}
} else {
console.log('Partially registered; try again later')
}
} catch(e) {
console.error(e)
// Keep it simple; force exit to restart.
process.exit(1)
}
}
start()