From abc540b40af6c8d63c80c88ac8e85ea7098ccf11 Mon Sep 17 00:00:00 2001 From: Nebz <28622481+NebzHB@users.noreply.github.com> Date: Wed, 24 Jan 2024 13:43:46 +0100 Subject: [PATCH] Queues pour cmd et scenarios --- lib/jeedom-api.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/jeedom-api.js b/lib/jeedom-api.js index 7de41bb..c1a4e5b 100755 --- a/lib/jeedom-api.js +++ b/lib/jeedom-api.js @@ -16,6 +16,7 @@ /* jshint esversion: 6,node: true */ 'use strict'; const axios = require('axios'); +const async = require('async'); var DEV_DEBUG; function JeedomClient(url, apikey, Plateform, myPlugin) { @@ -26,6 +27,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)); + } + }, 1); } JeedomClient.prototype.getModel = function() { @@ -220,6 +232,15 @@ JeedomClient.prototype.updateModelInfo = function(ID,value,internal=false) { }; JeedomClient.prototype.executeDeviceAction = function(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 +278,15 @@ JeedomClient.prototype.executeDeviceAction = function(ID, action, param) { }; JeedomClient.prototype.executeScenarioAction = function(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;