diff --git a/lwm2m/transformShadowUpdateToLwM2M.spec.ts b/lwm2m/transformShadowUpdateToLwM2M.spec.ts index 4de106e..9121e69 100644 --- a/lwm2m/transformShadowUpdateToLwM2M.spec.ts +++ b/lwm2m/transformShadowUpdateToLwM2M.spec.ts @@ -16,13 +16,30 @@ 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, @@ -30,9 +47,23 @@ void describe('transformShadowUpdateToLwM2M()', () => { '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), + }, + }, ], )) }) diff --git a/lwm2m/transformShadowUpdateToLwM2M.ts b/lwm2m/transformShadowUpdateToLwM2M.ts index 6432184..20c5c39 100644 --- a/lwm2m/transformShadowUpdateToLwM2M.ts +++ b/lwm2m/transformShadowUpdateToLwM2M.ts @@ -3,6 +3,7 @@ import { senMLtoLwM2M, type LwM2MObjectInstance, type Transformer, + definitions, } from '@hello.nrfcloud.com/proto-lwm2m' type Update = { @@ -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( + (o, undefinedResourceID) => ({ + ...o, + Resources: { + ...o.Resources, + [undefinedResourceID]: null, + }, + }), + o, + ) + }) + }) // Handle errors .catch((err) => { console.error(err) diff --git a/package-lock.json b/package-lock.json index 054c9bd..1a832d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "BSD-3-Clause", "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", @@ -3487,9 +3487,9 @@ } }, "node_modules/@hello.nrfcloud.com/proto-lwm2m": { - "version": "2.4.6", - "resolved": "https://registry.npmjs.org/@hello.nrfcloud.com/proto-lwm2m/-/proto-lwm2m-2.4.6.tgz", - "integrity": "sha512-94Ob8jMMwbAqJQC3umXpNSJP5PhUcik10QnCXugjofWvkdL1ICL2FurD5/JavgELG8649/HSpykCbz9Sp4Fxaw==", + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@hello.nrfcloud.com/proto-lwm2m/-/proto-lwm2m-2.5.1.tgz", + "integrity": "sha512-7kZUnidLgXCp0lZZN7xSaYbjueFFWcy7AjdInWbJK958iCBxZCq121n51w6bqmEczR6f+xUmUv6uVmQaGArTxQ==", "dependencies": { "@sinclair/typebox": "0.32.14", "ajv": "8.12.0", diff --git a/package.json b/package.json index 5b3a48f..7be89c4 100644 --- a/package.json +++ b/package.json @@ -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",