Skip to content

Commit

Permalink
Code enhancements
Browse files Browse the repository at this point in the history
- Actions that have failed will have its error message logged out.
- Plugin will now accurately detect failed logins.
- Fixed an issue where accessing the device status will not properly "de-authenticate" on failure.
  • Loading branch information
mrjackyliang committed Nov 18, 2019
1 parent 13bc76b commit 6c6d7e1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
58 changes: 37 additions & 21 deletions adt-pulse.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,18 @@ Pulse.prototype.login = function () {
if (error || !regex.test(responsePath)) {
authenticated = false;

let errorMessage = body.match(/<div id="warnMsgContents" class="p_signinWarning">(.*?)<\/div>/g)[0];
let errorMessageNoBreak = (errorMessage) ? errorMessage.replace(/<br\/>/ig, " ") : "";
let errorMessageNoHTML = errorMessageNoBreak.replace(/(<([^>]+)>)/ig, "");

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

deferred.reject({
"action": "LOGIN",
"success": false,
"info": {
"error": error,
"response": {
"body": body,
},
"message": (errorMessageNoHTML) ? errorMessageNoHTML : undefined,
},
});
} else {
Expand Down Expand Up @@ -282,16 +284,18 @@ Pulse.prototype.getDeviceStatus = function () {
if (error || !regex.test(responsePath)) {
authenticated = false;

let errorMessage = body.match(/<div id="warnMsgContents" class="p_signinWarning">(.*?)<\/div>/g)[0];
let errorMessageNoBreak = (errorMessage) ? errorMessage.replace(/<br\/>/ig, " ") : "";
let errorMessageNoHTML = errorMessageNoBreak.replace(/(<([^>]+)>)/ig, "");

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

deferred.reject({
"action": "GET_DEVICE_INFO",
"success": false,
"info": {
"error": error,
"response": {
"body": body,
},
"message": (errorMessageNoHTML) ? errorMessageNoHTML : undefined,
},
});
} else {
Expand Down Expand Up @@ -323,16 +327,20 @@ Pulse.prototype.getDeviceStatus = function () {
that.consoleLogger(`ADT Pulse: Response path matches -> ${regex.test(responsePath)}`, "log");

if (error || !regex.test(responsePath) || body.indexOf("<html") > -1) {
authenticated = false;

let errorMessage = body.match(/<div id="warnMsgContents" class="p_signinWarning">(.*?)<\/div>/g)[0];
let errorMessageNoBreak = (errorMessage) ? errorMessage.replace(/<br\/>/ig, " ") : "";
let errorMessageNoHTML = errorMessageNoBreak.replace(/(<([^>]+)>)/ig, "");

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

deferred.reject({
"action": "GET_DEVICE_STATUS",
"success": false,
"info": {
"error": error,
"response": {
"body": body,
},
"message": (errorMessageNoHTML) ? errorMessageNoHTML : undefined,
},
});
} else {
Expand Down Expand Up @@ -466,16 +474,18 @@ Pulse.prototype.setDeviceStatus = function (armState, arm) {
if (error || !regex.test(response.request.path)) {
authenticated = false;

let errorMessage = body.match(/<div id="warnMsgContents" class="p_signinWarning">(.*?)<\/div>/g)[0];
let errorMessageNoBreak = (errorMessage) ? errorMessage.replace(/<br\/>/ig, " ") : "";
let errorMessageNoHTML = errorMessageNoBreak.replace(/(<([^>]+)>)/ig, "");

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

deferred.reject({
"action": "SET_DEVICE_STATUS",
"success": false,
"info": {
"error": error,
"response": {
"body": body,
},
"message": (errorMessageNoHTML) ? errorMessageNoHTML : undefined,
},
});
} else {
Expand Down Expand Up @@ -514,16 +524,18 @@ Pulse.prototype.setDeviceStatus = function (armState, arm) {
if (error || !regex.test(response.request.path)) {
authenticated = false;

let errorMessage = body.match(/<div id="warnMsgContents" class="p_signinWarning">(.*?)<\/div>/g)[0];
let errorMessageNoBreak = (errorMessage) ? errorMessage.replace(/<br\/>/ig, " ") : "";
let errorMessageNoHTML = errorMessageNoBreak.replace(/(<([^>]+)>)/ig, "");

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

deferred.reject({
"action": "SET_DEVICE_STATUS",
"success": false,
"info": {
"error": error,
"response": {
"body": body,
},
"message": (errorMessageNoHTML) ? errorMessageNoHTML : undefined,
},
});
} else {
Expand Down Expand Up @@ -610,16 +622,18 @@ Pulse.prototype.getZoneStatus = function () {
if (error || !regex.test(response.request.path) || body.indexOf("<html") > -1) {
authenticated = false;

let errorMessage = body.match(/<div id="warnMsgContents" class="p_signinWarning">(.*?)<\/div>/g)[0];
let errorMessageNoBreak = (errorMessage) ? errorMessage.replace(/<br\/>/ig, " ") : "";
let errorMessageNoHTML = errorMessageNoBreak.replace(/(<([^>]+)>)/ig, "");

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

deferred.reject({
"action": "GET_ZONE_STATUS",
"success": false,
"info": {
"error": error,
"response": {
"body": body,
},
"message": (errorMessageNoHTML) ? errorMessageNoHTML : undefined,
},
});
} else {
Expand Down Expand Up @@ -719,16 +733,18 @@ Pulse.prototype.performPortalSync = function () {
if (error || !regex.test(responsePath) || body.indexOf("<html") > -1) {
authenticated = false;

let errorMessage = body.match(/<div id="warnMsgContents" class="p_signinWarning">(.*?)<\/div>/g)[0];
let errorMessageNoBreak = (errorMessage) ? errorMessage.replace(/<br\/>/ig, " ") : "";
let errorMessageNoHTML = errorMessageNoBreak.replace(/(<([^>]+)>)/ig, "");

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

deferred.reject({
"action": "SYNC",
"success": false,
"info": {
"error": error,
"response": {
"body": body,
},
"message": (errorMessageNoHTML) ? errorMessageNoHTML : undefined,
},
});
} else {
Expand Down
31 changes: 21 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ ADTPulsePlatform.prototype.portalSync = function () {
});
})
.catch((error) => {
let action = _.get(error, "action");
let action = _.get(error, "action");
let message = _.get(error, "info.message");

switch (action) {
case "GET_DEVICE_INFO":
Expand All @@ -671,8 +672,10 @@ ADTPulsePlatform.prototype.portalSync = function () {
break;
}

// Error response object.
this.logMessage(error, 40);
// Show error message.
if (message) {
this.logMessage(message, 10);
}
});

// Update sync code.
Expand All @@ -683,11 +686,14 @@ ADTPulsePlatform.prototype.portalSync = function () {
this.isSyncing = false;
})
.catch((error) => {
let action = _.get(error, "action");
let action = _.get(error, "action");
let message = _.get(error, "info.message");

switch (action) {
case "LOGIN":
this.failedLoginTimes++;
if (message.match(/(Sign In unsuccessful\.)/g)) {
this.failedLoginTimes++;
}

if (this.failedLoginTimes > 1) {
this.logMessage("Login failed more than once. Portal sync terminated.", 10);
Expand All @@ -706,8 +712,10 @@ ADTPulsePlatform.prototype.portalSync = function () {
break;
}

// Error response object.
this.logMessage(error, 40);
// Show error message.
if (message) {
this.logMessage(message, 10);
}

this.isSyncing = false;
});
Expand Down Expand Up @@ -1056,7 +1064,8 @@ ADTPulsePlatform.prototype.setDeviceStatus = function (accessory, arm, callback)
}, this.syncInterval * 1000);
})
.catch((error) => {
let action = _.get(error, "action");
let action = _.get(error, "action");
let message = _.get(error, "info.message");

switch (action) {
case "SET_DEVICE_STATUS":
Expand All @@ -1070,8 +1079,10 @@ ADTPulsePlatform.prototype.setDeviceStatus = function (accessory, arm, callback)
break;
}

// Error response object.
this.logMessage(error, 40);
// Show error message.
if (message) {
this.logMessage(message, 10);
}

callback(null);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-adt-pulse",
"version": "1.3.2",
"version": "1.3.3",
"description": "Homebridge security system platform for ADT Pulse",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 6c6d7e1

Please sign in to comment.