From 94cfc89187e1ff2c7150e056cc78474ce1fedf8e Mon Sep 17 00:00:00 2001 From: Andi Date: Thu, 2 Jul 2020 19:35:16 +0200 Subject: [PATCH] Only create an mqtt client if a mqtt host is configured (#592) --- src/index.ts | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7e5d5ede..3b89a209 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,7 +29,6 @@ class FfmpegPlatform implements DynamicPlatformPlugin { private config: PlatformConfig; private cameraConfigs: Map = new Map(); // configuration for each camera indexed by uuid private readonly accessories: Array = []; - private name = ''; constructor(log: Logging, config: PlatformConfig, api: API) { this.log = log; @@ -203,9 +202,9 @@ class FfmpegPlatform implements DynamicPlatformPlugin { this.accessories.push(cameraAccessory); } - mqttHandler(): void { + mqttHandler(name: string): void { this.accessories.forEach((accessory: PlatformAccessory) => { - if (accessory.displayName == this.name) { + if (accessory.displayName == name) { this.log('Switch Motion Detect On :', accessory.displayName); const motionSenSor = accessory.getService(hap.Service.MotionSensor); if (motionSenSor) { @@ -216,23 +215,24 @@ class FfmpegPlatform implements DynamicPlatformPlugin { } didFinishLaunching(): void { - const servermqtt = this.config.mqtt || '127.0.0.1'; - const port = this.config.portmqtt || '1883'; - const topics = this.config.topics || 'homebridge/motion'; - this.log('MQTT state message received:'); - const client = mqtt.connect('mqtt://' + servermqtt + ':' + port); - client.on('connect', () => { - this.log('MQTT CONNECTED!'); - }); - client.subscribe(topics); - client.on('message', (topic: string, message: Buffer) => { - const mess = message.toString(); - this.log('MQTT state message received:', mess); - const name = mess.replace('_', ' '); - this.name = name; - this.log('Motion Camera:', this.name); - this.mqttHandler(); - }); + if (this.config.mqtt) { + this.log('Setting up mqtt connection...'); + const servermqtt = this.config.mqtt; + const port = this.config.portmqtt || '1883'; + const topics = this.config.topics || 'homebridge/motion'; + const client = mqtt.connect('mqtt://' + servermqtt + ':' + port); + client.on('connect', () => { + this.log('MQTT CONNECTED!'); + client.subscribe(topics); + }); + client.on('message', (topic: string, message: Buffer) => { + const mess = message.toString(); + this.log('MQTT state message received:', mess); + const name = mess.replace('_', ' '); + this.log('Motion Camera:', name); + this.mqttHandler(name); + }); + } for (const [uuid, cameraConfig] of this.cameraConfigs) { const cameraName = cameraConfig.name;