Skip to content

Commit

Permalink
Resolves #40 in response to breaking changes with Node 19+ http agent…
Browse files Browse the repository at this point in the history
… keepalive defaults which caused implications with the node-fetch module used.
  • Loading branch information
mkormendy committed Apr 1, 2024
1 parent 3028d9c commit e39fafa
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/platform.ts
Expand Up @@ -76,6 +76,8 @@ export class KonnectedHomebridgePlatform implements DynamicPlatformPlugin {
private ssdpDiscovering = false; // for storing state of SSDP discovery process
private ssdpDiscoverAttempts = 0;

private httpAgent: Agent;

constructor(public readonly log: Logger, public readonly config: PlatformConfig, public readonly api: API) {
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
Expand All @@ -101,6 +103,9 @@ export class KonnectedHomebridgePlatform implements DynamicPlatformPlugin {
? this.config.advanced.discoveryTimeout * 1000
: 5000; // 5 seconds

// close fetch requests after request/response is performed/received
this.httpAgent = new http.Agent({keepAlive: false});

this.log.debug('Finished initializing platform');

// Homebridge looks for and fires this event when it has retrieved all cached accessories from disk
Expand Down Expand Up @@ -354,7 +359,9 @@ export class KonnectedHomebridgePlatform implements DynamicPlatformPlugin {
// dedupe responses, ignore excluded panels in environment variables, and then provision panel(s)
if (!ssdpDeviceIDs.includes(panelUUID) && !excludedUUIDs.includes(panelUUID)) {
// get panel status object (not using async await)
fetch(ssdpHeaderLocation.replace('Device.xml', 'status'))
fetch(ssdpHeaderLocation.replace('Device.xml', 'status'), {
agent: this.httpAgent,
})
// convert response to JSON
.then((fetchResponse) => fetchResponse.json())
.then((panelResponseObject) => {
Expand Down Expand Up @@ -537,6 +544,7 @@ export class KonnectedHomebridgePlatform implements DynamicPlatformPlugin {
await fetch(url, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
agent: this.httpAgent,
body: JSON.stringify(panelConfigurationPayload),
});
} catch (error: unknown) {
Expand Down Expand Up @@ -1128,6 +1136,7 @@ export class KonnectedHomebridgePlatform implements DynamicPlatformPlugin {
const response = await fetch(url, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
agent: this.httpAgent,
body: JSON.stringify(actuatorPayload),
});
if (
Expand Down

0 comments on commit e39fafa

Please sign in to comment.