Skip to content

Commit

Permalink
- implement queue mechanism for sending requests to cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
Black-Thunder committed Jun 27, 2020
1 parent c413192 commit 15c0db8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,9 @@ Documentation:

## Changelog

### 1.0.6-pre1 27.06.2020
* (Black-Thunder) implement queue mechanism for sending requests to cloud

### 1.0.5 21.06.2020
* (Black-Thunder) added more checks when processing HTTP response
* (Black-Thunder) corrected typo in subscribeStates()
Expand Down
2 changes: 1 addition & 1 deletion io-package.json
@@ -1,7 +1,7 @@
{
"common": {
"name": "melcloud",
"version": "1.0.5",
"version": "1.0.6-pre1",
"news": {
"1.0.5": {
"en": "technical optimizations",
Expand Down
95 changes: 60 additions & 35 deletions lib/melcloudDevice.js
Expand Up @@ -12,6 +12,8 @@ class MelcloudDevice {
gthat = that;
this.platform = null;
this.airInfo = null;
this.requestQueue = [];
this.currentRequestExecution = 0;

// Info
this.id = -1;
Expand Down Expand Up @@ -548,51 +550,66 @@ class MelcloudDevice {
if (gthis.airInfo != null) {
gthat.log.debug("Data already available for: " + gthis.id + " (" + gthis.name + ")");
callback && callback(deviceOption, value, gthis);

if (gthis.requestQueue.length) {
const args = gthis.requestQueue.shift();
gthat.log.debug("Dequeuing remote request for device option '" + args[1].id + "' with value '" + (args[2].value != undefined ? args[2].value : args[2]) + "'...");
gthis.getDeviceInfo.apply(gthis, args);
}

return;
}

gthat.log.info("Getting device data for " + gthis.id + " (" + gthis.name + ")");

const url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/Get?id=" + gthis.id + "&buildingID=" + gthis.buildingId;
const method = "get";
if (gthis.currentRequestExecution < 1) {
gthis.currentRequestExecution++;

request({
url: url,
method: method,
headers: {
"X-MitsContextKey": gthis.platform.contextKey
}
}, function handleDeviceInfoResponse(err, response) {
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("<!DOCTYPE html>") != -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) + ")");
const url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/Get?id=" + gthis.id + "&buildingID=" + gthis.buildingId;
const method = "get";

if(statusCode != HttpStatus.OK) {
request({
url: url,
method: method,
headers: {
"X-MitsContextKey": gthis.platform.contextKey
}
}, function handleDeviceInfoResponse(err, response) {
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("<!DOCTYPE html>") != -1) {
gthat.log.error("There was a problem receiving the response from: " + url);
gthis.airInfo = null;
gthat.log.error("Invalid HTTP status code (" + statusCode + " - " + HttpStatus.getStatusText(statusCode) + "). Getting device data failed!");
return;
}
else {
const statusCode = response.statusCode;
gthat.log.debug("Received response from: " + url + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")");

const responseBoy = response.body;
gthis.airInfo = JSONHelper.JSONHelper.ParseCloudResponse(responseBoy, gthat);
if (statusCode != HttpStatus.OK) {
gthis.airInfo = null;
gthat.log.error("Invalid HTTP status code (" + statusCode + " - " + HttpStatus.getStatusText(statusCode) + "). Getting device data failed!");
return;
}

// Cache airInfo data for 1 minute
setTimeout(function () {
gthis.airInfo = null;
}, 60 * 1000);
const responseBoy = response.body;
gthis.airInfo = JSONHelper.JSONHelper.ParseCloudResponse(responseBoy, gthat);

callback && callback(deviceOption, value, gthis);
}
});
// Cache airInfo data for 1 minute
setTimeout(function () {
gthis.airInfo = null;
}, 60 * 1000);

callback && callback(deviceOption, value, gthis);
}
});
}
else {
gthat.log.debug("Queing remote request for '" + deviceOption.id + "' with value '" + (value.value != undefined ? value.value : value) + "'...");
gthis.requestQueue.push(arguments);
}
}

setDevice(deviceOption, value, gthis) {
Expand Down Expand Up @@ -682,15 +699,15 @@ class MelcloudDevice {
gthat.log.error("There was a problem setting info to: " + url);
gthat.log.error(err);
}
else if(!response) {
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) {
if (statusCode != HttpStatus.OK) {
gthis.airInfo = null;
gthat.log.error("Invalid HTTP status code (" + statusCode + " - " + HttpStatus.getStatusText(statusCode) + "). Changing device option failed!");
return;
Expand All @@ -710,6 +727,14 @@ class MelcloudDevice {
gthis.vaneHorizontalDirection = jsonObject.VaneHorizontal;
gthis.deviceOnline = !jsonObject.Offline;
gthis.Update(); // write updated values

gthis.currentRequestExecution--;

if (gthis.requestQueue.length) {
const args = gthis.requestQueue.shift();
gthat.log.debug("Dequeuing remote request for device option '" + args[1].id + "' with value '" + (args[2].value != undefined ? args[2].value : args[2]) + "'");
gthis.getDeviceInfo.apply(gthis, args);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "iobroker.melcloud",
"version": "1.0.5",
"version": "1.0.6-pre1",
"description": "melcloud",
"author": {
"name": "black-thunder",
Expand Down

0 comments on commit 15c0db8

Please sign in to comment.