@@ -12,7 +12,7 @@ export function id() {
1212//
1313// Input object example:
1414// {
15- // regionName : "eu868",
15+ // regionConfigId : "eu868",
1616// regionCommonName: "EU868",
1717// devEui: "0102030405060708",
1818// macVersion: "1.0.3",
@@ -43,114 +43,80 @@ export function id() {
4343// txPowerIndex: 1,
4444// nbTrans: 1
4545// }
46- // Handle function for ADR request.
4746export function handle ( req ) {
48- // This defines the default response, which is equal to the current device state.
49- let resp = {
50- dr : req . DR ,
51- txPowerIndex : req . TxPowerIndex ,
52- nbTrans : req . NbTrans ,
53- } ;
47+ var resp = req ;
5448
5549 resp . txPowerIndex = 0 ;
5650
57- // If ADR is disabled, return with current values.
58- if ( ! req . ADR ) {
59- return [ resp , null ] ;
51+ if ( ! req . adr ) {
52+ return {
53+ dr : resp . dr ,
54+ txPowerIndex : resp . txPowerIndex ,
55+ nbTrans : resp . nbTrans
56+ } ;
6057 }
6158
62- let lostPackageCnt = getLostPackageCount ( req ) ;
63- // If 5 or more packages are lost during the last received packages, decrease the data-rate
64- if ( lostPackageCnt >= 5 ) {
59+ var lostPackageCntNew = getLostPackageCount ( req . uplinkHistory , 20 ) ;
60+
61+ if ( lostPackageCntNew >= 5 ) {
6562 resp . dr -= 1 ;
66- } else if (
67- req . UplinkHistory . length === 20 &&
68- getLostPackageCount ( req , 20 ) <= 2
69- ) {
70- // If 2 or fewer packages are lost during the last 20 received packages, the dr might be able to be increased
71-
72- // We only want to increase the dr if none of the previously received packages
73- // are within the installationMargin of the minimum required snr for the current dr
74- // The installationMargin should prevent us from increasing the dr too much, such that packages are lost
75- // when cars park in a spot.
76- let minSNR = getMinSNR ( req ) ;
77- let installationMargin = 10 ;
78- // A margin of around 10 seems okay
79- // This value has been chosen based on the loRaSNR values from sensors installed in a parking lot,
80- // where a fluctuation of +- 10 can be seen (should be caused by cars parking and leaving).
81- let diffSNR = minSNR - req . requiredHistoryCount - installationMargin ;
82-
83- // Examples:
84- // minSNR = -5
85- // requiredSNRforDR = -10
86- // installationMargin = 10
87- // diffSNR = -5 - -10 - 10 = -5
88- // => Not safe to increase the dr
89-
90- // minSNR = 5
91- // requiredSNRforDR = -10
92- // installationMargin = 10
93- // diffSNR = 5 - -10 - 10 = 5
94- // => The dr should be safe to increase
95-
96- if ( diffSNR > 0 ) {
63+ } else if ( req . uplinkHistory . length >= 20 && getLostPackageCount ( req . uplinkHistory , 20 ) <= 2 ) {
64+ var minSnr = getMinSnr ( req . uplinkHistory ) ;
65+ var installationMargin = 10 ;
66+
67+ var diffSnr = minSnr - req . requiredSnrForDr - installationMargin ;
68+
69+ if ( diffSnr > 0 ) {
9770 resp . dr += 1 ;
9871 }
9972 }
10073
101- // If the data rate is increased or reduced to something outside the Min - Max range,
102- // set it to the respective limit
10374 if ( req . maxDr < resp . dr ) {
10475 resp . dr = req . maxDr ;
10576 }
10677
107- if ( req . MinDR > resp . dr ) {
108- resp . dr = req . MinDR ;
78+ if ( req . minDr > resp . dr ) {
79+ resp . dr = req . minDr ;
10980 }
11081
111- return [ resp , null ] ;
82+ return {
83+ dr : resp . dr ,
84+ txPowerIndex : resp . txPowerIndex ,
85+ nbTrans : resp . nbTrans
86+ } ;
11287}
11388
114- function getMinSNR ( req ) {
115- let snrM = 999 ;
11689
117- for ( const uh of req . uplinkHistory ) {
118- if ( uh . maxSnr < snrM ) {
119- snrM = uh . maxSnr ;
120- }
121- }
122- return snrM ;
123- }
90+ function getLostPackageCount ( uplinkHistory , lastXElements ) {
91+ var len = uplinkHistory . length ;
12492
125- // Function to get the count of lost packages from the request.
126- function getLostPackageCount ( req , ...lastXElement ) {
127- if ( req . uplinkHistory . length < 2 ) {
128- return 0 ;
93+ if ( len < lastXElements ) {
94+ lastXElements = len ;
12995 }
13096
131- let elements = req . uplinkHistory ;
97+ var elements = uplinkHistory . slice ( - lastXElements ) ;
13298
133- if ( lastXElement . length > 0 ) {
134- let x = lastXElement [ 0 ] ;
135- if ( x < elements . length ) {
136- elements = elements . slice ( elements . length - x ) ;
137- }
99+ var prevFcnt = elements [ 0 ] . fCnt ;
100+ var lostPackages = 0 ;
101+
102+ for ( var element of elements . slice ( 1 ) ) {
103+ var curFcnt = element . fCnt ;
104+ var diff = curFcnt - prevFcnt ;
105+
106+ lostPackages += diff - 1 ;
107+ prevFcnt = curFcnt ;
138108 }
139109
140- let lostPackets = 0 ;
141- let previousFCnt = 0 ;
110+ return lostPackages ;
111+ }
142112
143- for ( let i = 0 ; i < elements . length ; i ++ ) {
144- const m = elements [ i ] ;
113+ function getMinSnr ( uplinkHistory ) {
114+ var snrM = 999 ;
145115
146- if ( i === 0 ) {
147- previousFCnt = m . FCnt ;
148- continue ;
116+ for ( var element of uplinkHistory ) {
117+ if ( element . maxSnr < snrM ) {
118+ snrM = element . maxSnr ;
149119 }
150-
151- lostPackets += m . FCnt - previousFCnt - 1 ; // there is always an expected difference of 1
152- previousFCnt = m . FCnt ;
153120 }
154-
155- return lostPackets ;
156- }
121+ return snrM ;
122+ }
0 commit comments