Skip to content

Commit c012c1b

Browse files
committed
wip
1 parent d2a103f commit c012c1b

File tree

5 files changed

+80
-50
lines changed

5 files changed

+80
-50
lines changed

cdk/stacks/NRPlusDemoStack.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export class NRPlusDemoStack extends Stack {
4848
'iot:CreateThing',
4949
],
5050
resources: [
51+
`arn:aws:iot:${Stack.of(this).region}:${Stack.of(this).account}:thing/*`,
5152
`arn:aws:iot:${Stack.of(this).region}:${Stack.of(this).account}:thing/*/shadow/lwm2m`,
5253
],
5354
}),

lambda/ensureThingExists.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { CreateThingCommand, type IoTClient } from '@aws-sdk/client-iot'
2-
import { thingExists } from '../nordicNRPlus/thingExists.ts'
32

3+
type EnsureThingFn = (
4+
iotClient: IoTClient,
5+
) => (thingName: string) => Promise<boolean>
46

5-
export const ensureThingExists =
6-
(iotClient: IoTClient, thingExists: EnsureThingFn) => {
7-
7+
export const ensureThingExists = (
8+
iotClient: IoTClient,
9+
thingExists: EnsureThingFn,
10+
) => {
811
const e = thingExists(iotClient)
9-
12+
1013
return async (thingName: string): Promise<void> => {
11-
if (await e(thingName)) return
14+
if (await e(thingName)) return
1215
await iotClient.send(
1316
new CreateThingCommand({
1417
thingName,
1518
thingTypeName: 'nordic-nrplus',
1619
}),
1720
)
1821
}
19-
20-
}
22+
}

lambda/webhookHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import type {
99
APIGatewayProxyResultV2,
1010
} from 'aws-lambda'
1111
import { processNrplusMessagesAndUpdateThingShadow } from '../nordicNRPlus/processNrplusMessagesAndUpdateThingShadow.ts'
12+
import { thingExists } from '../nordicNRPlus/thingExists.ts'
1213
import { ensureThingExists } from './ensureThingExists.ts'
1314
import { updateShadow } from './updateShadow.ts'
1415

1516
export const iotData = new IoTDataPlaneClient({})
1617
const iotClient = new IoTClient({})
1718

1819
const u = updateShadow(iotData)
19-
const ensureThing = ensureThingExists(iotClient)
20+
const ensureThing = ensureThingExists(iotClient, thingExists)
2021

2122
const neighborsInputSchema = Type.Object({
2223
0: Type.Number(),

nordicNRPlus/Thingexists.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ void describe('thingExists', () => {
1111
}),
1212
)
1313
const iotClient: IoTClient = { send: icSend } as unknown as IoTClient
14-
const result = await thingExists(iotClient, 'myDevice123')
14+
const e = thingExists(iotClient)
15+
const result = await e('myDevice123')
1516
assert.equal(result, true)
1617
assert.equal(icSend.mock.calls.length, 1)
1718

@@ -30,7 +31,8 @@ void describe('thingExists', () => {
3031
throw error
3132
})
3233
const iotClient: IoTClient = { send: icSend } as unknown as IoTClient
33-
const result = await thingExists(iotClient, 'mySecondDevice123')
34+
const e = thingExists(iotClient)
35+
const result = await e('mySecondDevice123')
3436

3537
assert.equal(result, false)
3638
assert.equal(icSend.mock.calls.length, 1)

nordicNRPlus/processNrplusMessagesAndUpdateThingShadow.spec.ts

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ void describe('processNrplusMessagesAndUpdateThingShadow', () => {
5151
type: 'device.messages',
5252
timestamp: '',
5353
})
54-
assert.equal(ensureThing.mock.calls.length, 1) // ensureThing is called
55-
assert.equal(updateShadow.mock.calls.length, 0) // updateShadow is not called because message is undefined
54+
assert.equal(ensureThing.mock.calls.length, 1, 'ensureThing is called')
55+
assert.equal(
56+
updateShadow.mock.calls.length,
57+
0,
58+
'updateShadow is not called because message is undefined',
59+
)
5660
})
5761
void it('should process a location message and update shadow when thing exists and format is correct', async () => {
5862
const thingName = 'team1'
@@ -83,27 +87,31 @@ void describe('processNrplusMessagesAndUpdateThingShadow', () => {
8387
type: 'device.messages',
8488
timestamp: '',
8589
})
86-
assert.equal(ensureThing.mock.calls.length, 1) // ensureThing is called
87-
assert.equal(updateShadow.mock.calls.length, 1) // updateShadow is called
90+
assert.equal(ensureThing.mock.calls.length, 1, 'ensureThing is called')
91+
assert.equal(updateShadow.mock.calls.length, 1, 'updateShadow is called')
8892

8993
const firstCallUpdate = updateShadow.mock.calls[0]
9094
const secondArgumentUpdate = Array.isArray(firstCallUpdate)
9195
? firstCallUpdate[1]
9296
: (firstCallUpdate as any).arguments?.[1]
93-
assert.deepEqual(secondArgumentUpdate, [
94-
{
95-
ObjectID: 14201,
96-
ObjectVersion: '1.0',
97-
ObjectInstanceID: 1,
98-
Resources: {
99-
'0': 63.4317043,
100-
'1': 10.4711207,
101-
'3': 17.245,
102-
'6': 'WIFI',
103-
'99': Date.parse(receivedAt) / 1000,
97+
assert.deepEqual(
98+
secondArgumentUpdate,
99+
[
100+
{
101+
ObjectID: 14201,
102+
ObjectVersion: '1.0',
103+
ObjectInstanceID: 1,
104+
Resources: {
105+
'0': 63.4317043,
106+
'1': 10.4711207,
107+
'3': 17.245,
108+
'6': 'WIFI',
109+
'99': Date.parse(receivedAt) / 1000,
110+
},
104111
},
105-
},
106-
]) // updateShadow is called with correct message
112+
],
113+
'updateShadow is called with correct message',
114+
)
107115
})
108116
void it('should not update shadow if message is unhandled', async () => {
109117
const thingName = 'team1'
@@ -131,41 +139,53 @@ void describe('processNrplusMessagesAndUpdateThingShadow', () => {
131139
type: 'device.messages',
132140
timestamp: '',
133141
})
134-
assert.equal(ensureThing.mock.calls.length, 1) // ensureThing is called
135-
assert.equal(updateShadow.mock.calls.length, 1) // updateShadow is called
142+
assert.equal(ensureThing.mock.calls.length, 1, 'ensureThing is called')
143+
assert.equal(updateShadow.mock.calls.length, 1, 'updateShadow is called')
136144

137145
const firstCall = ensureThing.mock.calls[0]
138146
const firstArgument = Array.isArray(firstCall)
139147
? firstCall[0]
140148
: (firstCall as any).arguments?.[0]
141-
assert.equal(firstArgument, thingName + '-' + deviceId) // ensureThing is called with correct thingName
149+
assert.equal(
150+
firstArgument,
151+
thingName + '-' + deviceId,
152+
'ensureThing is called with correct thingName',
153+
)
142154

143155
const firstCallUpdate = updateShadow.mock.calls[0]
144156
const firstArgumentUpdate = Array.isArray(firstCallUpdate)
145157
? firstCallUpdate[0]
146158
: (firstCallUpdate as any).arguments?.[0]
147-
assert.equal(firstArgumentUpdate, thingName + '-' + deviceId) // updateShadow is called with correct thingName
159+
assert.equal(
160+
firstArgumentUpdate,
161+
thingName + '-' + deviceId,
162+
'updateShadow is called with correct thingName',
163+
)
148164

149165
const secondArgumentUpdate = Array.isArray(firstCallUpdate)
150166
? firstCallUpdate[1]
151167
: (firstCallUpdate as any).arguments?.[1]
152-
assert.deepEqual(secondArgumentUpdate, [
153-
{
154-
ObjectID: 14503,
155-
ObjectVersion: '1.0',
156-
Resources: {
157-
'0': 1592461780,
158-
'1': 703710,
159-
'2': 'FT',
160-
'99': 1760514064,
168+
assert.deepEqual(
169+
secondArgumentUpdate,
170+
[
171+
{
172+
ObjectID: 14503,
173+
ObjectVersion: '1.0',
174+
Resources: {
175+
'0': 1592461780,
176+
'1': 703710,
177+
'2': 'FT',
178+
'99': 1760514064,
179+
},
180+
},
181+
{
182+
ObjectID: 14502,
183+
ObjectVersion: '1.0',
184+
Resources: { '0': 696113427, '1': 0, '99': 1760524760 },
161185
},
162-
},
163-
{
164-
ObjectID: 14502,
165-
ObjectVersion: '1.0',
166-
Resources: { '0': 696113427, '1': 0, '99': 1760524760 },
167-
},
168-
]) // updateShadow is called with correct message
186+
],
187+
'updateShadow is called with correct message',
188+
)
169189
})
170190
void it('should not update shadow if message is unhandled', async () => {
171191
const thingName = 'team1'
@@ -188,7 +208,11 @@ void describe('processNrplusMessagesAndUpdateThingShadow', () => {
188208
type: 'device.messages',
189209
timestamp: '',
190210
})
191-
assert.equal(ensureThing.mock.calls.length, 1) // ensureThing is called
192-
assert.equal(updateShadow.mock.calls.length, 0) // updateShadow is not called
211+
assert.equal(ensureThing.mock.calls.length, 1, 'ensureThing is called')
212+
assert.equal(
213+
updateShadow.mock.calls.length,
214+
0,
215+
'updateShadow is not called',
216+
)
193217
})
194218
})

0 commit comments

Comments
 (0)