Update main.js

This commit is contained in:
钟进 2018-09-26 14:18:21 +08:00
parent b6f44cc454
commit bf6c21434e

111
main.js
View File

@ -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--) {