|
113 | 113 | var promiseHTTP = require("request-promise-native"); |
114 | 114 | var chalk = require("chalk"); |
115 | 115 | var HSutilities = require("./lib/HomeSeerUtilities"); |
116 | | - |
117 | 116 | var Accessory, Service, Characteristic, UUIDGen; |
118 | | - |
119 | | -var _allAccessories = []; |
120 | | -var _globalHSRefs = []; |
121 | | - _globalHSRefs.pushUnique = function(item) { if (this.indexOf(item) == -1) this.push(item); } |
122 | | -var _pollingStartup = true; // This is used the first time the update procedure is called to ensure all devices get updated on startup |
123 | 117 |
|
124 | | -var _currentHSDeviceStatus = []; |
125 | | -var _allStatusUrl = []; |
126 | | -var _HSValues = []; |
| 118 | +// The following variable is set to "true" the first time HomeSeer is polled. |
| 119 | +// This ensures that all associated HomeKit devices get an .updateValue call during processing of updateAllFromHSData() |
| 120 | +// On subsequent polls, this is set to false and the HomeKit .updateValue function only executes |
| 121 | +// if there has been a (potential) change in the HomeKit value due to a HomeSeer change. |
| 122 | +var _pollingStartup = true; |
| 123 | + |
| 124 | +// Following variable stores the full HomeSeer JSON-ified status data structure. |
| 125 | +// This includes Device data for all of the HomeSeer devices of interest. |
| 126 | +var _currentHSDeviceStatus = []; |
127 | 127 |
|
128 | | -var _accessURL; |
| 128 | +// The next array variable (_HSValues) stores just the value of the associated HomeSeer reference. |
| 129 | +// This is a sparse array with most index values null. |
| 130 | +// The array index corresponds to the HomeSeer reference so _HSValues[211] would be the HomeSeer value for device 211. |
| 131 | +var _HSValues = []; |
129 | 132 |
|
130 | | -var _statusObjects = []; // Holds things that can be changed when HomeSeer values change! |
| 133 | +// The next array variable holds a list of all of the HomeKit HAP Characteristic objects |
| 134 | +// that can be affected by changes occurring at HomeSeer. |
| 135 | +// The array is populated during by the getServices function when a HomeKit device is created. |
| 136 | +// After HomeSeer is polled, each item in this array is analyzed by the updateAllFromHSData() function to determine |
| 137 | +// if it needs to be updated. |
| 138 | +var _statusObjects = []; |
131 | 139 |
|
132 | | -var updateEmitter; |
| 140 | +var HomeSeerHost = ""; |
133 | 141 |
|
134 | 142 | module.exports = function (homebridge) { |
135 | 143 | console.log("homebridge API version: " + homebridge.version); |
@@ -158,7 +166,7 @@ function forceHSValue(ref, level) |
158 | 166 | _HSValues[ref] = level; |
159 | 167 |
|
160 | 168 | // Debugging |
161 | | - console.log("** DEBUG ** - called forceHSValue with reference: %s, level: %s, resulting in new value: %s", ref, level, _HSValues[ref]); |
| 169 | + // console.log("** DEBUG ** - called forceHSValue with reference: %s, level: %s, resulting in new value: %s", ref, level, _HSValues[ref]); |
162 | 170 | } |
163 | 171 |
|
164 | 172 |
|
@@ -249,23 +257,24 @@ HomeSeerPlatform.prototype = { |
249 | 257 |
|
250 | 258 |
|
251 | 259 | /////////////////////// |
| 260 | + var allHSRefs = []; |
| 261 | + allHSRefs.pushUnique = function(item) { if (this.indexOf(item) == -1) this.push(item); } |
252 | 262 |
|
253 | 263 | for (var i = 0; i < this.config.accessories.length; i++) { |
254 | 264 |
|
255 | 265 | refList.push(this.config.accessories[i].ref); |
256 | 266 |
|
257 | | - //Gather all HS References For polling. References in _globalHSRefs can include references that do not |
| 267 | + //Gather all HS References For polling. References in allHSRefs can include references that do not |
258 | 268 | // create a new HomeKit device such as batteries |
259 | | - _globalHSRefs.pushUnique(this.config.accessories[i].ref); |
| 269 | + allHSRefs.pushUnique(this.config.accessories[i].ref); |
260 | 270 |
|
261 | | - if(this.config.accessories[i].batteryRef) _globalHSRefs.pushUnique(this.config.accessories[i].batteryRef); |
| 271 | + if(this.config.accessories[i].batteryRef) allHSRefs.pushUnique(this.config.accessories[i].batteryRef); |
262 | 272 | } // end for |
263 | 273 |
|
264 | 274 | //For New Polling Method to poll all devices at once |
265 | | - _globalHSRefs.sort(); |
266 | | - _allStatusUrl = this.config["host"] + "/JSON?request=getstatus&ref=" + _globalHSRefs.concat(); |
| 275 | + allHSRefs.sort(); |
| 276 | + |
267 | 277 |
|
268 | | - this.log("Retrieve All HomeSeer Device Status URL is " + _allStatusUrl); |
269 | 278 |
|
270 | 279 | var url = this.config["host"] + "/JSON?request=getstatus&ref=" + refList.concat(); |
271 | 280 |
|
@@ -294,11 +303,14 @@ HomeSeerPlatform.prototype = { |
294 | 303 | } //end else. |
295 | 304 |
|
296 | 305 | // This is the new Polling Mechanism to poll all at once. |
297 | | - |
298 | | - updateEmitter = setInterval( function () |
| 306 | + |
| 307 | + var allStatusUrl = this.config["host"] + "/JSON?request=getstatus&ref=" + allHSRefs.concat(); |
| 308 | + this.log("Retrieve All HomeSeer Device Status URL is " + allStatusUrl); |
| 309 | + |
| 310 | + setInterval( function () |
299 | 311 | { |
300 | 312 | // Now do the poll |
301 | | - promiseHTTP({ uri: _allStatusUrl, json:true}) |
| 313 | + promiseHTTP({ uri: allStatusUrl, json:true}) |
302 | 314 | .then( function(json) |
303 | 315 | { |
304 | 316 | _currentHSDeviceStatus = json.Devices; |
@@ -348,9 +360,11 @@ function HomeSeerAccessory(log, platformConfig, accessoryConfig, status) { |
348 | 360 | this.model = status.device_type_string; |
349 | 361 |
|
350 | 362 | this.access_url = platformConfig["host"] + "/JSON?"; |
351 | | - _accessURL = this.access_url; |
352 | | - this.control_url = this.access_url + "request=controldevicebyvalue&ref=" + this.ref + "&value="; |
353 | | - this.status_url = this.access_url + "request=getstatus&ref=" + this.ref; |
| 363 | + |
| 364 | + this.HomeSeerHost = platformConfig["host"]; |
| 365 | + // _accessURL = this.access_url; |
| 366 | + HomeSeerHost = this.HomeSeerHost; |
| 367 | + |
354 | 368 |
|
355 | 369 | if (this.config.name) |
356 | 370 | this.name = this.config.name; |
@@ -379,7 +393,6 @@ HomeSeerAccessory.prototype = { |
379 | 393 |
|
380 | 394 | // Uncomment for Debugging |
381 | 395 | // console.log ("** Debug ** - Called setHSValue with level %s for UUID %s", level, this.UUID); |
382 | | - // console.log ("** Debug ** access_url is %s", _accessURL); |
383 | 396 |
|
384 | 397 | if (!this.UUID) { |
385 | 398 | var error = "*** PROGRAMMING ERROR **** - setHSValue called by something without a UUID"; |
@@ -476,7 +489,7 @@ HomeSeerAccessory.prototype = { |
476 | 489 |
|
477 | 490 | }; |
478 | 491 |
|
479 | | - url = _accessURL + "request=controldevicebyvalue&ref=" + this.HSRef + "&value=" + transmitValue; |
| 492 | + url = HomeSeerHost + "/JSON?request=controldevicebyvalue&ref=" + this.HSRef + "&value=" + transmitValue; |
480 | 493 |
|
481 | 494 | // For debugging |
482 | 495 | //console.log ("Debug - Called setHSValue has URL = %s", url); |
@@ -868,9 +881,6 @@ HomeSeerAccessory.prototype = { |
868 | 881 | .setCharacteristic(Characteristic.SerialNumber, "HS " + this.config.type + " ref " + this.ref); |
869 | 882 | services.push(informationService); |
870 | 883 | // |
871 | | - |
872 | | - |
873 | | - _allAccessories.push(services); |
874 | 884 |
|
875 | 885 | return services; |
876 | 886 | } |
@@ -1103,41 +1113,6 @@ function updateAllFromHSData() |
1103 | 1113 | } // end function |
1104 | 1114 |
|
1105 | 1115 |
|
1106 | | -// The following code was to update a single characteristicObject after it is changed |
1107 | | -// But it doesn't seem to work right, so unused for now! |
1108 | | -/* |
1109 | | -function updateCharacteristic(characteristicObject) |
1110 | | -{ |
1111 | | - if (characteristicObject.HSRef == null) |
1112 | | - { |
1113 | | - console.log("** Programming Error ** - updateCharacteristic passed characteristic object %s with displayName %s but without a HomeSeer reference HSREf ", characteristicObject.UUID, characteristicObject.displayName); |
1114 | | - return; |
1115 | | - } |
1116 | | - |
1117 | | - var updateURL = _accessURL + "request=getstatus&ref=" + characteristicObject.HSRef; |
1118 | | - // console.log("** DEBUG ** -- update URL is %s", url); |
1119 | | - |
1120 | | - promiseHTTP({uri: updateURL, json:true}) |
1121 | | - .then( function(jsonData) { |
1122 | | - |
1123 | | - var thisDevice = jsonData.Devices[0]; |
1124 | | - |
1125 | | - // Debugging - Uncomment following code for debugging |
1126 | | - console.log("Error attempting to update Characteristic %s, with current value %s, and HS Ref", characteristicObject.displayName, characteristicObject.value, characteristicObject.HSRef); |
1127 | | - console.log("** Debug ** - Polling obtained the single device %s with reference %s and value %s", thisDevice, thisDevice.ref, thisDevice.value); |
1128 | | - // End Debugging |
1129 | | -
|
1130 | | - _HSValues[thisDevice.ref] = thisDevice.value; |
1131 | | - updateCharacteristicFromHSData(characteristicObject); |
1132 | | - |
1133 | | - }).catch(function(err) |
1134 | | - { |
1135 | | - console.log("Error attempting to update Characteristic %s, with error %s", characteristicObject.displayName, characteristicObject.UUID, err); |
1136 | | - } |
1137 | | - ); |
1138 | | -} |
1139 | | -*/ |
1140 | | - |
1141 | 1116 | //////////////////// End of Polling HomeSeer Code ///////////////////////////// |
1142 | 1117 |
|
1143 | 1118 | module.exports.platform = HomeSeerPlatform; |
0 commit comments