Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the Indoor/Outdoor Thermo-Hygrometer (WoIOSensorTH) #200

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions lib/switchbot-advertising.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class SwitchbotAdvertising {
sd = this._parseServiceDataForWoSensorTHPlus(buf, onlog);// WoMeterPlus
} else if (model === "r") {
sd = this._parseServiceDataForWoStrip(buf, onlog);// WoStrip
} else if (model === "w") {
sd = this._parseServiceDataForWoIOSensorTH(buf, manufacturerData, onlog); // Indoor/Outdoor Thermo-Hygrometer
} else {
if (onlog && typeof onlog === "function") {
onlog(
Expand Down Expand Up @@ -662,6 +664,49 @@ class SwitchbotAdvertising {

return data;
}

_parseServiceDataForWoIOSensorTH(serviceDataBuf, manufacturerDataBuf, onlog) {
if (serviceDataBuf.length !== 3) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoIOSensorTH] Service Data Buffer length ${serviceDataBuf.length} !== 3!`
);
}
return null;
}
if (manufacturerDataBuf.length !== 14) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoIOSensorTH] Manufacturer Data Buffer length ${manufacturerDataBuf.length} !== 14!`
);
}
return null;
}
const mdByte10 = manufacturerDataBuf.readUInt8(10);
const mdByte11 = manufacturerDataBuf.readUInt8(11);
const mdByte12 = manufacturerDataBuf.readUInt8(12);

const sdByte2 = serviceDataBuf.readUInt8(2);

const temp_sign = mdByte11 & 0b10000000 ? 1 : -1;
const temp_c = temp_sign * ((mdByte11 & 0b01111111) + (mdByte10 & 0b00001111) / 10);
const temp_f = Math.round(((temp_c * 9 / 5) + 32) * 10) / 10;

const data = {
model: "w",
modelName: "WoIOSensorTH",
temperature: {
c: temp_c,
f: temp_f,
},
fahrenheit: mdByte12 & 0b10000000 ? true : false, // needs to be confirmed!
humidity: mdByte12 & 0b01111111,
battery: sdByte2 & 0b01111111,
};

console.log(data);
return data;
}
}

module.exports = new SwitchbotAdvertising();