File tree Expand file tree Collapse file tree 4 files changed +51
-2
lines changed
Expand file tree Collapse file tree 4 files changed +51
-2
lines changed Original file line number Diff line number Diff line change 77 timestampResources ,
88 type LwM2MObjectInstance ,
99} from '@hello.nrfcloud.com/proto-map/lwm2m'
10+ import { isNumber } from 'lodash-es'
11+ import { correctOffset } from '../lwm2m/correctOffset.js'
1012import { InvalidTimeError } from '../lwm2m/InvalidTimeError.js'
1113import { isNumeric } from '../lwm2m/isNumeric.js'
1214import { isUnixTimeInSeconds } from '../lwm2m/isUnixTimeInSeconds.js'
@@ -30,7 +32,16 @@ export const instanceToMeasures = ({
3032 }
3133 }
3234
33- if ( ! isUnixTimeInSeconds ( ts ) ) {
35+ if ( ! isNumber ( ts ) ) {
36+ return {
37+ error : new InvalidTimeError (
38+ `Not a timestamp resource defined for ${ ObjectID } : ${ ts . toString ( ) } !` ,
39+ ) ,
40+ }
41+ }
42+ const correctedTs = correctOffset ( ts )
43+
44+ if ( ! isUnixTimeInSeconds ( correctedTs ) ) {
3445 return {
3546 error : new InvalidTimeError (
3647 `Timestamp ${ JSON . stringify ( ts ) } for ${ ObjectID } is not a valid unix time in seconds!` ,
Original file line number Diff line number Diff line change 1+ import assert from 'node:assert/strict'
2+ import { describe , it } from 'node:test'
3+ import { correctOffset } from './correctOffset.js'
4+ import { isUnixTimeInSeconds } from './isUnixTimeInSeconds.js'
5+
6+ void describe ( 'correctOffset()' , ( ) => {
7+ void it ( 'should correct timestamps in the future of up to 60 seconds' , ( ) => {
8+ assert . equal (
9+ isUnixTimeInSeconds ( correctOffset ( Date . now ( ) / 1000 + 1 ) ) ,
10+ true ,
11+ 'should correct 1 second in the future' ,
12+ )
13+ assert . equal (
14+ isUnixTimeInSeconds ( correctOffset ( Date . now ( ) / 1000 + 60 ) ) ,
15+ true ,
16+ 'should correct 60 seconds in the future' ,
17+ )
18+ assert . equal (
19+ isUnixTimeInSeconds ( correctOffset ( Date . now ( ) / 1000 + 61 ) ) ,
20+ false ,
21+ 'should not correct 61 seconds in the future' ,
22+ )
23+ } )
24+ } )
Original file line number Diff line number Diff line change 1+ /**
2+ * Corrects an offset of up to 60 seconds in the future.
3+ * This is needed because some devices clocks drift, and they report timestamps in the future.
4+ */
5+ export const correctOffset = ( ts : number , maxOffsetSeconds = 60 ) : number => {
6+ const nowSeconds = Math . ceil ( Date . now ( ) / 1000 )
7+ const offset = ts * 1000 - nowSeconds
8+ if ( offset < 0 ) {
9+ return ts
10+ }
11+ return ts - Math . min ( offset , maxOffsetSeconds )
12+ }
Original file line number Diff line number Diff line change 1+ import { isNumber } from 'lodash-es'
2+
13export const isUnixTimeInSeconds = ( value : unknown ) : value is number => {
2- if ( typeof value !== 'number' ) return false
4+ if ( ! isNumber ( value ) ) return false
35 if ( value < 1700000000 ) return false
46 if ( value * 1000 > Date . now ( ) ) return false
57 return true
You can’t perform that action at this time.
0 commit comments