Skip to content

Commit 9d3cde3

Browse files
authored
Simplify Code (#2)
* Update package.json * Code Cleanup Further reduction in use of global variables. Add comments. Remove some debugging code.
1 parent 32015e5 commit 9d3cde3

File tree

2 files changed

+41
-66
lines changed

2 files changed

+41
-66
lines changed

index.js

Lines changed: 40 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,31 @@
113113
var promiseHTTP = require("request-promise-native");
114114
var chalk = require("chalk");
115115
var HSutilities = require("./lib/HomeSeerUtilities");
116-
117116
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
123117

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 = [];
127127

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 = [];
129132

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 = [];
131139

132-
var updateEmitter;
140+
var HomeSeerHost = "";
133141

134142
module.exports = function (homebridge) {
135143
console.log("homebridge API version: " + homebridge.version);
@@ -158,7 +166,7 @@ function forceHSValue(ref, level)
158166
_HSValues[ref] = level;
159167

160168
// 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]);
162170
}
163171

164172

@@ -249,23 +257,24 @@ HomeSeerPlatform.prototype = {
249257

250258

251259
///////////////////////
260+
var allHSRefs = [];
261+
allHSRefs.pushUnique = function(item) { if (this.indexOf(item) == -1) this.push(item); }
252262

253263
for (var i = 0; i < this.config.accessories.length; i++) {
254264

255265
refList.push(this.config.accessories[i].ref);
256266

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
258268
// create a new HomeKit device such as batteries
259-
_globalHSRefs.pushUnique(this.config.accessories[i].ref);
269+
allHSRefs.pushUnique(this.config.accessories[i].ref);
260270

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);
262272
} // end for
263273

264274
//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+
267277

268-
this.log("Retrieve All HomeSeer Device Status URL is " + _allStatusUrl);
269278

270279
var url = this.config["host"] + "/JSON?request=getstatus&ref=" + refList.concat();
271280

@@ -294,11 +303,14 @@ HomeSeerPlatform.prototype = {
294303
} //end else.
295304

296305
// 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 ()
299311
{
300312
// Now do the poll
301-
promiseHTTP({ uri: _allStatusUrl, json:true})
313+
promiseHTTP({ uri: allStatusUrl, json:true})
302314
.then( function(json)
303315
{
304316
_currentHSDeviceStatus = json.Devices;
@@ -348,9 +360,11 @@ function HomeSeerAccessory(log, platformConfig, accessoryConfig, status) {
348360
this.model = status.device_type_string;
349361

350362
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+
354368

355369
if (this.config.name)
356370
this.name = this.config.name;
@@ -379,7 +393,6 @@ HomeSeerAccessory.prototype = {
379393

380394
// Uncomment for Debugging
381395
// console.log ("** Debug ** - Called setHSValue with level %s for UUID %s", level, this.UUID);
382-
// console.log ("** Debug ** access_url is %s", _accessURL);
383396

384397
if (!this.UUID) {
385398
var error = "*** PROGRAMMING ERROR **** - setHSValue called by something without a UUID";
@@ -476,7 +489,7 @@ HomeSeerAccessory.prototype = {
476489

477490
};
478491

479-
url = _accessURL + "request=controldevicebyvalue&ref=" + this.HSRef + "&value=" + transmitValue;
492+
url = HomeSeerHost + "/JSON?request=controldevicebyvalue&ref=" + this.HSRef + "&value=" + transmitValue;
480493

481494
// For debugging
482495
//console.log ("Debug - Called setHSValue has URL = %s", url);
@@ -868,9 +881,6 @@ HomeSeerAccessory.prototype = {
868881
.setCharacteristic(Characteristic.SerialNumber, "HS " + this.config.type + " ref " + this.ref);
869882
services.push(informationService);
870883
//
871-
872-
873-
_allAccessories.push(services);
874884

875885
return services;
876886
}
@@ -1103,41 +1113,6 @@ function updateAllFromHSData()
11031113
} // end function
11041114

11051115

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-
11411116
//////////////////// End of Polling HomeSeer Code /////////////////////////////
11421117

11431118
module.exports.platform = HomeSeerPlatform;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "homebridge-homeseer-plugin-2018",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Homeseer Plugin for homebridge: https://github.com/jvmahon/homebridge-homeseer",
55
"license": "ISC",
66
"keywords": [

0 commit comments

Comments
 (0)