Skip to content

Commit

Permalink
Update woblindtilt.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker committed Jul 23, 2024
1 parent ae0ddbb commit 96495d5
Showing 1 changed file with 117 additions and 10 deletions.
127 changes: 117 additions & 10 deletions src/device/woblindtilt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import { SwitchbotDevice } from '../device.js';
import { SwitchBotBLEModel, SwitchBotBLEModelFriendlyName, SwitchBotBLEModelName } from '../types.js';

export class WoBlindTilt extends SwitchbotDevice {
[x: string]: any;
static parseServiceData(buf: Buffer, onlog: ((message: string) => void) | undefined) {
if (buf.length !== 5 && buf.length !== 6) {
if (onlog && typeof onlog === 'function') {
onlog(
`[parseServiceDataForWoBlindTilt] Buffer length ${buf.length} !== 5 or 6!`,
);
onlog(`[parseServiceDataForWoBlindTilt] Buffer length ${buf.length} !== 5 or 6!`);
}
return null;
}
Expand Down Expand Up @@ -39,8 +38,23 @@ export class WoBlindTilt extends SwitchbotDevice {
}

/* ------------------------------------------------------------------
* open()
* - Open the blindtilt
* open()
* - open the blindtilt
*
* [Arguments]
* - none
*
* [Return value]
* - Promise object
* Nothing will be passed to the `resolve()`.
* ---------------------------------------------------------------- */
open() {
return this._operateBlindTilt([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x32]);
}

/* ------------------------------------------------------------------
* closeUp()
* - close the blindtilt up
*
* [Arguments]
* - none
Expand All @@ -49,13 +63,28 @@ export class WoBlindTilt extends SwitchbotDevice {
* - Promise object
* Nothing will be passed to the `resolve()`.
* ---------------------------------------------------------------- */
open() {
closeUp() {
return this._operateBlindTilt([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x64]);
}

/* ------------------------------------------------------------------
* closeDown()
* - close the blindtilt down
*
* [Arguments]
* - none
*
* [Return value]
* - Promise object
* Nothing will be passed to the `resolve()`.
* ---------------------------------------------------------------- */
closeDown() {
return this._operateBlindTilt([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x00]);
}

/* ------------------------------------------------------------------
* close()
* - close the blindtilt
* - close the blindtilt to the nearest endpoint
*
* [Arguments]
* - none
Expand All @@ -65,7 +94,85 @@ export class WoBlindTilt extends SwitchbotDevice {
* Nothing will be passed to the `resolve()`.
* ---------------------------------------------------------------- */
close() {
return this._operateBlindTilt([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x64]);
const position = this.getPosition();
if (position > 50) {
return this.closeUp();
} else {
return this.closeDown();
}
}

/* ------------------------------------------------------------------
* getPosition()
* - get the current position of the blindtilt
*
* [Arguments]
* - none
*
* [Return value]
* - number
* The current position of the blindtilt (0-100).
* ---------------------------------------------------------------- */
getPosition() {
// Retrieve the current tilt position using the _getAdvValue method
const tiltPosition = this._getAdvValue('tilt');

// Ensure the tilt position is within the valid range (0-100)
return Math.max(0, Math.min(tiltPosition, 100));
}

/* ------------------------------------------------------------------
* getBasicInfo()
* - get the basic information of the blindtilt
*
* [Arguments]
* - none
*
* [Return value]
* - Promise object
* The basic information of the blindtilt.
* ---------------------------------------------------------------- */
async getBasicInfo() {
const data = await this._getBasicInfo();
if (!data) {
return null;
}

const tilt = Math.max(Math.min(data[6], 100), 0);
const moving = Boolean(data[5] & 0b00000011);
let opening = false;
let closing = false;
let up = false;

if (moving) {
opening = Boolean(data[5] & 0b00000010);
closing = !opening && Boolean(data[5] & 0b00000001);
if (opening) {
const flag = Boolean(data[5] & 0b00000001);
up = flag ? this._reverse : !flag;
} else {
up = tilt < 50 ? this._reverse : tilt > 50;
}
}

return {
battery: data[1],
firmware: data[2] / 10.0,
light: Boolean(data[4] & 0b00100000),
fault: Boolean(data[4] & 0b00001000),
solarPanel: Boolean(data[5] & 0b00001000),
calibration: Boolean(data[5] & 0b00000100),
calibrated: Boolean(data[5] & 0b00000100),
inMotion: moving,
motionDirection: {
opening: moving && opening,
closing: moving && closing,
up: moving && up,
down: moving && !up,
},
tilt: this._reverse ? 100 - tilt : tilt,
timers: data[7],
};
}

/* ------------------------------------------------------------------
Expand Down Expand Up @@ -96,7 +203,7 @@ export class WoBlindTilt extends SwitchbotDevice {
* ---------------------------------------------------------------- */
runToPos(percent: number, mode: number) {
if (typeof percent !== 'number') {
return new Promise((resolve, reject) => {
return new Promise((_resolve, reject) => {
reject(
new Error(
'The type of target position percentage is incorrect: ' +
Expand All @@ -109,7 +216,7 @@ export class WoBlindTilt extends SwitchbotDevice {
mode = 0xff;
} else {
if (typeof mode !== 'number') {
return new Promise((resolve, reject) => {
return new Promise((_resolve, reject) => {
reject(
new Error('The type of running mode is incorrect: ' + typeof mode),
);
Expand Down

0 comments on commit 96495d5

Please sign in to comment.