Skip to content

Commit

Permalink
added null data support for e0 #459
Browse files Browse the repository at this point in the history
  • Loading branch information
io53 committed Dec 3, 2024
1 parent f8d7fdc commit 712b94b
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions src/decoder/e0.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const parseRawRuuvi = function (data) {
const robject = {};

let temperature = (data[5] << 8) | (data[6] & 0xff);
if (temperature > 32767) {
temperature -= 65536;
if (temperature !== 0x8000) {
if (temperature > 32767) {
temperature -= 65536;
}
robject.temperature = temperature / 200.0;
robject.temperature = round(robject.temperature, 2);
}
robject.temperature = temperature / 200.0;
robject.temperature = round(robject.temperature, 2);

if (!(data[7] === 255 && data[8] === 255)) {
robject.humidity = (((data[7] & 0xff) << 8) | (data[8] & 0xff)) / 400.0;
Expand All @@ -19,28 +21,39 @@ const parseRawRuuvi = function (data) {
robject.pressure = (((data[9] & 0xff) << 8) | (data[10] & 0xff)) + 50000;
}

const pmFrom = offset => {
let pm = (data[offset] << 8) | data[offset + 1];
return round(pm / 10.0, 2);
}
const pmTypes = ["pm1p0", "pm2p5", "pm4p0", "pm10p0"];
const pmOffsets = [11, 13, 15, 17];

pmOffsets.forEach((offset, index) => {
const pm = (data[offset] << 8) | data[offset + 1];
const value = pm === 0xFFFF ? null : round(pm / 10.0, 2);
if (value !== null) {
robject[pmTypes[index]] = value;
}
});

const co2 = ((data[19] << 8) | data[20])
if (co2 !== 0xFFFF) robject.co2 = co2

const voc = (((data[21] & 0b00000001) << 8) | data[22])
if (voc !== 511) robject.voc = voc

const nox = (((data[23] & 0b00000001) << 8) | data[24])
if (nox !== 511) robject.nox = nox

const illuminance = ((data[25] << 8) | data[26])
if (illuminance !== 0xFFFF) robject.illuminance = illuminance

if (data[27] !== 0xFF) robject.soundLevelAvg = data[27] * 0.5

robject.pm1p0 = pmFrom(11)
robject.pm2p5 = pmFrom(13)
robject.pm4p0 = pmFrom(15)
robject.pm10p0 = pmFrom(17)
if (data[28] !== 0xFF) robject.soundLevelPeak = data[28] * 0.5

robject.co2 = ((data[19] << 8) | data[20])
robject.voc = (((data[21] & 0b00000001) << 8) | data[22])
robject.nox = (((data[23] & 0b00000001) << 8) | data[24])
robject.illuminance = ((data[25] << 8) | data[26])
const measurementSequenceNumber = ((data[29] & 0xff) << 8) | (data[30] & 0xff);
if (robject.measurementSequenceNumber !== 0xFFFF) robject.measurementSequenceNumber = measurementSequenceNumber

robject.soundLevelAvg = data[27] * 0.5
robject.soundLevelPeak = data[28] * 0.5
if (data[31] === 0xFF) robject.battery = data[31] * 0.03 * 1000;

robject.measurementSequenceNumber = ((data[29] & 0xff) << 8) | (data[30] & 0xff);
robject.battery = data[31] * 0.03 * 1000;


robject.flags = {
usbOn: (data[32] & 0b10000000) >> 7,
lowBattery: (data[32] & 0b01000000) >> 6,
Expand Down

0 comments on commit 712b94b

Please sign in to comment.