Skip to content

Commit

Permalink
Refactor payload parsing logic to handle missing "bn" variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Freddyminu committed Nov 18, 2024
1 parent 8a61b9d commit 17d607d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions decoders/connector/khomp/its-402/v1.0.0/payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,25 @@ describe("The data below should not be parsed", () => {
]);
});
});

describe("The data below should not be parsed", () => {
let payload = [
{ bn: "222996", bt: 1731937005, metadata: { mqtt_topic: "its/222996/data" } },
{ n: "ext_pwr", vb: true, metadata: { mqtt_topic: "its/222996/data" } },
{ n: "battery", u: "%EL", v: 99.92, metadata: { mqtt_topic: "its/222996/data" } },
{ n: "battery_status", vs: "battery_full", metadata: { mqtt_topic: "its/222996/data" } },
{ n: "c1_count", u: "count", v: 10691, metadata: { mqtt_topic: "its/222996/data" } },
{ n: "c2_status", vb: false, metadata: { mqtt_topic: "its/222996/data" } },
{ n: "rssi", u: "dBW", v: -83, metadata: { mqtt_topic: "its/222996/data" } },
{ n: "signal_status", vs: "excellent", metadata: { mqtt_topic: "its/222996/data" } },
];

let device = { params: [{ key: "language", value: "EN" }] };
payload = decoderRun(file_path, { payload, device });
test("Output Result", () => {
expect(Array.isArray(payload)).toBe(true);
});

expect(payload).toEqual([{ variable: "serial_number", value: "222996" }]); // the rest wont show up because dayjs isn't inside of the test
test("Output Result", () => {});
});
2 changes: 1 addition & 1 deletion decoders/connector/khomp/its-402/v1.0.0/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ if (language_obj) {
console.log("Language not found, using EN as fallback. Set Configuration Parameters 'language' with params: 'EN', 'PT', 'raw'.");
}

const decodeData = payload.find((x) => x.variable === "bn");
const decodeData = payload.find((x) => x.bn);

if (decodeData) {
payload = parsePayload(payload);
Expand Down

0 comments on commit 17d607d

Please sign in to comment.