Skip to content

Commit

Permalink
Code enhancements
Browse files Browse the repository at this point in the history
- Improved Web Portal version message grammar.
- Log levels with 40 or 50 require Homebridge Debug Mode to be enabled.
- Debug mode now prints error response objects passed on from the ADT Pulse script.
- Enforced getDeviceStatus response checks.

DEV ENHANCEMENTS
- Improved package.json scripts.
- Format zone status "glass" device comments.
- Changed constructor name.
  • Loading branch information
mrjackyliang committed Nov 4, 2019
1 parent 4407213 commit 7e63c19
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 85 deletions.
2 changes: 1 addition & 1 deletion adt-pulse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ if (debugValue === "true") {
/**
* Initialize ADT Pulse function.
*
* @type {pulse}
* @type {Pulse}
*
* @since 1.0.0
*/
Expand Down
126 changes: 53 additions & 73 deletions adt-pulse.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let lastKnownVersion = "";
*
* @since 1.0.0
*/
pulse = function (options) {
Pulse = function (options) {
this.username = _.get(options, "username", "");
this.password = _.get(options, "password", "");
this.debug = _.get(options, "debug", false);
Expand All @@ -54,7 +54,7 @@ pulse = function (options) {
*
* @since 1.0.0
*/
pulse.prototype.login = function () {
Pulse.prototype.login = function () {
let deferred = q.defer();
let that = this;

Expand Down Expand Up @@ -126,18 +126,15 @@ pulse.prototype.login = function () {

that.consoleLogger("ADT Pulse: Login failed.", "error");

if (error) {
that.consoleLogger(`ADT Pulse: Error -> ${error}`, "error");
}

if (body) {
that.consoleLogger(`ADT Pulse: Body -> ${body}`, "log");
}

deferred.reject({
"action": "LOGIN",
"success": false,
"info": null,
"info": {
"error": error,
"response": {
"body": body,
},
},
});
} else {
let version = response.request.path.match(regex)[2];
Expand Down Expand Up @@ -176,14 +173,15 @@ pulse.prototype.login = function () {
return deferred.promise;
};

// noinspection JSUnusedGlobalSymbols
/**
* ADT Pulse logout.
*
* @returns {Q.Promise<Object>}
*
* @since 1.0.0
*/
pulse.prototype.logout = function () {
Pulse.prototype.logout = function () {
let deferred = q.defer();
let that = this;

Expand Down Expand Up @@ -247,7 +245,7 @@ pulse.prototype.logout = function () {
*
* @since 1.0.0
*/
pulse.prototype.getDeviceStatus = function () {
Pulse.prototype.getDeviceStatus = function () {
let deferred = q.defer();
let that = this;

Expand Down Expand Up @@ -284,18 +282,15 @@ pulse.prototype.getDeviceStatus = function () {

that.consoleLogger("ADT Pulse: Get device information failed.", "error");

if (error) {
that.consoleLogger(`ADT Pulse: Error -> ${error}`, "error");
}

if (body) {
that.consoleLogger(`ADT Pulse: Body -> ${body}`, "log");
}

deferred.reject({
"action": "GET_DEVICE_INFO",
"success": false,
"info": null,
"info": {
"error": error,
"response": {
"body": body,
},
},
});
} else {
let $ = cheerio.load(body);
Expand Down Expand Up @@ -325,21 +320,18 @@ pulse.prototype.getDeviceStatus = function () {
that.consoleLogger(`ADT Pulse: Response path -> ${responsePath}`, "log");
that.consoleLogger(`ADT Pulse: Response path matches -> ${regex.test(responsePath)}`, "log");

if (error || !regex.test(responsePath)) {
if (error || !regex.test(responsePath) || body.indexOf("<html") > -1) {
that.consoleLogger("ADT Pulse: Get device status failed.", "error");

if (error) {
that.consoleLogger(`ADT Pulse: Error -> ${error}`, "error");
}

if (body) {
that.consoleLogger(`ADT Pulse: Body -> ${body}`, "log");
}

deferred.reject({
"action": "GET_DEVICE_STATUS",
"success": false,
"info": null,
"info": {
"error": error,
"response": {
"body": body,
},
},
});
} else {
let $ = cheerio.load(body);
Expand Down Expand Up @@ -411,7 +403,7 @@ pulse.prototype.getDeviceStatus = function () {
*
* @since 1.0.0
*/
pulse.prototype.setDeviceStatus = function (armState, arm) {
Pulse.prototype.setDeviceStatus = function (armState, arm) {
let deferred = q.defer();
let that = this;

Expand Down Expand Up @@ -471,18 +463,15 @@ pulse.prototype.setDeviceStatus = function (armState, arm) {

that.consoleLogger(`ADT Pulse: Set device status to ${arm} failed.`, "error");

if (error) {
that.consoleLogger(`ADT Pulse: Error -> ${error}`, "error");
}

if (body) {
that.consoleLogger(`ADT Pulse: Body -> ${body}`, "log");
}

deferred.reject({
"action": "SET_DEVICE_STATUS",
"success": false,
"info": null,
"info": {
"error": error,
"response": {
"body": body,
},
},
});
} else {
let $ = cheerio.load(body);
Expand Down Expand Up @@ -522,18 +511,15 @@ pulse.prototype.setDeviceStatus = function (armState, arm) {

that.consoleLogger(`ADT Pulse: Set device status to ${arm} failed.`, "error");

if (error) {
that.consoleLogger(`ADT Pulse: Error -> ${error}`, "error");
}

if (body) {
that.consoleLogger(`ADT Pulse: Body -> ${body}`, "log");
}

deferred.reject({
"action": "SET_DEVICE_STATUS",
"success": false,
"info": null,
"info": {
"error": error,
"response": {
"body": body,
},
},
});
} else {
that.consoleLogger(`ADT Pulse: Set device status to ${arm} success.`, "log");
Expand Down Expand Up @@ -578,7 +564,7 @@ pulse.prototype.setDeviceStatus = function (armState, arm) {
*
* @since 1.0.0
*/
pulse.prototype.getZoneStatus = function () {
Pulse.prototype.getZoneStatus = function () {
let deferred = q.defer();
let that = this;

Expand Down Expand Up @@ -615,18 +601,15 @@ pulse.prototype.getZoneStatus = function () {

that.consoleLogger("ADT Pulse: Get zone status failed.", "error");

if (error) {
that.consoleLogger(`ADT Pulse: Error -> ${error}`, "error");
}

if (body) {
that.consoleLogger(`ADT Pulse: Body -> ${body}`, "log");
}

deferred.reject({
"action": "GET_ZONE_STATUS",
"success": false,
"info": null,
"info": {
"error": error,
"response": {
"body": body,
},
},
});
} else {
let allDevices = JSON.parse(body)["items"];
Expand Down Expand Up @@ -689,7 +672,7 @@ pulse.prototype.getZoneStatus = function () {
*
* @since 1.0.0
*/
pulse.prototype.performPortalSync = function () {
Pulse.prototype.performPortalSync = function () {
let deferred = q.defer();
let that = this;

Expand Down Expand Up @@ -727,18 +710,15 @@ pulse.prototype.performPortalSync = function () {

that.consoleLogger("ADT Pulse: Failed to sync with portal.", "error");

if (error) {
that.consoleLogger(`ADT Pulse: Error -> ${error}`, "error");
}

if (body) {
that.consoleLogger(`ADT Pulse: Body -> ${body}`, "log");
}

deferred.reject({
"action": "SYNC",
"success": false,
"info": null,
"info": {
"error": error,
"response": {
"body": body,
},
},
});
} else {
/**
Expand Down Expand Up @@ -777,7 +757,7 @@ pulse.prototype.performPortalSync = function () {
* @param {string} content - The message or content being recorded into the logs.
* @param {string} type - Can be "error", "warn", or "log".
*/
pulse.prototype.consoleLogger = function (content, type) {
Pulse.prototype.consoleLogger = function (content, type) {
if (this.debug) {
switch (type) {
case "error":
Expand All @@ -793,4 +773,4 @@ pulse.prototype.consoleLogger = function (content, type) {
}
};

module.exports = pulse;
module.exports = Pulse;
25 changes: 18 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ ADTPulsePlatform.prototype.portalSync = function () {
let supportedVersion = ["16.0.0-131", "17.0.0-69"];

if (version !== undefined && !supportedVersion.includes(version) && version !== this.sessionVersion) {
this.logMessage(`Web Portal version ${version} does not match ${supportedVersion.join(", ")}.`, 20);
this.logMessage(`Web Portal version ${version} does not match ${supportedVersion.join(" or ")}.`, 20);
}

// Bind version to session so message doesn't keep showing up.
Expand Down Expand Up @@ -670,6 +670,9 @@ ADTPulsePlatform.prototype.portalSync = function () {
this.logMessage(`Action failed on ${action}.`, 10);
break;
}

// Error response object.
this.logMessage(error, 40);
});

// Update sync code.
Expand Down Expand Up @@ -703,6 +706,9 @@ ADTPulsePlatform.prototype.portalSync = function () {
break;
}

// Error response object.
this.logMessage(error, 40);

this.isSyncing = false;
});
} else {
Expand Down Expand Up @@ -1032,6 +1038,9 @@ ADTPulsePlatform.prototype.setDeviceStatus = function (accessory, arm, callback)
break;
}

// Error response object.
this.logMessage(error, 40);

callback(null);
});
};
Expand Down Expand Up @@ -1077,10 +1086,10 @@ ADTPulsePlatform.prototype.formatZoneStatus = function (type, state) {
break;
case "glass":
if (state === "devStatOK") {
// No tamper detected.
// No glass broken.
status = false;
} else if (state === "devStatTamper") {
// Tamper detected.
// Glass broken.
status = true;
}
break;
Expand Down Expand Up @@ -1122,14 +1131,17 @@ ADTPulsePlatform.prototype.formatZoneStatus = function (type, state) {
* Manages the messages that will be recorded in the logs.
*
* @param {string|Object} content - The message or content being recorded into the logs.
* @param {number} priority - 10 (error), 20 (warn), 30 (info), 40 (debug), 50 (verbose).
* @param {number} priority - 10 (error), 20 (warn), 30 (info), 40 (debug), 50 (verbose).
*/
ADTPulsePlatform.prototype.logMessage = function (content, priority) {
let logLevel = this.logLevel;
let log = this.log;

if (logLevel >= priority) {
// Messages won't be logged if not within specs.
/**
* Messages won't be logged if not within specs.
* Homebridge Debug Mode must be enabled for priorities 40 and 50.
*/
switch (priority) {
case 10:
log.error(content);
Expand All @@ -1141,11 +1153,10 @@ ADTPulsePlatform.prototype.logMessage = function (content, priority) {
log.info(content);
break;
case 40:
// Homebridge debug must be enabled.
log.debug(content);
break;
case 50:
log(content);
log.debug(content);
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "homebridge-adt-pulse",
"version": "1.2.7",
"version": "1.2.8",
"description": "Homebridge security system platform for ADT Pulse",
"main": "index.js",
"scripts": {
"start": "homebridge --debug --plugin-path $(pwd) && exit 1",
"test": "echo \"Error: no test specified\" && exit 1",
"publish": "npm publish && exit 1"
"start": "homebridge --plugin-path $(pwd) && exit 1",
"start:debug": "homebridge --debug --plugin-path $(pwd) && exit 1",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 7e63c19

Please sign in to comment.