Skip to content

Commit

Permalink
feat(wirepas): implement cloud to gateway through shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 12, 2024
1 parent 969f92c commit 05dfdc8
Show file tree
Hide file tree
Showing 8 changed files with 796 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules/
npm-debug.log
.swc/
cdk.out/
dist/
dist/
certificates/
1 change: 1 addition & 0 deletions cdk/resources/WebsocketAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class WebsocketAPI extends Construct {
new IAM.PolicyStatement({
actions: [
'iot:GetThingShadow',
'iot:UpdateThingShadow',
'iot:ListThings',
'iot:DescribeThing',
'iot:SearchIndex',
Expand Down
74 changes: 60 additions & 14 deletions lambda/onMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
IoTClient,
SearchIndexCommand,
} from '@aws-sdk/client-iot'
import {
UpdateThingShadowCommand,
type UpdateThingShadowCommandInput,
} from '@aws-sdk/client-iot-data-plane'
import { ApiGatewayManagementApi } from '@aws-sdk/client-apigatewaymanagementapi'
import { sendEvent } from './notifyClients.js'
import type { LwM2MObjectInstance } from '@hello.nrfcloud.com/proto-lwm2m'
Expand All @@ -26,13 +30,40 @@ const { TableName, websocketManagementAPIURL } = fromEnv({
websocketManagementAPIURL: 'WEBSOCKET_MANAGEMENT_API_URL',
})(process.env)

const deviceControl = Type.Object({
deviceId: Type.String({ minLength: 1 }),
code: Type.String({ minLength: 1 }),
})

const message = Type.Object({
message: Type.Literal('sendmessage'),
data: Type.Object({
deviceId: Type.String({ minLength: 1 }),
code: Type.String({ minLength: 1 }),
nrplusCtrl: Type.String({ minLength: 1 }),
}),
data: Type.Union([
Type.Intersect([
deviceControl,
Type.Object({
nrplusCtrl: Type.String({ minLength: 1 }),
}),
]),
Type.Intersect([
deviceControl,
Type.Object({
wirepasCtrl: Type.Object({
nodes: Type.Record(
Type.String({ minLength: 1 }),
Type.Object({
payload: Type.Object({
led: Type.Object({
r: Type.Boolean(),
g: Type.Boolean(),
b: Type.Boolean(),
}),
}),
}),
),
}),
}),
]),
]),
})
const validateMessage = validateWithTypeBox(message)

Expand Down Expand Up @@ -169,7 +200,8 @@ export const handler = async (
statusCode: 400,
}
} else {
const { deviceId, code, nrplusCtrl } = maybeValidMessage.value.data
const msg = maybeValidMessage.value.data
const { deviceId, code } = msg
const attributes = (
await iot.send(new DescribeThingCommand({ thingName: deviceId }))
).attributes
Expand All @@ -183,14 +215,28 @@ export const handler = async (
body: `Code ${code} not valid for device ${deviceId}!`,
}
}
await iotData.send(
new PublishCommand({
topic: `${deviceId}/nrplus-ctrl`,
payload: Buffer.from(nrplusCtrl, 'utf-8'),
qos: 1,
}),
)
console.log(`>`, `${deviceId}/nrplus-ctrl`, nrplusCtrl)
if ('nrplusCtrl' in msg) {
await iotData.send(
new PublishCommand({
topic: `${deviceId}/nrplus-ctrl`,
payload: Buffer.from(msg.nrplusCtrl, 'utf-8'),
qos: 1,
}),
)
console.log(`>`, `${deviceId}/nrplus-ctrl`, msg.nrplusCtrl)
}
if ('wirepasCtrl' in msg) {
const update: UpdateThingShadowCommandInput = {
thingName: deviceId,
payload: JSON.stringify({
state: {
desired: msg.wirepasCtrl,
},
}),
}
await iotData.send(new UpdateThingShadowCommand(update))
console.log(JSON.stringify({ update }))
}
return {
statusCode: 202,
}
Expand Down
Loading

0 comments on commit 05dfdc8

Please sign in to comment.