iobroker.digitalstrom 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/dss.js DELETED
@@ -1,193 +0,0 @@
1
- const EventEmitter = require('events');
2
-
3
- class DSS extends EventEmitter {
4
-
5
- constructor(options) {
6
- super();
7
-
8
- this.options = options || {
9
- logger: console.log
10
- };
11
- /* specify dss ip as parameter */
12
- this.wrapper = require('ds-wrapper')(this.options.host, 'warn');
13
- this.appToken = options.appToken;
14
- this.subScriptionId = options.subScriptionId || 42;
15
- this.subScriptionTimeout = options.subScriptionTimeout || 40 * 1000;
16
-
17
- this.subscriptions = {};
18
-
19
- this.sensortypes = {
20
-
21
- };
22
- }
23
-
24
- async createAppTokenAsync(username, password, readableName) {
25
- readableName = readableName || 'ioBroker';
26
- return new Promise((resolve, reject) => {
27
- this.wrapper.initializeAuthentication(readableName, username, password).then((token) => {
28
- this.appToken = token;
29
- resolve(token);
30
- }, (err) => {
31
- reject(err);
32
- });
33
- });
34
- }
35
-
36
- /**
37
- * Do Request to DSS
38
- * @param {string} dssClass
39
- * @param {string} dssFunction
40
- * @param {object} [params]
41
- * @returns {Promise} Promise: resolve(json) | reject(err, statusCode)
42
- */
43
- async requestAsync(dssClass, dssFunction, params) {
44
- return new Promise((resolve, reject) => {
45
- if (!this.appToken) {
46
- return void reject(new Error('You need to provide a appToken or do a login'));
47
- }
48
- this.wrapper.requestWithSessionFromDSS('/json/' + dssClass + '/' + dssFunction, params || {}, this.appToken).then((body) => {
49
- resolve(body);
50
- }, (err) => {
51
- reject(err);
52
- });
53
- });
54
- }
55
-
56
- pollEvent(eventName) {
57
- if (!this.subscriptions[eventName]) return;
58
- this.subscriptions[eventName].lastPollTime = Date.now();
59
- this.requestAsync('event', 'get', {subscriptionID: this.subscriptions[eventName].subscriptionId, timeout: this.subscriptions[eventName].timeout}).then( events => {
60
- //console.log('EVENT ' + JSON.stringify(events));
61
- if (events && events.result && events.result.events) {
62
- events.result.events.forEach(event => {
63
- // TempFix: dSID ist eigentlich eine dSUID
64
- if (typeof event.source.dsid !== 'undefined')
65
- event.source['dSUID'] = event.source.dsid;
66
- // TempFix: Schreibfehler
67
- if (typeof event.properties.sceneId !== 'undefined')
68
- event.properties['sceneID'] = event.properties.sceneId;
69
-
70
- //! this.log.debug(`dSS - Event received: ${JSON.stringify(event)}`);
71
- if (this.subscriptions[eventName]) {
72
- this.emit(event.name, event);
73
- this.subscriptions[eventName].errorCount = 0
74
- }
75
- });
76
- }
77
- this.pollEvent(eventName);
78
- }, (err) => {
79
- if (this.subscriptions[eventName]) {
80
- this.subscriptions[eventName].errorCount = this.subscriptions[eventName].errorCount ? this.subscriptions[eventName].errorCount++ : 1;
81
- this.options.logger && this.options.logger.warn('Err: polling Event ' + eventName + ' (' + this.subscriptions[eventName].errorCount + '): ' + JSON.stringify(err));
82
- let delay = 5000;
83
- if (this.subscriptions[eventName].errorCount > 2 || err.status_code === 500) {
84
- this.subscribeEvent(eventName, this.subscriptions[eventName].subscriptionId, this.subscriptions[eventName].timeout, err => {
85
- if (err) {
86
- this.options.logger && this.options.logger.warn('Error on resubscribing to ' + eventName + ': ' + err);
87
- this.emit('eventError', eventName, this.subscriptions[eventName].errorCount, 'Error on resubscribing to ' + eventName + ': ' + err);
88
- }
89
- else {
90
- this.subscriptions[eventName].errorCount = 0;
91
- this.options.logger && this.options.logger.info('Successfully resubscribed to ' + eventName);
92
- }
93
- });
94
- }
95
- else {
96
- setTimeout(() => this.pollEvent(eventName), delay);
97
- }
98
- }
99
- });
100
- }
101
-
102
- subscribeEvent(eventName, subscriptionId, timeout, callback) {
103
- this.requestAsync('event' , 'subscribe', {subscriptionID: subscriptionId, name: eventName}).then( (body) => {
104
- if (!body || !body.ok) return;
105
- //console.log('Subscribed event ' + eventName + '(' + subscriptionId + '): ' + JSON.stringify(body));
106
- if (!this.subscriptions[eventName]) {
107
- this.subscriptions[eventName] = {subscriptionId, timeout};
108
- }
109
- else {
110
- this.subscriptions[eventName].subscriptionId = subscriptionId;
111
- this.subscriptions[eventName].timeout = timeout;
112
- }
113
- this.pollEvent(eventName);
114
- callback && callback(null);
115
- }, (err) => {
116
- //console.log('Err:' + JSON.stringify(err));
117
- callback && callback(err);
118
- });
119
- }
120
-
121
- subscribeEvents(eventNames, startSubscriptionId, timeout, callback) {
122
- if (typeof startSubscriptionId === 'function') {
123
- callback = startSubscriptionId;
124
- timeout = undefined;
125
- startSubscriptionId = undefined;
126
- }
127
- if (typeof timeout === 'function') {
128
- callback = timeout;
129
- timeout = undefined;
130
- }
131
- timeout = timeout || this.subScriptionTimeout;
132
- startSubscriptionId = startSubscriptionId || this.subScriptionId;
133
- if (!Array.isArray(eventNames)) {
134
- eventNames = [eventNames];
135
- }
136
- let toSubscribe = eventNames.length;
137
- let errs = [];
138
- eventNames.forEach(eventName => this.subscribeEvent(eventName, startSubscriptionId++, timeout, (err) => {
139
- if (err) {
140
- errs.push(err);
141
- }
142
- if (!--toSubscribe) {
143
- if (!errs.length) {
144
- // @ts-ignore
145
- errs = null;
146
- }
147
- callback && callback(err);
148
- }
149
- }));
150
- }
151
-
152
- unsubscribeEvent(eventName, callback) {
153
- const sub = this.subscriptions[eventName];
154
- if (this.subscriptions[eventName]) {
155
- delete this.subscriptions[eventName];
156
- }
157
- this.requestAsync('event' , 'unsubscribe', {subscriptionID: sub.subscriptionId, name: eventName}).then( (body) => {
158
- if (!body || !body.ok) {
159
- return void (callback && callback(JSON.stringify(body)));
160
- }
161
- //console.log('Subscribed event ' + eventName + '(' + subscriptionId + '): ' + JSON.stringify(body));
162
- callback && callback(null);
163
- }, (err) => {
164
- //console.log('Err:' + JSON.stringify(err));
165
- callback && callback(err);
166
- });
167
- }
168
-
169
- unsubscribeAllEvents(callback) {
170
- let lastPollEnd = 0;
171
- const subscribedEvents = Object.keys(this.subscriptions);
172
- let openSubscriptions = subscribedEvents.length;
173
- if (!openSubscriptions) {
174
- this.subscriptions = {};
175
- return void (callback && callback(0));
176
- }
177
-
178
- subscribedEvents.forEach(eventName => {
179
- const pollEnd = this.subscriptions[eventName].lastPollTime + this.subscriptions[eventName].timeout;
180
- if (pollEnd > lastPollEnd) {
181
- lastPollEnd = pollEnd;
182
- }
183
- this.unsubscribeEvent(eventName , () => {
184
- if (!--openSubscriptions) {
185
- this.subscriptions = {};
186
- callback && callback(lastPollEnd);
187
- }
188
- });
189
- });
190
- }
191
- }
192
-
193
- module.exports = DSS;
package/lib/dssQueue.js DELETED
@@ -1,268 +0,0 @@
1
- class DSSQueue {
2
-
3
- constructor(options) {
4
- this.options = options || {
5
- logger: console.log
6
- };
7
- this.options.prioTimeouts = this.options.prioTimeouts || {
8
- 'high': 500,
9
- 'medium': 10000,
10
- 'low': 20000
11
- };
12
- this.dss = options.dss;
13
-
14
- this.queryQueue = {};
15
- this.nextEntryTimeout = {};
16
- this.lastProcessed = {};
17
- this.queryRunning = {};
18
- }
19
-
20
- clearQueue(circuit) {
21
- if (this.nextEntryTimeout[circuit]) {
22
- clearTimeout(this.nextEntryTimeout[circuit]);
23
- this.nextEntryTimeout[circuit] = null;
24
- }
25
-
26
- this.queryQueue[circuit] = [];
27
- this.lastProcessed[circuit] = 0;
28
- }
29
-
30
- clearQueues() {
31
- Object.keys(this.queryQueue).forEach(circuit => this.clearQueue(circuit));
32
- }
33
-
34
- findNextPrioEntry(circuit, prio) {
35
- prio = prio || 'high';
36
-
37
- const found = this.queryQueue[circuit].findIndex(entry => entry.prio === prio);
38
- if (found === -1) {
39
- switch (prio) {
40
- case 'high':
41
- prio = 'medium';
42
- break;
43
- case 'medium':
44
- prio = 'low';
45
- break;
46
- case 'low':
47
- prio = null;
48
- break;
49
- }
50
- if (prio) {
51
- return this.findNextPrioEntry(circuit, prio);
52
- }
53
- return -1;
54
- } else {
55
- return found;
56
- }
57
- }
58
-
59
- pushQueryQueue(circuit, entryId, entry, prio, callback) {
60
- if (typeof entryId !== 'string') {
61
- callback = prio;
62
- prio = entry;
63
- entry = entryId;
64
- entryId = entry.dssClass + '-' + entry.dssFunction + '-' + entry.params.dsuid + '-' + entry.params.id + '-' + entry.params.groupID + '-' + (entry.params.index || entry.params.offset || entry.params.sceneNumber || entry.params.name);
65
- }
66
- prio = prio || 'low';
67
- this.queryQueue[circuit] = this.queryQueue[circuit] || [];
68
- this.lastProcessed[circuit] = this.lastProcessed[circuit] || 0;
69
-
70
- const found = this.queryQueue[circuit].findIndex(entry => entry.entryId === entryId);
71
-
72
- if (found !== -1) {
73
- if (
74
- (prio === 'high' && this.queryQueue[circuit][found].prio !== 'high') ||
75
- (prio === 'medium' && this.queryQueue[circuit][found].prio === 'low')
76
- ) {
77
- this.queryQueue[circuit][found].prio = prio;
78
- }
79
- this.queryQueue[circuit][found].callbacks.push(callback);
80
- } else {
81
- this.queryQueue[circuit].push({
82
- prio,
83
- entryId,
84
- entry,
85
- callbacks: [callback]
86
- });
87
- }
88
-
89
- const nextProcessIndex = this.findNextPrioEntry(circuit);
90
-
91
- if (nextProcessIndex !== -1) {
92
- const sinceLastProcessed = Date.now() - this.lastProcessed[circuit];
93
- let nextEntryTimeout = this.options.prioTimeouts[this.queryQueue[circuit][nextProcessIndex].prio];
94
- if (sinceLastProcessed < nextEntryTimeout) {
95
- nextEntryTimeout = nextEntryTimeout - sinceLastProcessed;
96
- }
97
- else {
98
- nextEntryTimeout = 500;
99
- }
100
- if (this.nextEntryTimeout[circuit]) {
101
- clearTimeout(this.nextEntryTimeout[circuit]);
102
- this.nextEntryTimeout[circuit] = null;
103
- }
104
- this.options.logger && this.options.logger.debug(Date.now() + ' PUSH Plan next queue entry (' + entryId + ') for ' + circuit + ' and prio ' + this.queryQueue[circuit][nextProcessIndex].prio + ' in ' + nextEntryTimeout + 'ms');
105
- this.nextEntryTimeout[circuit] = setTimeout(() => {
106
- this.nextEntryTimeout[circuit] = null;
107
- this.processQueryQueue(circuit);
108
- }, nextEntryTimeout);
109
- }
110
- }
111
-
112
- processQueryQueue(circuit) {
113
- //console.log(circuit + ': ' + this.queryQueue[circuit].length + ': ' + JSON.stringify(this.queryQueue[circuit]));
114
- const toProcess = this.findNextPrioEntry(circuit);
115
- if (toProcess === -1 || this.queryRunning[circuit]) return;
116
-
117
- const query = this.queryQueue[circuit][toProcess];
118
- this.options.logger && this.options.logger.debug(Date.now() + ' PROCESS Queued entry ' + toProcess + ' - ' + query.entry.dssClass + '/' + query.entry.dssFunction + ' and ' + JSON.stringify(query.entry.params));
119
- this.queryRunning[circuit] = true;
120
- this.dss.requestAsync(query.entry.dssClass, query.entry.dssFunction, query.entry.params).then((res) => {
121
- this.queryRunning[circuit] = false;
122
- this.lastProcessed[circuit] = Date.now();
123
- const _query = this.queryQueue[circuit].splice(toProcess, 1)[0];
124
-
125
- this.options.logger && this.options.logger.debug(Date.now() + ' Queued entry processed ' + query.entry.dssClass + '/' + query.entry.dssFunction + ' and ' + JSON.stringify(query.entry.params) + ': ' + JSON.stringify(res));
126
-
127
- query.callbacks && query.callbacks.forEach(callback => callback(null, res));
128
-
129
- const nextProcessIndex = this.findNextPrioEntry(circuit);
130
-
131
- if (this.nextEntryTimeout[circuit]) {
132
- clearTimeout(this.nextEntryTimeout[circuit]);
133
- this.nextEntryTimeout[circuit] = null;
134
- }
135
-
136
- const nextEntryTimeout = nextProcessIndex !== -1 ? this.options.prioTimeouts[this.queryQueue[circuit][nextProcessIndex].prio] : 500;
137
- this.options.logger && this.options.logger.debug(Date.now() + ' PROCESS Plan next queue entry for ' + circuit + ' and prio ' + (nextProcessIndex !== -1 ? this.queryQueue[circuit][nextProcessIndex].prio : 'none') + ' in ' + nextEntryTimeout + 'ms');
138
- this.nextEntryTimeout[circuit] = setTimeout(() => {
139
- this.nextEntryTimeout[circuit] = null;
140
- this.processQueryQueue(circuit);
141
- }, nextEntryTimeout);
142
- }, (err) => {
143
- this.queryRunning[circuit] = false;
144
- this.lastProcessed[circuit] = Date.now();
145
- const _query = this.queryQueue[circuit].splice(toProcess, 1)[0];
146
-
147
- this.options.logger && this.options.logger.warn('Queued entry ERROR ' + query.entry.dssClass + '/' + query.entry.dssFunction + ' and ' + JSON.stringify(query.entry.params) + ': ' + JSON.stringify(err));
148
-
149
- query.callbacks && query.callbacks.forEach(callback => callback(err));
150
-
151
- const nextProcessIndex = this.findNextPrioEntry(circuit);
152
-
153
- if (this.nextEntryTimeout[circuit]) {
154
- clearTimeout(this.nextEntryTimeout[circuit]);
155
- this.nextEntryTimeout[circuit] = null;
156
- }
157
-
158
- const nextEntryTimeout = nextProcessIndex !== -1 ? this.options.prioTimeouts[this.queryQueue[circuit][nextProcessIndex].prio] : 500;
159
- this.options.logger && this.options.logger.debug(Date.now() + ' PROCESS-ERR Plan next queue entry for ' + circuit + ' and prio ' + (nextProcessIndex !== -1 ? this.queryQueue[circuit][nextProcessIndex].prio : 'none') + ' in ' + nextEntryTimeout + 'ms');
160
- this.nextEntryTimeout[circuit] = setTimeout(() => {
161
- this.nextEntryTimeout[circuit] = null;
162
- this.processQueryQueue(circuit);
163
- }, nextEntryTimeout);
164
- });
165
- }
166
-
167
- queueUpdateOutputValue(dev, index, length, prio, callback) {
168
- if (typeof length === 'string') {
169
- callback = prio;
170
- prio = length;
171
- length = 1;
172
- }
173
- if (typeof prio === 'function') {
174
- callback = prio;
175
- prio = null;
176
- }
177
- if (length > 4) {
178
- if (length === 255) {
179
- length = 1;
180
- } else if (length === 65535) {
181
- length = 2;
182
- }
183
- }
184
- const callEntry = {
185
- dssClass: 'device',
186
- dssFunction: 'getOutputValue',
187
- params: {
188
- dsuid: dev.dSUID,
189
- offset: index,
190
- category: 'manual'
191
- }
192
- };
193
-
194
- if (length === 2 || index > 0) {
195
- callEntry.dssFunction = length === 2 ? 'getConfigWord' : 'getConfig';
196
- callEntry.params.class = 64;
197
- callEntry.params.index = callEntry.params.offset;
198
- delete callEntry.params.offset;
199
- }
200
- this.pushQueryQueue(dev.meterDSUID, callEntry, prio, (err, res) => {
201
- if (err) {
202
- callback && callback(err);
203
- } else if (res && (res.ok === false || !res.result)) {
204
- callback && callback(res.message || res.status_code || res);
205
- } else {
206
- callback && callback(null, res.result.value);
207
- }
208
- });
209
- }
210
-
211
- queueSetOutputValue(dev, index, length, value, prio, callback) {
212
- if (typeof value === 'string') {
213
- callback = prio;
214
- prio = value;
215
- length = 1;
216
- }
217
- if (typeof prio === 'function') {
218
- callback = prio;
219
- prio = null;
220
- }
221
- if (length > 4) {
222
- if (length === 255) {
223
- length = 1;
224
- } else if (length === 65535) {
225
- length = 2;
226
- }
227
- }
228
- const callEntry = {
229
- dssClass: 'device',
230
- dssFunction: 'setValue',
231
- params: {
232
- dsuid: dev.dSUID,
233
- offset: index,
234
- value,
235
- category: 'manual'
236
- }
237
- };
238
-
239
- // brigthness length = 1 index = 0 --> setValue
240
- // rolladen shade pos length = 2, index =2 -> setOutputValue
241
- // rollladen shade tilt length = 1, index = 4 -> setConfig
242
-
243
-
244
- if (length === 2 && index === 2) {
245
- callEntry.dssFunction = 'setOutputValue';
246
- }
247
- else if (index !== 0) {
248
- callEntry.dssFunction = 'setConfig';
249
- callEntry.params.class = 64;
250
- callEntry.params.index = callEntry.params.offset;
251
- delete callEntry.params.offset;
252
- }
253
- if (callEntry.dssFunction === 'setValue' && callEntry.params.offset !== undefined) {
254
- delete callEntry.params.offset;
255
- }
256
- this.pushQueryQueue(dev.meterDSUID, callEntry, prio, (err, res) => {
257
- if (err) {
258
- callback && callback(err);
259
- } else if (res && (res.ok === false)) {
260
- callback && callback(res.message || res.status_code || res);
261
- } else {
262
- callback && callback(null, value);
263
- }
264
- });
265
- }
266
- }
267
-
268
- module.exports = DSSQueue;