-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(lwm2m): use simpler shadow format
- Loading branch information
1 parent
ee6f01b
commit fa347af
Showing
6 changed files
with
183 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
}, | ||
], | ||
)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
This file was deleted.
Oops, something went wrong.