Here is a code block I have a wired issue with.
It states that I forgot to include a return statement although it's there.
Is it something I'm doing wrong?
// Decoding function
function decodeMessage(value, format) {
let result = "";
if (format === 'base64') {
const raw = atob(value.raw_message);
for (let i = 0; i < raw.length; i++) {
const hex = raw.charCodeAt(i).toString(16);
result += hex.length === 2 ? hex : "0" + hex;
}
} else if (format === 'hex') {
result = value.hex_message;
}
return result;
}
const retn = {}; // The return object
let index = 0;
index += 5;
// Struct format
let message_struct = getStructs.data[1].payload.split(",");
let temp;
let thisone = (element) => element.id == temp;
for (let i = 0; i < message_struct.length; ++i) {
temp = parseInt(message_struct[i]);
message_struct[i] = getMetrics.data[getMetrics.data.findIndex(thisone)];
let psubstn = result.substring((index) * 2, (index) * 2 + message_struct[i].size / 4);
var work = parseInt(psubstn, 16);
index += message_struct[i].size / 8;
if (["ff", "ffff", "ffffffff", "7f", "7fff", "7fffffff","ff7f","ffffff7f"].includes(psubstn)) {
retn[message_struct[i].column_name] = null;
continue;
}
if (message_struct[i].column_name === "res1"||message_struct[i].column_name === "res2"||message_struct[i].column_name === "error_flags"||message_struct[i].column_name === "unit_status")
continue;
if (message_struct[i].column_name === "timestamp") {
// Flip the bytes to correct the endianness
work = flipBytes(work, 4);
// Extract date and time from timestamp
retn[message_struct[i].column_name] = extractDateTime(work);
continue;
}
// Process other fields based on type
switch (message_struct[i].signed) {
case "unsigned":
work = flipBytes(work, message_struct[i].size / 8);
retn[message_struct[i].column_name] = work * message_struct[i].scaling - message_struct[i].offset;
break;
case "signed":
let isNegative = (work & (1 << (message_struct[i].size - 1))) !== 0;
if (isNegative) {
work = work - (1 << message_struct[i].size);
}
break;
case "float":
retn[message_struct[i].column_name] = ieee32ToFloat(flipBytes(parseInt(psubstn, 16), 4));
break;
case "bool":
retn[message_struct[i].column_name] = parseInt(psubstn, 16) !== 0;
break;
}
}
// Additional properties
retn.modem_id = value.asset_id;
retn.swarm_message_id = value.swarm_message_id;
retn.asset_id = value.buoy_id;
return retn;