diff --git a/lib/melcloudDevice.js b/lib/melcloudDevice.js index 153375bc..c65c4f31 100644 --- a/lib/melcloudDevice.js +++ b/lib/melcloudDevice.js @@ -3,6 +3,7 @@ const request = require("request"); const commonDefines = require("./commonDefines"); const JSONHelper = require("./jsonHelper"); +const HttpStatus = require("http-status-codes"); let gthat = null; // pointer to "this" from main.js/MelCloud instance @@ -562,12 +563,25 @@ class MelcloudDevice { "X-MitsContextKey": gthis.platform.contextKey } }, function handleDeviceInfoResponse(err, response) { - if (err || response.body.search("") != -1) { - gthat.log.error("There was a problem getting info from: " + url); + if (err) { + gthat.log.error("There was a problem getting device data from: " + url); gthat.log.error("Error: " + err); gthis.airInfo = null; } + else if(!response || response.body.search("") != -1) { + gthat.log.error("There was a problem receiving the response from: " + url); + gthis.airInfo = null; + } else { + const statusCode = response.statusCode; + gthat.log.debug("Received response from: " + url + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")"); + + if(statusCode != HttpStatus.OK) { + gthis.airInfo = null; + gthat.log.error("Invalid HTTP status code (see line above). Getting device data failed!"); + return; + } + const responseBoy = response.body; gthis.airInfo = JSONHelper.JSONHelper.ParseCloudResponse(responseBoy, gthat); @@ -668,7 +682,20 @@ class MelcloudDevice { gthat.log.error("There was a problem setting info to: " + url); gthat.log.error(err); } + else if(!response) { + gthat.log.error("There was a problem receiving the response from: " + url); + gthis.airInfo = null; + } else { + const statusCode = response.statusCode; + gthat.log.debug("Received response from: " + url + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")"); + + if(statusCode != HttpStatus.OK) { + gthis.airInfo = null; + gthat.log.error("Invalid HTTP status code (see line above). Changing device option failed!"); + return; + } + const responseBoy = response.body; const jsonObject = JSONHelper.JSONHelper.ParseCloudResponse(responseBoy, gthat); diff --git a/lib/melcloudPlatform.js b/lib/melcloudPlatform.js index 54fd88a6..1f33e902 100644 --- a/lib/melcloudPlatform.js +++ b/lib/melcloudPlatform.js @@ -3,6 +3,7 @@ const request = require("request"); const MelCloudDevice = require("./melcloudDevice"); const JSONHelper = require("./jsonHelper"); +const HttpStatus = require("http-status-codes"); let gthat = null; // pointer to "this" from main.js/MelCloud instance let gthis = null; // pointer to "this" from MelcloudPlatform @@ -51,7 +52,22 @@ class MelcloudPlatform { gthis.isConnected = false; gthat.setAdapterConnectionState(false); } + else if(!response) { + gthat.log.error("There was a problem receiving the response from: " + loginUrl); + gthis.isConnected = false; + gthat.setAdapterConnectionState(false); + } else { + const statusCode = response.statusCode; + gthat.log.debug("Received response from: " + loginUrl + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")"); + + if(statusCode != HttpStatus.OK) { + gthis.isConnected = false; + gthat.setAdapterConnectionState(false); + gthat.log.error("Invalid HTTP status code (see line above). Login failed!"); + return; + } + const r = JSONHelper.JSONHelper.ParseCloudResponse(response.body, gthat); if (r.LoginData == null) { @@ -101,7 +117,22 @@ class MelcloudPlatform { gthis.isConnected = false; gthat.setAdapterConnectionState(false); } + else if(!response) { + gthat.log.error("There was a problem receiving the response from: " + url); + gthis.isConnected = false; + gthat.setAdapterConnectionState(false); + } else { + const statusCode = response.statusCode; + gthat.log.debug("Received response from: " + url + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")"); + + if(statusCode != HttpStatus.OK) { + gthis.isConnected = false; + gthat.setAdapterConnectionState(false); + gthat.log.error("Invalid HTTP status code (see line above). Getting devices failed!"); + return; + } + gthis.isConnected = true; gthat.setAdapterConnectionState(true); const responseBody = response.body; diff --git a/package.json b/package.json index 536cfa51..9367388f 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ }, "dependencies": { "@iobroker/adapter-core": "^2.3.1", - "request": "^2.88.2" + "request": "^2.88.2", + "http-status-codes": "^1.4.0" }, "devDependencies": { "@iobroker/testing": "^2.2.0",