Skip to content

Commit af0934d

Browse files
committed
feat: do not maintain separate device for map devices
1 parent 04ae08a commit af0934d

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ and published before deploying the solutions.
4646
export OPENSSL_LAMBDA_CONTAINER_TAG=$(./cli.sh build-container openssl-lambda)
4747

4848
# You can add these outputs to your .env file
49-
echo "export OPENSSL_LAMBDA_CONTAINER_TAG=$OPENSSL_LAMBDA_CONTAINER_TAG"
49+
echo "export OPENSSL_LAMBDA_CONTAINER_TAG=$OPENSSL_LAMBDA_CONTAINER_TAG" >> .envrc
5050
direnv allow
5151
```
5252

cdk/resources/PublicDevices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class PublicDevices extends Construct {
3434
type: DynamoDB.AttributeType.STRING,
3535
},
3636
projectionType: DynamoDB.ProjectionType.INCLUDE,
37-
nonKeyAttributes: ['id'],
37+
nonKeyAttributes: ['id', 'secret__deviceId'],
3838
})
3939

4040
this.publicDevicesTable.addGlobalSecondaryIndex({

lambda/devicesData.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { aResponse } from '@hello.nrfcloud.com/lambda-helpers/aResponse'
2929
import { aProblem } from '@hello.nrfcloud.com/lambda-helpers/aProblem'
3030
import { addVersionHeader } from '@hello.nrfcloud.com/lambda-helpers/addVersionHeader'
3131
import { shadowToObjects } from '@hello.nrfcloud.com/proto-map/lwm2m/aws'
32+
import type { LwM2MObjectInstance } from '@hello.nrfcloud.com/proto-map/lwm2m'
3233

3334
const {
3435
publicDevicesTableName,
@@ -57,7 +58,7 @@ const h = async (
5758
): Promise<APIGatewayProxyResultV2> => {
5859
console.log(JSON.stringify({ event }))
5960

60-
const devicesToFetch: { id: string; model: string }[] = []
61+
const devicesToFetch: { id: string; deviceId: string; model: string }[] = []
6162
const minConfirmTime = Date.now() - consentDurationMS
6263

6364
const qs: Record<string, any> = event.queryStringParameters ?? {}
@@ -80,6 +81,7 @@ const h = async (
8081
'#model = :model AND #ownerConfirmed > :minConfirmTime',
8182
ExpressionAttributeNames: {
8283
'#id': 'id',
84+
'#deviceId': 'secret__deviceId',
8385
'#model': 'model',
8486
'#ownerConfirmed': 'ownerConfirmed',
8587
},
@@ -89,7 +91,7 @@ const h = async (
8991
S: new Date(minConfirmTime).toISOString(),
9092
},
9193
},
92-
ProjectionExpression: '#id',
94+
ProjectionExpression: '#id, #deviceId',
9395
}
9496

9597
if (maybeValidQuery.value.ids !== undefined) {
@@ -105,22 +107,34 @@ const h = async (
105107
console.log(JSON.stringify({ queryInput }))
106108

107109
const { Items } = await db.send(new QueryCommand(queryInput))
110+
108111
devicesToFetch.push(
109112
...(Items ?? [])
110-
.map((item) => unmarshall(item) as { id: string })
111-
.map(({ id }) => ({ id, model })),
113+
.map(
114+
(item) =>
115+
unmarshall(item) as { id: string; secret__deviceId: string },
116+
)
117+
.map(({ id, secret__deviceId }) => ({
118+
id,
119+
deviceId: secret__deviceId,
120+
model,
121+
})),
112122
)
113123
}
114124

115125
console.log(JSON.stringify({ devicesToFetch }))
116126

117-
const devices = (
127+
const devices: Array<{
128+
id: string
129+
model: string
130+
state?: Array<LwM2MObjectInstance>
131+
}> = (
118132
await Promise.all(
119-
devicesToFetch.map(async ({ id: id, model }) => {
133+
devicesToFetch.map(async ({ id, deviceId, model }) => {
120134
try {
121135
const shadow = await iotData.send(
122136
new GetThingShadowCommand({
123-
thingName: id,
137+
thingName: deviceId,
124138
shadowName: 'lwm2m',
125139
}),
126140
)
@@ -136,7 +150,7 @@ const h = async (
136150
}
137151
} catch (err) {
138152
if (err instanceof ResourceNotFoundException) {
139-
console.debug(`[${id}]: no shadow found.`)
153+
console.debug(`[${id}]: no shadow found for ${deviceId}.`)
140154
} else {
141155
console.error(err)
142156
}
@@ -163,5 +177,5 @@ const h = async (
163177

164178
export const handler = middy()
165179
.use(addVersionHeader(version))
166-
.use(corsOPTIONS('POST'))
180+
.use(corsOPTIONS('GET'))
167181
.handler(h)

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@aws-sdk/client-ecr": "3.614.0",
3333
"@aws-sdk/client-iot-data-plane": "3.614.0",
3434
"@bifravst/aws-cdk-ecr-helpers": "1.2.25",
35-
"@bifravst/aws-cdk-lambda-helpers": "1.8.34",
35+
"@bifravst/aws-cdk-lambda-helpers": "1.8.35",
3636
"@bifravst/aws-ssm-settings-helpers": "1.1.44",
3737
"@bifravst/ci": "2.0.1",
3838
"@bifravst/eslint-config-typescript": "6.1.9",

0 commit comments

Comments
 (0)