-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
269 lines (185 loc) · 7.58 KB
/
main.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
'use strict';
const utils = require('@iobroker/adapter-core');
const dali = require('./lib/dali');
const lib = require('./lib/devicelist');
class Dali extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options= {}]
*/
constructor(options) {
super( {
...options,
name: 'dali',
//systemConfig: true
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
this.device = new dali(this, this.config.host, this.config.port,
this.config.bus0, this.config.bus1, this.config.bus2, this.config.bus3);
this.log.info('config Bus0: ' + this.config.bus0);
this.log.info('config Bus1: ' + this.config.bus1);
this.log.info('config Bus2: ' + this.config.bus2);
this.log.info('config Bus3: ' + this.config.bus3);
this.log.info('config IP: ' + this.config.host);
this.log.info('config Port: ' + this.config.port);
if(this.config.bus0) {
this.log.debug('Bus0 is select');
this.log.info('Bus0 search start');
this.lamps = await this.device.startSearch(0);
this.log.debug('Respones light bus0 ' + JSON.stringify(this.lamps));
this.log.info('Bus0 search end');
this.searchDaliBus(0, this.lamps);
};
if(this.config.bus1) {
this.log.debug('Bus1 is select');
this.log.info('Bus1 search start');
this.lamps = await this.device.startSearch(1);
this.log.debug('Respones light bus0 ' + JSON.stringify(this.lamps));
this.log.info('Bus1 search end');
this.searchDaliBus(1, this.lamps);
};
if(this.config.bus2) {
this.log.debug('Bus2 is select');
this.log.info('Bus2 search start');
this.lamps = await this.device.startSearch(2);
this.log.debug('Respones light bus0 ' + JSON.stringify(this.lamps));
this.log.info('Bus2 search end');
this.searchDaliBus(2, this.lamps);
};
if(this.config.bus3) {
this.log.debug('Bus3 is select');
this.log.info('Bus3 search start');
this.lamps = await this.device.startSearch(3);
this.log.debug('Respones light bus0 ' + JSON.stringify(this.lamps));
this.log.info('Bus3 search end');
this.searchDaliBus(3, this.lamps);
};
if (this.config.bus0 || this.config.bus1 || this.config.bus2 || this.config.bus3) {
this.device.startCounter();
}
this.subscribeStates('*');
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
if(this.device) {
this.device.destroy();
this.log.debug('Device destroyed');
}
try {
this.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if(state && state.ack !== true && this.device) {
const busno = this.getbusnumber(id);
const name = id.substring(id.lastIndexOf('.') + 1);
if(id.startsWith(this.namespace + '.bus' + busno +'.lamps.')) {
this.device.sendLampState(busno, state.val, name);
} else if(id.startsWith(this.namespace + '.bus' + busno + '.groups.')) {
this.device.sendGroupState(busno, state.val, name);
} else if(id.startsWith(this.namespace + '.bus' + busno + '.scenes.')) {
if(state.val) {
this.device.sendScene(busno, name);
this.setState(this.namespace + '.bus' + busno + '.scenes.' + name, false);
}
} else if(id == this.namespace + '.bus' + busno + '.broadcast0') {
this.device.sendBroadcast(busno, state.val);
}
// The state was changed
//this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
//this.log.info(`state ${id} deleted`);
}
}
async searchDaliBus(bus, devices) {
this.createDatapoints(bus);
try{
for(const i in devices) {
const device = devices[i];
const name = device.getName();
this.log.info('start create ' + name);
const path = device.getPath();
this.log.debug('path ' + path);
const level = await device.getLevel();
if (level != null){
this.createStateData(path + name, lib.state.level, level);
}
const min = await device.getMinLevel()
if (min){
this.createStateData(path + 'min', lib.state.min, min);
}
const group = await device.getGroup()
if (group){
this.createStateData(path + 'group', lib.state.group, group);
}
const state = await device.getState()
if (state != null){
this.createStateData(path + name, lib.state.switchState, state);
}
const source = await device.getSource()
if (source){
this.createStateData(path + 'source', lib.state.eventSource, source);
}
const type = await device.getType()
if (type){
this.createStateData(path + 'type', lib.state.type, type);
}
}
}
catch(error) {
this.log.error(error);
}
};
getbusnumber(id) {
if (id.indexOf('bus0')===7) { return 0}
else if (id.indexOf('bus1')===7) { return 1}
else if (id.indexOf('bus2')===7) { return 2}
else if (id.indexOf('bus3')===7) { return 3};
}
async createDatapoints(bus) {
this.createStateData('bus' + bus, lib.state.bus)
this.createStateData('bus' + bus + '.lamps' , lib.state.lamps);
this.createStateData('bus' + bus + '.groups', lib.state.groups);
this.createStateData('bus' + bus + '.scenes', lib.state.scenes);
for (let s = 0; s < 16; s++) {
const sn = (s < 10) ? '0' + s : s;
this.createStateData('bus' + bus + '.scenes.s' + sn, lib.state.scene);
}
this.createStateData('bus' + bus + '.broadcast' + bus, lib.state.level, 0);
}
createStateData(id, state, value) {
this.setObjectNotExistsAsync(id, state,{
native: {}
});
this.setState(id, value, true);
}
}
// @ts-ignore
if(module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options= {}]
*/
module.exports = (options) => new Dali(options);
} else {
// otherwise start the instance directly
new Dali();
}