Skip to content

Commit

Permalink
feat(lwm2m): improve shadow fetching and handling
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Dec 6, 2023
1 parent b13aea0 commit bdff327
Show file tree
Hide file tree
Showing 5 changed files with 3,085 additions and 2,723 deletions.
1 change: 1 addition & 0 deletions cdk/resources/WebsocketAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class WebsocketAPI extends Construct {
'iot:GetThingShadow',
'iot:ListThings',
'iot:DescribeThing',
'iot:SearchIndex',
],
resources: ['*'],
}),
Expand Down
59 changes: 41 additions & 18 deletions lambda/onMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import type {
} from 'aws-lambda'
import { validateWithTypeBox } from './validateWithTypeBox.js'
import {
GetThingShadowCommand,
IoTDataPlaneClient,
PublishCommand,
} from '@aws-sdk/client-iot-data-plane'
import {
DescribeThingCommand,
IoTClient,
ListThingsCommand,
SearchIndexCommand,
} from '@aws-sdk/client-iot'
import { ApiGatewayManagementApi } from '@aws-sdk/client-apigatewaymanagementapi'
import { sendEvent } from './notifyClients.js'
Expand Down Expand Up @@ -46,7 +45,6 @@ const apiGwManagementClient = new ApiGatewayManagementApi({
})

const send = sendEvent(apiGwManagementClient)
const decoder = new TextDecoder()

const deviceInfo = getDeviceInfo(iot)

Expand Down Expand Up @@ -101,9 +99,9 @@ export const handler = async (
if (message.data === 'LWM2M-shadows') {
// Publish LwM2M shadows
const { things } = await iot.send(
new ListThingsCommand({
maxResults: 250,
thingTypeName: '',
new SearchIndexCommand({
// Find all things which have an LwM2M shadow
queryString: 'shadow.name.lwm2m.hasDelta:*',
}),
)
const shadows = (
Expand All @@ -112,24 +110,49 @@ export const handler = async (
alias?: string
objects: LwM2MObjectInstance[]
}>(
(things ?? []).map(async ({ thingName }) =>
iotData
.send(new GetThingShadowCommand({ thingName, shadowName: 'lwm2m' }))
.then(async ({ payload }) => ({
(things ?? []).map(async ({ thingName, shadow }) => {
const alias = (await deviceInfo(thingName as string)).alias
const reported = JSON.parse(shadow ?? '{}').name.lwm2m.reported
if (reported === undefined)
return {
deviceId: thingName as string,
alias: (await deviceInfo(thingName as string)).alias,
objects: shadowToObjects(
JSON.parse(decoder.decode(payload)).state.reported,
),
}))
.catch(() => ({
alias,
objects: [],
}

try {
return {
deviceId: thingName as string,
alias,
objects: shadowToObjects(reported),
}
} catch (err) {
console.error(`Failed to convert shadow for thing ${thingName}`)
console.log(
JSON.stringify({
thingName,
shadow: {
reported,
},
}),
)
console.error(err)
return {
deviceId: thingName as string,
alias,
objects: [],
})),
),
}
}
}),
)
).filter(({ objects }) => objects.length > 0)

console.log(
JSON.stringify({
lwm2mShadows: shadows,
}),
)

await send(
event.requestContext.connectionId,
{ shadows },
Expand Down
6 changes: 6 additions & 0 deletions lwm2m/transformShadowUpdateToLwM2M.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ export const transformShadowUpdateToLwM2M = (
.then((result) => result.filter((item) => item !== null))
// Convert it to LwM2M
.then(senMLtoLwM2M)
// Handle errors
.catch((err) => {
console.error(err)
console.error(`Failed to transform ${JSON.stringify(input)}!`)
return []
})
}
Loading

0 comments on commit bdff327

Please sign in to comment.