From a69eb444fa94b7b0af7b5ad3a3690a70ceaea2db Mon Sep 17 00:00:00 2001 From: Sebastian Maier Date: Mon, 19 Oct 2020 14:29:10 +0200 Subject: [PATCH] Add support for direct cache updates With this addition, updates triggered by Homebridge can directly update the last value cached --- index.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 27ad802..e5a03c3 100755 --- a/index.js +++ b/index.js @@ -9,7 +9,8 @@ var fs = require('fs'); var path = require('path'); var SmartHomeNGConnection = require("./SmartHomeNGConnection.js").SmartHomeNGConnection; -var milliTimeout = 300; // used to block responses while swiping +//var milliTimeout = 300; // used to block responses while swiping +var directCacheUpdate = true; var monitoring = []; var colorOn = "\x1b[30;47m"; var colorOff = "\x1b[0m"; @@ -287,6 +288,22 @@ SmartHomeNGAccessory.prototype = { callback(); }, +/** method for direct cache update (optional) + * (this is triggered by updates from Homebridge to update lastValue directly) + * + */ + direct_update: function(item, value) { + if (directCacheUpdate) { + //this.log("DIRECT_UPDATE: item " + item + " with value " + value); + for (var i = 0; i < monitoring.length; i++) { + // iterate through all registered addresses + if (monitoring[i].item == item) { + monitoring[i].lastValue = value; // or only invalidate here and read from SmartHomeNG again ? + } + } + } + }, + /** set methods used for creating callbacks * */ @@ -302,6 +319,7 @@ SmartHomeNGAccessory.prototype = { } this.log("[" + this.name + "] Setting " + shngitem + (inverted ? " (inverted)":"") + " boolean to %s", numericValue); this.shngcon.setValue(shngitem, numericValue); + this.direct_update(shngitem, numericValue); if (callback) callback(); } }, @@ -321,6 +339,7 @@ SmartHomeNGAccessory.prototype = { } this.log("[" + this.name + "] Setting " + shngitem + " percentage to %s", numericValue); this.shngcon.setValue(shngitem, numericValue); + this.direct_update(shngitem, numericValue); if (callback) callback(); } }, @@ -343,6 +362,7 @@ SmartHomeNGAccessory.prototype = { numericValue = numericValue / 3.6; //convert Angle to Percentage (0-100) this.shngcon.setValue(shngitem, numericValue); + this.direct_update(shngitem, numericValue); if (callback) callback(); } }, @@ -359,6 +379,7 @@ SmartHomeNGAccessory.prototype = { } this.log("["+ this.name +"] Setting " + shngitem + " int to %s", numericValue); this.shngcon.setValue(shngitem, numericValue); + this.direct_update(shngitem, numericValue); if (callback) callback(); } }, @@ -375,6 +396,7 @@ SmartHomeNGAccessory.prototype = { } this.log("["+ this.name +"] Setting " + shngitem + " float to %s", numericValue); this.shngcon.setValue(shngitem, numericValue); + this.direct_update(shngitem, numericValue); if (callback) callback(); } },