-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist-things.ts
More file actions
46 lines (42 loc) · 1.08 KB
/
list-things.ts
File metadata and controls
46 lines (42 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { DynamoDBClient, paginateScan } from '@aws-sdk/client-dynamodb'
import {
GetThingShadowCommand,
IoTDataPlaneClient,
} from '@aws-sdk/client-iot-data-plane'
import { unmarshall } from '@aws-sdk/util-dynamodb'
import { shadowToObjects } from '@hello.nrfcloud.com/proto-map/lwm2m/aws'
const db = new DynamoDBClient({})
const iotData = new IoTDataPlaneClient({})
const devices = []
for await (const { Items } of paginateScan(
{
client: db,
},
{
TableName:
'hello-nrfcloud-backend-DevicesTabledevicesTable87DE7A65-1CCQ7YDQXRWUB',
},
)) {
for (const device of Items ?? []) {
const deviceId = unmarshall(device).deviceId
devices.push(deviceId)
}
}
for (const deviceId of devices) {
try {
const { payload } = await iotData.send(
new GetThingShadowCommand({
shadowName: 'lwm2m',
thingName: deviceId,
}),
)
const objects = shadowToObjects(
JSON.parse(Buffer.from(payload!).toString('utf-8')).state.reported,
)
const imei = objects.find(({ ObjectID }) => ObjectID === 14204)
?.Resources[1]
console.log(deviceId, imei ?? '-')
} catch {
// pass
}
}