homebridge-prometheus-exporter/src/platform.ts

79 lines
3.2 KiB
TypeScript
Raw Normal View History

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-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-06 13:04:30 +01:00
2022-11-06 13:50:39 +01:00
constructor(public readonly log: Logger, public readonly config: PlatformConfig, public readonly api: API) {
this.log.debug('Initializing platform %s', this.config.platform)
2022-11-06 13:04:30 +01:00
2022-11-06 13:50:39 +01:00
this.configure()
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', this.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 14:26:08 +01:00
this.log.debug('Starting probe 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 14:26:08 +01:00
this.log.error('Failed to start probe 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 configure(): void {
if (this.config.pin !== 'string' || !this.config.pin.match(/^\d{3}-\d{2}-\d{3}$/)) {
this.log.error('"pin" must be defined in config and match format 000-00-000')
}
2022-11-06 13:04:30 +01:00
2022-11-06 13:50:39 +01:00
this.config.debug = this.config.debug ?? false
2022-11-08 14:26:08 +01:00
this.config.port = this.config.port ?? 36123
2022-11-08 14:28:13 +01:00
this.config.prefix = this.config.prefix ?? 'homebridge'
2022-11-06 13:50:39 +01:00
this.config.refresh_interval = this.config.refresh_interval || 60
this.config.request_timeout = this.config.request_timeout || 10
this.config.discovery_timeout = this.config.discovery_timeout || 20
2022-11-06 13:04:30 +01:00
2022-11-06 13:50:39 +01:00
this.log.debug('Configuration materialized: %o', this.config)
}
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
}
}