Skip to content

Commit

Permalink
fix(lwm2m): send with alias, update message format
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Nov 6, 2023
1 parent 3b7d91c commit be47a12
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
14 changes: 5 additions & 9 deletions cdk/resources/WebsocketAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,16 @@ export class WebsocketAPI extends Construct {
}:topic/*/nrplus-ctrl`,
],
}),
new IAM.PolicyStatement({
actions: ['iot:DescribeThing'],
resources: [
`arn:aws:iot:${Stack.of(parent).region}:${
Stack.of(parent).account
}:thing/nrplus-gw-*`,
],
}),
new IAM.PolicyStatement({
actions: ['execute-api:ManageConnections'],
resources: [this.websocketAPIArn],
}),
new IAM.PolicyStatement({
actions: ['iot:GetThingShadow', 'iot:ListThings'],
actions: [
'iot:GetThingShadow',
'iot:ListThings',
'iot:DescribeThing',
],
resources: ['*'],
}),
],
Expand Down
13 changes: 9 additions & 4 deletions lambda/onMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ApiGatewayManagementApi } from '@aws-sdk/client-apigatewaymanagementapi
import { sendEvent } from './notifyClients.js'
import type { LwM2MObject } from '@hello.nrfcloud.com/proto-lwm2m'
import { shadowToObjects } from '../lwm2m/shadowToObjects.js'
import { getDeviceInfo } from './withDeviceAlias.js'

const { TableName, websocketManagementAPIURL } = fromEnv({
TableName: 'CONNECTIONS_TABLE_NAME',
Expand Down Expand Up @@ -47,6 +48,8 @@ const apiGwManagementClient = new ApiGatewayManagementApi({
const send = sendEvent(apiGwManagementClient)
const decoder = new TextDecoder()

const deviceInfo = getDeviceInfo(iot)

export const handler = async (
event: APIGatewayProxyWebsocketEventV2,
): Promise<APIGatewayProxyStructuredResultV2> => {
Expand Down Expand Up @@ -106,24 +109,26 @@ export const handler = async (
const shadows = (
await Promise.all<{
deviceId: string
shadow: LwM2MObject[]
alias?: string
objects: LwM2MObject[]
}>(
(things ?? []).map(async ({ thingName }) =>
iotData
.send(new GetThingShadowCommand({ thingName, shadowName: 'lwm2m' }))
.then(async ({ payload }) => ({
deviceId: thingName as string,
shadow: shadowToObjects(
alias: (await deviceInfo(thingName as string)).alias,
objects: shadowToObjects(
JSON.parse(decoder.decode(payload)).state.reported,
),
}))
.catch(() => ({
deviceId: thingName as string,
shadow: [],
objects: [],
})),
),
)
).filter(({ shadow }) => shadow.length > 0)
).filter(({ objects }) => objects.length > 0)

await send(
event.requestContext.connectionId,
Expand Down
2 changes: 1 addition & 1 deletion lambda/withDeviceAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const withDeviceAlias = <N extends ReturnType<typeof notifyClients>>(

const deviceInfo: Record<string, { alias?: string; location?: string }> = {}

const getDeviceInfo =
export const getDeviceInfo =
(iot: IoTClient) =>
async (deviceId: string): Promise<{ alias?: string; location?: string }> => {
const info =
Expand Down

0 comments on commit be47a12

Please sign in to comment.