Skip to content

Commit

Permalink
improve decodeUplink and decodeDownlink
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkPark committed Dec 11, 2024
1 parent 7538eab commit 7e26008
Showing 1 changed file with 22 additions and 45 deletions.
67 changes: 22 additions & 45 deletions vendor/jooby/mtx1.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const MAX_DATA_SEGMENT_SIZE = 50;
* bytes - byte array containing the downlink payload
*/
function encodeDownlink ( input ) {
let bytes = toBytes(input.data.commands); // FRMPayload (byte array)
let bytes = toBytes(input.data.commands);

// send nothing if not fit in a single data segment
if ( bytes.length > MAX_DATA_SEGMENT_SIZE ) {
Expand All @@ -23,12 +23,7 @@ function encodeDownlink ( input ) {
bytes = setDataSegment(bytes);
}

return {
bytes,
fPort: 1,
warnings: [], // optional
errors: [], // optional (if set, the encoding failed)
};
return {bytes, fPort: 1};
}


Expand All @@ -37,32 +32,33 @@ function encodeDownlink ( input ) {
Input is an object with the following fields:
* bytes - byte array containing the uplink payload, e.g. [255, 230, 255, 0]
* fPort - downlink fPort
* fPort - uplink fPort
Output must be an object with the following fields:
* data - object representing the decoded payload
*/
function decodeDownlink ( input ) {
function decodeUplink ( input ) {
const data = {bytes: input.bytes, message: null};
const segment = getDataSegment(input.bytes);
let message = null;
const warnings = [];
const errors = [];

// just a single data segment
if ( segment ) {
message = fromBytes(segment);

// there may be a message.error (e.g. mismatched LRC)
// in that case message.message will contain everything parsed successfully
// it should be used with caution
const decodeResult = fromBytes(segment);

if ( decodeResult.error ) {
errors.push(decodeResult.error);
// there may be some partially decoded result
data.message = decodeResult.message;
} else {
data.message = decodeResult;
}
} else {
warnings.push('should be present one data segment');
}

return {
data: {
bytes: input.bytes,
message
},
warnings: [], // optional
errors: [] // optional (if set, the decoding failed)
};
return {data, warnings, errors};
}


Expand All @@ -71,32 +67,13 @@ function decodeDownlink ( input ) {
Input is an object with the following fields:
* bytes - byte array containing the uplink payload, e.g. [255, 230, 255, 0]
* fPort - uplink fPort
* fPort - downlink fPort
Output must be an object with the following fields:
* data - object representing the decoded payload
*/
function decodeUplink ( input ) {
const segment = getDataSegment(input.bytes);
let message = null;

// just a single data segment
if ( segment ) {
message = fromBytes(segment);

// there may be a message.error (e.g. mismatched LRC)
// in that case message.message will contain everything parsed successfully
// it should be used with caution
}

return {
data: {
bytes: input.bytes,
message
},
warnings: [], // optional
errors: [] // optional (if set, the decoding failed)
};
function decodeDownlink ( input ) {
return decodeUplink(input);
}


Expand Down

0 comments on commit 7e26008

Please sign in to comment.