-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from tago-io/fix/connector-kuando-busylight
Adding fix and unit tests
- Loading branch information
Showing
5 changed files
with
134 additions
and
54 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
52 changes: 0 additions & 52 deletions
52
decoders/connector/kuando/busylight-iot-omega/v1.0.0/payload.js
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
decoders/connector/kuando/busylight-iot-omega/v1.0.0/payload.test.ts
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,82 @@ | ||
/* eslint-disable unicorn/numeric-separators-style */ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { decoderRun } from "../../../../../src/functions/decoder-run"; | ||
|
||
const file_path = "decoders/connector/kuando/busylight-iot-omega/v1.0.0/payload.ts"; | ||
|
||
function preparePayload(payloadHex: string) { | ||
let payload = [{ variable: "payload", value: payloadHex }]; | ||
payload = decoderRun(file_path, { payload }); | ||
|
||
const RSSI = payload.find((item) => item.variable === "RSSI"); | ||
const SNR = payload.find((item) => item.variable === "SNR"); | ||
const messages_received = payload.find((item) => item.variable === "messages_received"); | ||
const messages_send = payload.find((item) => item.variable === "messages_send"); | ||
const lastcolor_red = payload.find((item) => item.variable === "lastcolor_red"); | ||
const lastcolor_blue = payload.find((item) => item.variable === "lastcolor_blue"); | ||
const lastcolor_green = payload.find((item) => item.variable === "lastcolor_green"); | ||
const lastcolor_ontime = payload.find((item) => item.variable === "lastcolor_ontime"); | ||
const lastcolor_offtime = payload.find((item) => item.variable === "lastcolor_offtime"); | ||
const sw_rev = payload.find((item) => item.variable === "sw_rev"); | ||
const hw_rev = payload.find((item) => item.variable === "hw_rev"); | ||
const adr_state = payload.find((item) => item.variable === "adr_state"); | ||
const parse_error = payload.find((item) => item.variable === "parse_error"); | ||
|
||
return { | ||
payload, | ||
RSSI, | ||
SNR, | ||
messages_received, | ||
messages_send, | ||
lastcolor_red, | ||
lastcolor_blue, | ||
lastcolor_green, | ||
lastcolor_ontime, | ||
lastcolor_offtime, | ||
sw_rev, | ||
hw_rev, | ||
adr_state, | ||
parse_error, | ||
}; | ||
} | ||
|
||
describe("Unit Test", () => { | ||
const payloadHex = "9cffffff140000005c000000c3040000000099ff00380c01"; | ||
const result = preparePayload(payloadHex); | ||
|
||
test("Variable values", () => { | ||
expect(result.RSSI?.value).toBe(-100); | ||
expect(result.SNR?.value).toBe(20); | ||
expect(result.messages_received?.value).toBe(92); | ||
expect(result.messages_send?.value).toBe(1219); | ||
expect(result.lastcolor_red?.value).toBe(0); | ||
expect(result.lastcolor_blue?.value).toBe(0); | ||
expect(result.lastcolor_green?.value).toBe(153); | ||
expect(result.lastcolor_ontime?.value).toBe(255); | ||
expect(result.lastcolor_offtime?.value).toBe(0); | ||
expect(result.sw_rev?.value).toBe(56); | ||
expect(result.hw_rev?.value).toBe(12); | ||
expect(result.adr_state?.value).toBe(1); | ||
}); | ||
}); | ||
|
||
describe("Shall not be parsed", () => { | ||
let payload = [ | ||
{ variable: "shallnotpass", value: "04096113950292" }, | ||
{ variable: "fport", value: 9 }, | ||
]; | ||
|
||
payload = decoderRun(file_path, { payload }); | ||
|
||
test("Output Result", () => { | ||
expect(Array.isArray(payload)).toBe(true); | ||
}); | ||
|
||
test("Not parsed Result", () => { | ||
expect(payload).toEqual([ | ||
{ variable: "shallnotpass", value: "04096113950292" }, | ||
{ variable: "fport", value: 9 }, | ||
]); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
decoders/connector/kuando/busylight-iot-omega/v1.0.0/payload.ts
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,50 @@ | ||
// @ts-nocheck | ||
// Payload parser for Busylight Uplink, Release 0.9 | ||
|
||
// Search the payload variable in the payload global variable. It's contents is always [ { variable, value...}, {variable, value...} ...] | ||
const payload_raw = payload.find((x) => x.variable === "payload_raw" || x.variable === "payload" || x.variable === "data"); | ||
if (payload_raw) { | ||
try { | ||
// Convert the data from Hex to Javascript Buffer. | ||
const buffer = Buffer.from(payload_raw.value, "hex"); | ||
|
||
// Lets say you have a payload of 5 bytes. | ||
// 0 - Protocol Version | ||
// 1,2 - Temperature | ||
// 3,4 - Humidity | ||
// More information about buffers can be found here: https://nodejs.org/api/buffer.html | ||
// payload= [ | ||
payload.push( | ||
{ variable: "RSSI", value: byteArrayToLong(buffer, 0), unit: "" }, | ||
{ variable: "SNR", value: byteArrayToLong(buffer, 4), unit: "" }, | ||
{ variable: "messages_received", value: byteArrayToLong(buffer, 8), unit: "" }, | ||
{ variable: "messages_send", value: byteArrayToLong(buffer, 12), unit: "" }, | ||
{ variable: "lastcolor_red", value: buffer[16], unit: "" }, | ||
{ variable: "lastcolor_blue", value: buffer[17], unit: "" }, | ||
{ variable: "lastcolor_green", value: buffer[18], unit: "" }, | ||
{ variable: "lastcolor_ontime", value: buffer[19], unit: "" }, | ||
{ variable: "lastcolor_offtime", value: buffer[20], unit: "" }, | ||
{ variable: "sw_rev", value: buffer[21], unit: "" }, | ||
{ variable: "hw_rev", value: buffer[22], unit: "" }, | ||
{ variable: "adr_state", value: buffer[23], unit: "" } | ||
); | ||
// ]; | ||
|
||
// This will concat the content sent by your device with the content generated in this payload parser. | ||
// It also add the field "group" and "time" to it, copying from your sensor data. | ||
// payload = payload.concat( | ||
// data.map((x) => ({ | ||
// })) | ||
// ); | ||
} catch (e) { | ||
// Print the error to the Live Inspector. | ||
console.error(e); | ||
|
||
// Return the variable parse_error for debugging. | ||
payload = [{ variable: "parse_error", value: e.message }]; | ||
} | ||
} | ||
|
||
function byteArrayToLong(byteArray, from) { | ||
return byteArray[from] | (byteArray[from + 1] << 8) | (byteArray[from + 2] << 16) | (byteArray[from + 3] << 24); | ||
} |