-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobaleCreateFunktionen.js
39 lines (36 loc) · 1.39 KB
/
GlobaleCreateFunktionen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// id muß ein gültiger String sein, alles weitere is optional
async function createFolderAsync(id, name='', desc='') {
return Promise.resolve(await _createObject(id, 'folder', name, desc))
}
async function createChannelAsync(id, name='', desc='') {
return Promise.resolve(await _createObject(id, 'channel', name, desc))
}
async function createDeviceAsync(id, name='', desc='') {
return Promise.resolve(await _createObject(id, 'device', name, desc))
}
async function _createObject(id, typ, name='', desc='') {
try {
if(existsObject(id)) log('Objekt: ' + id + ' existiert bereits!', 'warn');
else {
const obj = {
type: typ,
common: {
name: name,
desc: desc
},
native: {}
}
if (!id || typeof id !== 'string') {
throw new Error('Fehler in _createObject Parameter 1 ist kein string')
}
if (!(id.startsWith('alias.0') || id.startsWith('0_userdata.0') || id.startsWith('mqtt'))) {
throw new Error('Fehler in _createObject Parameter 1: ' + id.split('.').slice(0,2).join('.') + ' nicht erlaubt')
}
await setObjectAsync(id, obj);
}
return Promise.resolve(true);
} catch (error) {
log(error + '!', 'error')
}
return Promise.resolve(false);
}