Skip to content

Commit

Permalink
Merge pull request #199 from NebzHB/alpha
Browse files Browse the repository at this point in the history
Queueing
  • Loading branch information
NebzHB authored Jan 24, 2024
2 parents 5831593 + 0c2482d commit 44790fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
33 changes: 33 additions & 0 deletions lib/jeedom-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
/* jshint esversion: 6,node: true */
'use strict';
const axios = require('axios');
const async = require('async');
var DEV_DEBUG;
const USE_QUEUES=1; // 0 = NO, or 1 or 2 etc for the concurrent tasks

function JeedomClient(url, apikey, Plateform, myPlugin) {
this.apikey = apikey;
Expand All @@ -26,6 +28,17 @@ function JeedomClient(url, apikey, Plateform, myPlugin) {
this.log = this.Plateform.log;
this.myPlugin = myPlugin || "mobile";
DEV_DEBUG = Plateform.DEV_DEBUG || false;
this.queue = async.queue((task, callback) => {
if(task.type==='cmd') {
this._executeDeviceAction(task.ID, task.action, task.param)
.then((result) => callback(null, result))
.catch((err) => callback(err, null));
} else if(task.type==='scenario') {
this._executeScenarioAction(task.ID, task.action)
.then((result) => callback(null, result))
.catch((err) => callback(err, null));
}
}, ((USE_QUEUES===0)?1:USE_QUEUES));
}

JeedomClient.prototype.getModel = function() {
Expand Down Expand Up @@ -220,6 +233,16 @@ JeedomClient.prototype.updateModelInfo = function(ID,value,internal=false) {
};

JeedomClient.prototype.executeDeviceAction = function(ID, action, param) {
if(USE_QUEUES === 0) { return this._executeDeviceAction(ID, action, param); }
return new Promise((resolve, reject) => {
this.queue.push({ID, action, param, type:'cmd'}, (err, result) => {
if (err) { reject(err); }
else { resolve(result); }
});
});
};

JeedomClient.prototype._executeDeviceAction = function(ID, action, param) {
var that = this;
var options = {};
var url = that.url;
Expand Down Expand Up @@ -257,6 +280,16 @@ JeedomClient.prototype.executeDeviceAction = function(ID, action, param) {
};

JeedomClient.prototype.executeScenarioAction = function(ID, action) {
if(USE_QUEUES === 0) { return this._executeScenarioAction(ID, action); }
return new Promise((resolve, reject) => {
this.queue.push({ID, action, type:'scenario'}, (err, result) => {
if (err) { reject(err); }
else { resolve(result); }
});
});
};

JeedomClient.prototype._executeScenarioAction = function(ID, action) {
var that = this;
var url = that.url;

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nebz/homebridge-jeedom",
"version": "v1.7.0",
"cust_serial": "0008",
"cust_serial": "0009",
"description": "Homebridge plugin for Jeedom ",
"main": "index.js",
"private":true,
Expand All @@ -25,8 +25,9 @@
"Siri"
],
"dependencies": {
"async": "^3.2.5",
"axios": ">=1.6.5",
"express": "^4.18.2",
"express": "^4.18.2",
"fakegato-history": "NebzHB/fakegato-history#master"
},
"devDependencies": {
Expand Down

0 comments on commit 44790fe

Please sign in to comment.