From c88ad98d481a9fd86c23d4ce063cb143d6c42a83 Mon Sep 17 00:00:00 2001 From: HolmesZhao <269258864@qq.com> Date: Sun, 20 Oct 2019 20:38:22 +0800 Subject: [PATCH] Fix the bug when mac address starts with 0 Fix the bug that the gateway accessories cannot be found correctly when the gateway mac address starts with 0, such as buttons cherry picked from the commits (https://github.com/HolmesZhao/homebridge-mi-aqara/commit/13c61a305f4e596f7f9fa0d275f2632bf223b15e and https://github.com/HolmesZhao/homebridge-mi-aqara/commit/13c61a305f4e596f7f9fa0d275f2632bf223b15e ) --- index.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/index.js b/index.js index c3b25d3..50024c9 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,8 @@ const serverMQTTClientPrefix = "/homebridge-mi-aqara"; var PlatformAccessory, Accessory, Service, Characteristic, UUIDGen; +var logOnce = runOnce((e) => { console.log(e) }); + module.exports = function(homebridge) { PlatformAccessory = homebridge.platformAccessory; Accessory = homebridge.hap.Accessory; @@ -432,6 +434,12 @@ MiAqaraPlatform.prototype.parseMessage = function(msg, rinfo) { return; } + if (jsonObj['sid'].length < 12) { + jsonObj['sid'] = "0" + jsonObj['sid']; + } + + logOnce('sid_zwy: ' + jsonObj['sid']); + // send mqtt message if(that.mqttClient) { that.sendMQTTMessage4ParseMessage(msg, rinfo); @@ -866,3 +874,17 @@ MiAqaraPlatform.prototype.unregisterPlatformAccessories = function(accessories) that.AccessoryUtil.remove(accessory.UUID); }); } + +function runOnce(fn, context) { //Control the function to be triggered only once + return function () { + try { + fn.apply(context || this, arguments); + } + catch (e) { + // console.error(e); + } + finally { + fn = null; + } + } + }