From bf6c21434ee8baaa7e9e0528a26cdd0f23cf1147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E8=BF=9B?= Date: Wed, 26 Sep 2018 14:18:21 +0800 Subject: [PATCH] Update main.js --- main.js | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/main.js b/main.js index c459435..4f3e0e8 100644 --- a/main.js +++ b/main.js @@ -61,6 +61,8 @@ You can define the same variable name inside a child without produce a conflict let variable = 1234; let objects = {}; +let enums = {}; + // is called when adapter shuts down - callback has to be called under any circumstances! @@ -245,6 +247,115 @@ function pollData() { } + +function addToEnum(enumName, id, callback) { + adapter.getForeignObject(enumName, function (err, obj) { + if (!err && obj) { + let pos = obj.common.members.indexOf(id); + if (pos === -1) { + obj.common.members.push(id); + adapter.setForeignObject(obj._id, obj, function (err) { + if (callback) callback(err); + }); + } else { + if (callback) callback(err); + } + } else { + if (callback) callback(err); + } + }); +} + +function removeFromEnum(enumName, id, callback) { + adapter.getForeignObject(enumName, function (err, obj) { + if (!err && obj) { + let pos = obj.common.members.indexOf(id); + if (pos !== -1) { + obj.common.members.splice(pos, 1); + adapter.setForeignObject(obj._id, obj, function (err) { + if (callback) callback(err); + }); + } else { + if (callback) callback(err); + } + } else { + if (callback) callback(err); + } + }); +} + +function syncEnums(enumGroup, id, newEnumName, callback) { + if (!enums[enumGroup]) { + adapter.getEnum(enumGroup, function (err, _enums) { + enums[enumGroup] = _enums; + syncEnums(enumGroup, id, newEnumName, callback); + }); + return; + } + + // try to find this id in enums + let found = false; + let count = 0; + for (let e in enums[enumGroup]) { + if (enums[enumGroup].hasOwnProperty(e) && + enums[enumGroup][e].common && + enums[enumGroup][e].common.members && + enums[enumGroup][e].common.members.indexOf(id) !== -1) { + if (enums[enumGroup][e]._id !== newEnumName) { + count++; + removeFromEnum(enums[enumGroup][e]._id, id, function () { + if (!--count && typeof callback === 'function') callback(); + }); + } else { + found = true; + } + } + } + if (!found && newEnumName) { + count++; + addToEnum(newEnumName, id, function () { + if (!--count&& typeof callback === 'function') callback(); + }); + } + + if (!count && typeof callback === 'function') callback(); +} + + +function createExtendObject(id, objData, callback) { + adapter.getObject(id, function (err, oldObj) { + if (!err && oldObj) { + adapter.extendObject(id, objData, callback); + } else { + adapter.setObjectNotExists(id, objData, callback); + } + }); +} + +function processTasks(tasks, callback) { + if (!tasks || !tasks.length) { + if (typeof callback === 'function') callback(); + return; + } + let task = tasks.shift(); + if (task.name === 'add') { + createExtendObject(task.id, task.obj, function () { + setTimeout(processTasks, 0, tasks, callback); + }); + } else if (task.name === 'del') { + adapter.delObject(task.id, function () { + setTimeout(processTasks, 0, tasks, callback); + }); + } else if (task.name === 'syncEnums') { + syncEnums('rooms', task.id, task.obj, function () { + setTimeout(processTasks, 0, tasks, callback); + }); + } else { + throw 'Unknown task'; + } +} + + function checkDeviceIds(options, config, deviceIds) { for (let i = config.length - 1; i >= 0; i--) {