Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cli/commands/create-fake-nrfcloud-health-check-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@aws-sdk/client-iot'
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm'
import chalk from 'chalk'
import { randomUUID } from 'node:crypto'
import { STACK_NAME } from '../../cdk/stacks/stackConfig.js'
import {
updateSettings,
Expand All @@ -22,6 +23,8 @@ export const createFakeNrfCloudHealthCheckDevice = ({
}): CommandDefinition => ({
command: 'create-fake-nrfcloud-health-check-device',
action: async () => {
const deviceId = `health-check-${randomUUID()}`

const fakeTenantParameter = `/${STACK_NAME}/fakeTenant`
const tenantId = (
await ssm.send(
Expand Down Expand Up @@ -62,7 +65,7 @@ export const createFakeNrfCloudHealthCheckDevice = ({
const settings: Settings = {
healthCheckClientCert: credentials.certificatePem,
healthCheckPrivateKey: pk,
healthCheckClientId: 'health-check',
healthCheckClientId: deviceId,
healthCheckModel: 'PCA20035+solar',
healthCheckFingerPrint: '29a.ch3ckr',
}
Expand Down
5 changes: 3 additions & 2 deletions cli/commands/create-health-check-device.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SSMClient } from '@aws-sdk/client-ssm'
import type { Environment } from 'aws-cdk-lib'
import chalk from 'chalk'
import { randomUUID } from 'node:crypto'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { apiClient } from '../../nrfcloud/apiClient.js'
Expand Down Expand Up @@ -44,7 +45,7 @@ export const createHealthCheckDevice = ({
chalk.blue(caCertificates.certificate),
)

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

// Device private key
Expand Down Expand Up @@ -114,7 +115,7 @@ export const createHealthCheckDevice = ({
path.join(deviceCertificates.privateKey),
'utf-8',
),
healthCheckClientId: 'health-check',
healthCheckClientId: deviceId,
healthCheckModel: 'PCA20035+solar',
healthCheckFingerPrint: '29a.ch3ckr',
}
Expand Down
30 changes: 17 additions & 13 deletions lambda/healthCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,26 @@ const publishDeviceMessage =
key: devicePrivateKey,
cert: deviceCert,
ca: amazonRootCA1,
connectTimeout: 5000,
})

mqttClient.on('connect', () => {
const topic = `${nrfCloudSettings.mqttTopicPrefix}m/d/${deviceId}/d2c`
log.debug('mqtt publish', { mqttMessage: message, topic })
mqttClient.publish(topic, JSON.stringify(message), (error) => {
if (error) return reject(error)
mqttClient.end()
return resolve()
mqttClient
.on('connect', () => {
const topic = `${nrfCloudSettings.mqttTopicPrefix}m/d/${deviceId}/d2c`
log.debug('mqtt publish', { mqttMessage: message, topic })
mqttClient.publish(topic, JSON.stringify(message), (error) => {
if (error !== undefined) return reject(error)
mqttClient.end()
return resolve()
})
})
.on('error', (error) => {
log.error(`mqtt error`, { error })
reject(error)
})
.on('reconnect', () => {
log.debug(`mqtt reconnect`)
})
})

mqttClient.on('error', (error) => {
log.error(`mqtt error`, { error })
reject(error)
})

await promise
}
Expand Down