MCPcopy Create free account
hub / github.com/codetheweb/tuyapi / _sendPing

Method _sendPing

index.js:501–531  ·  view source on GitHub ↗

* Sends a heartbeat ping to the device * @private

()

Source from the content-addressed store, hash-verified

499 * @private
500 */
501 async _sendPing() {
502 debug(`Pinging ${this.device.ip}`);
503
504 // Create byte buffer
505 const buffer = this.device.parser.encode({
506 data: Buffer.allocUnsafe(0),
507 commandByte: CommandType.HEART_BEAT,
508 sequenceN: ++this._currentSequenceN
509 });
510
511 // Check for response
512 const now = new Date();
513
514 if (this._pingPongTimeout === null) {
515 // If we do not expect a pong from a former ping, we need to set a timeout
516 this._pingPongTimeout = setTimeout(() => {
517 if (this._lastPingAt < now) {
518 this.disconnect();
519 }
520 }, this._responseTimeout * 1000);
521 } else {
522 debug('There was no response to the last ping.');
523 }
524
525 // Send ping
526 this.client.write(buffer);
527 if (this.globalOptions.issueRefreshOnPing) {
528 this.refresh();
529 this.get();
530 }
531 }
532
533 /**
534 * Create a deferred promise that resolves as soon as the connection is established.

Callers 1

_finishConnectMethod · 0.95

Calls 4

disconnectMethod · 0.95
refreshMethod · 0.95
getMethod · 0.95
encodeMethod · 0.80

Tested by

no test coverage detected