Skip to content

Commit

Permalink
fix(lwm2m): use simpler shadow format
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Nov 5, 2023
1 parent ee6f01b commit fa347af
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 87 deletions.
4 changes: 2 additions & 2 deletions lambda/updatesToLwM2M.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { fromEnv } from '@nordicsemiconductor/from-env'
import { transformShadowUpdateToLwM2M } from '../lwm2m/transformShadowUpdateToLwM2M.js'
import { models, type LwM2MObject } from '@hello.nrfcloud.com/proto-lwm2m'
import { ulid } from 'ulid'
import { updatesToShadow } from '../lwm2m/objectsToShadow.js'
import { objectsToShadow } from '../lwm2m/objectsToShadow.js'

const { tableName } = fromEnv({
tableName: 'TABLE_NAME',
Expand Down Expand Up @@ -77,7 +77,7 @@ const updateShadow = async (
shadowName: 'lwm2m',
payload: JSON.stringify({
state: {
reported: updatesToShadow(objects),
reported: objectsToShadow(objects),
},
}),
}),
Expand Down
70 changes: 70 additions & 0 deletions lwm2m/objectsToShadow.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { objectsToShadow } from './objectsToShadow.js'

void describe('objectsToShadow()', () => {
void it('should convert a list of LwM2M objects to a shadow document', () =>
assert.deepEqual(
objectsToShadow([
{
ObjectID: 14205,
Resources: {
'0': 27.69,
'1': 18.9,
'2': 97.271,
'99': new Date('2023-11-05T15:13:28.705Z'),
},
},
{
ObjectID: 14202,
Resources: {
'0': 99,
'1': 4.174,
'2': 0,
'3': 25.9,
'99': new Date('2023-11-05T15:13:49.276Z'),
},
},
{
ObjectID: 14203,
Resources: {
'0': 'LTE-M',
'1': 20,
'2': -93,
'3': 2305,
'4': 34237196,
'5': 24202,
'6': '100.81.95.75',
'11': 7,
'99': new Date('2023-11-05T15:13:28.795Z'),
},
},
]),
{
'14205:1.0': {
'0': 27.69,
'1': 18.9,
'2': 97.271,
'99': 1699197208705,
},
'14203:1.0': {
'0': 'LTE-M',
'1': 20,
'2': -93,
'3': 2305,
'4': 34237196,
'5': 24202,
'6': '100.81.95.75',
'11': 7,
'99': 1699197208795,
},
'14202:1.0': {
'0': 99,
'1': 4.174,
'2': 0,
'3': 25.9,
'99': 1699197229276,
},
},
))
})
24 changes: 6 additions & 18 deletions lwm2m/objectsToShadow.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import type { LwM2MObject } from '@hello.nrfcloud.com/proto-lwm2m'

type LwM2MShadow = Record<
export type LwM2MShadow = Record<
string,
Record<
string,
{
v: string | number | boolean
ts: number
}
>
Record<string, string | number | boolean>
>

export const updatesToShadow = (updates: Array<LwM2MObject>): LwM2MShadow =>
updates
export const objectsToShadow = (objects: Array<LwM2MObject>): LwM2MShadow =>
objects
.sort((u1, u2) => {
const d1 = Object.values(u1.Resources).find(
(r) => r instanceof Date,
Expand All @@ -23,22 +17,16 @@ export const updatesToShadow = (updates: Array<LwM2MObject>): LwM2MShadow =>
return d1.getTime() > d2.getTime() ? 1 : -1
})
.reduce<LwM2MShadow>((shadow, update) => {
const ts = (
Object.values(update.Resources).find((r) => r instanceof Date) as Date
).getTime()
const key = `${update.ObjectID}:${update.ObjectVersion ?? '1.0'}`
return {
...shadow,
[key]: {
...(shadow[key] ?? {}),
...Object.entries(update.Resources).reduce((resources, [k, v]) => {
if (v instanceof Date) return resources
if (v instanceof Date) return { ...resources, [k]: v.getTime() }
return {
...resources,
[k]: {
v,
ts,
},
[k]: v,
}
}, {}),
},
Expand Down
73 changes: 73 additions & 0 deletions lwm2m/shadowToObjects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { shadowToObjects } from './shadowToObjects.js'

void describe('shadowToObjects()', () => {
void it('should convert a shadow to LwM2M objects', () =>
assert.deepEqual(
shadowToObjects({
'14205:1.0': {
'0': 27.63,
'1': 19.354,
'2': 97.465,
'99': 1699217636982,
},
'14203:1.0': {
'0': 'LTE-M',
'1': 20,
'2': -90,
'3': 2305,
'4': 34237196,
'5': 24202,
'6': '100.81.95.75',
'11': 7,
'99': 1699217637072,
},
'14202:1.0': {
'0': 99,
'1': 4.174,
'2': 0,
'3': 25.9,
'99': 1699217657553,
},
}),
[
{
ObjectID: 14205,
ObjectVersion: '1.0',
Resources: {
'0': 27.63,
'1': 19.354,
'2': 97.465,
'99': new Date(1699217636982),
},
},
{
ObjectID: 14203,
ObjectVersion: '1.0',
Resources: {
'0': 'LTE-M',
'1': 20,
'2': -90,
'3': 2305,
'4': 34237196,
'5': 24202,
'6': '100.81.95.75',
'11': 7,
'99': new Date(1699217637072),
},
},
{
ObjectID: 14202,
ObjectVersion: '1.0',
Resources: {
'0': 99,
'1': 4.174,
'2': 0,
'3': 25.9,
'99': new Date(1699217657553),
},
},
],
))
})
32 changes: 32 additions & 0 deletions lwm2m/shadowToObjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
timestampResources,
type LwM2MObject,
} from '@hello.nrfcloud.com/proto-lwm2m'
import type { LwM2MShadow } from './objectsToShadow'

export const shadowToObjects = (shadow: LwM2MShadow): LwM2MObject[] =>
Object.entries(shadow)
.map(([ObjectIdAndVersion, Resources]) => {
const [ObjectIDString, ObjectVersion] = ObjectIdAndVersion.split(':') as [
string,
string,
]
const ObjectID = parseInt(ObjectIDString, 10)
const tsResource = timestampResources[ObjectID]
if (tsResource === undefined) return null
return {
ObjectID,
ObjectVersion,
Resources: Object.entries(Resources).reduce(
(Resources, [k, v]) => ({
...Resources,
[k]:
typeof v === 'number' && parseInt(k, 10) === tsResource
? new Date(v)
: v,
}),
{},
),
}
})
.filter((o) => o !== null) as LwM2MObject[]
67 changes: 0 additions & 67 deletions lwm2m/updatesToShadow.spec.ts

This file was deleted.

0 comments on commit fa347af

Please sign in to comment.