-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias_script_V_0_1.js
228 lines (195 loc) · 6.74 KB
/
alias_script_V_0_1.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// script um Zigbee states zu spiegeln und unzubennen
const zigbee = 'zigbee.0';
const target = '0_userdata.0.alias_zigbee'
// Finger weg !
class aliasZigbee {
constructor() {
this.message = `zigbee alias script: Version 0.1, dev: xenon-s Have fun with it!`;
this.dev = 'xenon-s'
this.objName = []; // Namen aller Zigbee Devices
this.arrayTrigger = []; // Array mit allen Trigger Pfaden
this.ids = []; // Array aller Zigbee Ids aus dem Adapter
this.objNew = {}; // Objekt mit den neu zusammengebauten Objekten, die unter userdata angelegt werden
this.config = {};
this.attributes = [];
log(this.message);
loop('start');
};
};
new aliasZigbee();
// get ids from zigbee
/**
* @param {string} cmd
*/
async function loop(cmd) {
if (cmd === 'start') {
createConfig();
} else {
this.ids = await Array.prototype.slice.apply($(`state[id='${zigbee}*']`));
this.objName = await getIDs();
this.objNew = await getName();
this.arrayTrigger = await createDp();
await createTrigger(cmd);
};
};
async function createConfig() {
this.config = {
// refresh_Button: {
// parse: {
// "name": "Refresh Button",
// "type": "boolean",
// "read": false,
// "write": true,
// "role": "button"
// },
// },
attributes: {
parse: {
"name": "Zigbee State Types",
"type": "string",
"read": true,
"write": true,
"role": "state"
},
ini: ['state', 'temperature', 'occupancy']
}
};
// Config Dps erstellen
for (const i in this.config) {
let path = '';
path = `${target}.0_Config.${i}`;
let com = '';
com = JSON.stringify(this.config[i].parse);
// Pruefen ob DPs vorhanden
if (!getObject(path)) {
createStateAsync(path, JSON.parse(com), async () => {
if (i == 'attributes') {
setStateAsync(path, this.config[i].ini.toString(), true)
};
});
}
if (i == 'attributes') {
const result = await getStateAsync(path);
this.attributes = result !== null ? result.val.split(',') || [] : this.config.attributes.ini;
};
// // trigger auf config
on({ id: path, change: "any", ack: false }, async obj => {
if (obj.id === `${target}.0_Config.attributes`) {
this.attributes = obj.state.val.split(',');
const value = obj.state.val;
setState(path, value, true);
};
loop(null);
});
};
setTimeout(() => {
loop(null);
}, 500)
};
// eindeutige device namen aus der id holen
async function getIDs() {
let arrTemp = [];
this.ids.forEach(async data => {
let str = ``;
str = data;
let name = ``
let res = str.lastIndexOf('.')
name = str.slice(0, res);
// namen bereits im array enthalten?
// wenn nicht, hinzufügen
if (!arrTemp.includes(name)) {
arrTemp.push(name)
};
});
return arrTemp;
};
// neues Objekt bauen
async function getName() {
let objId = {};
for (const i in this.objName) {
const data = this.objName[i];
const id = data;
let objTempName = {};
let objOriTemp = {};
// namen aus dem device auslesen
objTempName = await getObjectAsync(data); // objekt auslesen
for (const name in this.attributes) {
let strTemp = ``;
//
if (objTempName.type == 'device') {
strTemp = `${data}.${this.attributes[name]}`;
// nach id suchen und prüfen ob 'angegebene attribute' enthalten sind
if (this.ids.includes(strTemp)) {
objOriTemp = await getObjectAsync(strTemp); // objekt auslesen
if (objOriTemp.type == 'state') {
// objekt neu schreiben und vom User ausgewaehlte DPs speichern
if (objOriTemp.common.name === 'On/off state of the switch' || objOriTemp.common.name === 'Switch state') {
objOriTemp.common.name = 'Switch on/off';
};
const deviceName = `${objOriTemp.common.name} ${objTempName.common.name}`
objId[`${id}.${this.attributes[name]}`] = {
id: id,
name: objTempName.common.name.replace(/ /g, '_'),
nameNew: deviceName,
idState: objOriTemp._id,
common: objOriTemp.common
};
};
};
};
};
};
return objId;
};
// datenpunkte erstellen
async function createDp() {
let arrTemp = [];
for (const i in this.objNew) {
let pathNew = ``;
let com = ``;
pathNew = `${target}.${this.objNew[i].name}.${this.objNew[i].common.name.replace(/ /g, '_')}`;
this.objNew[i].common.name = this.objNew[i].nameNew;
com = JSON.stringify(this.objNew[i].common);
// neue Datenpunkte erzeugen
if (!getObject(pathNew)) {
createStateAsync(pathNew, JSON.parse(com), async () => {
const value = await getStateAsync(this.objNew[i].idState)
if (value != null) {
await setStateAsync(pathNew, value.val, true)
};
});
};
const objTrigger = { triggerAdapter: this.objNew[i].idState, triggerCopy: pathNew }
arrTemp.push(objTrigger);
};
return arrTemp;
};
// trigger erstellen
/**
* @param {string} cmd
*/
async function createTrigger(cmd) {
for (const i in this.arrayTrigger) {
let triggerAdapter = '';
let triggerCopy = '';
triggerAdapter = this.arrayTrigger[i].triggerAdapter;
triggerCopy = this.arrayTrigger[i].triggerCopy;
// "subscriptions" löschen
if (cmd !== 'start') {
if (unsubscribe(triggerAdapter)) {
};
if (unsubscribe(triggerCopy)) {
};
};
// trigger auf adapter.state
on({ id: triggerAdapter, change: "any", ack: true }, async obj => {
const value = obj.state.val;
setState(triggerCopy, value, true);
});
// trigger auf copy.state
on({ id: triggerCopy, change: "any", ack: false }, async obj => {
const value = obj.state.val;
setState(triggerAdapter, value);
});
};
};