iobroker.sql 1.16.2 → 2.1.1
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/README.md +203 -62
- package/admin/i18n/de/translations.json +32 -6
- package/admin/i18n/en/translations.json +31 -5
- package/admin/i18n/es/translations.json +32 -6
- package/admin/i18n/fr/translations.json +32 -6
- package/admin/i18n/it/translations.json +32 -6
- package/admin/i18n/nl/translations.json +32 -6
- package/admin/i18n/pl/translations.json +32 -6
- package/admin/i18n/pt/translations.json +32 -6
- package/admin/i18n/ru/translations.json +32 -6
- package/admin/i18n/zh-cn/translations.json +31 -5
- package/admin/jsonConfig.json +92 -20
- package/admin/jsonCustom.json +112 -354
- package/admin/words.js +35 -9
- package/io-package.json +116 -54
- package/lib/aggregate.js +894 -579
- package/lib/mssql-client.js +7 -7
- package/lib/mssql.js +51 -19
- package/lib/mysql-client.js +62 -0
- package/lib/mysql.js +257 -231
- package/lib/postgresql-client.js +11 -11
- package/lib/postgresql.js +252 -228
- package/lib/sqlite.js +252 -226
- package/main.js +1215 -488
- package/package.json +10 -10
- package/admin/custom.html +0 -185
- package/admin/custom_m.html +0 -428
- package/admin/index.html +0 -282
- package/admin/index_m.html +0 -307
package/main.js
CHANGED
|
@@ -12,7 +12,7 @@ let SQLFuncs = null;
|
|
|
12
12
|
|
|
13
13
|
const clients = {
|
|
14
14
|
postgresql: {name: 'PostgreSQLClient', multiRequests: true},
|
|
15
|
-
mysql: {name: '
|
|
15
|
+
mysql: {name: 'MySQL2Client', multiRequests: true},
|
|
16
16
|
sqlite: {name: 'SQLite3Client', multiRequests: false},
|
|
17
17
|
mssql: {name: 'MSSQLClient', multiRequests: true}
|
|
18
18
|
};
|
|
@@ -52,6 +52,11 @@ let subscribeAll = false;
|
|
|
52
52
|
let clientPool;
|
|
53
53
|
let reconnectTimeout = null;
|
|
54
54
|
let testConnectTimeout = null;
|
|
55
|
+
let dpOverviewTimeout = null;
|
|
56
|
+
let bufferChecker = null;
|
|
57
|
+
let activeConnections = 0;
|
|
58
|
+
let poolBorrowGuard = []; // required until https://github.com/intellinote/sql-client/issues/7 is fixed
|
|
59
|
+
const logConnectionUsage = true;
|
|
55
60
|
|
|
56
61
|
function isEqual(a, b) {
|
|
57
62
|
//console.log('Compare ' + JSON.stringify(a) + ' with ' + JSON.stringify(b));
|
|
@@ -109,7 +114,7 @@ function startAdapter(options) {
|
|
|
109
114
|
const now = Date.now();
|
|
110
115
|
const formerAliasId = aliasMap[id] ? aliasMap[id] : id;
|
|
111
116
|
|
|
112
|
-
if (obj && obj.common && obj.common.custom
|
|
117
|
+
if (obj && obj.common && obj.common.custom && obj.common.custom[adapter.namespace] && typeof obj.common.custom[adapter.namespace] === 'object' && obj.common.custom[adapter.namespace].enabled) {
|
|
113
118
|
const realId = id;
|
|
114
119
|
let checkForRemove = true;
|
|
115
120
|
|
|
@@ -141,22 +146,10 @@ function startAdapter(options) {
|
|
|
141
146
|
adapter.subscribeForeignStates('*');
|
|
142
147
|
}
|
|
143
148
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (sqlDPs[id] && sqlDPs[id].index !== undefined) {
|
|
147
|
-
storedIndex = sqlDPs[id].index;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (sqlDPs[id] && sqlDPs[id].dbtype !== undefined) {
|
|
151
|
-
storedType = sqlDPs[id].dbtype;
|
|
152
|
-
} else if (sqlDPs[formerAliasId] && sqlDPs[formerAliasId].dbtype !== undefined) {
|
|
153
|
-
storedType = sqlDPs[formerAliasId].dbtype;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (storedIndex === undefined) {
|
|
157
|
-
getId(id, sqlDPs[id].dbtype, () => reInit(id, realId, formerAliasId, storedIndex, storedType, obj));
|
|
149
|
+
if (sqlDPs[id] && sqlDPs[id].index === undefined) {
|
|
150
|
+
getId(id, sqlDPs[id].dbtype, () => reInit(id, realId, formerAliasId, obj));
|
|
158
151
|
} else {
|
|
159
|
-
reInit(id, realId, formerAliasId,
|
|
152
|
+
reInit(id, realId, formerAliasId, obj);
|
|
160
153
|
}
|
|
161
154
|
} else {
|
|
162
155
|
if (aliasMap[id]) {
|
|
@@ -166,42 +159,59 @@ function startAdapter(options) {
|
|
|
166
159
|
|
|
167
160
|
id = formerAliasId;
|
|
168
161
|
|
|
169
|
-
if (sqlDPs[id]) {
|
|
170
|
-
adapter.log.info(
|
|
171
|
-
|
|
172
|
-
|
|
162
|
+
if (sqlDPs[id] && sqlDPs[id][adapter.namespace]) {
|
|
163
|
+
adapter.log.info(`disabled logging of ${id}`);
|
|
164
|
+
if (sqlDPs[id].relogTimeout) {
|
|
165
|
+
clearTimeout(sqlDPs[id].relogTimeout);
|
|
166
|
+
sqlDPs[id].relogTimeout = null;
|
|
167
|
+
}
|
|
168
|
+
if (sqlDPs[id].timeout) {
|
|
169
|
+
clearTimeout(sqlDPs[id].timeout);
|
|
170
|
+
sqlDPs[id].timeout = null;
|
|
171
|
+
}
|
|
173
172
|
|
|
174
173
|
tmpState = Object.assign({}, sqlDPs[id].state);
|
|
175
174
|
const state = sqlDPs[id].state ? tmpState : null;
|
|
176
175
|
|
|
177
|
-
if (sqlDPs[id].
|
|
178
|
-
|
|
179
|
-
sqlDPs[id].skipped
|
|
180
|
-
|
|
176
|
+
if (sqlDPs[id][adapter.namespace]) {
|
|
177
|
+
sqlDPs[id][adapter.namespace].enabled = false;
|
|
178
|
+
if (sqlDPs[id].skipped && !sqlDPs[id][adapter.namespace].disableSkippedValueLogging) {
|
|
179
|
+
pushValueIntoDB(id, sqlDPs[id].skipped, false, true);
|
|
180
|
+
sqlDPs[id].skipped = null;
|
|
181
|
+
}
|
|
181
182
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
183
|
+
if (adapter.config.writeNulls) {
|
|
184
|
+
const nullValue = {
|
|
185
|
+
val: null,
|
|
186
|
+
ts: now,
|
|
187
|
+
lc: now,
|
|
188
|
+
q: 0x40,
|
|
189
|
+
from: `system.adapter.${adapter.namespace}`
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
if (sqlDPs[id][adapter.namespace].changesOnly && state && state.val !== null) {
|
|
193
|
+
(function (_id, _state, _nullValue) {
|
|
194
|
+
_state.ts = now;
|
|
195
|
+
_state.from = `system.adapter.${adapter.namespace}`;
|
|
196
|
+
nullValue.ts += 4;
|
|
197
|
+
nullValue.lc += 4; // because of MS SQL
|
|
198
|
+
adapter.log.debug(`Write 1/2 "${_state.val}" _id: ${_id}`);
|
|
199
|
+
pushValueIntoDB(_id, _state, false, true, () => {
|
|
200
|
+
// terminate values with null to indicate adapter stop. timestamp + 1
|
|
201
|
+
adapter.log.debug(`Write 2/2 "null" _id: ${_id}`);
|
|
202
|
+
pushValueIntoDB(_id, _nullValue, () => delete sqlDPs[id]);
|
|
203
|
+
});
|
|
204
|
+
})(id, state, nullValue);
|
|
205
|
+
} else {
|
|
206
|
+
// terminate values with null to indicate adapter stop. timestamp + 1
|
|
207
|
+
adapter.log.debug(`Write 0 NULL _id: ${id}`);
|
|
208
|
+
pushValueIntoDB(id, nullValue, () => delete sqlDPs[id]);
|
|
209
|
+
}
|
|
198
210
|
} else {
|
|
199
|
-
|
|
200
|
-
adapter.log.debug('Write 0 NULL _id: ' + id);
|
|
201
|
-
pushValueIntoDB(id, nullValue, () => delete sqlDPs[id][adapter.namespace]);
|
|
211
|
+
storeCached(id, () => delete sqlDPs[id]);
|
|
202
212
|
}
|
|
203
213
|
} else {
|
|
204
|
-
delete sqlDPs[id]
|
|
214
|
+
delete sqlDPs[id];
|
|
205
215
|
}
|
|
206
216
|
}
|
|
207
217
|
}
|
|
@@ -232,13 +242,22 @@ function borrowClientFromPool(callback) {
|
|
|
232
242
|
}
|
|
233
243
|
setConnected(true);
|
|
234
244
|
|
|
245
|
+
if (activeConnections >= adapter.config.maxConnections) {
|
|
246
|
+
logConnectionUsage && adapter.log.debug(`Borrow connection not possible: ${activeConnections} >= Max - Store for Later`);
|
|
247
|
+
poolBorrowGuard.push(callback);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
activeConnections++;
|
|
251
|
+
logConnectionUsage && adapter.log.debug(`Borrow connection from pool: ${activeConnections} now`);
|
|
235
252
|
clientPool.borrow((err, client) => {
|
|
236
253
|
if (!err && client) {
|
|
237
254
|
// make sure we always have at least one error listener to prevent crashes
|
|
238
255
|
if (client.on && client.listenerCount && client.listenerCount('error') === 0) {
|
|
239
256
|
client.on('error', err =>
|
|
240
|
-
adapter.log.warn(
|
|
257
|
+
adapter.log.warn(`SQL client error: ${err}`));
|
|
241
258
|
}
|
|
259
|
+
} else if (!client) {
|
|
260
|
+
activeConnections--;
|
|
242
261
|
}
|
|
243
262
|
|
|
244
263
|
callback(err, client);
|
|
@@ -246,11 +265,34 @@ function borrowClientFromPool(callback) {
|
|
|
246
265
|
}
|
|
247
266
|
|
|
248
267
|
function returnClientToPool(client) {
|
|
249
|
-
|
|
268
|
+
if (client) {
|
|
269
|
+
activeConnections--;
|
|
270
|
+
}
|
|
271
|
+
logConnectionUsage && adapter.log.debug(`Return connection to pool: ${activeConnections} now`);
|
|
272
|
+
if (clientPool && client) {
|
|
273
|
+
if (poolBorrowGuard.length) {
|
|
274
|
+
activeConnections++;
|
|
275
|
+
logConnectionUsage && adapter.log.debug(`Borrow returned connection directly: ${activeConnections} now`);
|
|
276
|
+
const callback = poolBorrowGuard.shift();
|
|
277
|
+
callback(null, client);
|
|
278
|
+
} else {
|
|
279
|
+
try {
|
|
280
|
+
clientPool.return(client);
|
|
281
|
+
} catch (err) {
|
|
282
|
+
// Ignore
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
250
286
|
}
|
|
251
287
|
|
|
252
|
-
function reInit(id, realId, formerAliasId,
|
|
253
|
-
|
|
288
|
+
function reInit(id, realId, formerAliasId, obj) {
|
|
289
|
+
|
|
290
|
+
// maxLength
|
|
291
|
+
if (!obj.common.custom[adapter.namespace].maxLength && obj.common.custom[adapter.namespace].maxLength !== '0' && obj.common.custom[adapter.namespace].maxLength !== 0) {
|
|
292
|
+
obj.common.custom[adapter.namespace].maxLength = parseInt(adapter.config.maxLength, 10) || 0;
|
|
293
|
+
} else {
|
|
294
|
+
obj.common.custom[adapter.namespace].maxLength = parseInt(obj.common.custom[adapter.namespace].maxLength, 10);
|
|
295
|
+
}
|
|
254
296
|
|
|
255
297
|
// retention
|
|
256
298
|
if (obj.common.custom[adapter.namespace].retention || obj.common.custom[adapter.namespace].retention === 0) {
|
|
@@ -258,20 +300,77 @@ function reInit(id, realId, formerAliasId, storedIndex, storedType, obj) {
|
|
|
258
300
|
} else {
|
|
259
301
|
obj.common.custom[adapter.namespace].retention = adapter.config.retention;
|
|
260
302
|
}
|
|
303
|
+
if (obj.common.custom[adapter.namespace].retention === -1) {
|
|
304
|
+
// customRetentionDuration
|
|
305
|
+
if (obj.common.custom[adapter.namespace].customRetentionDuration !== undefined && obj.common.custom[adapter.namespace].customRetentionDuration !== null && obj.common.custom[adapter.namespace].customRetentionDuration !== '') {
|
|
306
|
+
obj.common.custom[adapter.namespace].customRetentionDuration = parseInt(obj.common.custom[adapter.namespace].customRetentionDuration, 10) || 0;
|
|
307
|
+
} else {
|
|
308
|
+
obj.common.custom[adapter.namespace].customRetentionDuration = adapter.config.customRetentionDuration;
|
|
309
|
+
}
|
|
310
|
+
obj.common.custom[adapter.namespace].retention = obj.common.custom[adapter.namespace].customRetentionDuration * 24 * 60 * 60
|
|
311
|
+
}
|
|
261
312
|
|
|
262
|
-
// debounce
|
|
263
|
-
if (obj.common.custom[adapter.namespace].
|
|
264
|
-
obj.common.custom[adapter.namespace].debounce
|
|
313
|
+
// debounceTime and debounce compatibility handling
|
|
314
|
+
if (!obj.common.custom[adapter.namespace].blockTime && obj.common.custom[adapter.namespace].blockTime !== '0' && obj.common.custom[adapter.namespace].blockTime !== 0) {
|
|
315
|
+
if (!obj.common.custom[adapter.namespace].debounce && obj.common.custom[adapter.namespace].debounce !== '0' && obj.common.custom[adapter.namespace].debounce !== 0) {
|
|
316
|
+
obj.common.custom[adapter.namespace].blockTime = parseInt(adapter.config.blockTime, 10) || 0;
|
|
317
|
+
} else {
|
|
318
|
+
obj.common.custom[adapter.namespace].blockTime = parseInt(obj.common.custom[adapter.namespace].debounce, 10) || 0;
|
|
319
|
+
}
|
|
265
320
|
} else {
|
|
266
|
-
obj.common.custom[adapter.namespace].
|
|
321
|
+
obj.common.custom[adapter.namespace].blockTime = parseInt(obj.common.custom[adapter.namespace].blockTime, 10) || 0;
|
|
322
|
+
}
|
|
323
|
+
if (!obj.common.custom[adapter.namespace].debounceTime && obj.common.custom[adapter.namespace].debounceTime !== '0' && obj.common.custom[adapter.namespace].debounceTime !== 0) {
|
|
324
|
+
obj.common.custom[adapter.namespace].debounceTime = parseInt(adapter.config.debounceTime, 10) || 0;
|
|
325
|
+
} else {
|
|
326
|
+
obj.common.custom[adapter.namespace].debounceTime = parseInt(obj.common.custom[adapter.namespace].debounceTime, 10) || 0;
|
|
267
327
|
}
|
|
268
328
|
|
|
269
329
|
// changesOnly
|
|
270
330
|
obj.common.custom[adapter.namespace].changesOnly = obj.common.custom[adapter.namespace].changesOnly === 'true' || obj.common.custom[adapter.namespace].changesOnly === true;
|
|
271
331
|
|
|
332
|
+
// ignoreZero
|
|
272
333
|
obj.common.custom[adapter.namespace].ignoreZero = obj.common.custom[adapter.namespace].ignoreZero === 'true' || obj.common.custom[adapter.namespace].ignoreZero === true;
|
|
273
|
-
obj.common.custom[adapter.namespace].ignoreBelowZero = obj.common.custom[adapter.namespace].ignoreBelowZero === 'true' || obj.common.custom[adapter.namespace].ignoreBelowZero === true;
|
|
274
334
|
|
|
335
|
+
// round
|
|
336
|
+
if (obj.common.custom[adapter.namespace].round !== null && obj.common.custom[adapter.namespace].round !== undefined && obj.common.custom[adapter.namespace] !== '') {
|
|
337
|
+
obj.common.custom[adapter.namespace].round = parseInt(obj.common.custom[adapter.namespace], 10);
|
|
338
|
+
if (!isFinite(obj.common.custom[adapter.namespace].round) || obj.common.custom[adapter.namespace].round < 0) {
|
|
339
|
+
obj.common.custom[adapter.namespace].round = adapter.config.round;
|
|
340
|
+
} else {
|
|
341
|
+
obj.common.custom[adapter.namespace].round = Math.pow(10, parseInt(obj.common.custom[adapter.namespace].round, 10));
|
|
342
|
+
}
|
|
343
|
+
} else {
|
|
344
|
+
obj.common.custom[adapter.namespace].round = adapter.config.round;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// ignoreAboveNumber
|
|
348
|
+
if (obj.common.custom[adapter.namespace].ignoreAboveNumber !== undefined && obj.common.custom[adapter.namespace].ignoreAboveNumber !== null && obj.common.custom[adapter.namespace].ignoreAboveNumber !== '') {
|
|
349
|
+
obj.common.custom[adapter.namespace].ignoreAboveNumber = parseFloat(obj.common.custom[adapter.namespace].ignoreAboveNumber) || null;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// ignoreBelowNumber incl. ignoreBelowZero compatibility handling
|
|
353
|
+
if (obj.common.custom[adapter.namespace].ignoreBelowNumber !== undefined && obj.common.custom[adapter.namespace].ignoreBelowNumber !== null && obj.common.custom[adapter.namespace].ignoreBelowNumber !== '') {
|
|
354
|
+
obj.common.custom[adapter.namespace].ignoreBelowNumber = parseFloat(obj.common.custom[adapter.namespace].ignoreBelowNumber) || null;
|
|
355
|
+
} else if (obj.common.custom[adapter.namespace].ignoreBelowZero === 'true' || obj.common.custom[adapter.namespace].ignoreBelowZero === true) {
|
|
356
|
+
obj.common.custom[adapter.namespace].ignoreBelowNumber = 0;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// disableSkippedValueLogging
|
|
360
|
+
if (obj.common.custom[adapter.namespace].disableSkippedValueLogging !== undefined && obj.common.custom[adapter.namespace].disableSkippedValueLogging !== null && obj.common.custom[adapter.namespace].disableSkippedValueLogging !== '') {
|
|
361
|
+
obj.common.custom[adapter.namespace].disableSkippedValueLogging = obj.common.custom[adapter.namespace].disableSkippedValueLogging === 'true' || obj.common.custom[adapter.namespace].disableSkippedValueLogging === true;
|
|
362
|
+
} else {
|
|
363
|
+
obj.common.custom[adapter.namespace].disableSkippedValueLogging = adapter.config.disableSkippedValueLogging;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// enableDebugLogs
|
|
367
|
+
if (obj.common.custom[adapter.namespace].enableDebugLogs !== undefined && obj.common.custom[adapter.namespace].enableDebugLogs !== null && obj.common.custom[adapter.namespace].enableDebugLogs !== '') {
|
|
368
|
+
obj.common.custom[adapter.namespace].enableDebugLogs = obj.common.custom[adapter.namespace].enableDebugLogs === 'true' || obj.common.custom[adapter.namespace].enableDebugLogs === true;
|
|
369
|
+
} else {
|
|
370
|
+
obj.common.custom[adapter.namespace].enableDebugLogs = adapter.config.enableDebugLogs;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// changesRelogInterval
|
|
275
374
|
if (obj.common.custom[adapter.namespace].changesRelogInterval || obj.common.custom[adapter.namespace].changesRelogInterval === 0) {
|
|
276
375
|
obj.common.custom[adapter.namespace].changesRelogInterval = parseInt(obj.common.custom[adapter.namespace].changesRelogInterval, 10) || 0;
|
|
277
376
|
} else {
|
|
@@ -298,29 +397,43 @@ function reInit(id, realId, formerAliasId, storedIndex, storedType, obj) {
|
|
|
298
397
|
if (sqlDPs[formerAliasId] &&
|
|
299
398
|
sqlDPs[formerAliasId][adapter.namespace] &&
|
|
300
399
|
isEqual(obj.common.custom[adapter.namespace], sqlDPs[formerAliasId][adapter.namespace])) {
|
|
301
|
-
return adapter.log.debug(`Object ${id} unchanged. Ignore`);
|
|
400
|
+
return obj.common.custom[adapter.namespace].enableDebugLogs && adapter.log.debug(`Object ${id} unchanged. Ignore`);
|
|
302
401
|
}
|
|
303
402
|
|
|
304
403
|
// relogTimeout
|
|
305
404
|
if (sqlDPs[formerAliasId] && sqlDPs[formerAliasId].relogTimeout) {
|
|
306
405
|
clearTimeout(sqlDPs[formerAliasId].relogTimeout);
|
|
406
|
+
sqlDPs[formerAliasId].relogTimeout = null;
|
|
307
407
|
}
|
|
308
408
|
|
|
309
|
-
sqlDPs[id]
|
|
310
|
-
sqlDPs[id].
|
|
409
|
+
const writeNull = !(sqlDPs[id] && sqlDPs[id][adapter.namespace]);
|
|
410
|
+
const state = sqlDPs[id] ? sqlDPs[id].state : null;
|
|
411
|
+
const list = sqlDPs[id] ? sqlDPs[id].list : null;
|
|
412
|
+
const inFlight = sqlDPs[id] ? sqlDPs[id].inFlight: null;
|
|
413
|
+
const timeout = sqlDPs[id] ? sqlDPs[id].timeout : null;
|
|
414
|
+
const ts = sqlDPs[id] ? sqlDPs[id].ts : null;
|
|
415
|
+
const lastCheck = sqlDPs[id] ? sqlDPs[id].lastCheck : null;
|
|
416
|
+
|
|
417
|
+
sqlDPs[id] = obj.common.custom;
|
|
418
|
+
sqlDPs[id].state = state;
|
|
419
|
+
sqlDPs[id].list = list || [];
|
|
420
|
+
sqlDPs[id].inFlight = inFlight || {};
|
|
421
|
+
sqlDPs[id].timeout = timeout;
|
|
422
|
+
sqlDPs[id].ts = ts;
|
|
423
|
+
sqlDPs[id].realId = realId;
|
|
424
|
+
sqlDPs[id].lastCheck = lastCheck || Date.now() - Math.floor(Math.random() * 21600000/* 6 hours */); // randomize lastCheck to avoid all datapoints to be checked at same timepoint
|
|
425
|
+
;
|
|
311
426
|
|
|
312
427
|
// changesRelogInterval
|
|
313
|
-
if (sqlDPs[id][adapter.namespace].changesRelogInterval > 0) {
|
|
314
|
-
sqlDPs[id].relogTimeout && clearTimeout(sqlDPs[id].relogTimeout);
|
|
428
|
+
if (sqlDPs[id][adapter.namespace].changesOnly && sqlDPs[id][adapter.namespace].changesRelogInterval > 0) {
|
|
315
429
|
sqlDPs[id].relogTimeout = setTimeout(reLogHelper, (sqlDPs[id][adapter.namespace].changesRelogInterval * 500 * Math.random()) + sqlDPs[id][adapter.namespace].changesRelogInterval * 500, id);
|
|
316
430
|
}
|
|
317
431
|
|
|
318
|
-
const writeNull = !(sqlDPs[id] && sqlDPs[id][adapter.namespace]);
|
|
319
432
|
if (writeNull && adapter.config.writeNulls) {
|
|
320
433
|
writeNulls(id);
|
|
321
434
|
}
|
|
322
435
|
|
|
323
|
-
adapter.log.info(`enabled logging of ${id}, Alias=${id !== realId}`);
|
|
436
|
+
adapter.log.info(`enabled logging of ${id}, Alias=${id !== realId}, WriteNulls=${writeNull}`);
|
|
324
437
|
}
|
|
325
438
|
|
|
326
439
|
function setConnected(isConnected) {
|
|
@@ -349,7 +462,7 @@ function connect(callback) {
|
|
|
349
462
|
};
|
|
350
463
|
|
|
351
464
|
if (adapter.config.maxConnections) {
|
|
352
|
-
params.max_active = adapter.config.maxConnections;
|
|
465
|
+
params.max_active = adapter.config.maxConnections + 1; // we use our own Pool limiter logic, so let library have one more to not block us too early
|
|
353
466
|
params.max_wait = 10000; // hard code for now
|
|
354
467
|
params.when_exhausted = 'block';
|
|
355
468
|
}
|
|
@@ -368,14 +481,26 @@ function connect(callback) {
|
|
|
368
481
|
return adapter.log.error(`SQL package "${clients[adapter.config.dbtype].name}" is not installed.`);
|
|
369
482
|
}
|
|
370
483
|
|
|
371
|
-
if (adapter.config.dbtype !== 'postgresql') {
|
|
372
|
-
params.options = {
|
|
373
|
-
encrypt: !!adapter.config.encrypt
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
484
|
if (adapter.config.dbtype === 'postgresql') {
|
|
378
485
|
params.database = 'postgres';
|
|
486
|
+
if (adapter.config.encrypt) {
|
|
487
|
+
params.ssl = {
|
|
488
|
+
rejectUnauthorized: !!adapter.config.rejectUnauthorized
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
} else if (adapter.config.dbtype === 'mssql') {
|
|
492
|
+
params.options = {
|
|
493
|
+
encrypt: !!adapter.config.encrypt,
|
|
494
|
+
};
|
|
495
|
+
if (adapter.config.encrypt) {
|
|
496
|
+
params.options.trustServerCertificate = !adapter.config.rejectUnauthorized
|
|
497
|
+
}
|
|
498
|
+
} else if (adapter.config.dbtype === 'mysql') {
|
|
499
|
+
if (adapter.config.encrypt) {
|
|
500
|
+
params.ssl = {
|
|
501
|
+
rejectUnauthorized: !!adapter.config.rejectUnauthorized
|
|
502
|
+
};
|
|
503
|
+
}
|
|
379
504
|
}
|
|
380
505
|
|
|
381
506
|
if (adapter.config.dbtype === 'sqlite') {
|
|
@@ -384,10 +509,10 @@ function connect(callback) {
|
|
|
384
509
|
// special solution for postgres. Connect first to Db "postgres", create new DB "iobroker" and then connect to "iobroker" DB.
|
|
385
510
|
if (adapter.config.dbtype === 'postgresql' && !postgresDbCreated) {
|
|
386
511
|
// connect first to DB postgres and create iobroker DB
|
|
387
|
-
adapter.log.info(
|
|
512
|
+
adapter.log.info(`Postgres connection options: ${JSON.stringify(params).replace(params.password, '****')}`);
|
|
388
513
|
const _client = new SQL[clients[adapter.config.dbtype].name](params);
|
|
389
514
|
_client.on && _client.on('error', err =>
|
|
390
|
-
adapter.log.warn(
|
|
515
|
+
adapter.log.warn(`SQL client error: ${err}`));
|
|
391
516
|
|
|
392
517
|
return _client.connect(err => {
|
|
393
518
|
if (err) {
|
|
@@ -403,7 +528,7 @@ function connect(callback) {
|
|
|
403
528
|
reconnectTimeout && clearTimeout(reconnectTimeout);
|
|
404
529
|
reconnectTimeout = setTimeout(() => connect(callback), 100);
|
|
405
530
|
} else {
|
|
406
|
-
_client.execute(
|
|
531
|
+
_client.execute(`CREATE DATABASE ${adapter.config.dbname};`, (err /* , rows, fields */) => {
|
|
407
532
|
_client.disconnect();
|
|
408
533
|
if (err && err.code !== '42P04') { // if error not about yet exists
|
|
409
534
|
postgresDbCreated = false;
|
|
@@ -427,12 +552,13 @@ function connect(callback) {
|
|
|
427
552
|
try {
|
|
428
553
|
if (!clients[adapter.config.dbtype].name) {
|
|
429
554
|
adapter.log.error(`Unknown SQL type selected: "${adapter.config.dbtype}"`);
|
|
430
|
-
} else if (!SQL[clients[adapter.config.dbtype].name
|
|
555
|
+
} else if (!SQL[`${clients[adapter.config.dbtype].name}Pool`]) {
|
|
431
556
|
adapter.log.error(`Selected SQL DB was not installed properly: "${adapter.config.dbtype}". SQLite requires build tools on system. See README.md`);
|
|
432
557
|
} else {
|
|
433
|
-
clientPool = new SQL[clients[adapter.config.dbtype].name
|
|
558
|
+
clientPool = new SQL[`${clients[adapter.config.dbtype].name}Pool`](params);
|
|
434
559
|
|
|
435
560
|
return clientPool.open(err => {
|
|
561
|
+
activeConnections = 0;
|
|
436
562
|
if (err) {
|
|
437
563
|
clientPool = null;
|
|
438
564
|
setConnected(false);
|
|
@@ -453,6 +579,7 @@ function connect(callback) {
|
|
|
453
579
|
adapter.log.error(ex.stack);
|
|
454
580
|
}
|
|
455
581
|
clientPool = null;
|
|
582
|
+
activeConnections = 0;
|
|
456
583
|
setConnected(false);
|
|
457
584
|
reconnectTimeout && clearTimeout(reconnectTimeout);
|
|
458
585
|
reconnectTimeout = setTimeout(() => connect(callback), 30000);
|
|
@@ -466,7 +593,7 @@ function connect(callback) {
|
|
|
466
593
|
reconnectTimeout && clearTimeout(reconnectTimeout);
|
|
467
594
|
reconnectTimeout = setTimeout(() => connect(callback), 30000);
|
|
468
595
|
} else {
|
|
469
|
-
adapter.log.info(
|
|
596
|
+
adapter.log.info(`Connected to ${adapter.config.dbtype}`);
|
|
470
597
|
// read all DB IDs and all FROM ids
|
|
471
598
|
getAllIds(() =>
|
|
472
599
|
getAllFroms(callback));
|
|
@@ -483,18 +610,18 @@ function getSqlLiteDir(fileName) {
|
|
|
483
610
|
} else {
|
|
484
611
|
// normally /opt/iobroker/node_modules/iobroker.js-controller
|
|
485
612
|
// but can be /example/ioBroker.js-controller
|
|
486
|
-
const tools = require(utils.controllerDir
|
|
613
|
+
const tools = require(`${utils.controllerDir}/lib/tools`);
|
|
487
614
|
let config = tools.getConfigFileName().replace(/\\/g, '/');
|
|
488
615
|
const parts = config.split('/');
|
|
489
616
|
parts.pop();
|
|
490
|
-
config = parts.join('/')
|
|
617
|
+
config = `${parts.join('/')}/sqlite`;
|
|
491
618
|
|
|
492
619
|
// create sqlite directory
|
|
493
620
|
if (!fs.existsSync(config)) {
|
|
494
621
|
fs.mkdirSync(config);
|
|
495
622
|
}
|
|
496
623
|
|
|
497
|
-
return config
|
|
624
|
+
return `${config}/${fileName}`;
|
|
498
625
|
}
|
|
499
626
|
}
|
|
500
627
|
|
|
@@ -520,12 +647,16 @@ function testConnection(msg) {
|
|
|
520
647
|
Object.keys(postgres)
|
|
521
648
|
.filter(attr => !SQL[attr])
|
|
522
649
|
.forEach(attr => SQL[attr] = postgres[attr]);
|
|
523
|
-
} else
|
|
524
|
-
if (msg.message.config.dbtype === 'mssql' && !SQL.MSSQLClient) {
|
|
650
|
+
} else if (msg.message.config.dbtype === 'mssql' && !SQL.MSSQLClient) {
|
|
525
651
|
const mssql = require('./lib/mssql-client');
|
|
526
652
|
Object.keys(mssql)
|
|
527
653
|
.filter(attr => !SQL[attr])
|
|
528
654
|
.forEach(attr => SQL[attr] = mssql[attr]);
|
|
655
|
+
} else if (msg.message.config.dbtype === 'mysql' && !SQL.MySQL2Client) {
|
|
656
|
+
const mysql = require('./lib/mysql-client');
|
|
657
|
+
Object.keys(mysql)
|
|
658
|
+
.filter(attr => !SQL[attr])
|
|
659
|
+
.forEach(attr => SQL[attr] = mysql[attr]);
|
|
529
660
|
}
|
|
530
661
|
|
|
531
662
|
if (msg.message.config.dbtype === 'postgresql') {
|
|
@@ -534,10 +665,31 @@ function testConnection(msg) {
|
|
|
534
665
|
params = getSqlLiteDir(msg.message.config.fileName);
|
|
535
666
|
}
|
|
536
667
|
|
|
668
|
+
if (msg.message.config.dbtype === 'postgresql') {
|
|
669
|
+
if (msg.message.config.encrypt) {
|
|
670
|
+
params.ssl = {
|
|
671
|
+
rejectUnauthorized: !!msg.message.config.rejectUnauthorized
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
} else if (msg.message.config.dbtype === 'mssql') {
|
|
675
|
+
params.options = {
|
|
676
|
+
encrypt: !!msg.message.config.encrypt,
|
|
677
|
+
};
|
|
678
|
+
if (msg.message.config.encrypt) {
|
|
679
|
+
params.options. trustServerCertificate= !msg.message.config.rejectUnauthorized
|
|
680
|
+
}
|
|
681
|
+
} else if (msg.message.config.dbtype === 'mysql') {
|
|
682
|
+
if (msg.message.config.encrypt) {
|
|
683
|
+
params.ssl = {
|
|
684
|
+
rejectUnauthorized: !!msg.message.config.rejectUnauthorized
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
537
689
|
try {
|
|
538
690
|
const client = new SQL[clients[msg.message.config.dbtype].name](params);
|
|
539
691
|
client.on && client.on('error', err =>
|
|
540
|
-
adapter.log.warn(
|
|
692
|
+
adapter.log.warn(`SQL client error: ${err}`));
|
|
541
693
|
|
|
542
694
|
testConnectTimeout = setTimeout(() => {
|
|
543
695
|
testConnectTimeout = null;
|
|
@@ -586,7 +738,7 @@ function destroyDB(msg) {
|
|
|
586
738
|
adapter.sendTo(msg.from, msg.command, {error: null, result: 'deleted'}, msg.callback);
|
|
587
739
|
// restart adapter
|
|
588
740
|
setTimeout(() =>
|
|
589
|
-
adapter.getForeignObject(
|
|
741
|
+
adapter.getForeignObject(`system.adapter.${adapter.namespace}`, (err, obj) => {
|
|
590
742
|
if (!err) {
|
|
591
743
|
adapter.setForeignObject(obj._id, obj);
|
|
592
744
|
} else {
|
|
@@ -611,11 +763,18 @@ function _userQuery(msg, callback) {
|
|
|
611
763
|
borrowClientFromPool((err, client) => {
|
|
612
764
|
if (err) {
|
|
613
765
|
adapter.sendTo(msg.from, msg.command, {error: err.toString()}, msg.callback);
|
|
766
|
+
returnClientToPool(client);
|
|
614
767
|
callback && callback();
|
|
615
768
|
} else {
|
|
616
769
|
client.execute(msg.message, (err, rows /* , fields */) => {
|
|
617
770
|
if (rows && rows.rows) rows = rows.rows;
|
|
618
771
|
returnClientToPool(client);
|
|
772
|
+
//convert ts for postgresql and ms sqlserver
|
|
773
|
+
if (!err && rows && rows[0] && typeof rows[0].ts === 'string') {
|
|
774
|
+
for (let i = 0; i < rows.length; i++) {
|
|
775
|
+
rows[i].ts = parseInt(rows[i].ts, 10);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
619
778
|
adapter.sendTo(msg.from, msg.command, {error: err ? err.toString() : null, result: rows}, msg.callback);
|
|
620
779
|
callback && callback();
|
|
621
780
|
});
|
|
@@ -631,7 +790,7 @@ function _userQuery(msg, callback) {
|
|
|
631
790
|
function query(msg) {
|
|
632
791
|
if (!multiRequests) {
|
|
633
792
|
if (tasks.length > MAXTASKS) {
|
|
634
|
-
const error =
|
|
793
|
+
const error = `Cannot queue new requests, because more than ${MAXTASKS}`;
|
|
635
794
|
adapter.log.error(error);
|
|
636
795
|
adapter.sendTo(msg.from, msg.command, {error}, msg.callback);
|
|
637
796
|
} else {
|
|
@@ -649,6 +808,7 @@ function oneScript(script, cb) {
|
|
|
649
808
|
borrowClientFromPool((err, client) => {
|
|
650
809
|
if (err || !client) {
|
|
651
810
|
clientPool && clientPool.close();
|
|
811
|
+
activeConnections = 0;
|
|
652
812
|
clientPool = null;
|
|
653
813
|
setConnected(false);
|
|
654
814
|
adapter.log.error(err);
|
|
@@ -658,7 +818,7 @@ function oneScript(script, cb) {
|
|
|
658
818
|
adapter.log.debug(script);
|
|
659
819
|
|
|
660
820
|
client.execute(script, (err /* , rows, fields */) => {
|
|
661
|
-
adapter.log.debug(
|
|
821
|
+
adapter.log.debug(`Response: ${JSON.stringify(err)}`);
|
|
662
822
|
if (err) {
|
|
663
823
|
// Database 'iobroker' already exists. Choose a different database name.
|
|
664
824
|
if (err.number === 1801 ||
|
|
@@ -732,6 +892,23 @@ function allScripts(scripts, index, cb) {
|
|
|
732
892
|
|
|
733
893
|
function finish(callback) {
|
|
734
894
|
|
|
895
|
+
function allFinished() {
|
|
896
|
+
if (clientPool) {
|
|
897
|
+
clientPool.close();
|
|
898
|
+
activeConnections = 0;
|
|
899
|
+
clientPool = null;
|
|
900
|
+
setConnected(false);
|
|
901
|
+
}
|
|
902
|
+
if (typeof finished === 'object') {
|
|
903
|
+
setTimeout(cb => {
|
|
904
|
+
for (let f = 0; f < cb.length; f++) {
|
|
905
|
+
typeof cb[f] === 'function' && cb[f]();
|
|
906
|
+
}
|
|
907
|
+
}, 500, finished);
|
|
908
|
+
finished = true;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
735
912
|
function finishId(id) {
|
|
736
913
|
if (!sqlDPs[id]) {
|
|
737
914
|
return;
|
|
@@ -747,57 +924,37 @@ function finish(callback) {
|
|
|
747
924
|
let tmpState = Object.assign({}, sqlDPs[id].state);
|
|
748
925
|
const state = sqlDPs[id].state ? tmpState : null;
|
|
749
926
|
|
|
750
|
-
if (sqlDPs[id].skipped) {
|
|
927
|
+
if (sqlDPs[id].skipped && !(sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].disableSkippedValueLogging)) {
|
|
751
928
|
count++;
|
|
752
|
-
pushValueIntoDB(id, sqlDPs[id].skipped, () => {
|
|
929
|
+
pushValueIntoDB(id, sqlDPs[id].skipped, false, true,() => {
|
|
753
930
|
if (!--count) {
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
setConnected(false);
|
|
758
|
-
}
|
|
759
|
-
if (typeof finished === 'object') {
|
|
760
|
-
setTimeout(cb => {
|
|
761
|
-
for (let f = 0; f < cb.length; f++) {
|
|
762
|
-
typeof cb[f] === 'function' && cb[f]();
|
|
763
|
-
}
|
|
764
|
-
}, 500, finished);
|
|
765
|
-
finished = true;
|
|
766
|
-
}
|
|
931
|
+
pushValuesIntoDB(id, sqlDPs[id].list, () => {
|
|
932
|
+
allFinished();
|
|
933
|
+
});
|
|
767
934
|
}
|
|
768
935
|
});
|
|
769
936
|
sqlDPs[id].skipped = null;
|
|
770
937
|
}
|
|
771
938
|
|
|
772
|
-
const nullValue = {val: null, ts: now, lc: now, q: 0x40, from:
|
|
939
|
+
const nullValue = {val: null, ts: now, lc: now, q: 0x40, from: `system.adapter.${adapter.namespace}`};
|
|
773
940
|
|
|
774
941
|
if (sqlDPs[id][adapter.namespace] && adapter.config.writeNulls) {
|
|
775
942
|
if (sqlDPs[id][adapter.namespace].changesOnly && state && state.val !== null) {
|
|
776
943
|
count++;
|
|
777
944
|
(function (_id, _state, _nullValue) {
|
|
778
945
|
_state.ts = now;
|
|
779
|
-
_state.from =
|
|
946
|
+
_state.from = `system.adapter.${adapter.namespace}`;
|
|
780
947
|
nullValue.ts += 4;
|
|
781
948
|
nullValue.lc += 4; // because of MS SQL
|
|
782
949
|
adapter.log.debug(`Write 1/2 "${_state.val}" _id: ${_id}`);
|
|
783
|
-
pushValueIntoDB(_id, _state, () => {
|
|
784
|
-
// terminate values with null to indicate adapter stop. timestamp + 1
|
|
950
|
+
pushValueIntoDB(_id, _state, false, true,() => {
|
|
951
|
+
// terminate values with null to indicate adapter stop. timestamp + 1
|
|
785
952
|
adapter.log.debug(`Write 2/2 "null" _id: ${_id}`);
|
|
786
|
-
pushValueIntoDB(_id, _nullValue, () => {
|
|
953
|
+
pushValueIntoDB(_id, _nullValue, false, true,() => {
|
|
787
954
|
if (!--count) {
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
setConnected(false);
|
|
792
|
-
}
|
|
793
|
-
if (typeof finished === 'object') {
|
|
794
|
-
setTimeout(cb => {
|
|
795
|
-
for (let f = 0; f < cb.length; f++) {
|
|
796
|
-
typeof cb[f] === 'function' && cb[f]();
|
|
797
|
-
}
|
|
798
|
-
}, 500, finished);
|
|
799
|
-
finished = true;
|
|
800
|
-
}
|
|
955
|
+
pushValuesIntoDB(id, sqlDPs[id].list, () => {
|
|
956
|
+
allFinished();
|
|
957
|
+
});
|
|
801
958
|
}
|
|
802
959
|
});
|
|
803
960
|
});
|
|
@@ -805,24 +962,12 @@ function finish(callback) {
|
|
|
805
962
|
} else {
|
|
806
963
|
// terminate values with null to indicate adapter stop. timestamp + 1
|
|
807
964
|
count++;
|
|
808
|
-
adapter.log.debug(
|
|
809
|
-
pushValueIntoDB(id, nullValue, () => {
|
|
965
|
+
adapter.log.debug(`Write 0 NULL _id: ${id}`);
|
|
966
|
+
pushValueIntoDB(id, nullValue, false, true,() => {
|
|
810
967
|
if (!--count) {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
clientPool = null;
|
|
815
|
-
setConnected(false);
|
|
816
|
-
}
|
|
817
|
-
if (typeof finished === 'object') {
|
|
818
|
-
setTimeout(cb => {
|
|
819
|
-
for (let f = 0; f < cb.length; f++) {
|
|
820
|
-
typeof cb[f] === 'function' && cb[f]();
|
|
821
|
-
}
|
|
822
|
-
}, 500, finished);
|
|
823
|
-
finished = true;
|
|
824
|
-
}
|
|
825
|
-
}, 5000);
|
|
968
|
+
pushValuesIntoDB(id, sqlDPs[id].list, () => {
|
|
969
|
+
allFinished();
|
|
970
|
+
});
|
|
826
971
|
}
|
|
827
972
|
});
|
|
828
973
|
}
|
|
@@ -837,7 +982,7 @@ function finish(callback) {
|
|
|
837
982
|
}
|
|
838
983
|
} else {
|
|
839
984
|
subscribeAll = false;
|
|
840
|
-
adapter.
|
|
985
|
+
adapter.unsubscribeForeignStates('*');
|
|
841
986
|
}
|
|
842
987
|
|
|
843
988
|
if (reconnectTimeout) {
|
|
@@ -848,6 +993,14 @@ function finish(callback) {
|
|
|
848
993
|
clearTimeout(testConnectTimeout);
|
|
849
994
|
testConnectTimeout = null;
|
|
850
995
|
}
|
|
996
|
+
if (dpOverviewTimeout) {
|
|
997
|
+
clearTimeout(dpOverviewTimeout);
|
|
998
|
+
dpOverviewTimeout = null;
|
|
999
|
+
}
|
|
1000
|
+
if (bufferChecker) {
|
|
1001
|
+
clearInterval(bufferChecker);
|
|
1002
|
+
bufferChecker = null;
|
|
1003
|
+
}
|
|
851
1004
|
|
|
852
1005
|
let count = 0;
|
|
853
1006
|
if (finished) {
|
|
@@ -865,7 +1018,9 @@ function finish(callback) {
|
|
|
865
1018
|
let dpcount = 0;
|
|
866
1019
|
let delay = 0;
|
|
867
1020
|
for (const id in sqlDPs) {
|
|
868
|
-
if (!sqlDPs.hasOwnProperty(id))
|
|
1021
|
+
if (!sqlDPs.hasOwnProperty(id)) {
|
|
1022
|
+
continue;
|
|
1023
|
+
}
|
|
869
1024
|
dpcount++;
|
|
870
1025
|
delay += (dpcount%50 === 0) ? 1000: 0;
|
|
871
1026
|
setTimeout(finishId, delay, id);
|
|
@@ -874,6 +1029,7 @@ function finish(callback) {
|
|
|
874
1029
|
if (!dpcount && callback) {
|
|
875
1030
|
if (clientPool) {
|
|
876
1031
|
clientPool.close();
|
|
1032
|
+
activeConnections = 0;
|
|
877
1033
|
clientPool = null;
|
|
878
1034
|
setConnected(false);
|
|
879
1035
|
}
|
|
@@ -930,7 +1086,7 @@ function processMessage(msg) {
|
|
|
930
1086
|
finish(() => {
|
|
931
1087
|
if (msg.callback) {
|
|
932
1088
|
adapter.sendTo(msg.from, msg.command, 'stopped', msg.callback);
|
|
933
|
-
setTimeout(() =>
|
|
1089
|
+
setTimeout(() => adapter.stop(), 200);
|
|
934
1090
|
}
|
|
935
1091
|
});
|
|
936
1092
|
}
|
|
@@ -948,13 +1104,13 @@ function processStartValues(callback) {
|
|
|
948
1104
|
lc: state ? now - 4 : now, // 4 is because of MS SQL
|
|
949
1105
|
ack: true,
|
|
950
1106
|
q: 0x40,
|
|
951
|
-
from:
|
|
1107
|
+
from: `system.adapter.${adapter.namespace}`
|
|
952
1108
|
});
|
|
953
1109
|
|
|
954
1110
|
if (state) {
|
|
955
1111
|
state.ts = now;
|
|
956
1112
|
state.lc = now;
|
|
957
|
-
state.from =
|
|
1113
|
+
state.from = `system.adapter.${adapter.namespace}`;
|
|
958
1114
|
pushHistory(task.id, state);
|
|
959
1115
|
}
|
|
960
1116
|
|
|
@@ -962,18 +1118,19 @@ function processStartValues(callback) {
|
|
|
962
1118
|
});
|
|
963
1119
|
}
|
|
964
1120
|
else {
|
|
1121
|
+
const now = Date.now();
|
|
965
1122
|
pushHistory(task.id, {
|
|
966
1123
|
val: null,
|
|
967
|
-
ts: task.now ||
|
|
968
|
-
lc: task.now ||
|
|
1124
|
+
ts: task.now || now,
|
|
1125
|
+
lc: task.now || now,
|
|
969
1126
|
ack: true,
|
|
970
1127
|
q: 0x40,
|
|
971
|
-
from:
|
|
1128
|
+
from: `system.adapter.${adapter.namespace}`
|
|
972
1129
|
});
|
|
973
1130
|
|
|
974
1131
|
setImmediate(processStartValues);
|
|
975
1132
|
}
|
|
976
|
-
if (sqlDPs[task.id][adapter.namespace] && sqlDPs[task.id][adapter.namespace].changesRelogInterval > 0) {
|
|
1133
|
+
if (sqlDPs[task.id][adapter.namespace] && sqlDPs[task.id][adapter.namespace].changesOnly && sqlDPs[task.id][adapter.namespace].changesRelogInterval > 0) {
|
|
977
1134
|
sqlDPs[task.id].relogTimeout && clearTimeout(sqlDPs[task.id].relogTimeout);
|
|
978
1135
|
sqlDPs[task.id].relogTimeout = setTimeout(reLogHelper, (sqlDPs[task.id][adapter.namespace].changesRelogInterval * 500 * Math.random()) + sqlDPs[task.id][adapter.namespace].changesRelogInterval * 500, task.id);
|
|
979
1136
|
}
|
|
@@ -1017,52 +1174,97 @@ function pushHistory(id, state, timerRelog) {
|
|
|
1017
1174
|
return adapter.log.warn(`state value undefined received for ${id} which is not allowed. Ignoring.`);
|
|
1018
1175
|
}
|
|
1019
1176
|
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
const f = parseFloat(state.val);
|
|
1024
|
-
// do not use here === or find a better way to validate if string is a valid float
|
|
1025
|
-
if (f == state.val) {
|
|
1026
|
-
state.val = f;
|
|
1177
|
+
if (typeof state.val === 'string' && settings.storageType !== 'String') {
|
|
1178
|
+
if (isFinite(state.val)) {
|
|
1179
|
+
state.val = parseFloat(state.val);
|
|
1027
1180
|
}
|
|
1028
1181
|
}
|
|
1029
1182
|
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1183
|
+
settings.enableDebugLogs && adapter.log.debug(`new value received for ${id} (storageType ${settings.storageType}), new-value=${state.val}, ts=${state.ts}, relog=${timerRelog}`);
|
|
1184
|
+
|
|
1185
|
+
let ignoreDebonce = false;
|
|
1186
|
+
|
|
1187
|
+
if (!timerRelog) {
|
|
1188
|
+
const valueUnstable = !!sqlDPs[id].timeout;
|
|
1189
|
+
// When a debounce timer runs and the value is the same as the last one, ignore it
|
|
1190
|
+
if (sqlDPs[id].timeout && state.ts !== state.lc) {
|
|
1191
|
+
settings.enableDebugLogs && adapter.log.debug(`value not changed debounce ${id}, value=${state.val}, ts=${state.ts}, debounce timer keeps running`);
|
|
1192
|
+
return;
|
|
1193
|
+
} else if (sqlDPs[id].timeout) { // if value changed, clear timer
|
|
1194
|
+
settings.enableDebugLogs && adapter.log.debug(`value changed during debounce time ${id}, value=${state.val}, ts=${state.ts}, debounce timer restarted`);
|
|
1195
|
+
clearTimeout(sqlDPs[id].timeout);
|
|
1196
|
+
sqlDPs[id].timeout = null;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
if (!valueUnstable && settings.blockTime && sqlDPs[id].state && (sqlDPs[id].state.ts + settings.blockTime) > state.ts) {
|
|
1200
|
+
settings.enableDebugLogs && adapter.log.debug(`value ignored blockTime ${id}, value=${state.val}, ts=${state.ts}, lastState.ts=${sqlDPs[id].state.ts}, blockTime=${settings.blockTime}`);
|
|
1201
|
+
return;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
if (settings.ignoreZero && (state.val === undefined || state.val === null || state.val === 0)) {
|
|
1205
|
+
settings.enableDebugLogs && adapter.log.debug(`value ignore because zero or null ${id}, new-value=${state.val}, ts=${state.ts}`);
|
|
1206
|
+
return;
|
|
1033
1207
|
} else
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1208
|
+
if (typeof settings.ignoreBelowNumber === 'number' && typeof state.val === 'number' && state.val < settings.ignoreBelowNumber) {
|
|
1209
|
+
settings.enableDebugLogs && adapter.log.debug(`value ignored because below ${settings.ignoreBelowNumber} for ${id}, new-value=${state.val}, ts=${state.ts}`);
|
|
1210
|
+
return;
|
|
1211
|
+
}
|
|
1212
|
+
if (typeof settings.ignoreAboveNumber === 'number' && typeof state.val === 'number' && state.val > settings.ignoreAboveNumber) {
|
|
1213
|
+
settings.enableDebugLogs && adapter.log.debug(`value ignored because above ${settings.ignoreAboveNumber} for ${id}, new-value=${state.val}, ts=${state.ts}`);
|
|
1214
|
+
return;
|
|
1039
1215
|
}
|
|
1040
|
-
}
|
|
1041
1216
|
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1217
|
+
if (sqlDPs[id].state && settings.changesOnly) {
|
|
1218
|
+
if (settings.changesRelogInterval === 0) {
|
|
1219
|
+
if ((sqlDPs[id].state.val !== null || state.val === null) && state.ts !== state.lc) {
|
|
1220
|
+
// remember new timestamp
|
|
1221
|
+
if (!valueUnstable && !settings.disableSkippedValueLogging) {
|
|
1222
|
+
sqlDPs[id].skipped = state;
|
|
1223
|
+
}
|
|
1224
|
+
settings.enableDebugLogs && adapter.log.debug(`value not changed ${id}, last-value=${sqlDPs[id].state.val}, new-value=${state.val}, ts=${state.ts}`);
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
} else if (sqlDPs[id].lastLogTime) {
|
|
1228
|
+
if ((sqlDPs[id].state.val !== null || state.val === null) && (state.ts !== state.lc) && (Math.abs(sqlDPs[id].lastLogTime - state.ts) < settings.changesRelogInterval * 1000)) {
|
|
1229
|
+
// remember new timestamp
|
|
1230
|
+
if (!valueUnstable && !settings.disableSkippedValueLogging) {
|
|
1231
|
+
sqlDPs[id].skipped = state;
|
|
1232
|
+
}
|
|
1233
|
+
settings.enableDebugLogs && adapter.log.debug(`value not changed ${id}, last-value=${sqlDPs[id].state.val}, new-value=${state.val}, ts=${state.ts}`);
|
|
1234
|
+
return;
|
|
1235
|
+
}
|
|
1236
|
+
if (state.ts !== state.lc) {
|
|
1237
|
+
settings.enableDebugLogs && adapter.log.debug(`value-not-changed-relog ${id}, value=${state.val}, lastLogTime=${sqlDPs[id].lastLogTime}, ts=${state.ts}`);
|
|
1238
|
+
ignoreDebonce = true;
|
|
1239
|
+
}
|
|
1052
1240
|
}
|
|
1053
|
-
if (state.
|
|
1054
|
-
|
|
1241
|
+
if (typeof state.val === 'number') {
|
|
1242
|
+
if (
|
|
1243
|
+
sqlDPs[id].state.val !== null &&
|
|
1244
|
+
settings.changesMinDelta !== 0 &&
|
|
1245
|
+
Math.abs(sqlDPs[id].state.val - state.val) < settings.changesMinDelta
|
|
1246
|
+
) {
|
|
1247
|
+
if (!valueUnstable && !settings.disableSkippedValueLogging) {
|
|
1248
|
+
sqlDPs[id].skipped = state;
|
|
1249
|
+
}
|
|
1250
|
+
settings.enableDebugLogs && adapter.log.debug(`Min-Delta not reached ${id}, last-value=${sqlDPs[id].state.val}, new-value=${state.val}, ts=${state.ts}`);
|
|
1251
|
+
return;
|
|
1252
|
+
} else if (settings.changesMinDelta !== 0) {
|
|
1253
|
+
settings.enableDebugLogs && adapter.log.debug(`Min-Delta reached ${id}, last-value=${sqlDPs[id].state.val}, new-value=${state.val}, ts=${state.ts}`);
|
|
1254
|
+
}
|
|
1255
|
+
} else {
|
|
1256
|
+
settings.enableDebugLogs && adapter.log.debug(`Min-Delta ignored because no number ${id}, last-value=${sqlDPs[id].state.val}, new-value=${state.val}, ts=${state.ts}`);
|
|
1055
1257
|
}
|
|
1056
1258
|
}
|
|
1259
|
+
}
|
|
1057
1260
|
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
adapter.log.debug(`Min-Delta ignored because no number ${id}, last-value=${sqlDPs[id].state.val}, new-value=${state.val}, ts=${state.ts}`);
|
|
1261
|
+
if (settings.counter && sqlDPs[id].state) {
|
|
1262
|
+
if (sqlDPs[id].type !== types.number) {
|
|
1263
|
+
adapter.log.error('Counter must have type "number"!');
|
|
1264
|
+
} else if (state.val < sqlDPs[id].state.val) {
|
|
1265
|
+
// if actual value is less then last seen counter, store both values
|
|
1266
|
+
pushValueIntoDB(id, sqlDPs[id].state, true);
|
|
1267
|
+
pushValueIntoDB(id, state, true);
|
|
1066
1268
|
}
|
|
1067
1269
|
}
|
|
1068
1270
|
|
|
@@ -1071,54 +1273,61 @@ function pushHistory(id, state, timerRelog) {
|
|
|
1071
1273
|
sqlDPs[id].relogTimeout = null;
|
|
1072
1274
|
}
|
|
1073
1275
|
|
|
1074
|
-
if (settings.changesRelogInterval > 0) {
|
|
1075
|
-
sqlDPs[id].relogTimeout = setTimeout(reLogHelper, settings.changesRelogInterval * 1000, id);
|
|
1076
|
-
}
|
|
1077
|
-
|
|
1078
|
-
let ignoreDebonce = false;
|
|
1079
1276
|
if (timerRelog) {
|
|
1277
|
+
state = Object.assign({}, state);
|
|
1080
1278
|
state.ts = Date.now();
|
|
1081
|
-
state.from =
|
|
1082
|
-
adapter.log.debug(`timed-relog ${id}, value=${state.val}, lastLogTime=${sqlDPs[id].lastLogTime}, ts=${state.ts}`);
|
|
1279
|
+
state.from = `system.adapter.${adapter.namespace}`;
|
|
1280
|
+
settings.enableDebugLogs && adapter.log.debug(`timed-relog ${id}, value=${state.val}, lastLogTime=${sqlDPs[id].lastLogTime}, ts=${state.ts}`);
|
|
1083
1281
|
ignoreDebonce = true;
|
|
1084
1282
|
} else {
|
|
1085
1283
|
if (settings.changesOnly && sqlDPs[id].skipped) {
|
|
1086
|
-
sqlDPs[id].
|
|
1087
|
-
pushHelper(id);
|
|
1284
|
+
settings.enableDebugLogs && adapter.log.debug(`Skipped value logged ${id}, value=${sqlDPs[id].skipped.val}, ts=${sqlDPs[id].skipped.ts}`);
|
|
1285
|
+
pushHelper(id, sqlDPs[id].skipped);
|
|
1286
|
+
sqlDPs[id].skipped = null;
|
|
1088
1287
|
}
|
|
1089
|
-
|
|
1090
1288
|
if (sqlDPs[id].state && ((sqlDPs[id].state.val === null && state.val !== null) || (sqlDPs[id].state.val !== null && state.val === null))) {
|
|
1091
1289
|
ignoreDebonce = true;
|
|
1092
1290
|
} else if (!sqlDPs[id].state && state.val === null) {
|
|
1093
1291
|
ignoreDebonce = true;
|
|
1094
1292
|
}
|
|
1095
|
-
|
|
1096
|
-
// only store state if really changed
|
|
1097
|
-
sqlDPs[id].state = state;
|
|
1098
1293
|
}
|
|
1099
1294
|
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
if (settings.debounce && !ignoreDebonce) {
|
|
1104
|
-
// Discard changes in debounce time to store last stable value
|
|
1295
|
+
if (settings.debounceTime && !ignoreDebonce && !timerRelog) {
|
|
1296
|
+
// Discard changes in de-bounce time to store last stable value
|
|
1105
1297
|
sqlDPs[id].timeout && clearTimeout(sqlDPs[id].timeout);
|
|
1106
|
-
sqlDPs[id].timeout = setTimeout(
|
|
1298
|
+
sqlDPs[id].timeout = setTimeout((id, state) => {
|
|
1299
|
+
sqlDPs[id].timeout = null;
|
|
1300
|
+
sqlDPs[id].state = state;
|
|
1301
|
+
sqlDPs[id].lastLogTime = state.ts;
|
|
1302
|
+
settings.enableDebugLogs && adapter.log.debug(`Value logged ${id}, value=${sqlDPs[id].state.val}, ts=${sqlDPs[id].state.ts}`);
|
|
1303
|
+
pushHelper(id);
|
|
1304
|
+
if (settings.changesOnly && settings.changesRelogInterval > 0) {
|
|
1305
|
+
sqlDPs[id].relogTimeout = setTimeout(reLogHelper, settings.changesRelogInterval * 1000, id);
|
|
1306
|
+
}
|
|
1307
|
+
}, settings.debounceTime, id, state);
|
|
1107
1308
|
} else {
|
|
1108
|
-
|
|
1309
|
+
if (!timerRelog) {
|
|
1310
|
+
sqlDPs[id].state = state;
|
|
1311
|
+
}
|
|
1312
|
+
sqlDPs[id].lastLogTime = state.ts;
|
|
1313
|
+
|
|
1314
|
+
settings.enableDebugLogs && adapter.log.debug(`Value logged ${id}, value=${sqlDPs[id].state.val}, ts=${sqlDPs[id].state.ts}`);
|
|
1315
|
+
pushHelper(id, state);
|
|
1316
|
+
if (settings.changesOnly && settings.changesRelogInterval > 0) {
|
|
1317
|
+
sqlDPs[id].relogTimeout = setTimeout(reLogHelper, settings.changesRelogInterval * 1000, id);
|
|
1318
|
+
}
|
|
1109
1319
|
}
|
|
1110
1320
|
}
|
|
1111
1321
|
}
|
|
1112
1322
|
|
|
1113
1323
|
function reLogHelper(_id) {
|
|
1114
1324
|
if (!sqlDPs[_id]) {
|
|
1115
|
-
adapter.log.info(
|
|
1325
|
+
adapter.log.info(`non-existing id ${_id}`);
|
|
1116
1326
|
} else {
|
|
1117
1327
|
sqlDPs[_id].relogTimeout = null;
|
|
1118
1328
|
if (sqlDPs[_id].skipped) {
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
sqlDPs[_id].skipped = null;
|
|
1329
|
+
pushHistory(_id, sqlDPs[_id].skipped, true);
|
|
1330
|
+
} else if (sqlDPs[_id].state) {
|
|
1122
1331
|
pushHistory(_id, sqlDPs[_id].state, true);
|
|
1123
1332
|
} else {
|
|
1124
1333
|
adapter.getForeignState(sqlDPs[_id].realId, (err, state) => {
|
|
@@ -1136,57 +1345,50 @@ function reLogHelper(_id) {
|
|
|
1136
1345
|
}
|
|
1137
1346
|
}
|
|
1138
1347
|
|
|
1139
|
-
function pushHelper(_id,
|
|
1140
|
-
if (!sqlDPs[_id] || !sqlDPs[_id].state) {
|
|
1348
|
+
function pushHelper(_id, state, cb) {
|
|
1349
|
+
if (!sqlDPs[_id] || (!sqlDPs[_id].state && !state)) {
|
|
1141
1350
|
return;
|
|
1142
1351
|
}
|
|
1352
|
+
if (!state) {
|
|
1353
|
+
state = sqlDPs[_id].state;
|
|
1354
|
+
}
|
|
1143
1355
|
|
|
1144
|
-
const _settings = sqlDPs[_id][adapter.namespace];
|
|
1145
|
-
|
|
1146
|
-
// if it was not deleted in this time
|
|
1147
|
-
if (_settings) {
|
|
1148
|
-
if (timeoutTriggered) {
|
|
1149
|
-
sqlDPs[_id].timeout = null;
|
|
1150
|
-
}
|
|
1356
|
+
const _settings = sqlDPs[_id] && sqlDPs[_id][adapter.namespace] || {};
|
|
1151
1357
|
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
if (sqlDPs[_id].state.val !== null && sqlDPs[_id].state.val !== undefined) {
|
|
1157
|
-
adapter.log.debug(`Datatype ${_id}: Currently: ${typeof sqlDPs[_id].state.val}, StorageType: ${_settings.storageType}`);
|
|
1358
|
+
if (state.val !== null && (typeof state.val === 'object' || typeof state.val === 'undefined')) {
|
|
1359
|
+
state.val = JSON.stringify(state.val);
|
|
1360
|
+
}
|
|
1158
1361
|
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
const f = parseFloat(sqlDPs[_id].state.val);
|
|
1362
|
+
if (state.val !== null && state.val !== undefined) {
|
|
1363
|
+
_settings.enableDebugLogs && adapter.log.debug(`Datatype ${_id}: Currently: ${typeof state.val}, StorageType: ${_settings.storageType}`);
|
|
1162
1364
|
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1365
|
+
if (typeof state.val === 'string' && _settings.storageType !== 'String') {
|
|
1366
|
+
_settings.enableDebugLogs && adapter.log.debug(`Do Automatic Datatype conversion for ${_id}`);
|
|
1367
|
+
if (isFinite(state.val)) {
|
|
1368
|
+
state.val = parseFloat(state.val);
|
|
1369
|
+
} else if (state.val === 'true') {
|
|
1370
|
+
state.val = true;
|
|
1371
|
+
} else if (state.val === 'false') {
|
|
1372
|
+
state.val = false;
|
|
1171
1373
|
}
|
|
1374
|
+
}
|
|
1172
1375
|
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
}
|
|
1181
|
-
} else if (_settings.storageType === 'Boolean' && typeof sqlDPs[_id].state.val !== 'boolean') {
|
|
1182
|
-
sqlDPs[_id].state.val = !!sqlDPs[_id].state.val;
|
|
1376
|
+
if (_settings.storageType === 'String' && typeof state.val !== 'string') {
|
|
1377
|
+
state.val = state.val.toString();
|
|
1378
|
+
} else if (_settings.storageType === 'Number' && typeof state.val !== 'number') {
|
|
1379
|
+
if (typeof state.val === 'boolean') {
|
|
1380
|
+
state.val = state.val ? 1 : 0;
|
|
1381
|
+
} else {
|
|
1382
|
+
return adapter.log.info(`Do not store value "${state.val}" for ${_id} because no number`);
|
|
1183
1383
|
}
|
|
1184
|
-
} else {
|
|
1185
|
-
|
|
1384
|
+
} else if (_settings.storageType === 'Boolean' && typeof state.val !== 'boolean') {
|
|
1385
|
+
state.val = !!state.val;
|
|
1186
1386
|
}
|
|
1187
|
-
|
|
1188
|
-
|
|
1387
|
+
} else {
|
|
1388
|
+
_settings.enableDebugLogs && adapter.log.debug(`Datatype ${_id}: Currently: null`);
|
|
1189
1389
|
}
|
|
1390
|
+
|
|
1391
|
+
pushValueIntoDB(_id, state, cb);
|
|
1190
1392
|
}
|
|
1191
1393
|
|
|
1192
1394
|
function getAllIds(cb) {
|
|
@@ -1195,6 +1397,7 @@ function getAllIds(cb) {
|
|
|
1195
1397
|
|
|
1196
1398
|
borrowClientFromPool((err, client) => {
|
|
1197
1399
|
if (err) {
|
|
1400
|
+
returnClientToPool(client);
|
|
1198
1401
|
return cb && cb(err);
|
|
1199
1402
|
}
|
|
1200
1403
|
|
|
@@ -1202,7 +1405,7 @@ function getAllIds(cb) {
|
|
|
1202
1405
|
returnClientToPool(client);
|
|
1203
1406
|
if (rows && rows.rows) rows = rows.rows;
|
|
1204
1407
|
if (err) {
|
|
1205
|
-
adapter.log.error(
|
|
1408
|
+
adapter.log.error(`Cannot select ${query}: ${err}`);
|
|
1206
1409
|
return cb && cb(err);
|
|
1207
1410
|
}
|
|
1208
1411
|
|
|
@@ -1226,6 +1429,7 @@ function getAllFroms(cb) {
|
|
|
1226
1429
|
|
|
1227
1430
|
borrowClientFromPool((err, client) => {
|
|
1228
1431
|
if (err) {
|
|
1432
|
+
returnClientToPool(client);
|
|
1229
1433
|
return cb && cb(err);
|
|
1230
1434
|
}
|
|
1231
1435
|
|
|
@@ -1233,7 +1437,7 @@ function getAllFroms(cb) {
|
|
|
1233
1437
|
returnClientToPool(client);
|
|
1234
1438
|
if (rows && rows.rows) rows = rows.rows;
|
|
1235
1439
|
if (err) {
|
|
1236
|
-
adapter.log.error(
|
|
1440
|
+
adapter.log.error(`Cannot select ${query}: ${err}`);
|
|
1237
1441
|
return cb && cb(err);
|
|
1238
1442
|
}
|
|
1239
1443
|
|
|
@@ -1253,12 +1457,13 @@ function _checkRetention(query, cb) {
|
|
|
1253
1457
|
|
|
1254
1458
|
borrowClientFromPool((err, client) => {
|
|
1255
1459
|
if (err) {
|
|
1460
|
+
returnClientToPool(client);
|
|
1256
1461
|
adapter.log.error(err);
|
|
1257
1462
|
cb && cb();
|
|
1258
1463
|
} else {
|
|
1259
1464
|
client.execute(query, (err /* , rows, fields */ ) => {
|
|
1260
1465
|
returnClientToPool(client);
|
|
1261
|
-
err && adapter.log.warn(
|
|
1466
|
+
err && adapter.log.warn(`Retention: Cannot delete ${query}: ${err}`);
|
|
1262
1467
|
cb && cb();
|
|
1263
1468
|
});
|
|
1264
1469
|
}
|
|
@@ -1267,8 +1472,7 @@ function _checkRetention(query, cb) {
|
|
|
1267
1472
|
|
|
1268
1473
|
function checkRetention(id) {
|
|
1269
1474
|
if (sqlDPs[id] && sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].retention) {
|
|
1270
|
-
const
|
|
1271
|
-
const dt = d.getTime();
|
|
1475
|
+
const dt = Date.now();
|
|
1272
1476
|
// check every 6 hours
|
|
1273
1477
|
if (!sqlDPs[id].lastCheck || dt - sqlDPs[id].lastCheck >= 21600000/* 6 hours */) {
|
|
1274
1478
|
sqlDPs[id].lastCheck = dt;
|
|
@@ -1280,14 +1484,14 @@ function checkRetention(id) {
|
|
|
1280
1484
|
|
|
1281
1485
|
if (!multiRequests) {
|
|
1282
1486
|
if (tasks.length > MAXTASKS) {
|
|
1283
|
-
return adapter.log.error(
|
|
1487
|
+
return adapter.log.error(`Cannot queue new requests, because more than ${MAXTASKS}`);
|
|
1284
1488
|
}
|
|
1285
1489
|
|
|
1286
1490
|
let start = tasks.length === 1;
|
|
1287
1491
|
tasks.push({operation: 'delete', query});
|
|
1288
1492
|
|
|
1289
1493
|
// delete counters too
|
|
1290
|
-
if (sqlDPs[id].type ===
|
|
1494
|
+
if (sqlDPs[id] && sqlDPs[id].type === 0) { // 0 === number
|
|
1291
1495
|
const query = SQLFuncs.retention(adapter.config.dbname, sqlDPs[id].index, 'ts_counter', sqlDPs[id][adapter.namespace].retention);
|
|
1292
1496
|
tasks.push({operation: 'delete', query});
|
|
1293
1497
|
}
|
|
@@ -1296,7 +1500,7 @@ function checkRetention(id) {
|
|
|
1296
1500
|
} else {
|
|
1297
1501
|
_checkRetention(query, () => {
|
|
1298
1502
|
// delete counters too
|
|
1299
|
-
if (sqlDPs[id].type ===
|
|
1503
|
+
if (sqlDPs[id] && sqlDPs[id].type === 0) { // 0 === number
|
|
1300
1504
|
const query = SQLFuncs.retention(adapter.config.dbname, sqlDPs[id].index, 'ts_counter', sqlDPs[id][adapter.namespace].retention);
|
|
1301
1505
|
_checkRetention(query);
|
|
1302
1506
|
}
|
|
@@ -1312,6 +1516,7 @@ function _insertValueIntoDB(query, id, cb) {
|
|
|
1312
1516
|
|
|
1313
1517
|
borrowClientFromPool((err, client) => {
|
|
1314
1518
|
if (err) {
|
|
1519
|
+
returnClientToPool(client);
|
|
1315
1520
|
adapter.log.error(err);
|
|
1316
1521
|
cb && cb(); // BF asked (2021.12.14): may be return here err?
|
|
1317
1522
|
} else {
|
|
@@ -1333,6 +1538,7 @@ function _executeQuery(query, id, cb) {
|
|
|
1333
1538
|
|
|
1334
1539
|
borrowClientFromPool((err, client) => {
|
|
1335
1540
|
if (err) {
|
|
1541
|
+
returnClientToPool(client);
|
|
1336
1542
|
adapter.log.error(err);
|
|
1337
1543
|
cb && cb();
|
|
1338
1544
|
} else {
|
|
@@ -1349,65 +1555,78 @@ function processReadTypes() {
|
|
|
1349
1555
|
if (tasksReadType && tasksReadType.length) {
|
|
1350
1556
|
const task = tasksReadType[0];
|
|
1351
1557
|
|
|
1352
|
-
if (!sqlDPs[task.id]
|
|
1353
|
-
adapter.log.warn(
|
|
1354
|
-
task.cb && task.cb(
|
|
1558
|
+
if (!sqlDPs[task.id]) {
|
|
1559
|
+
adapter.log.warn(`Ignore type lookup for ${task.id} because not enabled anymore`);
|
|
1560
|
+
task.cb && task.cb(`Ignore type lookup for ${task.id} because not enabled anymore`)
|
|
1355
1561
|
task.cb = null;
|
|
1356
|
-
|
|
1357
|
-
|
|
1562
|
+
return setImmediate(() => {
|
|
1563
|
+
tasksReadType.shift();
|
|
1564
|
+
processReadTypes();
|
|
1565
|
+
});
|
|
1358
1566
|
}
|
|
1359
1567
|
|
|
1360
|
-
adapter.log.debug(
|
|
1568
|
+
adapter.log.debug(`Type set in Def for ${task.id}: ${sqlDPs[task.id][adapter.namespace] && sqlDPs[task.id][adapter.namespace].storageType}`);
|
|
1361
1569
|
|
|
1362
|
-
if (sqlDPs[task.id][adapter.namespace].storageType) {
|
|
1570
|
+
if (sqlDPs[task.id][adapter.namespace] && sqlDPs[task.id][adapter.namespace].storageType) {
|
|
1363
1571
|
sqlDPs[task.id].type = types[sqlDPs[task.id][adapter.namespace].storageType.toLowerCase()];
|
|
1364
|
-
adapter.log.debug(
|
|
1572
|
+
adapter.log.debug(`Type (from Def) for ${task.id}: ${sqlDPs[task.id].type}`);
|
|
1365
1573
|
processVerifyTypes(task);
|
|
1366
1574
|
} else if (sqlDPs[task.id].dbtype !== undefined) {
|
|
1367
1575
|
sqlDPs[task.id].type = sqlDPs[task.id].dbtype;
|
|
1368
|
-
sqlDPs[task.id][adapter.namespace]
|
|
1369
|
-
|
|
1576
|
+
if (sqlDPs[task.id][adapter.namespace]) {
|
|
1577
|
+
sqlDPs[task.id][adapter.namespace].storageType = storageTypes[sqlDPs[task.id].type];
|
|
1578
|
+
}
|
|
1579
|
+
adapter.log.debug(`Type (from DB-Type) for ${task.id}: ${sqlDPs[task.id].type}`);
|
|
1370
1580
|
processVerifyTypes(task);
|
|
1371
1581
|
} else {
|
|
1372
1582
|
adapter.getForeignObject(sqlDPs[task.id].realId, (err, obj) => {
|
|
1373
|
-
err && adapter.log.warn(
|
|
1583
|
+
err && adapter.log.warn(`Error while get Object for Def for ${sqlDPs[task.id].realId}: ${err}`);
|
|
1374
1584
|
|
|
1375
|
-
if (!sqlDPs[task.id]
|
|
1376
|
-
adapter.log.warn(
|
|
1377
|
-
task.cb && task.cb(
|
|
1585
|
+
if (!sqlDPs[task.id]) {
|
|
1586
|
+
adapter.log.warn(`Ignore type lookup for ${task.id} because not enabled anymore`);
|
|
1587
|
+
task.cb && task.cb(`Ignore type lookup for ${task.id} because not enabled anymore`)
|
|
1378
1588
|
task.cb = null;
|
|
1379
|
-
|
|
1380
|
-
|
|
1589
|
+
return setImmediate(() => {
|
|
1590
|
+
tasksReadType.shift();
|
|
1591
|
+
processReadTypes();
|
|
1592
|
+
});
|
|
1381
1593
|
} else
|
|
1382
1594
|
// read type from object
|
|
1383
1595
|
if (obj && obj.common && obj.common.type && types[obj.common.type.toLowerCase()] !== undefined) {
|
|
1384
|
-
adapter.log.debug(obj.common.type.toLowerCase()
|
|
1596
|
+
adapter.log.debug(`${obj.common.type.toLowerCase()} / ${types[obj.common.type.toLowerCase()]} / ${JSON.stringify(obj.common)}`);
|
|
1385
1597
|
sqlDPs[task.id].type = types[obj.common.type.toLowerCase()];
|
|
1386
|
-
sqlDPs[task.id][adapter.namespace]
|
|
1387
|
-
|
|
1598
|
+
if (sqlDPs[task.id][adapter.namespace]) {
|
|
1599
|
+
sqlDPs[task.id][adapter.namespace].storageType = storageTypes[sqlDPs[task.id].type];
|
|
1600
|
+
}
|
|
1601
|
+
adapter.log.debug(`Type (from Obj) for ${task.id}: ${sqlDPs[task.id].type}`);
|
|
1388
1602
|
processVerifyTypes(task);
|
|
1389
1603
|
} else if (sqlDPs[task.id].type === undefined) {
|
|
1390
1604
|
adapter.getForeignState(sqlDPs[task.id].realId, (err, state) => {
|
|
1391
|
-
if (!sqlDPs[task.id]
|
|
1392
|
-
adapter.log.warn(
|
|
1393
|
-
task.cb && task.cb(
|
|
1605
|
+
if (!sqlDPs[task.id]) {
|
|
1606
|
+
adapter.log.warn(`Ignore type lookup for ${task.id} because not enabled anymore`);
|
|
1607
|
+
task.cb && task.cb(`Ignore type lookup for ${task.id} because not enabled anymore`)
|
|
1394
1608
|
task.cb = null;
|
|
1395
|
-
|
|
1396
|
-
|
|
1609
|
+
return setImmediate(() => {
|
|
1610
|
+
tasksReadType.shift();
|
|
1611
|
+
processReadTypes();
|
|
1612
|
+
});
|
|
1397
1613
|
}
|
|
1398
1614
|
|
|
1399
|
-
if (err) {
|
|
1400
|
-
adapter.log.warn(
|
|
1401
|
-
|
|
1402
|
-
}
|
|
1615
|
+
if (err && task.state) {
|
|
1616
|
+
adapter.log.warn(`Fallback to type of current state value because no other valid type found`);
|
|
1617
|
+
state = task.state;
|
|
1618
|
+
}
|
|
1619
|
+
if (state && state.val !== null && state.val !== undefined && types[typeof state.val] !== undefined) {
|
|
1403
1620
|
sqlDPs[task.id].type = types[typeof state.val];
|
|
1404
|
-
sqlDPs[task.id][adapter.namespace]
|
|
1621
|
+
if (sqlDPs[task.id][adapter.namespace]) {
|
|
1622
|
+
sqlDPs[task.id][adapter.namespace].storageType = storageTypes[sqlDPs[task.id].type];
|
|
1623
|
+
}
|
|
1405
1624
|
} else {
|
|
1406
|
-
adapter.log.warn(
|
|
1625
|
+
adapter.log.warn(`Store data for ${task.id} as string because no other valid type found (${state ? (typeof state.val) : 'state not existing'})`);
|
|
1407
1626
|
sqlDPs[task.id].type = 1; // string
|
|
1408
1627
|
}
|
|
1409
1628
|
|
|
1410
|
-
adapter.log.debug(
|
|
1629
|
+
adapter.log.debug(`Type (from State) for ${task.id}: ${sqlDPs[task.id].type}`);
|
|
1411
1630
|
processVerifyTypes(task);
|
|
1412
1631
|
});
|
|
1413
1632
|
} else {
|
|
@@ -1431,6 +1650,7 @@ function processVerifyTypes(task) {
|
|
|
1431
1650
|
|
|
1432
1651
|
return borrowClientFromPool((err, client) => {
|
|
1433
1652
|
if (err) {
|
|
1653
|
+
returnClientToPool(client);
|
|
1434
1654
|
return processVerifyTypes(task);
|
|
1435
1655
|
}
|
|
1436
1656
|
|
|
@@ -1439,7 +1659,7 @@ function processVerifyTypes(task) {
|
|
|
1439
1659
|
if (err) {
|
|
1440
1660
|
adapter.log.error(`error updating history config for ${task.id} to pin datatype: ${query}: ${err}`);
|
|
1441
1661
|
} else {
|
|
1442
|
-
adapter.log.info(
|
|
1662
|
+
adapter.log.info(`changed history configuration to pin detected datatype for ${task.id}`);
|
|
1443
1663
|
}
|
|
1444
1664
|
processVerifyTypes(task);
|
|
1445
1665
|
});
|
|
@@ -1447,12 +1667,18 @@ function processVerifyTypes(task) {
|
|
|
1447
1667
|
}
|
|
1448
1668
|
|
|
1449
1669
|
task.cb && task.cb();
|
|
1670
|
+
task.cb = null;
|
|
1450
1671
|
|
|
1451
|
-
|
|
1452
|
-
|
|
1672
|
+
setTimeout(() => {
|
|
1673
|
+
tasksReadType.shift();
|
|
1674
|
+
processReadTypes();
|
|
1675
|
+
}, 50);
|
|
1453
1676
|
}
|
|
1454
1677
|
|
|
1455
1678
|
function prepareTaskReadDbId(id, state, isCounter, cb) {
|
|
1679
|
+
if (!sqlDPs[id]) {
|
|
1680
|
+
return cb && cb(`${id} not active any more`);
|
|
1681
|
+
}
|
|
1456
1682
|
const type = sqlDPs[id].type;
|
|
1457
1683
|
|
|
1458
1684
|
if (type === undefined) { // Can not happen anymore
|
|
@@ -1479,7 +1705,7 @@ function prepareTaskReadDbId(id, state, isCounter, cb) {
|
|
|
1479
1705
|
if (sqlDPs[id].isRunning.length === 1) {
|
|
1480
1706
|
// read or create in DB
|
|
1481
1707
|
return getId(id, type, (err, _id) => {
|
|
1482
|
-
adapter.log.debug(
|
|
1708
|
+
adapter.log.debug(`prepareTaskCheckTypeAndDbId getId Result - isRunning length = ${sqlDPs[_id].isRunning ? sqlDPs[_id].isRunning.length : 'none'}`);
|
|
1483
1709
|
if (err) {
|
|
1484
1710
|
adapter.log.warn(`Cannot get index of "${_id}": ${err}`);
|
|
1485
1711
|
sqlDPs[_id].isRunning &&
|
|
@@ -1506,9 +1732,9 @@ function prepareTaskReadDbId(id, state, isCounter, cb) {
|
|
|
1506
1732
|
if (isFromRunning[state.from].length === 1) {
|
|
1507
1733
|
// read or create in DB
|
|
1508
1734
|
return getFrom(state.from, (err, from) => {
|
|
1509
|
-
adapter.log.debug(
|
|
1735
|
+
adapter.log.debug(`prepareTaskCheckTypeAndDbId getFrom ${from} Result - isRunning length = ${isFromRunning[from] ? isFromRunning[from].length : 'none'}`)
|
|
1510
1736
|
if (err) {
|
|
1511
|
-
adapter.log.warn(
|
|
1737
|
+
adapter.log.warn(`Cannot get "from" for "${from}": ${err}`);
|
|
1512
1738
|
isFromRunning[from] &&
|
|
1513
1739
|
isFromRunning[from].forEach(f => f.cb && f.cb(`Cannot get "from" for "${from}": ${err}`));
|
|
1514
1740
|
} else {
|
|
@@ -1522,12 +1748,7 @@ function prepareTaskReadDbId(id, state, isCounter, cb) {
|
|
|
1522
1748
|
}
|
|
1523
1749
|
}
|
|
1524
1750
|
|
|
1525
|
-
|
|
1526
|
-
if (state.ts > 946681200000) {
|
|
1527
|
-
state.ts = parseInt(state.ts, 10);
|
|
1528
|
-
} else {
|
|
1529
|
-
state.ts = parseInt(state.ts, 10) * 1000 + (parseInt(state.ms, 10) || 0);
|
|
1530
|
-
}
|
|
1751
|
+
state.ts = parseInt(state.ts, 10);
|
|
1531
1752
|
|
|
1532
1753
|
try {
|
|
1533
1754
|
if (state.val !== null && (typeof state.val === 'object' || typeof state.val === 'undefined')) {
|
|
@@ -1545,7 +1766,7 @@ function prepareTaskReadDbId(id, state, isCounter, cb) {
|
|
|
1545
1766
|
function prepareTaskCheckTypeAndDbId(id, state, isCounter, cb) {
|
|
1546
1767
|
// check if we know about this ID
|
|
1547
1768
|
if (!sqlDPs[id]) {
|
|
1548
|
-
return cb && setImmediate(cb,
|
|
1769
|
+
return cb && setImmediate(cb, `Unknown ID: ${id}`);
|
|
1549
1770
|
}
|
|
1550
1771
|
|
|
1551
1772
|
// Check sql connection
|
|
@@ -1553,38 +1774,46 @@ function prepareTaskCheckTypeAndDbId(id, state, isCounter, cb) {
|
|
|
1553
1774
|
adapter.log.warn('No Connection to database');
|
|
1554
1775
|
return cb && setImmediate(cb, 'No Connection to database');
|
|
1555
1776
|
}
|
|
1556
|
-
adapter.log.debug(
|
|
1777
|
+
adapter.log.debug(`prepareTaskCheckTypeAndDbId CALLED for ${id}`);
|
|
1557
1778
|
|
|
1558
1779
|
// read type of value
|
|
1559
1780
|
if (sqlDPs[id].type !== undefined) {
|
|
1560
1781
|
prepareTaskReadDbId(id, state, isCounter, cb)
|
|
1561
1782
|
} else {
|
|
1562
1783
|
// read type from DB
|
|
1563
|
-
tasksReadType.push({id, state, cb:
|
|
1784
|
+
tasksReadType.push({id, state, cb: err => {
|
|
1785
|
+
if (err) {
|
|
1786
|
+
return cb && cb(err);
|
|
1787
|
+
}
|
|
1788
|
+
prepareTaskReadDbId(id, state, isCounter, cb);
|
|
1789
|
+
}});
|
|
1564
1790
|
|
|
1565
1791
|
tasksReadType.length === 1 && processReadTypes();
|
|
1566
1792
|
}
|
|
1567
1793
|
}
|
|
1568
1794
|
|
|
1569
|
-
function pushValueIntoDB(id, state, isCounter, cb) {
|
|
1795
|
+
function pushValueIntoDB(id, state, isCounter, storeInCacheOnly, cb) {
|
|
1796
|
+
if (typeof storeInCacheOnly === 'function') {
|
|
1797
|
+
cb = storeInCacheOnly;
|
|
1798
|
+
storeInCacheOnly = false;
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1570
1801
|
if (typeof isCounter === 'function') {
|
|
1571
1802
|
cb = isCounter;
|
|
1572
1803
|
isCounter = false;
|
|
1573
1804
|
}
|
|
1574
1805
|
|
|
1575
|
-
if (sqlDPs[id]
|
|
1576
|
-
|
|
1577
|
-
return cb && cb();
|
|
1578
|
-
} else
|
|
1579
|
-
if (state && sqlDPs[id] && sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].ignoreBelowZero && typeof state.val === 'number' && state.val < 0) {
|
|
1580
|
-
adapter.log.debug(`pushValueIntoDB called for ${id} (type: ${sqlDPs[id].type}, ID: ${sqlDPs[id].index}) and state: ${JSON.stringify(state)} and it was ignored because the value is below 0`);
|
|
1581
|
-
return cb && cb();
|
|
1806
|
+
if (!sqlDPs[id] || !state) {
|
|
1807
|
+
return cb && cb()
|
|
1582
1808
|
}
|
|
1583
1809
|
|
|
1584
1810
|
adapter.log.debug(`pushValueIntoDB called for ${id} (type: ${sqlDPs[id].type}, ID: ${sqlDPs[id].index}) and state: ${JSON.stringify(state)}`);
|
|
1585
1811
|
|
|
1586
1812
|
prepareTaskCheckTypeAndDbId(id, state, isCounter, err => {
|
|
1587
|
-
|
|
1813
|
+
if (!sqlDPs[id]) {
|
|
1814
|
+
return cb && cb()
|
|
1815
|
+
}
|
|
1816
|
+
adapter.log.debug(`pushValueIntoDB-prepareTaskCheckTypeAndDbId RESULT for ${id} (type: ${sqlDPs[id].type}, ID: ${sqlDPs[id].index}) and state: ${JSON.stringify(state)}: ${err}`);
|
|
1588
1817
|
if (err) {
|
|
1589
1818
|
return cb && cb(err);
|
|
1590
1819
|
}
|
|
@@ -1599,21 +1828,72 @@ function pushValueIntoDB(id, state, isCounter, cb) {
|
|
|
1599
1828
|
// remember last timestamp
|
|
1600
1829
|
sqlDPs[id].ts = state.ts;
|
|
1601
1830
|
|
|
1602
|
-
|
|
1831
|
+
// if it was not deleted in this time
|
|
1832
|
+
sqlDPs[id].list = sqlDPs[id].list || [];
|
|
1603
1833
|
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1834
|
+
sqlDPs[id].list.push({state, from: from[state.from] || 0, db: isCounter ? 'ts_counter' : dbNames[type]});
|
|
1835
|
+
|
|
1836
|
+
const _settings = sqlDPs[id][adapter.namespace] || {};
|
|
1837
|
+
const maxLength = _settings.maxLength !== undefined ? _settings.maxLength : parseInt(adapter.config.maxLength, 10) || 0;
|
|
1838
|
+
if ((cb || _settings && sqlDPs[id].list.length > maxLength) && !storeInCacheOnly) {
|
|
1839
|
+
storeCached(id, cb);
|
|
1840
|
+
} else if (cb && storeInCacheOnly) {
|
|
1841
|
+
setImmediate(cb);
|
|
1842
|
+
}
|
|
1843
|
+
});
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
function storeCached(onlyId, cb) {
|
|
1847
|
+
let count = 0;
|
|
1848
|
+
for (const id in sqlDPs) {
|
|
1849
|
+
if (!sqlDPs.hasOwnProperty(id) || (onlyId !== undefined && onlyId !== id)) {
|
|
1850
|
+
continue;
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
const _settings = sqlDPs[id][adapter.namespace] || {};
|
|
1854
|
+
if (_settings && sqlDPs[id].list && sqlDPs[id].list.length) {
|
|
1855
|
+
_settings.enableDebugLogs && adapter.log.debug(`inserting ${sqlDPs[id].list.length} entries from ${id} to DB`);
|
|
1856
|
+
const inFlightId = `${id}_${Date.now()}_${Math.random()}`;
|
|
1857
|
+
sqlDPs[id].inFlight = sqlDPs[id].inFlight || {};
|
|
1858
|
+
sqlDPs[id].inFlight[inFlightId] = sqlDPs[id].list;
|
|
1859
|
+
sqlDPs[id].list = [];
|
|
1860
|
+
count++;
|
|
1861
|
+
pushValuesIntoDB(id, sqlDPs[id].inFlight[inFlightId], err => {
|
|
1862
|
+
delete sqlDPs[id].inFlight[inFlightId];
|
|
1863
|
+
if (!--count && cb) {
|
|
1864
|
+
cb(err);
|
|
1865
|
+
cb = null;
|
|
1866
|
+
}
|
|
1867
|
+
});
|
|
1868
|
+
if (onlyId !== undefined) {
|
|
1869
|
+
break;
|
|
1612
1870
|
}
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
if (!count && cb) {
|
|
1874
|
+
cb();
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
function pushValuesIntoDB(id, list, cb) {
|
|
1879
|
+
if (!list.length) {
|
|
1880
|
+
return cb && setImmediate(cb);
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
if (!multiRequests) {
|
|
1884
|
+
if (tasks.length > MAXTASKS) {
|
|
1885
|
+
const error = `Cannot queue new requests, because more than ${MAXTASKS}`;
|
|
1886
|
+
adapter.log.error(error);
|
|
1887
|
+
cb && cb(error);
|
|
1613
1888
|
} else {
|
|
1614
|
-
|
|
1889
|
+
tasks.push({operation: 'insert', index: sqlDPs[id].index, list, id, callback: cb});
|
|
1890
|
+
tasks.length === 1 && processTasks();
|
|
1615
1891
|
}
|
|
1616
|
-
}
|
|
1892
|
+
} else {
|
|
1893
|
+
const query = SQLFuncs.insert(adapter.config.dbname, sqlDPs[id].index, list);
|
|
1894
|
+
_insertValueIntoDB(query, id, cb);
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1617
1897
|
}
|
|
1618
1898
|
|
|
1619
1899
|
let lockTasks = false;
|
|
@@ -1633,8 +1913,23 @@ function processTasks() {
|
|
|
1633
1913
|
tasks.length && setTimeout(processTasks, adapter.config.requestInterval);
|
|
1634
1914
|
});
|
|
1635
1915
|
} else if (tasks[0].operation === 'insert') {
|
|
1636
|
-
|
|
1637
|
-
|
|
1916
|
+
const callbacks = [];
|
|
1917
|
+
if (tasks[0].callback) {
|
|
1918
|
+
callbacks.push(tasks[0].callback);
|
|
1919
|
+
}
|
|
1920
|
+
for (let i = 1; i < tasks.length; i++) {
|
|
1921
|
+
if (tasks[i].operation === 'insert' && tasks[0].index === tasks[i].index) {
|
|
1922
|
+
tasks[0].list = tasks[0].list.concat(tasks[i].list);
|
|
1923
|
+
if (tasks[i].callback) {
|
|
1924
|
+
callbacks.push(tasks[i].callback);
|
|
1925
|
+
}
|
|
1926
|
+
tasks.splice(i, 1);
|
|
1927
|
+
i--;
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
const query = SQLFuncs.insert(adapter.config.dbname, tasks[0].index, tasks[0].list);
|
|
1931
|
+
_insertValueIntoDB(query, tasks[0].id, () => {
|
|
1932
|
+
callbacks.forEach(cb => cb());
|
|
1638
1933
|
tasks.shift();
|
|
1639
1934
|
lockTasks = false;
|
|
1640
1935
|
tasks.length && setTimeout(processTasks, adapter.config.requestInterval);
|
|
@@ -1661,7 +1956,7 @@ function processTasks() {
|
|
|
1661
1956
|
tasks.length && setTimeout(processTasks, adapter.config.requestInterval);
|
|
1662
1957
|
});
|
|
1663
1958
|
} else {
|
|
1664
|
-
adapter.log.error(
|
|
1959
|
+
adapter.log.error(`unknown task: ${tasks[0].operation}`);
|
|
1665
1960
|
tasks[0].callback && tasks[0].callback();
|
|
1666
1961
|
tasks.shift();
|
|
1667
1962
|
lockTasks = false;
|
|
@@ -1677,17 +1972,22 @@ function getId(id, type, cb) {
|
|
|
1677
1972
|
|
|
1678
1973
|
borrowClientFromPool((err, client) => {
|
|
1679
1974
|
if (err) {
|
|
1975
|
+
returnClientToPool(client);
|
|
1680
1976
|
return cb && cb(err, id);
|
|
1681
1977
|
}
|
|
1682
1978
|
|
|
1683
1979
|
client.execute(query, (err, rows /* , fields */) => {
|
|
1980
|
+
if (!sqlDPs[id]) {
|
|
1981
|
+
returnClientToPool(client);
|
|
1982
|
+
return cb && cb(new Error(`ID ${id} no longer active`), id);
|
|
1983
|
+
}
|
|
1684
1984
|
if (rows && rows.rows) {
|
|
1685
1985
|
rows = rows.rows;
|
|
1686
1986
|
}
|
|
1687
1987
|
|
|
1688
1988
|
if (err) {
|
|
1689
1989
|
returnClientToPool(client);
|
|
1690
|
-
adapter.log.error(
|
|
1990
|
+
adapter.log.error(`Cannot select ${query}: ${err}`);
|
|
1691
1991
|
return cb && cb(err, id);
|
|
1692
1992
|
} else if (!rows.length) {
|
|
1693
1993
|
if (type !== null && type !== undefined) {
|
|
@@ -1699,7 +1999,7 @@ function getId(id, type, cb) {
|
|
|
1699
1999
|
client.execute(query, (err /* , rows, fields */) => {
|
|
1700
2000
|
if (err) {
|
|
1701
2001
|
returnClientToPool(client);
|
|
1702
|
-
adapter.log.error(
|
|
2002
|
+
adapter.log.error(`Cannot insert ${query}: ${err}`);
|
|
1703
2003
|
cb && cb(err, id);
|
|
1704
2004
|
} else {
|
|
1705
2005
|
query = SQLFuncs.getIdSelect(adapter.config.dbname,id);
|
|
@@ -1713,7 +2013,7 @@ function getId(id, type, cb) {
|
|
|
1713
2013
|
}
|
|
1714
2014
|
|
|
1715
2015
|
if (err) {
|
|
1716
|
-
adapter.log.error(
|
|
2016
|
+
adapter.log.error(`Cannot select ${query}: ${err}`);
|
|
1717
2017
|
cb && cb(err, id);
|
|
1718
2018
|
} else if (rows[0]) {
|
|
1719
2019
|
sqlDPs[id].index = rows[0].id;
|
|
@@ -1721,8 +2021,8 @@ function getId(id, type, cb) {
|
|
|
1721
2021
|
|
|
1722
2022
|
cb && cb(null, id);
|
|
1723
2023
|
} else {
|
|
1724
|
-
adapter.log.error(
|
|
1725
|
-
cb && cb(new Error(
|
|
2024
|
+
adapter.log.error(`No result for select ${query}: after insert`);
|
|
2025
|
+
cb && cb(new Error(`No result for select ${query}: after insert`), id);
|
|
1726
2026
|
}
|
|
1727
2027
|
});
|
|
1728
2028
|
}
|
|
@@ -1745,7 +2045,7 @@ function getId(id, type, cb) {
|
|
|
1745
2045
|
if (err) {
|
|
1746
2046
|
adapter.log.error(`error updating history config for ${id} to pin datatype: ${query}: ${err}`);
|
|
1747
2047
|
} else {
|
|
1748
|
-
adapter.log.info(
|
|
2048
|
+
adapter.log.info(`changed history configuration to pin detected datatype for ${id}`);
|
|
1749
2049
|
}
|
|
1750
2050
|
cb && cb(null, id);
|
|
1751
2051
|
});
|
|
@@ -1769,13 +2069,14 @@ function getFrom(_from, cb) {
|
|
|
1769
2069
|
|
|
1770
2070
|
borrowClientFromPool((err, client) => {
|
|
1771
2071
|
if (err) {
|
|
2072
|
+
returnClientToPool(client);
|
|
1772
2073
|
return cb && cb(err, _from);
|
|
1773
2074
|
}
|
|
1774
2075
|
client.execute(query, (err, rows /* , fields */) => {
|
|
1775
2076
|
if (rows && rows.rows) rows = rows.rows;
|
|
1776
2077
|
if (err) {
|
|
1777
2078
|
returnClientToPool(client);
|
|
1778
|
-
adapter.log.error(
|
|
2079
|
+
adapter.log.error(`Cannot select ${query}: ${err}`);
|
|
1779
2080
|
return cb && cb(err, _from);
|
|
1780
2081
|
}
|
|
1781
2082
|
if (!rows.length) {
|
|
@@ -1785,7 +2086,7 @@ function getFrom(_from, cb) {
|
|
|
1785
2086
|
client.execute(query, (err /* , rows, fields */) => {
|
|
1786
2087
|
if (err) {
|
|
1787
2088
|
returnClientToPool(client);
|
|
1788
|
-
adapter.log.error(
|
|
2089
|
+
adapter.log.error(`Cannot insert ${query}: ${err}`);
|
|
1789
2090
|
return cb && cb(err, _from);
|
|
1790
2091
|
}
|
|
1791
2092
|
|
|
@@ -1796,7 +2097,7 @@ function getFrom(_from, cb) {
|
|
|
1796
2097
|
|
|
1797
2098
|
if (rows && rows.rows) rows = rows.rows;
|
|
1798
2099
|
if (err) {
|
|
1799
|
-
adapter.log.error(
|
|
2100
|
+
adapter.log.error(`Cannot select ${query}: ${err}`);
|
|
1800
2101
|
return cb && cb(err, _from);
|
|
1801
2102
|
}
|
|
1802
2103
|
from[_from] = rows[0].id;
|
|
@@ -1821,52 +2122,137 @@ function sortByTs(a, b) {
|
|
|
1821
2122
|
return aTs < bTs ? -1 : (aTs > bTs ? 1 : 0);
|
|
1822
2123
|
}
|
|
1823
2124
|
|
|
2125
|
+
|
|
2126
|
+
function getOneCachedData(id, options, cache, addId) {
|
|
2127
|
+
addId = addId || options.addId;
|
|
2128
|
+
|
|
2129
|
+
if (sqlDPs[id]) {
|
|
2130
|
+
let res = [];
|
|
2131
|
+
for (let inFlightId in sqlDPs[id].inFlight) {
|
|
2132
|
+
adapter.log.debug(`getOneCachedData: add ${sqlDPs[id].inFlight[inFlightId].length} inFlight datapoints for ${options.index || options.id}`);
|
|
2133
|
+
res = res.concat(sqlDPs[id].inFlight[inFlightId]);
|
|
2134
|
+
}
|
|
2135
|
+
res = res.concat(sqlDPs[id].list);
|
|
2136
|
+
// todo can be optimized
|
|
2137
|
+
if (res) {
|
|
2138
|
+
let iProblemCount = 0;
|
|
2139
|
+
let vLast = null;
|
|
2140
|
+
for (let i = res.length - 1; i >= 0 ; i--) {
|
|
2141
|
+
if (!res[i] || !res[i].state) {
|
|
2142
|
+
iProblemCount++;
|
|
2143
|
+
continue;
|
|
2144
|
+
}
|
|
2145
|
+
if (options.start && res[i].state.ts < options.start) {
|
|
2146
|
+
// add one before start
|
|
2147
|
+
cache.unshift(Object.assign({}, res[i].state));
|
|
2148
|
+
break;
|
|
2149
|
+
} else if (res[i].state.ts > options.end) {
|
|
2150
|
+
// add one after end
|
|
2151
|
+
vLast = res[i].state;
|
|
2152
|
+
continue;
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
if (vLast) {
|
|
2156
|
+
cache.unshift(Object.assign({}, vLast));
|
|
2157
|
+
vLast = null;
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
cache.unshift(Object.assign({}, res[i].state));
|
|
2161
|
+
|
|
2162
|
+
if ((options.returnNewestEntries && options.count && cache.length >= options.count) && (options.aggregate === 'onchange' || options.aggregate === '' || options.aggregate === 'none')) {
|
|
2163
|
+
break;
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2167
|
+
iProblemCount && adapter.log.warn(`getOneCachedData: got null states ${iProblemCount} times for ${options.index || options.id}`);
|
|
2168
|
+
|
|
2169
|
+
adapter.log.debug(`getOneCachedData: got ${res.length} datapoints for ${options.index || options.id}`);
|
|
2170
|
+
} else {
|
|
2171
|
+
adapter.log.debug(`getOneCachedData: datapoints for ${options.index || options.id} do not yet exist`);
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
function getCachedData(options, callback) {
|
|
2177
|
+
const cache = [];
|
|
2178
|
+
|
|
2179
|
+
if (options.index || options.id) {
|
|
2180
|
+
getOneCachedData(options.index || options.id, options, cache);
|
|
2181
|
+
} else {
|
|
2182
|
+
for (const id in sqlDPs) {
|
|
2183
|
+
if (sqlDPs.hasOwnProperty(id)) {
|
|
2184
|
+
getOneCachedData(id, options, cache, true);
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2189
|
+
let earliestTs = null;
|
|
2190
|
+
for (let c = 0; c < cache.length; c++) {
|
|
2191
|
+
if (typeof cache[c].ts === 'string') {
|
|
2192
|
+
cache[c].ts = parseInt(cache[c].ts, 10);
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
if (adapter.common.loglevel === 'debug') {
|
|
2196
|
+
cache[c].date = new Date(parseInt(cache[c].ts, 10));
|
|
2197
|
+
}
|
|
2198
|
+
if (options.ack) {
|
|
2199
|
+
cache[c].ack = !!cache[c].ack;
|
|
2200
|
+
}
|
|
2201
|
+
if (typeof cache[c].val === 'number' && isFinite(cache[c].val) && options.round) {
|
|
2202
|
+
cache[c].val = Math.round(cache[c].val * options.round) / options.round;
|
|
2203
|
+
}
|
|
2204
|
+
if (sqlDPs[options.index || options.id] && sqlDPs[options.index || options.id].type === 2) { // 2 === boolean
|
|
2205
|
+
cache[c].val = !!cache[c].val;
|
|
2206
|
+
}
|
|
2207
|
+
if (options.addId && !cache[c].id && options.id) {
|
|
2208
|
+
cache[c].id = options.index || options.id;
|
|
2209
|
+
}
|
|
2210
|
+
if (cache[c].ts < earliestTs || earliestTs === null) {
|
|
2211
|
+
earliestTs = cache[c].ts;
|
|
2212
|
+
}
|
|
2213
|
+
}
|
|
2214
|
+
|
|
2215
|
+
options.length = cache.length;
|
|
2216
|
+
callback(cache, options.returnNewestEntries && options.count && cache.length >= options.count, !!(sqlDPs[options.index || options.id] && sqlDPs[options.index || options.id].inFlight && Object.keys(sqlDPs[options.index || options.id].inFlight).length), earliestTs);
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
|
|
1824
2220
|
function _getDataFromDB(query, options, callback) {
|
|
1825
2221
|
adapter.log.debug(query);
|
|
1826
2222
|
|
|
1827
2223
|
borrowClientFromPool((err, client) => {
|
|
1828
2224
|
if (err) {
|
|
2225
|
+
returnClientToPool(client);
|
|
1829
2226
|
return callback && callback(err);
|
|
1830
2227
|
}
|
|
1831
2228
|
client.execute(query, (err, rows /* , fields */) => {
|
|
1832
2229
|
returnClientToPool(client);
|
|
1833
2230
|
|
|
1834
|
-
if (rows && rows.rows) rows = rows.rows;
|
|
1835
|
-
// because descending
|
|
1836
|
-
if (!err && rows && !options.start && options.count) {
|
|
1837
|
-
rows.sort(sortByTs);
|
|
1838
|
-
}
|
|
2231
|
+
if (!err && rows && rows.rows) rows = rows.rows;
|
|
1839
2232
|
|
|
1840
|
-
if (rows) {
|
|
1841
|
-
let isNumber = null;
|
|
2233
|
+
if (!err && rows) {
|
|
1842
2234
|
for (let c = 0; c < rows.length; c++) {
|
|
1843
|
-
if (isNumber === null && rows[c].val !== null) {
|
|
1844
|
-
isNumber = parseFloat(rows[c].val) == rows[c].val;
|
|
1845
|
-
}
|
|
1846
2235
|
if (typeof rows[c].ts === 'string') {
|
|
1847
2236
|
rows[c].ts = parseInt(rows[c].ts, 10);
|
|
1848
2237
|
}
|
|
1849
2238
|
|
|
1850
|
-
// if less than 2000.01.01 00:00:00
|
|
1851
|
-
if (rows[c].ts < 946681200000) {
|
|
1852
|
-
rows[c].ts *= 1000;
|
|
1853
|
-
}
|
|
1854
|
-
|
|
1855
2239
|
if (adapter.common.loglevel === 'debug') {
|
|
1856
2240
|
rows[c].date = new Date(parseInt(rows[c].ts, 10));
|
|
1857
2241
|
}
|
|
1858
2242
|
if (options.ack) {
|
|
1859
2243
|
rows[c].ack = !!rows[c].ack;
|
|
1860
2244
|
}
|
|
1861
|
-
if (
|
|
1862
|
-
rows[c].val = Math.round(rows[c].val *
|
|
2245
|
+
if (typeof rows[c].val === 'number' && isFinite(rows[c].val) && options.round) {
|
|
2246
|
+
rows[c].val = Math.round(rows[c].val * options.round) / options.round;
|
|
1863
2247
|
}
|
|
1864
|
-
if (sqlDPs[options.index].type === 2) {
|
|
2248
|
+
if (sqlDPs[options.index || options.id] && sqlDPs[options.index || options.id].type === 2) { // 2 === boolean
|
|
1865
2249
|
rows[c].val = !!rows[c].val;
|
|
1866
2250
|
}
|
|
2251
|
+
if (options.addId && !rows[c].id && options.id) {
|
|
2252
|
+
rows[c].id = options.index || options.id;
|
|
2253
|
+
}
|
|
1867
2254
|
}
|
|
1868
2255
|
}
|
|
1869
|
-
|
|
1870
2256
|
callback && callback(err, rows);
|
|
1871
2257
|
});
|
|
1872
2258
|
});
|
|
@@ -1877,8 +2263,8 @@ function getDataFromDB(db, options, callback) {
|
|
|
1877
2263
|
adapter.log.debug(query);
|
|
1878
2264
|
if (!multiRequests) {
|
|
1879
2265
|
if (tasks.length > MAXTASKS) {
|
|
1880
|
-
adapter.log.error(
|
|
1881
|
-
callback && callback(
|
|
2266
|
+
adapter.log.error(`Cannot queue new requests, because more than ${MAXTASKS}`);
|
|
2267
|
+
callback && callback(`Cannot queue new requests, because more than ${MAXTASKS}`);
|
|
1882
2268
|
} else {
|
|
1883
2269
|
tasks.push({operation: 'select', query, options, callback});
|
|
1884
2270
|
tasks.length === 1 && processTasks();
|
|
@@ -1895,7 +2281,7 @@ function getCounterDataFromDB(options, callback) {
|
|
|
1895
2281
|
|
|
1896
2282
|
if (!multiRequests) {
|
|
1897
2283
|
if (tasks.length > MAXTASKS) {
|
|
1898
|
-
const error =
|
|
2284
|
+
const error = `Cannot queue new requests, because more than ${MAXTASKS}`;
|
|
1899
2285
|
adapter.log.error(error);
|
|
1900
2286
|
callback && callback(error);
|
|
1901
2287
|
} else {
|
|
@@ -1930,6 +2316,12 @@ function getCounterDiff(msg) {
|
|
|
1930
2316
|
function getHistory(msg) {
|
|
1931
2317
|
const startTime = Date.now();
|
|
1932
2318
|
|
|
2319
|
+
if (!msg.message || !msg.message.options) {
|
|
2320
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
2321
|
+
error: 'Invalid call. No options for getHistory provided'
|
|
2322
|
+
}, msg.callback);
|
|
2323
|
+
}
|
|
2324
|
+
|
|
1933
2325
|
const options = {
|
|
1934
2326
|
id: msg.message.id === '*' ? null : msg.message.id,
|
|
1935
2327
|
start: msg.message.options.start,
|
|
@@ -1937,19 +2329,82 @@ function getHistory(msg) {
|
|
|
1937
2329
|
step: parseInt(msg.message.options.step, 10) || null,
|
|
1938
2330
|
count: parseInt(msg.message.options.count, 10) || 500,
|
|
1939
2331
|
ignoreNull: msg.message.options.ignoreNull,
|
|
1940
|
-
aggregate: msg.message.options.aggregate || 'average', // One of: max, min, average, total
|
|
1941
|
-
limit: msg.message.options.limit || adapter.config.limit || 2000,
|
|
2332
|
+
aggregate: msg.message.options.aggregate || 'average', // One of: max, min, average, total, none, on-change
|
|
2333
|
+
limit: parseInt(msg.message.options.limit, 10) || parseInt(msg.message.options.count, 10) || adapter.config.limit || 2000,
|
|
1942
2334
|
from: msg.message.options.from || false,
|
|
1943
2335
|
q: msg.message.options.q || false,
|
|
1944
2336
|
ack: msg.message.options.ack || false,
|
|
1945
2337
|
ms: msg.message.options.ms || false,
|
|
1946
2338
|
addId: msg.message.options.addId || false,
|
|
1947
|
-
sessionId: msg.message.options.sessionId
|
|
2339
|
+
sessionId: msg.message.options.sessionId,
|
|
2340
|
+
returnNewestEntries: msg.message.options.returnNewestEntries || false,
|
|
2341
|
+
percentile: msg.message.options.aggregate === 'percentile' ? parseInt(msg.message.options.percentile, 10) || 50 : null,
|
|
2342
|
+
quantile: msg.message.options.aggregate === 'quantile' ? parseFloat(msg.message.options.quantile) || 0.5 : null,
|
|
2343
|
+
integralUnit: msg.message.options.aggregate === 'integral' ? parseInt(msg.message.options.integralUnit, 10) || 60 : null,
|
|
2344
|
+
integralInterpolation: msg.message.options.aggregate === 'integral' ? msg.message.options.integralInterpolation || 'none' : null,
|
|
2345
|
+
removeBorderValues: msg.message.options.removeBorderValues || false,
|
|
2346
|
+
logId: (msg.message.id ? msg.message.id : 'all') + Date.now() + Math.random()
|
|
1948
2347
|
};
|
|
2348
|
+
|
|
2349
|
+
try {
|
|
2350
|
+
if (options.start && typeof options.start !== 'number') {
|
|
2351
|
+
options.start = new Date(options.start).getTime();
|
|
2352
|
+
}
|
|
2353
|
+
} catch (err) {
|
|
2354
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
2355
|
+
error: 'Invalid call. Start date ' + JSON.stringify(options.start) + ' is not a valid date'
|
|
2356
|
+
}, msg.callback);
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
try {
|
|
2360
|
+
if (options.end && typeof options.end !== 'number') {
|
|
2361
|
+
options.end = new Date(options.end).getTime();
|
|
2362
|
+
}
|
|
2363
|
+
} catch (err) {
|
|
2364
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
2365
|
+
error: 'Invalid call. End date ' + JSON.stringify(options.end) + ' is not a valid date'
|
|
2366
|
+
}, msg.callback);
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
if (!options.start && options.count) {
|
|
2370
|
+
options.returnNewestEntries = true;
|
|
2371
|
+
}
|
|
2372
|
+
|
|
2373
|
+
if (msg.message.options.round !== null && msg.message.options.round !== undefined && msg.message.options.round !== '') {
|
|
2374
|
+
msg.message.options.round = parseInt(msg.message.options.round, 10);
|
|
2375
|
+
if (!isFinite(msg.message.options.round) || msg.message.options.round < 0) {
|
|
2376
|
+
options.round = adapter.config.round;
|
|
2377
|
+
} else {
|
|
2378
|
+
options.round = Math.pow(10, parseInt(msg.message.options.round, 10));
|
|
2379
|
+
}
|
|
2380
|
+
} else {
|
|
2381
|
+
options.round = adapter.config.round;
|
|
2382
|
+
}
|
|
2383
|
+
|
|
2384
|
+
adapter.log.debug(`${options.logId} getHistory call: ${JSON.stringify(options)}`);
|
|
2385
|
+
|
|
1949
2386
|
if (options.id && aliasMap[options.id]) {
|
|
1950
2387
|
options.id = aliasMap[options.id];
|
|
1951
2388
|
}
|
|
1952
2389
|
|
|
2390
|
+
if (options.aggregate === 'percentile' && options.percentile < 0 || options.percentile > 100) {
|
|
2391
|
+
adapter.log.error(`Invalid percentile value: ${options.percentile}, use 50 as default`);
|
|
2392
|
+
options.percentile = 50;
|
|
2393
|
+
}
|
|
2394
|
+
|
|
2395
|
+
if (options.aggregate === 'quantile' && options.quantile < 0 || options.quantile > 1) {
|
|
2396
|
+
adapter.log.error(`Invalid quantile value: ${options.quantile}, use 0.5 as default`);
|
|
2397
|
+
options.quantile = 0.5;
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
if (options.aggregate === 'integral' && (typeof options.integralUnit !== 'number' || options.integralUnit <= 0)) {
|
|
2401
|
+
adapter.log.error(`Invalid integralUnit value: ${options.integralUnit}, use 60s as default`);
|
|
2402
|
+
options.integralUnit = 60;
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
sqlDPs[options.id] = sqlDPs[options.id] || {};
|
|
2406
|
+
const debugLog = options.debugLog = !!(sqlDPs[options.id] && sqlDPs[options.id][adapter.namespace] && sqlDPs[options.id][adapter.namespace].enableDebugLogs);
|
|
2407
|
+
|
|
1953
2408
|
if (options.ignoreNull === 'true') options.ignoreNull = true; // include nulls and replace them with last value
|
|
1954
2409
|
if (options.ignoreNull === 'false') options.ignoreNull = false; // include nulls
|
|
1955
2410
|
if (options.ignoreNull === '0') options.ignoreNull = 0; // include nulls and replace them with 0
|
|
@@ -1957,10 +2412,6 @@ function getHistory(msg) {
|
|
|
1957
2412
|
options.ignoreNull = false;
|
|
1958
2413
|
}
|
|
1959
2414
|
|
|
1960
|
-
if (!sqlDPs[options.id]) {
|
|
1961
|
-
return commons.sendResponse(adapter, msg, options, [], startTime);
|
|
1962
|
-
}
|
|
1963
|
-
|
|
1964
2415
|
if (options.start > options.end) {
|
|
1965
2416
|
const _end = options.end;
|
|
1966
2417
|
options.end = options.start;
|
|
@@ -1968,18 +2419,18 @@ function getHistory(msg) {
|
|
|
1968
2419
|
}
|
|
1969
2420
|
|
|
1970
2421
|
if (!options.start && !options.count) {
|
|
1971
|
-
options.start = Date.now() -
|
|
2422
|
+
options.start = Date.now() - 86400000; // - 1 day
|
|
1972
2423
|
}
|
|
1973
2424
|
|
|
1974
2425
|
if (sqlDPs[options.id].type === undefined && sqlDPs[options.id].dbtype !== undefined) {
|
|
1975
2426
|
if (sqlDPs[options.id][adapter.namespace] && sqlDPs[options.id][adapter.namespace].storageType) {
|
|
1976
2427
|
if (storageTypes.indexOf(sqlDPs[options.id][adapter.namespace].storageType) === sqlDPs[options.id].dbtype) {
|
|
1977
|
-
adapter.log.debug(
|
|
2428
|
+
debugLog && adapter.log.debug(`${options.logId} For getHistory for id ${options.id}: Type empty, use storageType dbtype ${sqlDPs[options.id].dbtype}`);
|
|
1978
2429
|
sqlDPs[options.id].type = sqlDPs[options.id].dbtype;
|
|
1979
2430
|
}
|
|
1980
2431
|
}
|
|
1981
2432
|
else {
|
|
1982
|
-
adapter.log.debug(
|
|
2433
|
+
debugLog && adapter.log.debug(`${options.logId} For getHistory for id ${options.id}: Type empty, use dbtype ${sqlDPs[options.id].dbtype}`);
|
|
1983
2434
|
sqlDPs[options.id].type = sqlDPs[options.id].dbtype;
|
|
1984
2435
|
}
|
|
1985
2436
|
}
|
|
@@ -2006,123 +2457,258 @@ function getHistory(msg) {
|
|
|
2006
2457
|
options.id = sqlDPs[options.id].index;
|
|
2007
2458
|
}
|
|
2008
2459
|
|
|
2460
|
+
if (options.debugLog) {
|
|
2461
|
+
options.log = adapter.log.debug;
|
|
2462
|
+
}
|
|
2463
|
+
|
|
2009
2464
|
// if specific id requested
|
|
2010
|
-
if (options.id
|
|
2011
|
-
|
|
2012
|
-
|
|
2465
|
+
if (options.id) {
|
|
2466
|
+
getCachedData(options, (cacheData, isFull, includesInFlightData, earliestTs) => {
|
|
2467
|
+
debugLog && adapter.log.debug(`${options.logId} after getCachedData: length = ${cacheData.length}, isFull=${isFull}`);
|
|
2468
|
+
|
|
2469
|
+
// if all data read
|
|
2470
|
+
if ((isFull && cacheData.length) && (options.aggregate === 'onchange' || options.aggregate === '' || options.aggregate === 'none')) {
|
|
2471
|
+
cacheData = cacheData.sort(sortByTs);
|
|
2472
|
+
if (options.count && cacheData.length > options.count && options.aggregate === 'none') {
|
|
2473
|
+
cacheData = cacheData.slice(-options.count);
|
|
2474
|
+
debugLog && adapter.log.debug(`${options.logId} cut cacheData to ${options.count} values`);
|
|
2475
|
+
}
|
|
2476
|
+
adapter.log.debug(`${options.logId} Send: ${cacheData.length} values in: ${Date.now() - startTime}ms`);
|
|
2477
|
+
|
|
2478
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
2479
|
+
result: cacheData,
|
|
2480
|
+
step: null,
|
|
2481
|
+
error: null
|
|
2482
|
+
}, msg.callback);
|
|
2483
|
+
} else {
|
|
2484
|
+
const origEnd = options.end;
|
|
2485
|
+
if (includesInFlightData) {
|
|
2486
|
+
options.end = earliestTs;
|
|
2487
|
+
}
|
|
2488
|
+
// if not all data read
|
|
2489
|
+
getDataFromDB(dbNames[type], options, (err, data) => {
|
|
2490
|
+
if (!err && data) {
|
|
2491
|
+
options.end = origEnd;
|
|
2492
|
+
if (options.aggregate === 'none' && options.count && options.returnNewestEntries) {
|
|
2493
|
+
cacheData = cacheData.reverse()
|
|
2494
|
+
data = cacheData.concat(data);
|
|
2495
|
+
} else {
|
|
2496
|
+
data = data.concat(cacheData);
|
|
2497
|
+
}
|
|
2498
|
+
debugLog && adapter.log.debug(`${options.logId} after getDataFromDB: length = ${data.length}`);
|
|
2499
|
+
if (options.count && data.length > options.count && options.aggregate === 'none' && !options.returnNewestEntries) {
|
|
2500
|
+
if (options.start) {
|
|
2501
|
+
for (let i = 0; i < data.length; i++) {
|
|
2502
|
+
if (data[i].ts < options.start) {
|
|
2503
|
+
data.splice(i, 1);
|
|
2504
|
+
i--;
|
|
2505
|
+
} else {
|
|
2506
|
+
break;
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
data = data.slice(0, options.count);
|
|
2511
|
+
options.debugLog && adapter.log.debug(`${options.logId} pre-cut data to ${options.count} oldest values`);
|
|
2512
|
+
}
|
|
2513
|
+
|
|
2514
|
+
data.sort(sortByTs);
|
|
2515
|
+
}
|
|
2516
|
+
commons.sendResponse(adapter, msg, options, (err ? err.toString() : null) || data, startTime)
|
|
2517
|
+
});
|
|
2518
|
+
}
|
|
2519
|
+
});
|
|
2013
2520
|
} else {
|
|
2014
2521
|
// if all IDs requested
|
|
2015
2522
|
let rows = [];
|
|
2016
2523
|
let count = 0;
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2524
|
+
getCachedData(options, (cacheData, isFull, includesInFlightData, earliestTs) => {
|
|
2525
|
+
if (isFull && cacheData.length) {
|
|
2526
|
+
cacheData.sort(sortByTs);
|
|
2527
|
+
commons.sendResponse(adapter, msg, options, cacheData, startTime);
|
|
2528
|
+
} else {
|
|
2529
|
+
if (includesInFlightData) {
|
|
2530
|
+
options.end = earliestTs;
|
|
2022
2531
|
}
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2532
|
+
for (let db = 0; db < dbNames.length; db++) {
|
|
2533
|
+
count++;
|
|
2534
|
+
getDataFromDB(dbNames[db], options, (err, data) => {
|
|
2535
|
+
if (data) {
|
|
2536
|
+
rows = rows.concat(data);
|
|
2537
|
+
}
|
|
2538
|
+
if (!--count) {
|
|
2539
|
+
rows.sort(sortByTs);
|
|
2540
|
+
commons.sendResponse(adapter, msg, options, rows, startTime);
|
|
2541
|
+
}
|
|
2542
|
+
});
|
|
2026
2543
|
}
|
|
2027
|
-
}
|
|
2028
|
-
}
|
|
2544
|
+
}
|
|
2545
|
+
});
|
|
2029
2546
|
}
|
|
2030
2547
|
}
|
|
2031
2548
|
|
|
2032
2549
|
function update(id, state, cb) {
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2550
|
+
// first try to find the value in not yet saved data
|
|
2551
|
+
let found = false;
|
|
2552
|
+
if (sqlDPs[id]) {
|
|
2553
|
+
const res = sqlDPs[id].list;
|
|
2554
|
+
if (res) {
|
|
2555
|
+
for (let i = res.length - 1; i >= 0; i--) {
|
|
2556
|
+
if (res[i].state.ts === state.ts) {
|
|
2557
|
+
if (state.val !== undefined) {
|
|
2558
|
+
res[i].state.val = state.val;
|
|
2559
|
+
}
|
|
2560
|
+
if (state.q !== undefined && res[i].q !== undefined) {
|
|
2561
|
+
res[i].state.q = state.q;
|
|
2562
|
+
}
|
|
2563
|
+
if (state.from !== undefined && res[i].from !== undefined) {
|
|
2564
|
+
res[i].state.from = state.from;
|
|
2565
|
+
}
|
|
2566
|
+
if (state.ack !== undefined) {
|
|
2567
|
+
res[i].state.ack = state.ack;
|
|
2568
|
+
}
|
|
2569
|
+
found = true;
|
|
2570
|
+
break;
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2036
2573
|
}
|
|
2574
|
+
}
|
|
2037
2575
|
|
|
2038
|
-
|
|
2576
|
+
if (!found) {
|
|
2577
|
+
prepareTaskCheckTypeAndDbId(id, state, false, err => {
|
|
2578
|
+
if (err) {
|
|
2579
|
+
return cb && cb(err);
|
|
2580
|
+
}
|
|
2039
2581
|
|
|
2040
|
-
|
|
2582
|
+
const type = sqlDPs[id].type;
|
|
2041
2583
|
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2584
|
+
const query = SQLFuncs.update(adapter.config.dbname, sqlDPs[id].index, state, from[state.from], dbNames[type]);
|
|
2585
|
+
|
|
2586
|
+
if (!multiRequests) {
|
|
2587
|
+
if (tasks.length > MAXTASKS) {
|
|
2588
|
+
const error = `Cannot queue new requests, because more than ${MAXTASKS}`;
|
|
2589
|
+
adapter.log.error(error);
|
|
2590
|
+
cb && cb(error);
|
|
2591
|
+
} else {
|
|
2592
|
+
tasks.push({operation: 'query', query, id, callback: cb});
|
|
2593
|
+
tasks.length === 1 && processTasks();
|
|
2594
|
+
}
|
|
2047
2595
|
} else {
|
|
2048
|
-
|
|
2049
|
-
tasks.length === 1 && processTasks();
|
|
2596
|
+
_executeQuery(query, id, cb);
|
|
2050
2597
|
}
|
|
2051
|
-
}
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
}
|
|
2598
|
+
});
|
|
2599
|
+
} else {
|
|
2600
|
+
cb && cb();
|
|
2601
|
+
}
|
|
2055
2602
|
}
|
|
2056
2603
|
|
|
2057
2604
|
function _delete(id, state, cb) {
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2605
|
+
// first try to find the value in not yet saved data
|
|
2606
|
+
let found = false;
|
|
2607
|
+
if (sqlDPs[id]) {
|
|
2608
|
+
const res = sqlDPs[id].list;
|
|
2609
|
+
if (res) {
|
|
2610
|
+
if (!state.ts && !state.start && !state.end) {
|
|
2611
|
+
sqlDPs[id].list = [];
|
|
2612
|
+
} else {
|
|
2613
|
+
for (let i = res.length - 1; i >= 0; i--) {
|
|
2614
|
+
if (state.start && state.end) {
|
|
2615
|
+
if (res[i].state.ts >= state.start && res[i].state.ts <= state.end) {
|
|
2616
|
+
res.splice(i, 1);
|
|
2617
|
+
}
|
|
2618
|
+
} else if (state.start) {
|
|
2619
|
+
if (res[i].state.ts >= state.start) {
|
|
2620
|
+
res.splice(i, 1);
|
|
2621
|
+
}
|
|
2622
|
+
} else if (state.end) {
|
|
2623
|
+
if (res[i].state.ts <= state.end) {
|
|
2624
|
+
res.splice(i, 1);
|
|
2625
|
+
}
|
|
2626
|
+
} else
|
|
2627
|
+
if (res[i].state.ts === state.ts) {
|
|
2628
|
+
res.splice(i, 1);
|
|
2629
|
+
found = true;
|
|
2630
|
+
break;
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2061
2634
|
}
|
|
2635
|
+
}
|
|
2062
2636
|
|
|
2063
|
-
|
|
2637
|
+
if (!found) {
|
|
2638
|
+
prepareTaskCheckTypeAndDbId(id, state, false, err => {
|
|
2639
|
+
if (err) {
|
|
2640
|
+
return cb && cb(err);
|
|
2641
|
+
}
|
|
2064
2642
|
|
|
2065
|
-
|
|
2066
|
-
if (state.start && state.end) {
|
|
2067
|
-
query = SQLFuncs.delete(adapter.config.dbname, dbNames[type], sqlDPs[id].index, state.start, state.end);
|
|
2068
|
-
} else if (state.ts) {
|
|
2069
|
-
query = SQLFuncs.delete(adapter.config.dbname, dbNames[type], sqlDPs[id].index, state.ts);
|
|
2070
|
-
} else {
|
|
2071
|
-
query = SQLFuncs.delete(adapter.config.dbname, dbNames[type], sqlDPs[id].index);
|
|
2072
|
-
}
|
|
2643
|
+
const type = sqlDPs[id].type;
|
|
2073
2644
|
|
|
2074
|
-
|
|
2075
|
-
if (
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2645
|
+
let query;
|
|
2646
|
+
if (state.start && state.end) {
|
|
2647
|
+
query = SQLFuncs.delete(adapter.config.dbname, dbNames[type], sqlDPs[id].index, state.start, state.end);
|
|
2648
|
+
} else if (state.ts) {
|
|
2649
|
+
query = SQLFuncs.delete(adapter.config.dbname, dbNames[type], sqlDPs[id].index, state.ts);
|
|
2079
2650
|
} else {
|
|
2080
|
-
|
|
2081
|
-
tasks.length === 1 && processTasks();
|
|
2651
|
+
query = SQLFuncs.delete(adapter.config.dbname, dbNames[type], sqlDPs[id].index);
|
|
2082
2652
|
}
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2653
|
+
|
|
2654
|
+
if (!multiRequests) {
|
|
2655
|
+
if (tasks.length > MAXTASKS) {
|
|
2656
|
+
const error = `Cannot queue new requests, because more than ${MAXTASKS}`;
|
|
2657
|
+
adapter.log.error(error);
|
|
2658
|
+
cb && cb(error);
|
|
2659
|
+
} else {
|
|
2660
|
+
tasks.push({operation: 'query', query, id, callback: cb});
|
|
2661
|
+
tasks.length === 1 && processTasks();
|
|
2662
|
+
}
|
|
2663
|
+
} else {
|
|
2664
|
+
_executeQuery(query, id, cb);
|
|
2665
|
+
}
|
|
2666
|
+
});
|
|
2667
|
+
} else {
|
|
2668
|
+
cb && cb();
|
|
2669
|
+
}
|
|
2087
2670
|
}
|
|
2088
2671
|
|
|
2089
2672
|
function updateState(msg) {
|
|
2090
2673
|
if (!msg.message) {
|
|
2091
2674
|
adapter.log.error('updateState called with invalid data');
|
|
2092
2675
|
return adapter.sendTo(msg.from, msg.command, {
|
|
2093
|
-
error:
|
|
2676
|
+
error: `Invalid call: ${JSON.stringify(msg)}`
|
|
2094
2677
|
}, msg.callback);
|
|
2095
2678
|
}
|
|
2096
2679
|
let id;
|
|
2097
2680
|
if (Array.isArray(msg.message)) {
|
|
2098
|
-
adapter.log.debug(
|
|
2681
|
+
adapter.log.debug(`updateState ${msg.message.length} items`);
|
|
2099
2682
|
for (let i = 0; i < msg.message.length; i++) {
|
|
2100
2683
|
id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
2101
2684
|
|
|
2102
2685
|
if (msg.message[i].state && typeof msg.message[i].state === 'object') {
|
|
2103
2686
|
update(id, msg.message[i].state);
|
|
2104
2687
|
} else {
|
|
2105
|
-
adapter.log.warn(
|
|
2688
|
+
adapter.log.warn(`Invalid state for ${JSON.stringify(msg.message[i])}`);
|
|
2106
2689
|
}
|
|
2107
2690
|
}
|
|
2108
2691
|
} else if (msg.message.state && Array.isArray(msg.message.state)) {
|
|
2109
|
-
adapter.log.debug(
|
|
2692
|
+
adapter.log.debug(`updateState ${msg.message.state.length} items`);
|
|
2110
2693
|
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2111
2694
|
for (let j = 0; j < msg.message.state.length; j++) {
|
|
2112
2695
|
if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
|
|
2113
2696
|
update(id, msg.message.state[j]);
|
|
2114
2697
|
} else {
|
|
2115
|
-
adapter.log.warn(
|
|
2698
|
+
adapter.log.warn(`Invalid state for ${JSON.stringify(msg.message.state[j])}`);
|
|
2116
2699
|
}
|
|
2117
2700
|
}
|
|
2118
2701
|
} else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
|
|
2119
2702
|
adapter.log.debug('updateState 1 item');
|
|
2120
2703
|
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2121
|
-
update(id, msg.message.state)
|
|
2704
|
+
return update(id, msg.message.state, () => adapter.sendTo(msg.from, msg.command, {
|
|
2705
|
+
success: true,
|
|
2706
|
+
connected: !!clientPool
|
|
2707
|
+
}, msg.callback));
|
|
2122
2708
|
} else {
|
|
2123
2709
|
adapter.log.error('updateState called with invalid data');
|
|
2124
2710
|
return adapter.sendTo(msg.from, msg.command, {
|
|
2125
|
-
error:
|
|
2711
|
+
error: `Invalid call: ${JSON.stringify(msg)}`
|
|
2126
2712
|
}, msg.callback);
|
|
2127
2713
|
}
|
|
2128
2714
|
|
|
@@ -2136,12 +2722,12 @@ function deleteState(msg) {
|
|
|
2136
2722
|
if (!msg.message) {
|
|
2137
2723
|
adapter.log.error('deleteState called with invalid data');
|
|
2138
2724
|
return adapter.sendTo(msg.from, msg.command, {
|
|
2139
|
-
error:
|
|
2725
|
+
error: `Invalid call: ${JSON.stringify(msg)}`
|
|
2140
2726
|
}, msg.callback);
|
|
2141
2727
|
}
|
|
2142
2728
|
let id;
|
|
2143
2729
|
if (Array.isArray(msg.message)) {
|
|
2144
|
-
adapter.log.debug(
|
|
2730
|
+
adapter.log.debug(`deleteState ${msg.message.length} items`);
|
|
2145
2731
|
for (let i = 0; i < msg.message.length; i++) {
|
|
2146
2732
|
id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
2147
2733
|
|
|
@@ -2170,11 +2756,11 @@ function deleteState(msg) {
|
|
|
2170
2756
|
}
|
|
2171
2757
|
_delete(id, {start: msg.message[i].state.start, end: msg.message[i].state.end || Date.now()});
|
|
2172
2758
|
} else {
|
|
2173
|
-
adapter.log.warn(
|
|
2759
|
+
adapter.log.warn(`Invalid state for ${JSON.stringify(msg.message[i])}`);
|
|
2174
2760
|
}
|
|
2175
2761
|
}
|
|
2176
2762
|
} else if (msg.message.state && Array.isArray(msg.message.state)) {
|
|
2177
|
-
adapter.log.debug(
|
|
2763
|
+
adapter.log.debug(`deleteState ${msg.message.state.length} items`);
|
|
2178
2764
|
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2179
2765
|
|
|
2180
2766
|
for (let j = 0; j < msg.message.state.length; j++) {
|
|
@@ -2193,30 +2779,36 @@ function deleteState(msg) {
|
|
|
2193
2779
|
} else if (msg.message.state[j] && typeof msg.message.state[j] === 'number') {
|
|
2194
2780
|
_delete(id, {ts: msg.message.state[j]});
|
|
2195
2781
|
} else {
|
|
2196
|
-
adapter.log.warn(
|
|
2782
|
+
adapter.log.warn(`Invalid state for ${JSON.stringify(msg.message.state[j])}`);
|
|
2197
2783
|
}
|
|
2198
2784
|
}
|
|
2199
2785
|
} else if (msg.message.ts && Array.isArray(msg.message.ts)) {
|
|
2200
|
-
adapter.log.debug(
|
|
2786
|
+
adapter.log.debug(`deleteState ${msg.message.ts.length} items`);
|
|
2201
2787
|
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2202
2788
|
for (let j = 0; j < msg.message.ts.length; j++) {
|
|
2203
2789
|
if (msg.message.ts[j] && typeof msg.message.ts[j] === 'number') {
|
|
2204
2790
|
_delete(id, {ts: msg.message.ts[j]});
|
|
2205
2791
|
} else {
|
|
2206
|
-
adapter.log.warn(
|
|
2792
|
+
adapter.log.warn(`Invalid state for ${JSON.stringify(msg.message.ts[j])}`);
|
|
2207
2793
|
}
|
|
2208
2794
|
}
|
|
2209
2795
|
} else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
|
|
2210
2796
|
adapter.log.debug('deleteState 1 item');
|
|
2211
2797
|
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2212
|
-
_delete(id, {ts: msg.message.state.ts})
|
|
2798
|
+
return _delete(id, {ts: msg.message.state.ts}, () => adapter.sendTo(msg.from, msg.command, {
|
|
2799
|
+
success: true,
|
|
2800
|
+
connected: !!clientPool
|
|
2801
|
+
}, msg.callback));
|
|
2213
2802
|
} else if (msg.message.id && msg.message.ts && typeof msg.message.ts === 'number') {
|
|
2214
2803
|
adapter.log.debug('deleteState 1 item');
|
|
2215
2804
|
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2216
|
-
_delete(id, {ts: msg.message.ts})
|
|
2805
|
+
return _delete(id, {ts: msg.message.ts}, () => adapter.sendTo(msg.from, msg.command, {
|
|
2806
|
+
success: true,
|
|
2807
|
+
connected: !!clientPool
|
|
2808
|
+
}, msg.callback));
|
|
2217
2809
|
} else {
|
|
2218
2810
|
adapter.log.error('deleteState called with invalid data');
|
|
2219
|
-
return adapter.sendTo(msg.from, msg.command, {error:
|
|
2811
|
+
return adapter.sendTo(msg.from, msg.command, {error: `Invalid call: ${JSON.stringify(msg)}`}, msg.callback);
|
|
2220
2812
|
}
|
|
2221
2813
|
|
|
2222
2814
|
adapter.sendTo(msg.from, msg.command, {
|
|
@@ -2229,12 +2821,12 @@ function deleteStateAll(msg) {
|
|
|
2229
2821
|
if (!msg.message) {
|
|
2230
2822
|
adapter.log.error('deleteState called with invalid data');
|
|
2231
2823
|
return adapter.sendTo(msg.from, msg.command, {
|
|
2232
|
-
error:
|
|
2824
|
+
error: `Invalid call: ${JSON.stringify(msg)}`
|
|
2233
2825
|
}, msg.callback);
|
|
2234
2826
|
}
|
|
2235
2827
|
let id;
|
|
2236
2828
|
if (Array.isArray(msg.message)) {
|
|
2237
|
-
adapter.log.debug(
|
|
2829
|
+
adapter.log.debug(`deleteStateAll ${msg.message.length} items`);
|
|
2238
2830
|
for (let i = 0; i < msg.message.length; i++) {
|
|
2239
2831
|
id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
2240
2832
|
_delete(id, {});
|
|
@@ -2242,10 +2834,13 @@ function deleteStateAll(msg) {
|
|
|
2242
2834
|
} else if (msg.message.id) {
|
|
2243
2835
|
adapter.log.debug('deleteStateAll 1 item');
|
|
2244
2836
|
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2245
|
-
_delete(id, {})
|
|
2837
|
+
return _delete(id, {}, () => adapter.sendTo(msg.from, msg.command, {
|
|
2838
|
+
success: true,
|
|
2839
|
+
connected: !!clientPool
|
|
2840
|
+
}, msg.callback));
|
|
2246
2841
|
} else {
|
|
2247
2842
|
adapter.log.error('deleteStateAll called with invalid data');
|
|
2248
|
-
return adapter.sendTo(msg.from, msg.command, {error:
|
|
2843
|
+
return adapter.sendTo(msg.from, msg.command, {error: `Invalid call: ${JSON.stringify(msg)}`}, msg.callback);
|
|
2249
2844
|
}
|
|
2250
2845
|
|
|
2251
2846
|
adapter.sendTo(msg.from, msg.command, {
|
|
@@ -2254,53 +2849,90 @@ function deleteStateAll(msg) {
|
|
|
2254
2849
|
}, msg.callback);
|
|
2255
2850
|
}
|
|
2256
2851
|
|
|
2257
|
-
function
|
|
2852
|
+
function storeStatePushData(id, state, applyRules) {
|
|
2853
|
+
if (!state || typeof state !== 'object') {
|
|
2854
|
+
throw new Error(`State ${JSON.stringify(state)} for ${id} is not valid`);
|
|
2855
|
+
}
|
|
2856
|
+
|
|
2857
|
+
let pushFunc = applyRules ? pushHistory : pushHelper;
|
|
2858
|
+
if (!sqlDPs[id] || !sqlDPs[id][adapter.namespace]) {
|
|
2859
|
+
if (applyRules) {
|
|
2860
|
+
throw new Error(`history not enabled for ${id}, so can not apply the rules as requested`);
|
|
2861
|
+
}
|
|
2862
|
+
sqlDPs[id] = sqlDPs[id] || {};
|
|
2863
|
+
}
|
|
2864
|
+
return new Promise((resolve, reject) => {
|
|
2865
|
+
pushFunc(id, state , err => {
|
|
2866
|
+
if (err) {
|
|
2867
|
+
reject(new Error(`Error writing state for ${id}: ${err.message}, Data: ${JSON.stringify(state)}`));
|
|
2868
|
+
} else {
|
|
2869
|
+
resolve(true);
|
|
2870
|
+
}
|
|
2871
|
+
});
|
|
2872
|
+
});
|
|
2873
|
+
}
|
|
2874
|
+
|
|
2875
|
+
async function storeState(msg) {
|
|
2258
2876
|
if (!msg.message) {
|
|
2259
2877
|
adapter.log.error('storeState called with invalid data');
|
|
2260
2878
|
return adapter.sendTo(msg.from, msg.command, {
|
|
2261
|
-
error:
|
|
2879
|
+
error: `Invalid call: ${JSON.stringify(msg)}`
|
|
2262
2880
|
}, msg.callback);
|
|
2263
2881
|
}
|
|
2264
2882
|
|
|
2265
|
-
let
|
|
2266
|
-
|
|
2267
|
-
pushFunc = pushHistory;
|
|
2268
|
-
}
|
|
2269
|
-
|
|
2270
|
-
let id;
|
|
2883
|
+
let errors = [];
|
|
2884
|
+
let successCount = 0;
|
|
2271
2885
|
if (Array.isArray(msg.message)) {
|
|
2272
|
-
adapter.log.debug(
|
|
2886
|
+
adapter.log.debug(`storeState ${msg.message.length} items`);
|
|
2273
2887
|
for (let i = 0; i < msg.message.length; i++) {
|
|
2274
|
-
id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2888
|
+
const id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
2889
|
+
try {
|
|
2890
|
+
await storeStatePushData(id, msg.message[i].state, msg.message.rules);
|
|
2891
|
+
successCount++;
|
|
2892
|
+
} catch (err) {
|
|
2893
|
+
errors.push(err.message);
|
|
2279
2894
|
}
|
|
2280
2895
|
}
|
|
2281
|
-
} else if (msg.message.
|
|
2282
|
-
adapter.log.debug(
|
|
2283
|
-
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2896
|
+
} else if (msg.message.id && Array.isArray(msg.message.state)) {
|
|
2897
|
+
adapter.log.debug(`storeState ${msg.message.state.length} items`);
|
|
2898
|
+
const id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2284
2899
|
for (let j = 0; j < msg.message.state.length; j++) {
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2900
|
+
try {
|
|
2901
|
+
await storeStatePushData(id, msg.message.state[j], msg.message.rules);
|
|
2902
|
+
successCount++;
|
|
2903
|
+
} catch (err) {
|
|
2904
|
+
errors.push(err.message);
|
|
2289
2905
|
}
|
|
2290
2906
|
}
|
|
2291
|
-
} else if (msg.message.id && msg.message.state
|
|
2907
|
+
} else if (msg.message.id && msg.message.state) {
|
|
2292
2908
|
adapter.log.debug('storeState 1 item');
|
|
2293
|
-
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2294
|
-
|
|
2909
|
+
const id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
|
|
2910
|
+
try {
|
|
2911
|
+
await storeStatePushData(id, msg.message.state, msg.message.rules);
|
|
2912
|
+
successCount++;
|
|
2913
|
+
} catch (err) {
|
|
2914
|
+
errors.push(err.message);
|
|
2915
|
+
}
|
|
2295
2916
|
} else {
|
|
2296
2917
|
adapter.log.error('storeState called with invalid data');
|
|
2297
2918
|
return adapter.sendTo(msg.from, msg.command, {
|
|
2298
2919
|
error: `Invalid call: ${JSON.stringify(msg)}`
|
|
2299
2920
|
}, msg.callback);
|
|
2300
2921
|
}
|
|
2922
|
+
if (errors.length) {
|
|
2923
|
+
adapter.log.warn(`storeState executed with ${errors.length} errors: ${errors.join(', ')}`);
|
|
2924
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
2925
|
+
error: `${errors.length} errors happened while storing data`,
|
|
2926
|
+
errors: errors,
|
|
2927
|
+
successCount
|
|
2928
|
+
}, msg.callback);
|
|
2929
|
+
}
|
|
2930
|
+
|
|
2931
|
+
adapter.log.debug(`storeState executed with ${successCount} states successfully`);
|
|
2301
2932
|
|
|
2302
2933
|
adapter.sendTo(msg.from, msg.command, {
|
|
2303
2934
|
success: true,
|
|
2935
|
+
successCount,
|
|
2304
2936
|
connected: !!clientPool
|
|
2305
2937
|
}, msg.callback);
|
|
2306
2938
|
}
|
|
@@ -2312,6 +2944,7 @@ function getDpOverview(msg) {
|
|
|
2312
2944
|
|
|
2313
2945
|
borrowClientFromPool((err, client) => {
|
|
2314
2946
|
if (err) {
|
|
2947
|
+
returnClientToPool(client);
|
|
2315
2948
|
return adapter.sendTo(msg.from, msg.command, {
|
|
2316
2949
|
error: `Cannot select ${query}: ${err}`
|
|
2317
2950
|
}, msg.callback);
|
|
@@ -2331,7 +2964,7 @@ function getDpOverview(msg) {
|
|
|
2331
2964
|
return;
|
|
2332
2965
|
}
|
|
2333
2966
|
|
|
2334
|
-
adapter.log.info(
|
|
2967
|
+
adapter.log.info(`Query result ${JSON.stringify(rows)}`);
|
|
2335
2968
|
|
|
2336
2969
|
if (rows.length) {
|
|
2337
2970
|
for (let r = 0; r < rows.length; r++) {
|
|
@@ -2358,7 +2991,7 @@ function getDpOverview(msg) {
|
|
|
2358
2991
|
}
|
|
2359
2992
|
}
|
|
2360
2993
|
|
|
2361
|
-
adapter.log.info(
|
|
2994
|
+
adapter.log.info(`initialisation result: ${JSON.stringify(result)}`);
|
|
2362
2995
|
getFirstTsForIds(client, 0, result, msg);
|
|
2363
2996
|
}
|
|
2364
2997
|
});
|
|
@@ -2399,7 +3032,10 @@ function getFirstTsForIds(dbClient, typeId, resultData, msg) {
|
|
|
2399
3032
|
}
|
|
2400
3033
|
|
|
2401
3034
|
adapter.log.info(`enhanced result (${typeId}): ${JSON.stringify(resultData)}`);
|
|
2402
|
-
setTimeout(
|
|
3035
|
+
dpOverviewTimeout = setTimeout(() => {
|
|
3036
|
+
dpOverviewTimeout = null;
|
|
3037
|
+
getFirstTsForIds();
|
|
3038
|
+
}, 5000, dbClient, typeId + 1, resultData, msg);
|
|
2403
3039
|
});
|
|
2404
3040
|
}
|
|
2405
3041
|
} else {
|
|
@@ -2428,7 +3064,7 @@ function getFirstTsForIds(dbClient, typeId, resultData, msg) {
|
|
|
2428
3064
|
}
|
|
2429
3065
|
}
|
|
2430
3066
|
}
|
|
2431
|
-
adapter.log.info(
|
|
3067
|
+
adapter.log.info(`Result: ${JSON.stringify(result)}`);
|
|
2432
3068
|
adapter.sendTo(msg.from, msg.command, {
|
|
2433
3069
|
success: true,
|
|
2434
3070
|
result: result
|
|
@@ -2459,7 +3095,7 @@ function enableHistory(msg) {
|
|
|
2459
3095
|
|
|
2460
3096
|
adapter.extendForeignObject(msg.message.id, obj, err => {
|
|
2461
3097
|
if (err) {
|
|
2462
|
-
adapter.log.error(
|
|
3098
|
+
adapter.log.error(`enableHistory: ${err}`);
|
|
2463
3099
|
adapter.sendTo(msg.from, msg.command, {
|
|
2464
3100
|
error: err
|
|
2465
3101
|
}, msg.callback);
|
|
@@ -2488,7 +3124,7 @@ function disableHistory(msg) {
|
|
|
2488
3124
|
|
|
2489
3125
|
adapter.extendForeignObject(msg.message.id, obj, err => {
|
|
2490
3126
|
if (err) {
|
|
2491
|
-
adapter.log.error(
|
|
3127
|
+
adapter.log.error(`disableHistory: ${err}`);
|
|
2492
3128
|
adapter.sendTo(msg.from, msg.command, {
|
|
2493
3129
|
error: err
|
|
2494
3130
|
}, msg.callback);
|
|
@@ -2504,7 +3140,7 @@ function disableHistory(msg) {
|
|
|
2504
3140
|
function getEnabledDPs(msg) {
|
|
2505
3141
|
const data = {};
|
|
2506
3142
|
for (const id in sqlDPs) {
|
|
2507
|
-
if (sqlDPs.hasOwnProperty(id) && sqlDPs[id] && sqlDPs[id][adapter.namespace]) {
|
|
3143
|
+
if (sqlDPs.hasOwnProperty(id) && sqlDPs[id] && sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].enabled) {
|
|
2508
3144
|
data[sqlDPs[id].realId] = sqlDPs[id][adapter.namespace];
|
|
2509
3145
|
}
|
|
2510
3146
|
}
|
|
@@ -2536,6 +3172,9 @@ function main() {
|
|
|
2536
3172
|
}
|
|
2537
3173
|
|
|
2538
3174
|
adapter.config.retention = parseInt(adapter.config.retention, 10) || 0;
|
|
3175
|
+
if (adapter.config.retention === -1 ) { // Custom timeframe
|
|
3176
|
+
adapter.config.retention = (parseInt(adapter.config.customRetentionDuration, 10) || 0) * 24 * 60 * 60;
|
|
3177
|
+
}
|
|
2539
3178
|
adapter.config.debounce = parseInt(adapter.config.debounce, 10) || 0;
|
|
2540
3179
|
adapter.config.requestInterval = (adapter.config.requestInterval === undefined || adapter.config.requestInterval === null || adapter.config.requestInterval === '') ? 0 : parseInt(adapter.config.requestInterval, 10) || 0;
|
|
2541
3180
|
|
|
@@ -2558,7 +3197,7 @@ function main() {
|
|
|
2558
3197
|
adapter.config.maxConnections = 100;
|
|
2559
3198
|
}
|
|
2560
3199
|
} else {
|
|
2561
|
-
adapter.config.maxConnections =
|
|
3200
|
+
adapter.config.maxConnections = 1; // SQLite does not support multiple connections
|
|
2562
3201
|
}
|
|
2563
3202
|
|
|
2564
3203
|
if (adapter.config.changesMinDelta !== null && adapter.config.changesMinDelta !== undefined) {
|
|
@@ -2567,6 +3206,22 @@ function main() {
|
|
|
2567
3206
|
adapter.config.changesMinDelta = 0;
|
|
2568
3207
|
}
|
|
2569
3208
|
|
|
3209
|
+
if (adapter.config.blockTime !== null && adapter.config.blockTime !== undefined) {
|
|
3210
|
+
adapter.config.blockTime = parseInt(adapter.config.blockTime, 10) || 0;
|
|
3211
|
+
} else {
|
|
3212
|
+
if (adapter.config.debounce !== null && adapter.config.debounce !== undefined) {
|
|
3213
|
+
adapter.config.debounce = parseInt(adapter.config.debounce, 10) || 0;
|
|
3214
|
+
} else {
|
|
3215
|
+
adapter.config.blockTime = 0;
|
|
3216
|
+
}
|
|
3217
|
+
}
|
|
3218
|
+
|
|
3219
|
+
if (adapter.config.debounceTime !== null && adapter.config.debounceTime !== undefined) {
|
|
3220
|
+
adapter.config.debounceTime = parseInt(adapter.config.debounceTime, 10) || 0;
|
|
3221
|
+
} else {
|
|
3222
|
+
adapter.config.debounceTime = 0;
|
|
3223
|
+
}
|
|
3224
|
+
|
|
2570
3225
|
multiRequests = clients[adapter.config.dbtype].multiRequests;
|
|
2571
3226
|
if (!multiRequests) {
|
|
2572
3227
|
adapter.config.writeNulls = false;
|
|
@@ -2574,29 +3229,41 @@ function main() {
|
|
|
2574
3229
|
|
|
2575
3230
|
adapter.config.port = parseInt(adapter.config.port, 10) || 0;
|
|
2576
3231
|
|
|
2577
|
-
if (adapter.config.round !== null && adapter.config.round !== undefined) {
|
|
2578
|
-
adapter.config.round =
|
|
3232
|
+
if (adapter.config.round !== null && adapter.config.round !== undefined && adapter.config.round !== '') {
|
|
3233
|
+
adapter.config.round = parseInt(adapter.config.round, 10);
|
|
3234
|
+
if (!isFinite(adapter.config.round) || adapter.config.round < 0) {
|
|
3235
|
+
adapter.config.round = null;
|
|
3236
|
+
adapter.log.info(`Invalid round value: ${adapter.config.round} - ignore, do not round values`);
|
|
3237
|
+
} else {
|
|
3238
|
+
adapter.config.round = Math.pow(10, parseInt(adapter.config.round, 10));
|
|
3239
|
+
}
|
|
2579
3240
|
} else {
|
|
2580
3241
|
adapter.config.round = null;
|
|
2581
3242
|
}
|
|
2582
3243
|
|
|
2583
3244
|
if (adapter.config.dbtype === 'postgresql' && !SQL.PostgreSQLClient) {
|
|
2584
|
-
const postgres = require(__dirname
|
|
3245
|
+
const postgres = require(`${__dirname}/lib/postgresql-client`);
|
|
2585
3246
|
for (const attr in postgres) {
|
|
2586
3247
|
if (postgres.hasOwnProperty(attr) && !SQL[attr]) {
|
|
2587
3248
|
SQL[attr] = postgres[attr];
|
|
2588
3249
|
}
|
|
2589
3250
|
}
|
|
2590
|
-
} else
|
|
2591
|
-
|
|
2592
|
-
const mssql = require(__dirname + '/lib/mssql-client');
|
|
3251
|
+
} else if (adapter.config.dbtype === 'mssql' && !SQL.MSSQLClient) {
|
|
3252
|
+
const mssql = require(`${__dirname}/lib/mssql-client`);
|
|
2593
3253
|
for (const attr_ in mssql) {
|
|
2594
3254
|
if (mssql.hasOwnProperty(attr_) && !SQL[attr_]) {
|
|
2595
3255
|
SQL[attr_] = mssql[attr_];
|
|
2596
3256
|
}
|
|
2597
3257
|
}
|
|
3258
|
+
} else if (adapter.config.dbtype === 'mysql' && !SQL.MySQL2Client) {
|
|
3259
|
+
const mysql = require(`${__dirname}/lib/mysql-client`);
|
|
3260
|
+
for (const attr_ in mysql) {
|
|
3261
|
+
if (mysql.hasOwnProperty(attr_) && !SQL[attr_]) {
|
|
3262
|
+
SQL[attr_] = mysql[attr_];
|
|
3263
|
+
}
|
|
3264
|
+
}
|
|
2598
3265
|
}
|
|
2599
|
-
SQLFuncs = require(__dirname
|
|
3266
|
+
SQLFuncs = require(`${__dirname}/lib/${adapter.config.dbtype}`);
|
|
2600
3267
|
|
|
2601
3268
|
if (adapter.config.dbtype === 'sqlite' || adapter.config.host) {
|
|
2602
3269
|
connect(() => {
|
|
@@ -2637,18 +3304,43 @@ function main() {
|
|
|
2637
3304
|
count++;
|
|
2638
3305
|
adapter.log.info(`enabled logging of ${id}, Alias=${id !== realId}, ${count} points now activated`);
|
|
2639
3306
|
|
|
3307
|
+
// maxLength
|
|
3308
|
+
if (!sqlDPs[id][adapter.namespace].maxLength && sqlDPs[id][adapter.namespace].maxLength !== '0' && sqlDPs[id][adapter.namespace].maxLength !== 0) {
|
|
3309
|
+
sqlDPs[id][adapter.namespace].maxLength = parseInt(adapter.config.maxLength, 10) || 0;
|
|
3310
|
+
} else {
|
|
3311
|
+
sqlDPs[id][adapter.namespace].maxLength = parseInt(sqlDPs[id][adapter.namespace].maxLength, 10);
|
|
3312
|
+
}
|
|
3313
|
+
|
|
2640
3314
|
// retention
|
|
2641
3315
|
if (sqlDPs[id][adapter.namespace].retention !== undefined && sqlDPs[id][adapter.namespace].retention !== null && sqlDPs[id][adapter.namespace].retention !== '') {
|
|
2642
3316
|
sqlDPs[id][adapter.namespace].retention = parseInt(sqlDPs[id][adapter.namespace].retention, 10) || 0;
|
|
2643
3317
|
} else {
|
|
2644
3318
|
sqlDPs[id][adapter.namespace].retention = adapter.config.retention;
|
|
2645
3319
|
}
|
|
3320
|
+
if (sqlDPs[id][adapter.namespace].retention === -1) {
|
|
3321
|
+
// customRetentionDuration
|
|
3322
|
+
if (sqlDPs[id][adapter.namespace].customRetentionDuration !== undefined && sqlDPs[id][adapter.namespace].customRetentionDuration !== null && sqlDPs[id][adapter.namespace].customRetentionDuration !== '') {
|
|
3323
|
+
sqlDPs[id][adapter.namespace].customRetentionDuration = parseInt(sqlDPs[id][adapter.namespace].customRetentionDuration, 10) || 0;
|
|
3324
|
+
} else {
|
|
3325
|
+
sqlDPs[id][adapter.namespace].customRetentionDuration = adapter.config.customRetentionDuration;
|
|
3326
|
+
}
|
|
3327
|
+
sqlDPs[id][adapter.namespace].retention = sqlDPs[id][adapter.namespace].customRetentionDuration * 24 * 60 * 60
|
|
3328
|
+
}
|
|
2646
3329
|
|
|
2647
|
-
// debounce
|
|
2648
|
-
if (sqlDPs[id][adapter.namespace].
|
|
2649
|
-
sqlDPs[id][adapter.namespace].debounce
|
|
3330
|
+
// debounceTime and debounce compatibility handling
|
|
3331
|
+
if (!sqlDPs[id][adapter.namespace].blockTime && sqlDPs[id][adapter.namespace].blockTime !== '0' && sqlDPs[id][adapter.namespace].blockTime !== 0) {
|
|
3332
|
+
if (!sqlDPs[id][adapter.namespace].debounce && sqlDPs[id][adapter.namespace].debounce !== '0' && sqlDPs[id][adapter.namespace].debounce !== 0) {
|
|
3333
|
+
sqlDPs[id][adapter.namespace].blockTime = parseInt(adapter.config.blockTime, 10) || 0;
|
|
3334
|
+
} else {
|
|
3335
|
+
sqlDPs[id][adapter.namespace].blockTime = parseInt(sqlDPs[id][adapter.namespace].debounce, 10) || 0;
|
|
3336
|
+
}
|
|
3337
|
+
} else {
|
|
3338
|
+
sqlDPs[id][adapter.namespace].blockTime = parseInt(sqlDPs[id][adapter.namespace].blockTime, 10) || 0;
|
|
3339
|
+
}
|
|
3340
|
+
if (!sqlDPs[id][adapter.namespace].debounceTime && sqlDPs[id][adapter.namespace].debounceTime !== '0' && sqlDPs[id][adapter.namespace].debounceTime !== 0) {
|
|
3341
|
+
sqlDPs[id][adapter.namespace].debounceTime = parseInt(adapter.config.debounceTime, 10) || 0;
|
|
2650
3342
|
} else {
|
|
2651
|
-
sqlDPs[id][adapter.namespace].
|
|
3343
|
+
sqlDPs[id][adapter.namespace].debounceTime = parseInt(sqlDPs[id][adapter.namespace].debounceTime, 10) || 0;
|
|
2652
3344
|
}
|
|
2653
3345
|
|
|
2654
3346
|
// changesOnly
|
|
@@ -2657,8 +3349,43 @@ function main() {
|
|
|
2657
3349
|
// ignoreZero
|
|
2658
3350
|
sqlDPs[id][adapter.namespace].ignoreZero = sqlDPs[id][adapter.namespace].ignoreZero === 'true' || sqlDPs[id][adapter.namespace].ignoreZero === true;
|
|
2659
3351
|
|
|
2660
|
-
//
|
|
2661
|
-
sqlDPs[id][adapter.namespace].
|
|
3352
|
+
// round
|
|
3353
|
+
if (sqlDPs[id][adapter.namespace].round !== null && sqlDPs[id][adapter.namespace].round !== undefined && sqlDPs[id][adapter.namespace] !== '') {
|
|
3354
|
+
sqlDPs[id][adapter.namespace].round = parseInt(sqlDPs[id][adapter.namespace], 10);
|
|
3355
|
+
if (!isFinite(sqlDPs[id][adapter.namespace].round) || sqlDPs[id][adapter.namespace].round < 0) {
|
|
3356
|
+
sqlDPs[id][adapter.namespace].round = adapter.config.round;
|
|
3357
|
+
} else {
|
|
3358
|
+
sqlDPs[id][adapter.namespace].round = Math.pow(10, parseInt(sqlDPs[id][adapter.namespace].round, 10));
|
|
3359
|
+
}
|
|
3360
|
+
} else {
|
|
3361
|
+
sqlDPs[id][adapter.namespace].round = adapter.config.round;
|
|
3362
|
+
}
|
|
3363
|
+
|
|
3364
|
+
// ignoreAboveNumber
|
|
3365
|
+
if (sqlDPs[id][adapter.namespace].ignoreAboveNumber !== undefined && sqlDPs[id][adapter.namespace].ignoreAboveNumber !== null && sqlDPs[id][adapter.namespace].ignoreAboveNumber !== '') {
|
|
3366
|
+
sqlDPs[id][adapter.namespace].ignoreAboveNumber = parseFloat(sqlDPs[id][adapter.namespace].ignoreAboveNumber) || null;
|
|
3367
|
+
}
|
|
3368
|
+
|
|
3369
|
+
// ignoreBelowNumber and ignoreBelowZero compatibility handling
|
|
3370
|
+
if (sqlDPs[id][adapter.namespace].ignoreBelowNumber !== undefined && sqlDPs[id][adapter.namespace].ignoreBelowNumber !== null && sqlDPs[id][adapter.namespace].ignoreBelowNumber !== '') {
|
|
3371
|
+
sqlDPs[id][adapter.namespace].ignoreBelowNumber = parseFloat(sqlDPs[id][adapter.namespace].ignoreBelowNumber) || null;
|
|
3372
|
+
} else if (sqlDPs[id][adapter.namespace].ignoreBelowZero === 'true' || sqlDPs[id][adapter.namespace].ignoreBelowZero === true) {
|
|
3373
|
+
sqlDPs[id][adapter.namespace].ignoreBelowNumber = 0;
|
|
3374
|
+
}
|
|
3375
|
+
|
|
3376
|
+
// disableSkippedValueLogging
|
|
3377
|
+
if (sqlDPs[id][adapter.namespace].disableSkippedValueLogging !== undefined && sqlDPs[id][adapter.namespace].disableSkippedValueLogging !== null && sqlDPs[id][adapter.namespace].disableSkippedValueLogging !== '') {
|
|
3378
|
+
sqlDPs[id][adapter.namespace].disableSkippedValueLogging = sqlDPs[id][adapter.namespace].disableSkippedValueLogging === 'true' || sqlDPs[id][adapter.namespace].disableSkippedValueLogging === true;
|
|
3379
|
+
} else {
|
|
3380
|
+
sqlDPs[id][adapter.namespace].disableSkippedValueLogging = adapter.config.disableSkippedValueLogging;
|
|
3381
|
+
}
|
|
3382
|
+
|
|
3383
|
+
// enableDebugLogs
|
|
3384
|
+
if (sqlDPs[id][adapter.namespace].enableDebugLogs !== undefined && sqlDPs[id][adapter.namespace].enableDebugLogs !== null && sqlDPs[id][adapter.namespace].enableDebugLogs !== '') {
|
|
3385
|
+
sqlDPs[id][adapter.namespace].enableDebugLogs = sqlDPs[id][adapter.namespace].enableDebugLogs === 'true' || sqlDPs[id][adapter.namespace].enableDebugLogs === true;
|
|
3386
|
+
} else {
|
|
3387
|
+
sqlDPs[id][adapter.namespace].enableDebugLogs = adapter.config.enableDebugLogs;
|
|
3388
|
+
}
|
|
2662
3389
|
|
|
2663
3390
|
// changesRelogInterval
|
|
2664
3391
|
if (sqlDPs[id][adapter.namespace].changesRelogInterval !== undefined && sqlDPs[id][adapter.namespace].changesRelogInterval !== null && sqlDPs[id][adapter.namespace].changesRelogInterval !== '') {
|
|
@@ -2669,7 +3396,7 @@ function main() {
|
|
|
2669
3396
|
|
|
2670
3397
|
// changesMinDelta
|
|
2671
3398
|
if (sqlDPs[id][adapter.namespace].changesMinDelta !== undefined && sqlDPs[id][adapter.namespace].changesMinDelta !== null && sqlDPs[id][adapter.namespace].changesMinDelta !== '') {
|
|
2672
|
-
sqlDPs[id][adapter.namespace].changesMinDelta = parseFloat(sqlDPs[id][adapter.namespace].changesMinDelta) || 0;
|
|
3399
|
+
sqlDPs[id][adapter.namespace].changesMinDelta = parseFloat(sqlDPs[id][adapter.namespace].changesMinDelta.toString().replace(/,/g, '.')) || 0;
|
|
2673
3400
|
} else {
|
|
2674
3401
|
sqlDPs[id][adapter.namespace].changesMinDelta = adapter.config.changesMinDelta;
|
|
2675
3402
|
}
|
|
@@ -2685,12 +3412,17 @@ function main() {
|
|
|
2685
3412
|
}
|
|
2686
3413
|
|
|
2687
3414
|
// relogTimeout
|
|
2688
|
-
if (sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].changesRelogInterval > 0) {
|
|
3415
|
+
if (sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].changesOnly && sqlDPs[id][adapter.namespace].changesRelogInterval > 0) {
|
|
2689
3416
|
sqlDPs[id].relogTimeout && clearTimeout(sqlDPs[id].relogTimeout);
|
|
2690
3417
|
sqlDPs[id].relogTimeout = setTimeout(reLogHelper, (sqlDPs[id][adapter.namespace].changesRelogInterval * 500 * Math.random()) + sqlDPs[id][adapter.namespace].changesRelogInterval * 500, id);
|
|
2691
3418
|
}
|
|
2692
3419
|
|
|
2693
3420
|
sqlDPs[id].realId = realId;
|
|
3421
|
+
sqlDPs[id].list = sqlDPs[id].list || [];
|
|
3422
|
+
sqlDPs[id].inFlight = sqlDPs[id].inFlight || {};
|
|
3423
|
+
|
|
3424
|
+
// randomize lastCheck to avoid all datapoints to be checked at same timepoint
|
|
3425
|
+
sqlDPs[id].lastCheck = Date.now() - Math.floor(Math.random() * 21600000/* 6 hours */);
|
|
2694
3426
|
}
|
|
2695
3427
|
}
|
|
2696
3428
|
}
|
|
@@ -2715,19 +3447,14 @@ function main() {
|
|
|
2715
3447
|
adapter.log.debug('Initialization done');
|
|
2716
3448
|
setConnected(true);
|
|
2717
3449
|
processStartValues();
|
|
3450
|
+
|
|
3451
|
+
// store all buffered data every 10 minutes to not lost the data
|
|
3452
|
+
bufferChecker = setInterval(() => storeCached(), 10 * 60000);
|
|
2718
3453
|
});
|
|
2719
3454
|
});
|
|
2720
3455
|
}
|
|
2721
3456
|
}
|
|
2722
3457
|
|
|
2723
|
-
// close connection to DB
|
|
2724
|
-
process.on('SIGINT', () => finish());
|
|
2725
|
-
|
|
2726
|
-
// close connection to DB
|
|
2727
|
-
process.on('SIGTERM', () => finish());
|
|
2728
|
-
|
|
2729
|
-
process.on('uncaughtException', err => adapter.log.warn('Exception: ' + err));
|
|
2730
|
-
|
|
2731
3458
|
// If started as allInOne/compact mode => return function to create instance
|
|
2732
3459
|
if (module.parent) {
|
|
2733
3460
|
module.exports = startAdapter;
|