diff --git a/lib/jeedom-api.js b/lib/jeedom-api.js index 7de41bb..25b82bf 100755 --- a/lib/jeedom-api.js +++ b/lib/jeedom-api.js @@ -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; @@ -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() { @@ -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; @@ -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; diff --git a/package.json b/package.json index 23d7512..0275870 100644 --- a/package.json +++ b/package.json @@ -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, @@ -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": {