Skip to content

Commit

Permalink
fix(lwm2m): table with objects is not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Nov 9, 2023
1 parent 5029f8d commit eb53670
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 97 deletions.
1 change: 0 additions & 1 deletion cdk/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const packagesInLayer: string[] = [
'@nordicsemiconductor/timestream-helpers',
'@hello.nrfcloud.com/proto-lwm2m',
'jsonata',
'ulid',
]
const pack = async (
id: string,
Expand Down
16 changes: 0 additions & 16 deletions cdk/resources/LwM2M.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Construct } from 'constructs'
import {
aws_dynamodb as DynamoDB,
RemovalPolicy,
Duration,
aws_iam as IAM,
aws_iot as IoT,
Expand All @@ -15,7 +13,6 @@ import type { PackedLambda } from '../backend'
* Contains resources that provide LwM2M based data for devices
*/
export class LwM2M extends Construct {
public readonly table: DynamoDB.ITable
constructor(
parent: Construct,
{
Expand All @@ -30,16 +27,6 @@ export class LwM2M extends Construct {
) {
super(parent, 'LwM2M')

this.table = new DynamoDB.Table(this, 'table', {
billingMode: DynamoDB.BillingMode.PAY_PER_REQUEST,
partitionKey: {
name: 'id',
type: DynamoDB.AttributeType.STRING,
},
timeToLiveAttribute: 'ttl',
removalPolicy: RemovalPolicy.DESTROY,
})

const fn = new Lambda.Function(this, 'updatesToLwM2M', {
handler: lambdaSources.updatesToLwM2M.handler,
architecture: Lambda.Architecture.ARM_64,
Expand All @@ -52,7 +39,6 @@ export class LwM2M extends Construct {
layers: [baseLayer],
environment: {
VERSION: this.node.tryGetContext('version'),
TABLE_NAME: this.table.tableName,
},
initialPolicy: [
new IAM.PolicyStatement({
Expand All @@ -63,8 +49,6 @@ export class LwM2M extends Construct {
logRetention: Logs.RetentionDays.ONE_WEEK,
})

this.table.grantWriteData(fn)

const ruleRole = new IAM.Role(this, 'ruleRole', {
assumedBy: new IAM.ServicePrincipal(
'iot.amazonaws.com',
Expand Down
7 changes: 1 addition & 6 deletions cdk/stacks/BackendStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class BackendStack extends Stack {
lambdaSources,
})

const lwm2m = new LwM2M(this, {
new LwM2M(this, {
lambdaSources,
baseLayer,
})
Expand Down Expand Up @@ -128,11 +128,6 @@ export class BackendStack extends Stack {
value: userAuthentication.identityPool.ref,
exportName: `${this.stackName}:identityPoolId`,
})

new CfnOutput(this, 'lwm2mTableName', {
value: lwm2m.table.tableName,
exportName: `${this.stackName}:lwm2mTableName`,
})
}
}

Expand Down
57 changes: 0 additions & 57 deletions lambda/updatesToLwM2M.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,19 @@
import {
AttributeValue,
BatchWriteItemCommand,
DynamoDBClient,
} from '@aws-sdk/client-dynamodb'
import {
IoTDataPlaneClient,
UpdateThingShadowCommand,
} from '@aws-sdk/client-iot-data-plane'
import { fromEnv } from '@nordicsemiconductor/from-env'
import { transformShadowUpdateToLwM2M } from '../lwm2m/transformShadowUpdateToLwM2M.js'
import {
models,
type LwM2MObjectInstance,
} from '@hello.nrfcloud.com/proto-lwm2m'
import { ulid } from 'ulid'
import { objectsToShadow } from '../lwm2m/objectsToShadow.js'

const { tableName } = fromEnv({
tableName: 'TABLE_NAME',
})(process.env)

const db = new DynamoDBClient({})
const iotData = new IoTDataPlaneClient({})
const transformUpdate = transformShadowUpdateToLwM2M(
models['asset_tracker_v2+AWS'].transforms,
)

const persist = async (
deviceId: string,
objects: LwM2MObjectInstance[],
): Promise<void> => {
await db.send(
new BatchWriteItemCommand({
RequestItems: {
[tableName]: objects.map((object) => ({
PutRequest: {
Item: {
id: { S: ulid() },
deviceId: { S: deviceId },
ObjectId: { N: object.ObjectID.toString() },
ObjectVersion:
object.ObjectVersion !== undefined
? { S: object.ObjectVersion }
: { S: '1.0' },
Resources: {
M: Object.entries(object.Resources).reduce(
(map, [k, v]) => ({
...map,
[k]: convertValue(v),
}),
{},
),
},
ttl: {
N: Math.round(Date.now() / 1000 + 30 * 24 * 60 * 60).toString(),
},
},
},
})),
},
}),
)
}

const convertValue = (v: string | number | boolean | Date): AttributeValue => {
if (typeof v === 'number') return { N: v.toString() }
if (typeof v === 'boolean') return { BOOL: v }
if (typeof v === 'object' && v instanceof Date) return { S: v.toISOString() }
return { S: v }
}

const updateShadow = async (
deviceId: string,
objects: LwM2MObjectInstance[],
Expand Down Expand Up @@ -111,6 +55,5 @@ export const handler = async (event: {
}),
)

void persist(deviceId, objects)
void updateShadow(deviceId, objects)
}
16 changes: 1 addition & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"@nordicsemiconductor/timestream-helpers": "6.0.1",
"@sinclair/typebox": "0.31.23",
"ajv": "8.12.0",
"jsonata": "2.0.3",
"ulid": "2.3.0"
"jsonata": "2.0.3"
}
}

0 comments on commit eb53670

Please sign in to comment.