Fix Promise issue

This commit is contained in:
Lars Strojny 2022-11-07 19:30:55 +01:00
parent 8e183b9fb7
commit 60be04925e
2 changed files with 62 additions and 33 deletions

View file

@ -1,22 +1,32 @@
import type { HapDiscover } from './api' import type {HapDiscover} from './api'
import { HAPNodeJSClient } from 'hap-node-client' import {HAPNodeJSClient} from 'hap-node-client'
import { Device, DeviceBoundary } from '../boundaries' import {Device, DeviceBoundary} from '../boundaries'
import { Array, Unknown } from 'runtypes' import {Array, Unknown} from 'runtypes'
import {Logger} from "homebridge";
const MaybeDevices = Array(Unknown) const MaybeDevices = Array(Unknown)
export const discover: HapDiscover = ({ pin, refreshInterval, discoveryTimeout, requestTimeout, logger, debug }) => { type HapConfig = {
return new Promise((resolve, reject) => { debug: boolean
try { refresh: number
const client = new HAPNodeJSClient({ timeout: number
debug: debug, reqTimeout: number
refresh: refreshInterval, pin: string
timeout: discoveryTimeout, }
reqTimeout: requestTimeout, type HapClient = typeof HAPNodeJSClient
pin, type ResolveFunc = (devices: Device[]) => void
}) type RejectFunc = (error: unknown) => void
const clientMap: Record<string,HapClient> = {}
const promiseMap: Record<string,[ResolveFunc,RejectFunc]> = {}
function startDiscovery(logger: Logger, config: HapConfig, resolve: ResolveFunc, reject: RejectFunc) {
const key = JSON.stringify(config)
if (!clientMap[key]) {
logger.debug('Creating new HAP client')
const client = new HAPNodeJSClient(config)
client.on('Ready', (deviceData: unknown) => { client.on('Ready', (deviceData: unknown) => {
try { try {
const devices: Device[] = [] const devices: Device[] = []
@ -33,13 +43,31 @@ export const discover: HapDiscover = ({ pin, refreshInterval, discoveryTimeout,
} }
} }
resolve(devices) if (promiseMap[key]) promiseMap[key][0](devices)
} catch (e) { } catch (e) {
reject(e) if (promiseMap[key]) promiseMap[key][1](e)
} }
}) })
} catch (e) { clientMap[key] = client
reject(e) } else {
logger.debug('Reusing existing HAP client')
} }
promiseMap[key] = [resolve, reject]
}
export const discover: HapDiscover = ({pin, refreshInterval, discoveryTimeout, requestTimeout, logger, debug}) => {
return new Promise((resolve, reject) => {
startDiscovery(
logger,
{
debug: debug,
refresh: refreshInterval,
timeout: discoveryTimeout,
reqTimeout: requestTimeout,
pin,
},
resolve,
reject
)
}) })
} }

View file

@ -112,6 +112,7 @@ export class PrometheusExporterPlatform implements IndependentPlatformPlugin {
this.metrics = aggregate(devices) this.metrics = aggregate(devices)
this.metricsDiscovered = true this.metricsDiscovered = true
this.log.debug('HAP discovery completed, %d metrics discovered', this.metrics.length) this.log.debug('HAP discovery completed, %d metrics discovered', this.metrics.length)
this.startHapDiscovery()
}) })
.catch((e) => { .catch((e) => {
this.log.error('HAP discovery error', e) this.log.error('HAP discovery error', e)