2022-11-07 22:31:17 +01:00
|
|
|
import { API, IndependentPlatformPlugin, Logger, PlatformConfig } from 'homebridge'
|
2022-11-06 13:04:30 +01:00
|
|
|
|
2022-11-07 22:31:17 +01:00
|
|
|
import { aggregate } from './metrics'
|
|
|
|
import { discover } from './adapters/discovery/hap_node_js_client'
|
|
|
|
import { serve } from './adapters/http/fastify'
|
|
|
|
import { HttpServerController } from './adapters/http/api'
|
|
|
|
import { PrometheusServer } from './prometheus'
|
2022-11-09 21:36:23 +01:00
|
|
|
import { Config, ConfigBoundary, checkBoundary } from './boundaries'
|
2022-11-06 13:04:30 +01:00
|
|
|
|
2022-11-06 13:50:39 +01:00
|
|
|
export class PrometheusExporterPlatform implements IndependentPlatformPlugin {
|
2022-11-07 22:31:17 +01:00
|
|
|
private readonly httpServer: PrometheusServer
|
|
|
|
private httpServerController: HttpServerController | undefined = undefined
|
2022-11-08 19:13:34 +01:00
|
|
|
private readonly config: Config
|
2022-11-06 13:04:30 +01:00
|
|
|
|
2022-11-08 19:13:34 +01:00
|
|
|
constructor(public readonly log: Logger, config: PlatformConfig, public readonly api: API) {
|
|
|
|
this.log.debug('Initializing platform %s', config.platform)
|
2022-11-06 13:04:30 +01:00
|
|
|
|
2022-11-09 21:36:23 +01:00
|
|
|
this.config = checkBoundary(ConfigBoundary, config)
|
2022-11-08 19:13:34 +01:00
|
|
|
|
|
|
|
this.log.debug('Configuration parsed', this.config)
|
2022-11-06 13:04:30 +01:00
|
|
|
|
2022-11-06 13:50:39 +01:00
|
|
|
this.api.on('shutdown', () => {
|
2022-11-08 19:13:34 +01:00
|
|
|
this.log.debug('Shutting down %s', config.platform)
|
2022-11-07 22:31:17 +01:00
|
|
|
if (this.httpServerController) {
|
|
|
|
this.httpServerController.shutdown()
|
2022-11-06 13:50:39 +01:00
|
|
|
}
|
|
|
|
})
|
2022-11-06 13:04:30 +01:00
|
|
|
|
2022-11-08 22:15:44 +01:00
|
|
|
this.log.debug('Starting Prometheus HTTP server on port %d', this.config.port)
|
2022-11-07 22:31:17 +01:00
|
|
|
|
2022-11-08 14:28:13 +01:00
|
|
|
this.httpServer = new PrometheusServer(this.config.port, this.log, this.config.debug, this.config.prefix)
|
2022-11-07 22:31:17 +01:00
|
|
|
serve(this.httpServer)
|
|
|
|
.then((httpServerController) => {
|
2022-11-08 14:26:08 +01:00
|
|
|
this.log.debug('HTTP server started on port %d', this.config.port)
|
2022-11-07 22:31:17 +01:00
|
|
|
this.httpServerController = httpServerController
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
2022-11-08 22:15:44 +01:00
|
|
|
this.log.error('Failed to start Prometheus HTTP server on port %d: %o', this.config.port, e)
|
2022-11-07 22:31:17 +01:00
|
|
|
})
|
2022-11-06 13:04:30 +01:00
|
|
|
|
2022-11-06 13:50:39 +01:00
|
|
|
this.api.on('didFinishLaunching', () => {
|
|
|
|
this.log.debug('Finished launching %s', this.config.platform)
|
|
|
|
this.startHapDiscovery()
|
|
|
|
})
|
|
|
|
}
|
2022-11-06 13:04:30 +01:00
|
|
|
|
2022-11-06 13:50:39 +01:00
|
|
|
private startHapDiscovery(): void {
|
|
|
|
this.log.debug('Starting HAP discovery')
|
|
|
|
discover({
|
|
|
|
logger: this.log,
|
|
|
|
refreshInterval: this.config.refresh_interval,
|
|
|
|
discoveryTimeout: this.config.discovery_timeout,
|
|
|
|
requestTimeout: this.config.request_timeout,
|
|
|
|
pin: this.config.pin,
|
|
|
|
debug: this.config.debug,
|
|
|
|
})
|
|
|
|
.then((devices) => {
|
2022-11-07 22:31:17 +01:00
|
|
|
const metrics = aggregate(devices, new Date())
|
|
|
|
this.httpServer.updateMetrics(metrics)
|
|
|
|
this.log.debug('HAP discovery completed, %d metrics discovered', metrics.length)
|
2022-11-07 19:30:55 +01:00
|
|
|
this.startHapDiscovery()
|
2022-11-06 13:50:39 +01:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.log.error('HAP discovery error', e)
|
|
|
|
})
|
2022-11-06 13:04:30 +01:00
|
|
|
}
|
|
|
|
}
|