Skip to content

Commit

Permalink
Merge pull request #102 from tago-io/fix/connector-khomp-its-312
Browse files Browse the repository at this point in the history
Connector Update - Fixing variable value from being a Object
  • Loading branch information
FabianoEger authored Dec 19, 2024
2 parents d21a9a5 + 3b9ac18 commit fcc0ab7
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
2 changes: 1 addition & 1 deletion decoders/connector/khomp/its-312-iot/connector.jsonc
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"networks": [
"../../../../network/mqtt/v1.0.0/payload.js"
]
}
}
104 changes: 104 additions & 0 deletions decoders/connector/khomp/its-312-iot/v1.0.0/payload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* eslint-disable unicorn/numeric-separators-style */
import { describe, expect, test } from "vitest";

import { decoderRun } from "../../../../../src/functions/decoder-run";

const file_path = "/decoders/connector/khomp/its-312-iot/v1.0.0/payload.ts" as const;

describe("The data below should not be parsed", () => {
let payload = [
{ bn: "142143", bt: 1733859162, metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "battery", u: "%EL", v: 100, metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "rssi", u: "dBW", v: -109, metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "ext_pwr", vb: true, metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "update", vb: true, metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "c1_count", u: "count", v: 640, metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "c2_status", vb: false, metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "relay", vs: "NC", metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "env_temp", u: "Cel", v: 32.4, metadata: { mqtt_topic: "uns/euclides/pipa" } },
{ n: "env_hum", u: "%RH", v: 32.4, metadata: { mqtt_topic: "uns/euclides/pipa" } },
];

payload = decoderRun(file_path, { payload });

console.log(payload);

test("Output Result", () => {
expect(Array.isArray(payload)).toBe(true);
});

test("Expected values", () => {
expect(payload).toEqual(
expect.arrayContaining([
expect.objectContaining({ variable: "device_number", value: "142143" }),
expect.objectContaining({
variable: "battery",
value: 100,
metadata: undefined,
unit: "%EL",
location: undefined,
}),
expect.objectContaining({
variable: "rssi",
value: -109,
metadata: undefined,
unit: "dBW",
location: undefined,
}),
expect.objectContaining({
variable: "ext_pwr",
value: true,
metadata: undefined,
unit: undefined,
location: undefined,
}),
expect.objectContaining({
variable: "update",
value: true,
metadata: undefined,
unit: undefined,
location: undefined,
}),
expect.objectContaining({
variable: "c_count",
value: 640,
metadata: undefined,
unit: "count",
location: undefined,
}),
expect.objectContaining({
variable: "c_status",
value: undefined,
metadata: undefined,
unit: undefined,
location: undefined,
}),
expect.objectContaining({
variable: "relay",
value: "NC",
metadata: undefined,
unit: undefined,
location: undefined,
}),
expect.objectContaining({
variable: "env_temp",
value: 32.4,
metadata: undefined,
unit: "Cel",
location: undefined,
}),
expect.objectContaining({
variable: "env_hum",
value: 32.4,
metadata: undefined,
unit: "%RH",
location: undefined,
}),
expect.objectContaining({
variable: "payload",
value: expect.any(String),
}),
])
);
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-nocheck
/* eslint-disable eqeqeq */
/* eslint-disable max-len */
/* eslint-disable no-plusplus */
Expand All @@ -21,12 +22,12 @@
const ignore_vars = [];

// Function to convert an object to TagoIO data format.
function ToTagoFormat(object_item, serie, prefix = '') {
function ToTagoFormat(object_item, serie, prefix = "") {
const result = [];
for (const key in object_item) {
if (ignore_vars.includes(key)) continue; // ignore chosen vars

if (typeof object_item[key] === 'object') {
if (typeof object_item[key] === "object") {
result.push({
variable: object_item[key].MessageType || `${prefix}${key}`,
value: object_item[key].value || object_item[key].Value,
Expand Down Expand Up @@ -65,21 +66,19 @@ function Decoder(payload) {
if (data.n) {
if (data.v || data.vb || data.vs) decodedData.value = data.v || data.vb || data.vs;
if (data.u) decodedData.unit = data.u;
insertData(decoded, data.n.replace(/[?*!<>.-=$]/, ''), decodedData);
insertData(decoded, data.n.replace(/[?*!<>.-=$]/, ""), decodedData);
} else if (data.bn) {
insertData(decoded, 'device_number', data.bn);
insertData(decoded, "device_number", data.bn);
}
});
return decoded;
}

const aux = payload;
payload = eval(payload);
console.log(typeof(payload));

const serie = Date.now();
const decodedPayload = Decoder(payload);

// Parse the payload_raw to JSON format (it comes in a String format)
payload = ToTagoFormat(decodedPayload, serie);
payload.push({ variable: 'payload', value: aux });
payload.push({ variable: "payload", value: JSON.stringify(aux) }); // Added stringify since this was causing issues when

0 comments on commit fcc0ab7

Please sign in to comment.