Skip to content

Commit

Permalink
Create wohub2.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker authored May 31, 2024
1 parent f9fcd5c commit 8d8564c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/device/wohub2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright(C) 2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
*
* wohub2.ts: Switchbot BLE API registration.
*/
import { SwitchbotDevice } from '../device.js';

export class WoHub2 extends SwitchbotDevice {
static parseServiceData(buf: Buffer, onlog: ((message: string) => void) | undefined) {
if (buf.length !== 6) {
if (onlog && typeof onlog === 'function') {
onlog(
`[parseServiceDataForWoSensorTH] Buffer length ${buf.length} !== 6!`,
);
}
return null;
}
const byte2 = buf.readUInt8(2);
const byte3 = buf.readUInt8(3);
const byte4 = buf.readUInt8(4);
const byte5 = buf.readUInt8(5);

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

const data = {
model: 'v',
modelName: 'WoHub2',
temperature: {
c: temp_c,
f: temp_f,
},
fahrenheit: byte5 & 0b10000000 ? true : false,
humidity: byte5 & 0b01111111,
battery: byte2 & 0b01111111,
};

return data;
}
}

0 comments on commit 8d8564c

Please sign in to comment.