Skip to content

Commit

Permalink
fix(lwm2m): improve message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Dec 11, 2023
1 parent 6fecf44 commit 51cb1b3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 19 deletions.
73 changes: 62 additions & 11 deletions lwm2m/transformShadowUpdateToLwM2M.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,71 @@ export const transformShadowUpdateToLwM2M = (
transformers: Readonly<Array<Transformer>>,
): ((update: Update) => Promise<ReturnType<typeof senMLtoLwM2M>>) => {
// Turn the JSONata in the transformers into executable functions
const transformerFns = transformers.map(({ match, transform }) => ({
match: jsonata(match),
transform: jsonata(transform),
}))
const transformerFns: Array<{
match: ReturnType<typeof jsonata>
matchExpression: string
transform: ReturnType<typeof jsonata>
transformExpression: string
}> = []

for (const {
match: matchExpression,
transform: transformExpression,
} of transformers) {
let match: ReturnType<typeof jsonata>
let transform: ReturnType<typeof jsonata>
try {
match = jsonata(matchExpression)
} catch {
throw new Error(`Failed to parse match expression '${matchExpression}'`)
}
try {
transform = jsonata(transformExpression)
} catch {
throw new Error(
`Failed to parse match expression '${transformExpression}'`,
)
}
transformerFns.push({
match,
matchExpression,
transform,
transformExpression,
})
}

return async (input: Update): Promise<Array<LwM2MObjectInstance>> =>
Promise.all(
transformerFns.map(async ({ match, transform }) => {
// Check if the `matched` JSONata returns `true`.
const matched = await match.evaluate(input)
if (typeof matched !== 'boolean' || matched !== true) return null
// Apply the transform
return transform.evaluate(input)
}),
transformerFns.map(
async ({ match, matchExpression, transform, transformExpression }) => {
// Check if the `matched` JSONata returns `true`.
try {
const matched = await match.evaluate(input)
if (typeof matched !== 'boolean') return null
if (matched === false) return null
} catch (err) {
console.error(err)
console.error(
`Failed to match ${JSON.stringify(
input,
)} using expression '${matchExpression}'!`,
)
return false
}
// Transform
try {
return await transform.evaluate(input)
} catch (err) {
console.error(err)
console.error(
`Failed to transform ${JSON.stringify(
input,
)} using expression '${transformExpression}'!`,
)
return null
}
},
),
)
.then((result) => result.flat())
// Ignore unmatched transformers
Expand Down
14 changes: 7 additions & 7 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 @@ -86,7 +86,7 @@
},
"prettier": "@nordicsemiconductor/asset-tracker-cloud-code-style/.prettierrc",
"dependencies": {
"@hello.nrfcloud.com/proto-lwm2m": "2.2.0",
"@hello.nrfcloud.com/proto-lwm2m": "2.2.1",
"@nordicsemiconductor/from-env": "3.0.0",
"@nordicsemiconductor/timestream-helpers": "6.0.1",
"@sinclair/typebox": "0.31.28",
Expand Down

0 comments on commit 51cb1b3

Please sign in to comment.