Skip to content

Commit d655098

Browse files
committed
wip
1 parent ef4d377 commit d655098

File tree

5 files changed

+93
-91
lines changed

5 files changed

+93
-91
lines changed

lambda/webhookHandler.ts

Lines changed: 5 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { IoTDataPlaneClient } from '@aws-sdk/client-iot-data-plane'
33
import { validateInput } from '@hello.nrfcloud.com/lambda-helpers/validateInput'
44
import middy from '@middy/core'
55
import inputOutputLogger from '@middy/input-output-logger'
6-
import { Type } from '@sinclair/typebox'
76
import type {
87
APIGatewayProxyEventV2,
98
APIGatewayProxyResultV2,
109
} from 'aws-lambda'
11-
import { processNrplusMessagesAndUpdateThingShadow } from '../nordicNRPlus/processNrplusMessagesAndUpdateThingShadow.ts'
10+
import { ensureThingExists } from '../nordicNRPlus/ensureThingExists.ts'
11+
import {
12+
inputSchemaLwm2mMessage,
13+
processNrplusMessagesAndUpdateThingShadow,
14+
} from '../nordicNRPlus/processNrplusMessagesAndUpdateThingShadow.ts'
1215
import { thingExists } from '../nordicNRPlus/thingExists.ts'
13-
import { ensureThingExists } from './ensureThingExists.ts'
1416
import { updateShadow } from './updateShadow.ts'
1517

1618
export const iotData = new IoTDataPlaneClient({})
@@ -19,90 +21,6 @@ const iotClient = new IoTClient({})
1921
const u = updateShadow(iotData)
2022
const ensureThing = ensureThingExists(iotClient, thingExists)
2123

22-
const neighborsInputSchema = Type.Object({
23-
0: Type.Number(),
24-
1: Type.Number(),
25-
99: Type.Number(),
26-
})
27-
const neighborsObjectSchema = Type.Record(Type.String(), neighborsInputSchema)
28-
29-
const networkInputSchema = Type.Object({
30-
0: Type.Number(),
31-
1: Type.Number(),
32-
2: Type.String(),
33-
99: Type.Number(),
34-
})
35-
const networkObjectSchema = Type.Record(Type.String(), networkInputSchema)
36-
37-
const buttonPressInputSchema = Type.Object({
38-
99: Type.Number(),
39-
})
40-
const buttonPressObjectSchema = Type.Record(
41-
Type.String(),
42-
buttonPressInputSchema,
43-
)
44-
45-
const lwm2mSchema = Type.Object({
46-
'14502:1.0': Type.Optional(neighborsObjectSchema),
47-
'14503:1.0': Type.Optional(networkObjectSchema),
48-
'14220:1.0': Type.Optional(buttonPressObjectSchema),
49-
})
50-
51-
const shadowMessageSchema = Type.Object({
52-
previous: Type.Optional(
53-
Type.Object({
54-
state: Type.Object({
55-
desired: Type.Optional(Type.Any()),
56-
reported: Type.Optional(
57-
Type.Object({ lwm2m: Type.Optional(lwm2mSchema) }),
58-
),
59-
}),
60-
}),
61-
),
62-
current: Type.Optional(
63-
Type.Object({
64-
state: Type.Object({
65-
desired: Type.Optional(Type.Any()),
66-
reported: Type.Optional(
67-
Type.Object({ lwm2m: Type.Optional(lwm2mSchema) }),
68-
),
69-
}),
70-
}),
71-
),
72-
})
73-
74-
const coapMessageSchema = Type.Object({
75-
request: Type.Object({
76-
body: Type.String(), // base64
77-
}),
78-
response: Type.Optional(
79-
Type.Object({
80-
body: Type.Optional(Type.String()),
81-
}),
82-
),
83-
})
84-
85-
const messageSchema = Type.Union([
86-
shadowMessageSchema,
87-
coapMessageSchema,
88-
Type.Any(),
89-
])
90-
91-
export const inputSchemaLwm2mMessage = Type.Object({
92-
type: Type.Literal('device.messages'),
93-
messages: Type.Array(
94-
Type.Object({
95-
teamId: Type.String(),
96-
deviceId: Type.String(),
97-
messageId: Type.String(),
98-
topic: Type.Optional(Type.String()),
99-
coapRequestUrl: Type.Optional(Type.String()),
100-
receivedAt: Type.Optional(Type.String()),
101-
message: messageSchema,
102-
}),
103-
),
104-
timestamp: Type.String(),
105-
})
10624
const handle = processNrplusMessagesAndUpdateThingShadow({
10725
ensureThing,
10826
updateShadow: u,
File renamed without changes.

nordicNRPlus/processMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { LwM2MObjectInstance } from '@hello.nrfcloud.com/proto-map/lwm2m'
22
import { shadowToObjects } from '@hello.nrfcloud.com/proto-map/lwm2m/aws'
3-
import type { inputSchemaLwm2mMessage } from '../lambda/webhookHandler.ts'
43
import { locationDataFromCOAPToLwm2m } from './locationDataFromCOAPToLwm2m.ts'
54
import { parseCBORLocationData } from './parseCbor.ts'
5+
import type { inputSchemaLwm2mMessage } from './processNrplusMessagesAndUpdateThingShadow.ts'
66

77
type messageType = (typeof inputSchemaLwm2mMessage.messages)[0]
88

nordicNRPlus/processNrplusMessagesAndUpdateThingShadow.ts

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,91 @@
1-
import type { Static } from '@sinclair/typebox'
2-
import type { inputSchemaLwm2mMessage } from '../lambda/webhookHandler.ts'
1+
import { Type, type Static } from '@sinclair/typebox'
32
import { processMessage } from './processMessage.ts'
43

4+
const neighborsInputSchema = Type.Object({
5+
0: Type.Number(),
6+
1: Type.Number(),
7+
99: Type.Number(),
8+
})
9+
const neighborsObjectSchema = Type.Record(Type.String(), neighborsInputSchema)
10+
11+
const networkInputSchema = Type.Object({
12+
0: Type.Number(),
13+
1: Type.Number(),
14+
2: Type.String(),
15+
99: Type.Number(),
16+
})
17+
const networkObjectSchema = Type.Record(Type.String(), networkInputSchema)
18+
19+
const buttonPressInputSchema = Type.Object({
20+
99: Type.Number(),
21+
})
22+
const buttonPressObjectSchema = Type.Record(
23+
Type.String(),
24+
buttonPressInputSchema,
25+
)
26+
27+
const lwm2mSchema = Type.Object({
28+
'14502:1.0': Type.Optional(neighborsObjectSchema),
29+
'14503:1.0': Type.Optional(networkObjectSchema),
30+
'14220:1.0': Type.Optional(buttonPressObjectSchema),
31+
})
32+
33+
const shadowMessageSchema = Type.Object({
34+
previous: Type.Optional(
35+
Type.Object({
36+
state: Type.Object({
37+
desired: Type.Optional(Type.Any()),
38+
reported: Type.Optional(
39+
Type.Object({ lwm2m: Type.Optional(lwm2mSchema) }),
40+
),
41+
}),
42+
}),
43+
),
44+
current: Type.Optional(
45+
Type.Object({
46+
state: Type.Object({
47+
desired: Type.Optional(Type.Any()),
48+
reported: Type.Optional(
49+
Type.Object({ lwm2m: Type.Optional(lwm2mSchema) }),
50+
),
51+
}),
52+
}),
53+
),
54+
})
55+
56+
const coapMessageSchema = Type.Object({
57+
request: Type.Object({
58+
body: Type.String(), // base64
59+
}),
60+
response: Type.Optional(
61+
Type.Object({
62+
body: Type.Optional(Type.String()),
63+
}),
64+
),
65+
})
66+
67+
const messageSchema = Type.Union([
68+
shadowMessageSchema,
69+
coapMessageSchema,
70+
Type.Any(),
71+
])
72+
73+
export const inputSchemaLwm2mMessage = Type.Object({
74+
type: Type.Literal('device.messages'),
75+
messages: Type.Array(
76+
Type.Object({
77+
teamId: Type.String(),
78+
deviceId: Type.String(),
79+
messageId: Type.String(),
80+
topic: Type.Optional(Type.String()),
81+
coapRequestUrl: Type.Optional(Type.String()),
82+
receivedAt: Type.Optional(Type.String()),
83+
message: messageSchema,
84+
}),
85+
),
86+
timestamp: Type.String(),
87+
})
88+
589
export const processNrplusMessagesAndUpdateThingShadow =
690
({
791
ensureThing,

nordicNRPlus/validateInput.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Value } from '@sinclair/typebox/value'
22
import assert from 'node:assert'
33
import { describe, it } from 'node:test'
4-
import { inputSchemaLwm2mMessage } from '../lambda/webhookHandler.ts'
4+
import { inputSchemaLwm2mMessage } from './processNrplusMessagesAndUpdateThingShadow.ts'
55

66
void describe('inputSchemaLwm2mMessage', () => {
77
const jsonString = `{"type":"device.messages","messages":[{"teamId":"123123","deviceId":"123123","messageId":"b6903898-9998-4767-a7c2-123123123","topic":"$aws/things/123123/shadow/update/documents","message":{"previous":null,"current":{"state":{"desired":{"nrfcloud_mqtt_topic_prefix":"prod/randomTeamId/","pairing":{"state":"paired","topics":{"d2c":"prod/randomNum/m/d/randomNum/d2c","c2d":"prod/123/m/d/123123/+/r"}}}},"metadata":{"desired":{"nrfcloud_mqtt_topic_prefix":{"timestamp":1759240425},"pairing":{"state":{"timestamp":1759240425},"topics":{"d2c":{"timestamp":1759240425},"c2d":{"timestamp":1759240425}}}}},"version":1},"timestamp":1759240425},"receivedAt":"2025-09-30T13:53:45.262Z"}],"timestamp":"2025-10-01T12:23:52.583464498Z"}`

0 commit comments

Comments
 (0)