packet-decoder
Version:
SenRa Packet Decoder For IOT devices
117 lines (107 loc) • 3.65 kB
JavaScript
const ClusterLineChart = ({data, unchangedDevices}) => {
const chart = {
label: 'Smart Bin Status',
data: [],
labels: []
};
return chart;
};
const getStatus = (fillLevel) => {
if (fillLevel >= 0 && fillLevel <= 25) {
return 1;
}
if (fillLevel >= 26 && fillLevel <= 75) {
return 2;
}
if (fillLevel >= 76 && fillLevel <= 100) {
return 3;
}
return -1;
};
const ClusterMetaData = ({cluster, deviceData}) => {
let metaData = [
{name: 'Total SmartBins', style: 'color:#00000', styleNg: {color: '#00000'}, label: '20'},
{name: '75%-100%', style: 'color:#fc5e5e', styleNg: {color: '#fc5e5e'}, label: '8'},
{name: '25%-75%', style: 'color:#c6ca24', styleNg: {color: '#c6ca24'}, label: '8'},
{name: '0%-25%', style: 'color:#81b040', styleNg: {color: '#81b040'}, label: '8'},
{name: 'Offline', style: 'color:#b7b7b7', styleNg: {color: '#b7b7b7'}, label: '4'}
];
if (deviceData) {
let totalSmartBins = 0;
let level3 = 0;
let level2 = 0;
let level1 = 0;
let notWorking = 0;
deviceData.forEach(device => {
totalSmartBins++;
if (device.packet.pdu && device.packet.pdu.payload) {
switch (getStatus(parseInt(device.packet.pdu.payload.status))) {
case 1: {
level1++;
break;
}
case 2: {
level2++;
break;
}
case 3: {
level3++;
break;
}
case -1: {
notWorking++;
break;
}
}
} else {
notWorking++;
}
});
metaData[0].label = totalSmartBins;
metaData[1].label = level3;
metaData[2].label = level2;
metaData[3].label = level1;
metaData[4].label = notWorking;
if (Math.max(level1, level2, level3) === level1) {
cluster.icon = '/icons/SmartBin/cluster-level1.png';
}
if (Math.max(level1, level2, level3) === level2) {
cluster.icon = '/icons/SmartBin/cluster-level2.png';
} else {
cluster.icon = '/icons/SmartBin/cluster-level3.png';
}
}
return metaData;
};
const DeviceCardData = ({device, data}) => {
data.url = '/icons/SmartBin/device-not-working.png';
if (!device.packet.pdu.payload) {
device.card = data;
return;
}
data.battery = device.packet.pdu.payload.battery_status;
const fillLevel = parseInt(device.packet.pdu.payload.status);
switch (getStatus(fillLevel)) {
case 1: {
data.status = `${fillLevel}%`;
data.url = '/icons/SmartBin/device-level1.png';
break;
}
case 2: {
data.status = `${fillLevel}%`;
data.url = '/icons/SmartBin/device-level2.png';
break;
}
case 3: {
data.status = `${fillLevel}%`;
data.url = '/icons/SmartBin/device-level3.png';
break;
}
default: {
data.status = 'Offline';
data.url = '/icons/SmartBin/device-offline.png';
}
}
device.card = data;
};
module.exports = {ClusterLineChart, ClusterMetaData, DeviceCardData};