homebridge-prometheus-exporter/src/platform.ts

60 lines
2.4 KiB
TypeScript
Raw Normal View History

import type { 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'
2022-11-14 01:20:53 +01:00
import { hapNodeJsClientDiscover as discover } from './adapters/discovery'
import { type HttpServerController, fastifyServe as serve } from './adapters/http'
2022-11-07 22:31:17 +01:00
import { PrometheusServer } from './prometheus'
2022-11-13 13:34:09 +01:00
import { type 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
private readonly config: Config
2022-11-06 13:04:30 +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
this.config = checkBoundary(ConfigBoundary, config)
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', () => {
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-14 01:20:53 +01:00
this.httpServer = new PrometheusServer(this.config, this.log)
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')
2022-11-14 01:20:53 +01:00
discover({ log: this.log, config: this.config })
2022-11-06 13:50:39 +01:00
.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
}
}