Skip to content

Commit

Permalink
Changes to support automated testing
Browse files Browse the repository at this point in the history
  • Loading branch information
NorthernMan54 committed May 23, 2024
1 parent 5bd164a commit 218443a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions HAPNodeJSClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ axiosRetry(axios, { retries: 3 });
var discovered = [];
var mdnsCache = {};
var populateCache = false;
var populateCacheTimeout;

var filter = false;
var pins = {};
var type = 'hap';

module.exports = {
HAPNodeJSClient: HAPNodeJSClient
Expand Down Expand Up @@ -47,6 +49,7 @@ function HAPNodeJSClient(options) {
this.timeout = options.timeout || 20;
this.reqTimeout = options.reqTimeout || 7000;
this.RegisterPin('default', options.pin || '031-45-154');
type = options.type || 'hap';
filter = options.filter || false;
if (this.debug) {
let debugEnable = require('debug');
Expand All @@ -65,7 +68,7 @@ function HAPNodeJSClient(options) {
this.eventRegistry = {};
_discovery.call(this);
this._eventBus = new EventEmitter();
setInterval(_discovery.bind(this), this.refresh * 1000);
this.discoveryTimer = setInterval(_discovery.bind(this), this.refresh * 1000);

/**
* HomeKit Accessory Characteristic event pass thru
Expand Down Expand Up @@ -154,7 +157,7 @@ function _populateCache(timeout, discovery, callback) {
populateCache = true;
// debug('_populateCache', new Error().stack);
var browser = bonjour.find({
type: 'hap'
type: (type ? type : 'hap')
}, function (result) {
if (result.txt) {
debug('HAP Device discovered', result.name, result.addresses);
Expand Down Expand Up @@ -192,7 +195,7 @@ function _populateCache(timeout, discovery, callback) {
debug('Unsupported device found, skipping', result.name);
}
});
setTimeout(function () {
populateCacheTimeout = setTimeout(function () {
// debug('Timeout:');
browser.stop();
populateCache = false;
Expand All @@ -212,6 +215,16 @@ function _findPinByKey(key) {
return pins[key] || pins['default'];
}

/**
* Destroy and shutdown HAPNodeJSClient - Used by testing
*/
HAPNodeJSClient.prototype.destroy = function() {
clearInterval(this.discoveryTimer);
clearInterval(populateCacheTimeout);
bonjour.destroy();
// this.monitorBridgeUpdates.destroy();
}

/**
* HAPNodeJSClient.prototype.RegisterPin - Register pin numbers ()
*
Expand Down

0 comments on commit 218443a

Please sign in to comment.