Skip to content

Commit

Permalink
Merge pull request #92 from tago-io/fix/connector-kuando-busylight
Browse files Browse the repository at this point in the history
Adding fix and unit tests
  • Loading branch information
FabianoEger authored Oct 29, 2024
2 parents 9bcb354 + e771b85 commit 30a8f1d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 54 deletions.
2 changes: 1 addition & 1 deletion decoders/connector/dragino/cs01lb/v1.0.0/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ if (cs01lb_payload) {
try {
// Convert the data from Hex to Javascript Buffer.
const buffer = Buffer.from(cs01lb_payload.value, "hex");
const group = new Date().getTime();
const group = new Date().getTime().toString();
const payload_aux = Decoder(buffer, Number(port.value));
payload = payload.concat(toTagoFormat(payload_aux, group));
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"versions": {
"v1.0.0": {
"src": "./v1.0.0/payload.js",
"src": "./v1.0.0/payload.ts",
"manifest": "./v1.0.0/payload-config.jsonc"
}
}
Expand Down
52 changes: 0 additions & 52 deletions decoders/connector/kuando/busylight-iot-omega/v1.0.0/payload.js

This file was deleted.

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 decoders/connector/kuando/busylight-iot-omega/v1.0.0/payload.ts
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);
}

0 comments on commit 30a8f1d

Please sign in to comment.