Skip to content

Commit

Permalink
feat: Added phoneWatchdog verification
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Apr 3, 2021
1 parent 62dc4dd commit 6616fa2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,23 @@ export class HostLayer {
public async getBatteryLevel() {
return await this.page.evaluate(() => WAPI.getBatteryLevel());
}

/**
* Start phone Watchdog, forcing the phone connection verification.
*
* @param interval interval number in miliseconds
*/
public async startPhoneWatchdog(interval: number = 15000) {
return await this.page.evaluate(
(interval) => WAPI.startPhoneWatchdog(interval),
interval
);
}

/**
* Stop phone Watchdog, more details in {@link startPhoneWatchdog}
*/
public async stopPhoneWatchdog(interval: number) {
return await this.page.evaluate(() => WAPI.stopPhoneWatchdog());
}
}
1 change: 1 addition & 0 deletions src/lib/wapi/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ export { getSessionTokenBrowser } from './get-session-token';
export { sendMute } from './send-mute';
export { getListMute, interfaceMute } from './get-list-mute';
export { downloadMedia } from './download-media';
export * from './phoneWatchdog';
65 changes: 65 additions & 0 deletions src/lib/wapi/functions/phoneWatchdog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This file is part of WPPConnect.
*
* WPPConnect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WPPConnect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/

let timer = null;
let pong = true;

async function sendPing() {
return window.WAPI.waitForStore(['State', 'Stream'], () => {
console.log(window.Store.Stream.mode, window.Store.State.default.state);
// Check only if the interface is in CHAT and not disconnected
if (
window.Store.Stream.mode !== 'MAIN' ||
window.Store.State.default.state === 'TIMEOUT'
) {
return;
}

// Start phoneWatchdog if ping fails
if (!pong) {
window.Store.State.default.phoneWatchdog.activate();
window.Store.State.default.phoneWatchdog.poke(250);
return;
}

// Reset ping state
pong = false;

// Send a ping request
return window.Store.State.default
.sendBasic({
tag: window.Store.State.default.tag('ping'),
data: ['admin', 'test'],
})
.then(() => {
pong = true;
});
});
}

export function startPhoneWatchdog(interval = 15000) {
stopPhoneWatchdog();

timer = setInterval(sendPing, interval);
}

export function stopPhoneWatchdog() {
if (timer) {
clearInterval(timer);
}
timer = null;
}
4 changes: 4 additions & 0 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ import {
getListMute,
interfaceMute,
downloadMedia,
startPhoneWatchdog,
stopPhoneWatchdog,
} from './functions';
import {
base64ToFile,
Expand Down Expand Up @@ -311,6 +313,8 @@ if (typeof window.WAPI === 'undefined') {
window.WAPI.restartService = restartService;
window.WAPI.killServiceWorker = killServiceWorker;
window.WAPI.sendMute = sendMute;
window.WAPI.startPhoneWatchdog = startPhoneWatchdog;
window.WAPI.stopPhoneWatchdog = stopPhoneWatchdog;

// Listeners initialization
window.WAPI._newMessagesQueue = [];
Expand Down
2 changes: 2 additions & 0 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ interface WAPI {
setMyStatus: (to: string) => void;
setProfilePic: (path: string, to?: string) => Promise<boolean>;
setTheme: (theme?: string) => boolean;
startPhoneWatchdog: (interval: number) => void;
startTyping: (to: string) => void;
stopPhoneWatchdog: () => void;
stopTyping: (to: string) => void;
takeOver: () => boolean;
unblockContact: (messageId: string) => boolean;
Expand Down

1 comment on commit 6616fa2

@edgardmessias
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref #89

Please sign in to comment.