Skip to content

Commit

Permalink
fix(lwm2m): unset undefined resources
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 26, 2024
1 parent 83c2e63 commit 7bf2d7c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
33 changes: 32 additions & 1 deletion lwm2m/transformShadowUpdateToLwM2M.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,54 @@ void describe('transformShadowUpdateToLwM2M()', () => {
ts: 1699202473044,
},
bat: { v: 4382, ts: 1699202473174 },
fg: {
ts: 1708942457126,
v: {
V: 3710,
SoC: 20,
I: -442,
T: 297,
},
},
},
},
}),
[
{
ObjectID: 14202,
Resources: { '1': 4.382, '99': new Date('2023-11-05T16:41:13.174Z') },
Resources: {
'0': null,
'1': 4.382,
'2': null,
'3': null,
'4': null,
'5': null,
'99': new Date('2023-11-05T16:41:13.174Z'),
},
},
{
ObjectID: 14205,
Resources: {
'0': 27.07,
'1': 29.261,
'2': 97.13,
'10': null,
'99': new Date('2023-11-05T16:41:13.044Z'),
},
},
// Make sure optional resources are unset
{
ObjectID: 14202,
Resources: {
'0': 20,
'1': 3.71,
'2': -442,
'3': 29.7,
'4': null,
'5': null,
'99': new Date(1708942457126),
},
},
],
))
})
20 changes: 20 additions & 0 deletions lwm2m/transformShadowUpdateToLwM2M.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
senMLtoLwM2M,
type LwM2MObjectInstance,
type Transformer,
definitions,
} from '@hello.nrfcloud.com/proto-lwm2m'

type Update = {
Expand Down Expand Up @@ -90,6 +91,25 @@ export const transformShadowUpdateToLwM2M = (
.then((result) => result.filter((item) => item !== null))
// Convert it to LwM2M
.then(senMLtoLwM2M)
// Mark omitted properties as unset
.then((lwm2m) => {
return lwm2m.map((o) => {
const res = definitions[o.ObjectID]?.Resources ?? {}
const resourcesInObject = Object.keys(o.Resources)
return Object.keys(res)
.filter((r) => !resourcesInObject.includes(r))
.reduce<LwM2MObjectInstance>(
(o, undefinedResourceID) => ({
...o,
Resources: {
...o.Resources,
[undefinedResourceID]: null,
},
}),
o,
)
})
})
// Handle errors
.catch((err) => {
console.error(err)
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"prettier": "@bifravst/prettier-config",
"dependencies": {
"@aws-sdk/client-s3": "3.521.0",
"@hello.nrfcloud.com/proto-lwm2m": "2.4.6",
"@hello.nrfcloud.com/proto-lwm2m": "2.5.1",
"@nordicsemiconductor/from-env": "3.0.1",
"@nordicsemiconductor/timestream-helpers": "6.0.2",
"@protobuf-ts/runtime": "2.9.3",
Expand Down

0 comments on commit 7bf2d7c

Please sign in to comment.