Skip to content

Commit e7665ca

Browse files
authored
feat: adding suffix to health check device (#52)
1 parent 985f2ff commit e7665ca

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

cli/commands/create-fake-nrfcloud-health-check-device.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '@aws-sdk/client-iot'
66
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm'
77
import chalk from 'chalk'
8+
import { randomUUID } from 'node:crypto'
89
import { STACK_NAME } from '../../cdk/stacks/stackConfig.js'
910
import {
1011
updateSettings,
@@ -22,6 +23,8 @@ export const createFakeNrfCloudHealthCheckDevice = ({
2223
}): CommandDefinition => ({
2324
command: 'create-fake-nrfcloud-health-check-device',
2425
action: async () => {
26+
const deviceId = `health-check-${randomUUID()}`
27+
2528
const fakeTenantParameter = `/${STACK_NAME}/fakeTenant`
2629
const tenantId = (
2730
await ssm.send(
@@ -62,7 +65,7 @@ export const createFakeNrfCloudHealthCheckDevice = ({
6265
const settings: Settings = {
6366
healthCheckClientCert: credentials.certificatePem,
6467
healthCheckPrivateKey: pk,
65-
healthCheckClientId: 'health-check',
68+
healthCheckClientId: deviceId,
6669
healthCheckModel: 'PCA20035+solar',
6770
healthCheckFingerPrint: '29a.ch3ckr',
6871
}

cli/commands/create-health-check-device.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SSMClient } from '@aws-sdk/client-ssm'
22
import type { Environment } from 'aws-cdk-lib'
33
import chalk from 'chalk'
4+
import { randomUUID } from 'node:crypto'
45
import { readFile } from 'node:fs/promises'
56
import path from 'node:path'
67
import { apiClient } from '../../nrfcloud/apiClient.js'
@@ -44,7 +45,7 @@ export const createHealthCheckDevice = ({
4445
chalk.blue(caCertificates.certificate),
4546
)
4647

47-
const deviceId = `health-check`
48+
const deviceId = `health-check-${randomUUID()}`
4849
console.log(chalk.yellow('Device ID:'), chalk.blue(deviceId))
4950

5051
// Device private key
@@ -114,7 +115,7 @@ export const createHealthCheckDevice = ({
114115
path.join(deviceCertificates.privateKey),
115116
'utf-8',
116117
),
117-
healthCheckClientId: 'health-check',
118+
healthCheckClientId: deviceId,
118119
healthCheckModel: 'PCA20035+solar',
119120
healthCheckFingerPrint: '29a.ch3ckr',
120121
}

lambda/healthCheck.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,26 @@ const publishDeviceMessage =
9191
key: devicePrivateKey,
9292
cert: deviceCert,
9393
ca: amazonRootCA1,
94+
connectTimeout: 5000,
9495
})
9596

96-
mqttClient.on('connect', () => {
97-
const topic = `${nrfCloudSettings.mqttTopicPrefix}m/d/${deviceId}/d2c`
98-
log.debug('mqtt publish', { mqttMessage: message, topic })
99-
mqttClient.publish(topic, JSON.stringify(message), (error) => {
100-
if (error) return reject(error)
101-
mqttClient.end()
102-
return resolve()
97+
mqttClient
98+
.on('connect', () => {
99+
const topic = `${nrfCloudSettings.mqttTopicPrefix}m/d/${deviceId}/d2c`
100+
log.debug('mqtt publish', { mqttMessage: message, topic })
101+
mqttClient.publish(topic, JSON.stringify(message), (error) => {
102+
if (error !== undefined) return reject(error)
103+
mqttClient.end()
104+
return resolve()
105+
})
106+
})
107+
.on('error', (error) => {
108+
log.error(`mqtt error`, { error })
109+
reject(error)
110+
})
111+
.on('reconnect', () => {
112+
log.debug(`mqtt reconnect`)
103113
})
104-
})
105-
106-
mqttClient.on('error', (error) => {
107-
log.error(`mqtt error`, { error })
108-
reject(error)
109-
})
110114

111115
await promise
112116
}

0 commit comments

Comments
 (0)