Skip to content

Commit

Permalink
Make normalizers account for undefined fields
Browse files Browse the repository at this point in the history
  • Loading branch information
KrishnaIyer committed Nov 25, 2024
1 parent 3c69b2e commit 07034be
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 80 deletions.
42 changes: 25 additions & 17 deletions vendor/dragino/lht65.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,30 @@ default:
}

function normalizeUplink(input) {
return {
data: [
{
air: {
location: "indoor",
temperature: input.data.TempC_SHT,
relativeHumidity: input.data.Hum_SHT,
}
},
{
air: {
location: "outdoor",
temperature: input.data.TempC_DS,
},
battery: input.data.BatV
var data = [];

if (input.data.TempC_SHT) {
data.push({
air: {
location: "indoor",
temperature: input.data.TempC_SHT,
relativeHumidity: input.data.Hum_SHT,
}
]
};
});
}

if (input.data.TempC_DS) {
var val = {
air: {
location: "outdoor",
temperature: input.data.TempC_DS
}
}
if (input.data.BatV) {
val.battery = input.data.BatV
}
data.push(val);
}

return { data: data };
}
46 changes: 33 additions & 13 deletions vendor/dragino/lse01.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,37 @@ switch (input.fPort) {
}

function normalizeUplink(input) {
return {
data: {
air: {
temperature: Number(input.data.TempC_DS18B20),
},
soil: {
temperature: Number(input.data.temp_SOIL),
moisture: Number(input.data.water_SOIL),
ec: input.data.conduct_SOIL / 1000,
},
battery: input.data.Bat,
}
};
var data = {};
var air = {};
var soil = {};

if (input.data.TempC_DS18B20) {
air.temperature = Number(input.data.TempC_DS18B20);
}

if (input.data.temp_SOIL) {
soil.temperature = Number(input.data.temp_SOIL);
}

if (input.data.water_SOIL) {
soil.moisture = Number(input.data.water_SOIL);
}

if (input.data.conduct_SOIL) {
soil.ec = input.data.conduct_SOIL / 1000;
}

if (Object.keys(air).length > 0) {
data.air = air;
}

if (Object.keys(soil).length > 0) {
data.soil = soil;
}

if (input.data.Bat) {
data.battery = input.data.Bat;
}

return { data: data };
}
41 changes: 29 additions & 12 deletions vendor/dragino/lsn50-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,33 @@ default:
}

function normalizeUplink(input) {
return {
data: {
air: {
temperature: input.data.TempC_SHT,
relativeHumidity: input.data.Hum_SHT,
},
action: {
contactState: input.data.Door_status === "CLOSE" ? "closed" : input.data.Door_status === "OPEN" ? "open" : undefined
},
battery: input.data.BatV,
}
};
var data = {};
var air = {};
var action = {};

if (input.data.TempC_SHT) {
air.temperature = input.data.TempC_SHT;
}

if (input.data.Hum_SHT) {
air.relativeHumidity = input.data.Hum_SHT;
}

if (input.data.Door_status === "CLOSE" || input.data.Door_status === "OPEN") {
action.contactState = input.data.Door_status === "CLOSE" ? "closed" : "open";
}

if (Object.keys(air).length > 0) {
data.air = air;
}

if (Object.keys(action).length > 0) {
data.action = action;
}

if (input.data.BatV) {
data.battery = input.data.BatV;
}

return { data: data };
}
41 changes: 29 additions & 12 deletions vendor/dragino/lsn50v2-s31.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,33 @@ default:
}

function normalizeUplink(input) {
return {
data: {
air: {
temperature: input.data.TempC_SHT,
relativeHumidity: input.data.Hum_SHT
},
action: {
contactState: input.data.Door_status === "CLOSE" ? "closed" : input.data.Door_status === "OPEN" ? "open" : undefined
},
battery: input.data.BatV,
}
};
var data = {};
var air = {};
var action = {};

if (input.data.TempC_SHT) {
air.temperature = input.data.TempC_SHT;
}

if (input.data.Hum_SHT) {
air.relativeHumidity = input.data.Hum_SHT;
}

if (input.data.Door_status === "CLOSE" || input.data.Door_status === "OPEN") {
action.contactState = input.data.Door_status === "CLOSE" ? "closed" : "open";
}

if (Object.keys(air).length > 0) {
data.air = air;
}

if (Object.keys(action).length > 0) {
data.action = action;
}

if (input.data.BatV) {
data.battery = input.data.BatV;
}

return { data: data };
}
28 changes: 19 additions & 9 deletions vendor/milesight-iot/em300-th.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,27 @@ function Decoder(bytes, port) {
}

function normalizeUplink(input) {
return {
data: {
air: {
temperature: input.data.temperature,
relativeHumidity: input.data.humidity
},
battery: input.data.battery,
var data = {};
var air = {};

if (input.data.temperature) {
air.temperature = input.data.temperature;
}
};
}

if (input.data.humidity) {
air.relativeHumidity = input.data.humidity;
}

if (Object.keys(air).length > 0) {
data.air = air;
}

if (input.data.battery) {
data.battery = input.data.battery;
}

return { data: data };
}

/* ******************************************
* bytes to number
Expand Down
21 changes: 13 additions & 8 deletions vendor/milesight-iot/ws301.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ function Decoder(bytes, port) {


function normalizeUplink(input) {
return {
data: {
action: {
contactState: input.data.door === "close" ? "closed" : input.data.door === "open" ? "open" : undefined
},
battery: input.data.battery,
}
};
var data = {};

if (input.data.door && (input.data.door === "close" || input.data.door === "open")) {
data.action = {
contactState: input.data.door === "close" ? "closed" : "open"
};
}

if (input.data.battery) {
data.battery = input.data.battery;
}

return { data: data };
}
21 changes: 12 additions & 9 deletions vendor/sensative/strips.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,15 @@ function decodeUplink(input) {
}

function normalizeUplink(input) {
return {
data: {
air: {
temperature: input.data.temperature.value,
},
battery: input.data.battery
}
};
}
var data = {};

if (input.data && input.data.temperature && input.data.temperature.value) {
data.air = { temperature: input.data.temperature.value };
}

if (input.data && input.data.battery) {
data.battery = input.data.battery;
}

return { data: data };
}

0 comments on commit 07034be

Please sign in to comment.