Skip to content

Commit 70fb89e

Browse files
committed
Updates in relation to pr comments
1 parent 4d5ccd2 commit 70fb89e

File tree

1 file changed

+79
-46
lines changed

1 file changed

+79
-46
lines changed

configuration/chirpstack/adr-modules/sensade-adr-mod/sensade-adr.js

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,62 @@ export function id() {
4343
// txPowerIndex: 1,
4444
// nbTrans: 1
4545
// }
46-
export function handle(req) {
47-
var resp = req;
4846

49-
resp.txPowerIndex = 0;
47+
// Handle function for ADR request.
48+
export function handle(req) {
49+
// This defines the default response, which is equal to the current device state.
50+
let resp = {
51+
dr: req.dr,
52+
txPowerIndex: 0,
53+
nbTrans: req.nbTrans,
54+
};
5055

56+
// If ADR is disabled, return with current values.
5157
if (!req.adr) {
52-
return {
53-
dr: resp.dr,
54-
txPowerIndex: resp.txPowerIndex,
55-
nbTrans: resp.nbTrans
56-
};
58+
return resp;
5759
}
5860

59-
var lostPackageCntNew = getLostPackageCount(req.uplinkHistory, 20);
60-
61-
if (lostPackageCntNew >= 5) {
61+
let lostPackageCnt = getLostPackageCount(req);
62+
// If 5 or more packages are lost during the last received packages, decrease the data-rate
63+
if (lostPackageCnt >= 5) {
6264
resp.dr -= 1;
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) {
65+
} else if (
66+
req.uplinkHistory.length >= 20 &&
67+
getLostPackageCount(req, 20) <= 2
68+
) {
69+
// If 2 or fewer packages are lost during the last 20 received packages, the dr might be able to be increased
70+
71+
// We only want to increase the dr if none of the previously received packages
72+
// are within the installationMargin of the minimum required snr for the current dr
73+
// The installationMargin should prevent us from increasing the dr too much, such that packages are lost
74+
// when cars park in a spot.
75+
let minSNR = getMinSNR(req);
76+
let installationMargin = 10;
77+
// A margin of around 10 seems okay
78+
// This value has been chosen based on the loRaSNR values from sensors installed in a parking lot,
79+
// where a fluctuation of +- 10 can be seen (should be caused by cars parking and leaving).
80+
let diffSNR = minSNR - req.requiredSnrForDr - installationMargin;
81+
82+
// Examples:
83+
// minSNR = -5
84+
// requiredSNRforDR = -10
85+
// installationMargin = 10
86+
// diffSNR = -5 - -10 - 10 = -5
87+
// => Not safe to increase the dr
88+
89+
// minSNR = 5
90+
// requiredSNRforDR = -10
91+
// installationMargin = 10
92+
// diffSNR = 5 - -10 - 10 = 5
93+
// => The dr should be safe to increase
94+
95+
if (diffSNR > 0) {
7096
resp.dr += 1;
7197
}
7298
}
7399

100+
// If the data rate is increased or reduced to something outside the Min - Max range,
101+
// set it to the respective limit
74102
if (req.maxDr < resp.dr) {
75103
resp.dr = req.maxDr;
76104
}
@@ -79,44 +107,49 @@ export function handle(req) {
79107
resp.dr = req.minDr;
80108
}
81109

82-
return {
83-
dr: resp.dr,
84-
txPowerIndex: resp.txPowerIndex,
85-
nbTrans: resp.nbTrans
86-
};
110+
return resp;
87111
}
88112

113+
function getMinSNR(req) {
114+
let snrM = 999;
89115

90-
function getLostPackageCount(uplinkHistory, lastXElements) {
91-
var len = uplinkHistory.length;
92-
93-
if (len < lastXElements) {
94-
lastXElements = len;
116+
for (const uh of req.uplinkHistory) {
117+
if (uh.maxSnr < snrM) {
118+
snrM = uh.maxSnr;
119+
}
95120
}
121+
return snrM;
122+
}
96123

97-
var elements = uplinkHistory.slice(-lastXElements);
98-
99-
var prevFcnt = elements[0].fCnt;
100-
var lostPackages = 0;
124+
// Function to get the count of lost packages from the request.
125+
function getLostPackageCount(req, ...lastXElement) {
126+
if (req.uplinkHistory.length < 2) {
127+
return 0;
128+
}
101129

102-
for (var element of elements.slice(1)) {
103-
var curFcnt = element.fCnt;
104-
var diff = curFcnt - prevFcnt;
130+
let elements = req.uplinkHistory;
105131

106-
lostPackages += diff - 1;
107-
prevFcnt = curFcnt;
132+
if (lastXElement.length > 0) {
133+
let x = lastXElement[0];
134+
if (x < elements.length) {
135+
elements = elements.slice(elements.length - x);
136+
}
108137
}
109138

110-
return lostPackages;
111-
}
139+
let lostPackets = 0;
140+
let previousFCnt = 0;
112141

113-
function getMinSnr(uplinkHistory) {
114-
var snrM = 999;
142+
for (let i = 0; i < elements.length; i++) {
143+
const m = elements[i];
115144

116-
for (var element of uplinkHistory) {
117-
if (element.maxSnr < snrM) {
118-
snrM = element.maxSnr;
145+
if (i === 0) {
146+
previousFCnt = m.fCnt;
147+
continue;
119148
}
149+
150+
lostPackets += m.fCnt - previousFCnt - 1; // there is always an expected difference of 1
151+
previousFCnt = m.fCnt;
120152
}
121-
return snrM;
122-
}
153+
154+
return lostPackets;
155+
}

0 commit comments

Comments
 (0)