iobroker.sql 1.15.5 → 1.16.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/main.js CHANGED
@@ -38,6 +38,7 @@ const storageTypes = [
38
38
 
39
39
  const sqlDPs = {};
40
40
  const from = {};
41
+ const MAXTASKS = 100;
41
42
  const tasks = [];
42
43
  const tasksReadType = [];
43
44
  const tasksStart = [];
@@ -115,17 +116,17 @@ function startAdapter(options) {
115
116
  if (obj.common.custom && obj.common.custom[adapter.namespace] && obj.common.custom[adapter.namespace].aliasId) {
116
117
  if (obj.common.custom[adapter.namespace].aliasId !== id) {
117
118
  aliasMap[id] = obj.common.custom[adapter.namespace].aliasId;
118
- adapter.log.debug('Registered Alias: ' + id + ' --> ' + aliasMap[id]);
119
+ adapter.log.debug(`Registered Alias: ${id} --> ${aliasMap[id]}`);
119
120
  id = aliasMap[id];
120
121
  checkForRemove = false;
121
122
  } else {
122
- adapter.log.warn('Ignoring Alias-ID because identical to ID for ' + id);
123
+ adapter.log.warn(`Ignoring Alias-ID because identical to ID for ${id}`);
123
124
  obj.common.custom[adapter.namespace].aliasId = '';
124
125
  }
125
126
  }
126
127
 
127
128
  if (checkForRemove && aliasMap[id]) {
128
- adapter.log.debug('Removed Alias: ' + id + ' !-> ' + aliasMap[id]);
129
+ adapter.log.debug(`Removed Alias: ${id} !-> ${aliasMap[id]}`);
129
130
  delete aliasMap[id];
130
131
  }
131
132
 
@@ -159,7 +160,7 @@ function startAdapter(options) {
159
160
  }
160
161
  } else {
161
162
  if (aliasMap[id]) {
162
- adapter.log.debug('Removed Alias: ' + id + ' !-> ' + aliasMap[id]);
163
+ adapter.log.debug(`Removed Alias: ${id} !-> ${aliasMap[id]}`);
163
164
  delete aliasMap[id];
164
165
  }
165
166
 
@@ -187,10 +188,10 @@ function startAdapter(options) {
187
188
  _state.from = 'system.adapter.' + adapter.namespace;
188
189
  nullValue.ts += 4;
189
190
  nullValue.lc += 4; // because of MS SQL
190
- adapter.log.debug('Write 1/2 "' + _state.val + '" _id: ' + _id);
191
+ adapter.log.debug(`Write 1/2 "${_state.val}" _id: ${_id}`);
191
192
  pushValueIntoDB(_id, _state, () => {
192
193
  // terminate values with null to indicate adapter stop. timestamp + 1#
193
- adapter.log.debug('Write 2/2 "null" _id: ' + _id);
194
+ adapter.log.debug(`Write 2/2 "null" _id: ${_id}`);
194
195
  pushValueIntoDB(_id, _nullValue, () => delete sqlDPs[id][adapter.namespace]);
195
196
  });
196
197
  })(id, state, nullValue);
@@ -268,6 +269,9 @@ function reInit(id, realId, formerAliasId, storedIndex, storedType, obj) {
268
269
  // changesOnly
269
270
  obj.common.custom[adapter.namespace].changesOnly = obj.common.custom[adapter.namespace].changesOnly === 'true' || obj.common.custom[adapter.namespace].changesOnly === true;
270
271
 
272
+ 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
+
271
275
  if (obj.common.custom[adapter.namespace].changesRelogInterval || obj.common.custom[adapter.namespace].changesRelogInterval === 0) {
272
276
  obj.common.custom[adapter.namespace].changesRelogInterval = parseInt(obj.common.custom[adapter.namespace].changesRelogInterval, 10) || 0;
273
277
  } else {
@@ -294,7 +298,7 @@ function reInit(id, realId, formerAliasId, storedIndex, storedType, obj) {
294
298
  if (sqlDPs[formerAliasId] &&
295
299
  sqlDPs[formerAliasId][adapter.namespace] &&
296
300
  isEqual(obj.common.custom[adapter.namespace], sqlDPs[formerAliasId][adapter.namespace])) {
297
- return adapter.log.debug('Object ' + id + ' unchanged. Ignore');
301
+ return adapter.log.debug(`Object ${id} unchanged. Ignore`);
298
302
  }
299
303
 
300
304
  // relogTimeout
@@ -316,7 +320,7 @@ function reInit(id, realId, formerAliasId, storedIndex, storedType, obj) {
316
320
  writeNulls(id);
317
321
  }
318
322
 
319
- adapter.log.info('enabled logging of ' + id + ', Alias=' + (id !== realId));
323
+ adapter.log.info(`enabled logging of ${id}, Alias=${id !== realId}`);
320
324
  }
321
325
 
322
326
  function setConnected(isConnected) {
@@ -338,7 +342,7 @@ function connect(callback) {
338
342
 
339
343
  let params = {
340
344
  server: adapter.config.host, // needed for MSSQL
341
- host: adapter.config.host, // needed for PostgeSQL , MySQL
345
+ host: adapter.config.host, // needed for PostgreSQL , MySQL
342
346
  user: adapter.config.user || '',
343
347
  password: adapter.config.password || '',
344
348
  max_idle: (adapter.config.dbtype === 'sqlite') ? 1 : 2
@@ -358,10 +362,10 @@ function connect(callback) {
358
362
  return adapter.log.error('DB Type is not defined!');
359
363
  } else
360
364
  if (!clients[adapter.config.dbtype] || !clients[adapter.config.dbtype].name) {
361
- return adapter.log.error('Unknown type "' + adapter.config.dbtype + '"');
365
+ return adapter.log.error(`Unknown type "${adapter.config.dbtype}"`);
362
366
  } else
363
367
  if (!SQL[clients[adapter.config.dbtype].name]) {
364
- return adapter.log.error('SQL package "' + clients[adapter.config.dbtype].name + '" is not installed.');
368
+ return adapter.log.error(`SQL package "${clients[adapter.config.dbtype].name}" is not installed.`);
365
369
  }
366
370
 
367
371
  if (adapter.config.dbtype !== 'postgresql') {
@@ -422,9 +426,9 @@ function connect(callback) {
422
426
 
423
427
  try {
424
428
  if (!clients[adapter.config.dbtype].name) {
425
- adapter.log.error('Unknown SQL type selected: "' + adapter.config.dbtype + '"');
429
+ adapter.log.error(`Unknown SQL type selected: "${adapter.config.dbtype}"`);
426
430
  } else if (!SQL[clients[adapter.config.dbtype].name + 'Pool']) {
427
- adapter.log.error('Selected SQL DB was not installed properly: "' + adapter.config.dbtype + '". SQLite requires build tools on system. See README.md');
431
+ adapter.log.error(`Selected SQL DB was not installed properly: "${adapter.config.dbtype}". SQLite requires build tools on system. See README.md`);
428
432
  } else {
429
433
  clientPool = new SQL[clients[adapter.config.dbtype].name + 'Pool'](params);
430
434
 
@@ -443,7 +447,7 @@ function connect(callback) {
443
447
  }
444
448
  } catch (ex) {
445
449
  if (ex.toString() === 'TypeError: undefined is not a function') {
446
- adapter.log.error('Node.js DB driver for "' + adapter.config.dbtype + '" could not be installed.');
450
+ adapter.log.error(`Node.js DB driver for "${adapter.config.dbtype}" could not be installed.`);
447
451
  } else {
448
452
  adapter.log.error(ex.toString());
449
453
  adapter.log.error(ex.stack);
@@ -495,6 +499,10 @@ function getSqlLiteDir(fileName) {
495
499
  }
496
500
 
497
501
  function testConnection(msg) {
502
+ if (!msg.message.config) {
503
+ return msg.callback && adapter.sendTo(msg.from, msg.command, {error: 'invalid config'}, msg.callback);
504
+ }
505
+
498
506
  msg.message.config.port = parseInt(msg.message.config.port, 10) || 0;
499
507
  let params = {
500
508
  server: msg.message.config.host,
@@ -575,14 +583,14 @@ function destroyDB(msg) {
575
583
  adapter.log.error(err);
576
584
  adapter.sendTo(msg.from, msg.command, {error: err.toString()}, msg.callback);
577
585
  } else {
578
- adapter.sendTo(msg.from, msg.command, {error: null}, msg.callback);
586
+ adapter.sendTo(msg.from, msg.command, {error: null, result: 'deleted'}, msg.callback);
579
587
  // restart adapter
580
588
  setTimeout(() =>
581
589
  adapter.getForeignObject('system.adapter.' + adapter.namespace, (err, obj) => {
582
590
  if (!err) {
583
591
  adapter.setForeignObject(obj._id, obj);
584
592
  } else {
585
- adapter.log.error('Cannot read object "system.adapter.' + adapter.namespace + '": ' + err);
593
+ adapter.log.error(`Cannot read object "system.adapter.${adapter.namespace}": ${err}`);
586
594
  adapter.stop();
587
595
  }
588
596
  }), 2000);
@@ -622,8 +630,8 @@ function _userQuery(msg, callback) {
622
630
  // execute custom query
623
631
  function query(msg) {
624
632
  if (!multiRequests) {
625
- if (tasks.length > 100) {
626
- const error = 'Cannot queue new requests, because more than 100';
633
+ if (tasks.length > MAXTASKS) {
634
+ const error = 'Cannot queue new requests, because more than '+MAXTASKS;
627
635
  adapter.log.error(error);
628
636
  adapter.sendTo(msg.from, msg.command, {error}, msg.callback);
629
637
  } else {
@@ -674,7 +682,7 @@ function oneScript(script, cb) {
674
682
  else if (err.code === '42P07') {
675
683
  const match = script.match(/CREATE\s+TABLE\s+(\w*)\s+\(/);
676
684
  if (match) {
677
- adapter.log.debug('OK. Table "' + match[1] + '" yet exists');
685
+ adapter.log.debug(`OK. Table "${match[1]}" yet exists`);
678
686
  err = null;
679
687
  } else {
680
688
  adapter.log.error(script);
@@ -683,7 +691,7 @@ function oneScript(script, cb) {
683
691
  } else if (script.startsWith('CREATE INDEX')) {
684
692
  adapter.log.info('Ignore Error on Create index. You might want to create the index yourself!');
685
693
  adapter.log.info(script);
686
- adapter.log.info(err.code + ': ' + err);
694
+ adapter.log.info(`${err.code}: ${err}`);
687
695
  err = null;
688
696
  } else {
689
697
  adapter.log.error(script);
@@ -771,10 +779,10 @@ function finish(callback) {
771
779
  _state.from = 'system.adapter.' + adapter.namespace;
772
780
  nullValue.ts += 4;
773
781
  nullValue.lc += 4; // because of MS SQL
774
- adapter.log.debug('Write 1/2 "' + _state.val + '" _id: ' + _id);
782
+ adapter.log.debug(`Write 1/2 "${_state.val}" _id: ${_id}`);
775
783
  pushValueIntoDB(_id, _state, () => {
776
784
  // terminate values with null to indicate adapter stop. timestamp + 1#
777
- adapter.log.debug('Write 2/2 "null" _id: ' + _id);
785
+ adapter.log.debug(`Write 2/2 "null" _id: ${_id}`);
778
786
  pushValueIntoDB(_id, _nullValue, () => {
779
787
  if (!--count) {
780
788
  if (clientPool) {
@@ -928,26 +936,6 @@ function processMessage(msg) {
928
936
  }
929
937
  }
930
938
 
931
- function fixSelector(callback) {
932
- // fix _design/custom object
933
- adapter.getForeignObject('_design/custom', (err, obj) => {
934
- if (!obj || !obj.views.state.map.includes('common.custom')) {
935
- obj = {
936
- _id: '_design/custom',
937
- language: 'javascript',
938
- views: {
939
- state: {
940
- map: 'function(doc) { doc.type === \'state\' && doc.common.custom && emit(doc._id, doc.common.custom) }'
941
- }
942
- }
943
- };
944
- adapter.setForeignObject('_design/custom', obj, err => callback && callback(err));
945
- } else {
946
- callback && callback(err);
947
- }
948
- });
949
- }
950
-
951
939
  function processStartValues(callback) {
952
940
  if (tasksStart && tasksStart.length) {
953
941
  const task = tasksStart.shift();
@@ -1166,7 +1154,7 @@ function pushHelper(_id, timeoutTriggered) {
1166
1154
  }
1167
1155
 
1168
1156
  if (sqlDPs[_id].state.val !== null && sqlDPs[_id].state.val !== undefined) {
1169
- adapter.log.debug('Datatype ' + _id + ': Currently: ' + typeof sqlDPs[_id].state.val + ', StorageType: ' + _settings.storageType);
1157
+ adapter.log.debug(`Datatype ${_id}: Currently: ${typeof sqlDPs[_id].state.val}, StorageType: ${_settings.storageType}`);
1170
1158
 
1171
1159
  if (typeof sqlDPs[_id].state.val === 'string' && _settings.storageType !== 'String') {
1172
1160
  adapter.log.debug('Do Automatic Datatype conversion for ' + _id);
@@ -1291,8 +1279,8 @@ function checkRetention(id) {
1291
1279
  const query = SQLFuncs.retention(adapter.config.dbname, sqlDPs[id].index, dbNames[sqlDPs[id].type], sqlDPs[id][adapter.namespace].retention);
1292
1280
 
1293
1281
  if (!multiRequests) {
1294
- if (tasks.length > 100) {
1295
- return adapter.log.error('Cannot queue new requests, because more than 100');
1282
+ if (tasks.length > MAXTASKS) {
1283
+ return adapter.log.error('Cannot queue new requests, because more than '+MAXTASKS);
1296
1284
  }
1297
1285
 
1298
1286
  let start = tasks.length === 1;
@@ -1325,7 +1313,7 @@ function _insertValueIntoDB(query, id, cb) {
1325
1313
  borrowClientFromPool((err, client) => {
1326
1314
  if (err) {
1327
1315
  adapter.log.error(err);
1328
- cb && cb();
1316
+ cb && cb(); // BF asked (2021.12.14): may be return here err?
1329
1317
  } else {
1330
1318
  client.execute(query, (err /* , rows, fields */) => {
1331
1319
  returnClientToPool(client);
@@ -1334,7 +1322,7 @@ function _insertValueIntoDB(query, id, cb) {
1334
1322
  } else {
1335
1323
  checkRetention(id);
1336
1324
  }
1337
- cb && cb();
1325
+ cb && cb(); // BF asked (2021.12.14): may be return here err?
1338
1326
  });
1339
1327
  }
1340
1328
  });
@@ -1584,6 +1572,15 @@ function pushValueIntoDB(id, state, isCounter, cb) {
1584
1572
  isCounter = false;
1585
1573
  }
1586
1574
 
1575
+ if (sqlDPs[id][adapter.namespace].ignoreZero && (!state || state.val === undefined || state.val === null || state.val === 0)) {
1576
+ 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 zero or null`);
1577
+ return cb && cb();
1578
+ } else
1579
+ if (state && 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();
1582
+ }
1583
+
1587
1584
  adapter.log.debug(`pushValueIntoDB called for ${id} (type: ${sqlDPs[id].type}, ID: ${sqlDPs[id].index}) and state: ${JSON.stringify(state)}`);
1588
1585
 
1589
1586
  prepareTaskCheckTypeAndDbId(id, state, isCounter, err => {
@@ -1605,8 +1602,8 @@ function pushValueIntoDB(id, state, isCounter, cb) {
1605
1602
  const query = SQLFuncs.insert(adapter.config.dbname, sqlDPs[id].index, state, from[state.from] || 0, isCounter ? 'ts_counter' : dbNames[type]);
1606
1603
 
1607
1604
  if (!multiRequests) {
1608
- if (tasks.length > 100) {
1609
- const error = 'Cannot queue new requests, because more than 100';
1605
+ if (tasks.length > MAXTASKS) {
1606
+ const error = 'Cannot queue new requests, because more than ' + MAXTASKS;
1610
1607
  adapter.log.error(error);
1611
1608
  cb && cb(error);
1612
1609
  } else {
@@ -1879,9 +1876,9 @@ function getDataFromDB(db, options, callback) {
1879
1876
  const query = SQLFuncs.getHistory(adapter.config.dbname, db, options);
1880
1877
  adapter.log.debug(query);
1881
1878
  if (!multiRequests) {
1882
- if (tasks.length > 100) {
1883
- adapter.log.error('Cannot queue new requests, because more than 100');
1884
- callback && callback('Cannot queue new requests, because more than 100');
1879
+ if (tasks.length > MAXTASKS) {
1880
+ adapter.log.error('Cannot queue new requests, because more than '+MAXTASKS);
1881
+ callback && callback('Cannot queue new requests, because more than '+MAXTASKS);
1885
1882
  } else {
1886
1883
  tasks.push({operation: 'select', query, options, callback});
1887
1884
  tasks.length === 1 && processTasks();
@@ -1897,8 +1894,8 @@ function getCounterDataFromDB(options, callback) {
1897
1894
  adapter.log.debug(query);
1898
1895
 
1899
1896
  if (!multiRequests) {
1900
- if (tasks.length > 100) {
1901
- const error = 'Cannot queue new requests, because more than 100';
1897
+ if (tasks.length > MAXTASKS) {
1898
+ const error = 'Cannot queue new requests, because more than '+MAXTASKS;
1902
1899
  adapter.log.error(error);
1903
1900
  callback && callback(error);
1904
1901
  } else {
@@ -2043,8 +2040,8 @@ function update(id, state, cb) {
2043
2040
  const query = SQLFuncs.update(adapter.config.dbname, sqlDPs[id].index, state, from[state.from], dbNames[type]);
2044
2041
 
2045
2042
  if (!multiRequests) {
2046
- if (tasks.length > 100) {
2047
- const error = 'Cannot queue new requests, because more than 100';
2043
+ if (tasks.length > MAXTASKS) {
2044
+ const error = 'Cannot queue new requests, because more than '+MAXTASKS;
2048
2045
  adapter.log.error(error);
2049
2046
  cb && cb(error);
2050
2047
  } else {
@@ -2075,8 +2072,8 @@ function _delete(id, state, cb) {
2075
2072
  }
2076
2073
 
2077
2074
  if (!multiRequests) {
2078
- if (tasks.length > 100) {
2079
- const error = 'Cannot queue new requests, because more than 100';
2075
+ if (tasks.length > MAXTASKS) {
2076
+ const error = 'Cannot queue new requests, because more than '+MAXTASKS;
2080
2077
  adapter.log.error(error);
2081
2078
  cb && cb(error);
2082
2079
  } else {
@@ -2288,7 +2285,7 @@ function storeState(msg) {
2288
2285
  if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
2289
2286
  pushFunc(id, msg.message.state[j]);
2290
2287
  } else {
2291
- adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message.state[j]));
2288
+ adapter.log.warn(`Invalid state for ${JSON.stringify(msg.message.state[j])}`);
2292
2289
  }
2293
2290
  }
2294
2291
  } else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
@@ -2298,7 +2295,7 @@ function storeState(msg) {
2298
2295
  } else {
2299
2296
  adapter.log.error('storeState called with invalid data');
2300
2297
  return adapter.sendTo(msg.from, msg.command, {
2301
- error: 'Invalid call: ' + JSON.stringify(msg)
2298
+ error: `Invalid call: ${JSON.stringify(msg)}`
2302
2299
  }, msg.callback);
2303
2300
  }
2304
2301
 
@@ -2316,7 +2313,7 @@ function getDpOverview(msg) {
2316
2313
  borrowClientFromPool((err, client) => {
2317
2314
  if (err) {
2318
2315
  return adapter.sendTo(msg.from, msg.command, {
2319
- error: 'Cannot select ' + query + ': ' + err
2316
+ error: `Cannot select ${query}: ${err}`
2320
2317
  }, msg.callback);
2321
2318
  }
2322
2319
  client.execute(query, (err, rows /* , fields */) => {
@@ -2326,9 +2323,10 @@ function getDpOverview(msg) {
2326
2323
  if (err) {
2327
2324
  returnClientToPool(client);
2328
2325
 
2329
- adapter.log.error('Cannot select ' + query + ': ' + err);
2326
+ adapter.log.error(`Cannot select ${query}: ${err}`);
2327
+
2330
2328
  adapter.sendTo(msg.from, msg.command, {
2331
- error: 'Cannot select ' + query + ': ' + err
2329
+ error: `Cannot select ${query}: ${err}`
2332
2330
  }, msg.callback);
2333
2331
  return;
2334
2332
  }
@@ -2337,11 +2335,15 @@ function getDpOverview(msg) {
2337
2335
 
2338
2336
  if (rows.length) {
2339
2337
  for (let r = 0; r < rows.length; r++) {
2340
- if (!result[rows[r].type]) result[rows[r].type] = {};
2338
+ if (!result[rows[r].type]) {
2339
+ result[rows[r].type] = {};
2340
+ }
2341
+
2341
2342
  result[rows[r].type][rows[r].id] = {};
2342
2343
 
2343
2344
  result[rows[r].type][rows[r].id].name = rows[r].name;
2344
- switch(dbNames[rows[r].type]) {
2345
+
2346
+ switch (dbNames[rows[r].type]) {
2345
2347
  case 'ts_number':
2346
2348
  result[rows[r].type][rows[r].id].type = 'number';
2347
2349
  break;
@@ -2379,14 +2381,14 @@ function getFirstTsForIds(dbClient, typeId, resultData, msg) {
2379
2381
  if (err) {
2380
2382
  returnClientToPool(dbClient);
2381
2383
 
2382
- adapter.log.error('Cannot select ' + query + ': ' + err);
2384
+ adapter.log.error(`Cannot select ${query}: ${err}`);
2383
2385
  adapter.sendTo(msg.from, msg.command, {
2384
- error: 'Cannot select ' + query + ': ' + err
2386
+ error: `Cannot select ${query}: ${err}`
2385
2387
  }, msg.callback);
2386
2388
  return;
2387
2389
  }
2388
2390
 
2389
- adapter.log.info('Query result ' + JSON.stringify(rows));
2391
+ adapter.log.info(`Query result ${JSON.stringify(rows)}`);
2390
2392
 
2391
2393
  if (rows.length) {
2392
2394
  for (let r = 0; r < rows.length; r++) {
@@ -2396,7 +2398,7 @@ function getFirstTsForIds(dbClient, typeId, resultData, msg) {
2396
2398
  }
2397
2399
  }
2398
2400
 
2399
- adapter.log.info('enhanced result (' + typeId + '): ' + JSON.stringify(resultData));
2401
+ adapter.log.info(`enhanced result (${typeId}): ${JSON.stringify(resultData)}`);
2400
2402
  setTimeout(getFirstTsForIds, 5000, dbClient, typeId + 1, resultData, msg);
2401
2403
  });
2402
2404
  }
@@ -2408,7 +2410,9 @@ function getFirstTsForIds(dbClient, typeId, resultData, msg) {
2408
2410
  for (let ti = 0; ti < dbNames.length; ti++ ) {
2409
2411
  if (resultData[ti]) {
2410
2412
  for (let index in resultData[ti]) {
2411
- if (!resultData[ti].hasOwnProperty(index)) continue;
2413
+ if (!resultData[ti].hasOwnProperty(index)) {
2414
+ continue;
2415
+ }
2412
2416
 
2413
2417
  const id = resultData[ti][index].name;
2414
2418
  if (!result[id]) {
@@ -2436,7 +2440,7 @@ function enableHistory(msg) {
2436
2440
  if (!msg.message || !msg.message.id) {
2437
2441
  adapter.log.error('enableHistory called with invalid data');
2438
2442
  adapter.sendTo(msg.from, msg.command, {
2439
- error: 'Invalid call'
2443
+ error: 'Invalid call'
2440
2444
  }, msg.callback);
2441
2445
  return;
2442
2446
  }
@@ -2472,7 +2476,7 @@ function disableHistory(msg) {
2472
2476
  if (!msg.message || !msg.message.id) {
2473
2477
  adapter.log.error('disableHistory called with invalid data');
2474
2478
  return adapter.sendTo(msg.from, msg.command, {
2475
- error: 'Invalid call'
2479
+ error: 'Invalid call'
2476
2480
  }, msg.callback);
2477
2481
  }
2478
2482
 
@@ -2517,9 +2521,9 @@ function main() {
2517
2521
  obj.common.defaultHistory = adapter.namespace;
2518
2522
  adapter.setForeignObject('system.config', obj, err => {
2519
2523
  if (err) {
2520
- adapter.log.error('Cannot set default history instance: ' + err);
2524
+ adapter.log.error(`Cannot set default history instance: ${err}`);
2521
2525
  } else {
2522
- adapter.log.info('Set default history instance to "' + adapter.namespace + '"');
2526
+ adapter.log.info(`Set default history instance to "${adapter.namespace}"`);
2523
2527
  }
2524
2528
  });
2525
2529
  }
@@ -2542,7 +2546,7 @@ function main() {
2542
2546
  }
2543
2547
 
2544
2548
  if (!clients[adapter.config.dbtype]) {
2545
- adapter.log.error('Unknown DB type: ' + adapter.config.dbtype);
2549
+ adapter.log.error(`Unknown DB type: ${adapter.config.dbtype}`);
2546
2550
  adapter.stop();
2547
2551
  }
2548
2552
  if (adapter.config.multiRequests !== undefined && adapter.config.dbtype !== 'SQLite3Client' && adapter.config.dbtype !== 'sqlite') {
@@ -2596,117 +2600,121 @@ function main() {
2596
2600
 
2597
2601
  if (adapter.config.dbtype === 'sqlite' || adapter.config.host) {
2598
2602
  connect(() => {
2599
- fixSelector(() => {
2600
- // read all custom settings
2601
- adapter.getObjectView('custom', 'state', {}, (err, doc) => {
2602
- let count = 0;
2603
- if (doc && doc.rows) {
2604
- for (let i = 0, l = doc.rows.length; i < l; i++) {
2605
- if (doc.rows[i].value) {
2606
- let id = doc.rows[i].id;
2607
- const realId = id;
2608
- if (doc.rows[i].value[adapter.namespace] && doc.rows[i].value[adapter.namespace].aliasId) {
2609
- aliasMap[id] = doc.rows[i].value[adapter.namespace].aliasId;
2610
- adapter.log.debug('Found Alias: ' + id + ' --> ' + aliasMap[id]);
2611
- id = aliasMap[id];
2612
- }
2603
+ // read all custom settings
2604
+ adapter.getObjectView('system', 'custom', {}, (err, doc) => {
2605
+ let count = 0;
2606
+ if (doc && doc.rows) {
2607
+ for (let i = 0, l = doc.rows.length; i < l; i++) {
2608
+ if (doc.rows[i].value) {
2609
+ let id = doc.rows[i].id;
2610
+ const realId = id;
2611
+ if (doc.rows[i].value[adapter.namespace] && doc.rows[i].value[adapter.namespace].aliasId) {
2612
+ aliasMap[id] = doc.rows[i].value[adapter.namespace].aliasId;
2613
+ adapter.log.debug(`Found Alias: ${id} --> ${aliasMap[id]}`);
2614
+ id = aliasMap[id];
2615
+ }
2613
2616
 
2614
- let storedIndex = null;
2615
- let storedType = null;
2616
- if (sqlDPs[id] && sqlDPs[id].index !== undefined) {
2617
- storedIndex = sqlDPs[id].index;
2618
- }
2619
- if (sqlDPs[id] && sqlDPs[id].dbtype !== undefined) {
2620
- storedType = sqlDPs[id].dbtype;
2621
- }
2617
+ let storedIndex = null;
2618
+ let storedType = null;
2619
+ if (sqlDPs[id] && sqlDPs[id].index !== undefined) {
2620
+ storedIndex = sqlDPs[id].index;
2621
+ }
2622
+ if (sqlDPs[id] && sqlDPs[id].dbtype !== undefined) {
2623
+ storedType = sqlDPs[id].dbtype;
2624
+ }
2622
2625
 
2623
- sqlDPs[id] = doc.rows[i].value;
2624
- if (storedIndex !== null) {
2625
- sqlDPs[id].index = storedIndex;
2626
- }
2627
- if (storedType !== null) {
2628
- sqlDPs[id].dbtype = storedType;
2626
+ sqlDPs[id] = doc.rows[i].value;
2627
+ if (storedIndex !== null) {
2628
+ sqlDPs[id].index = storedIndex;
2629
+ }
2630
+ if (storedType !== null) {
2631
+ sqlDPs[id].dbtype = storedType;
2632
+ }
2633
+
2634
+ if (!sqlDPs[id][adapter.namespace] || typeof sqlDPs[id][adapter.namespace] !== 'object' || sqlDPs[id][adapter.namespace].enabled === false) {
2635
+ delete sqlDPs[id];
2636
+ } else {
2637
+ count++;
2638
+ adapter.log.info(`enabled logging of ${id}, Alias=${id !== realId}, ${count} points now activated`);
2639
+
2640
+ // retention
2641
+ if (sqlDPs[id][adapter.namespace].retention !== undefined && sqlDPs[id][adapter.namespace].retention !== null && sqlDPs[id][adapter.namespace].retention !== '') {
2642
+ sqlDPs[id][adapter.namespace].retention = parseInt(sqlDPs[id][adapter.namespace].retention, 10) || 0;
2643
+ } else {
2644
+ sqlDPs[id][adapter.namespace].retention = adapter.config.retention;
2629
2645
  }
2630
2646
 
2631
- if (!sqlDPs[id][adapter.namespace] || typeof sqlDPs[id][adapter.namespace] !== 'object' || sqlDPs[id][adapter.namespace].enabled === false) {
2632
- delete sqlDPs[id];
2647
+ // debounce
2648
+ if (sqlDPs[id][adapter.namespace].debounce !== undefined && sqlDPs[id][adapter.namespace].debounce !== null && sqlDPs[id][adapter.namespace].debounce !== '') {
2649
+ sqlDPs[id][adapter.namespace].debounce = parseInt(sqlDPs[id][adapter.namespace].debounce, 10) || 0;
2633
2650
  } else {
2634
- count++;
2635
- adapter.log.info('enabled logging of ' + id + ', Alias=' + (id !== realId) + ', ' + count + ' points now activated');
2636
-
2637
- // retention
2638
- if (sqlDPs[id][adapter.namespace].retention !== undefined && sqlDPs[id][adapter.namespace].retention !== null && sqlDPs[id][adapter.namespace].retention !== '') {
2639
- sqlDPs[id][adapter.namespace].retention = parseInt(sqlDPs[id][adapter.namespace].retention, 10) || 0;
2640
- } else {
2641
- sqlDPs[id][adapter.namespace].retention = adapter.config.retention;
2642
- }
2651
+ sqlDPs[id][adapter.namespace].debounce = adapter.config.debounce;
2652
+ }
2643
2653
 
2644
- // debounce
2645
- if (sqlDPs[id][adapter.namespace].debounce !== undefined && sqlDPs[id][adapter.namespace].debounce !== null && sqlDPs[id][adapter.namespace].debounce !== '') {
2646
- sqlDPs[id][adapter.namespace].debounce = parseInt(sqlDPs[id][adapter.namespace].debounce, 10) || 0;
2647
- } else {
2648
- sqlDPs[id][adapter.namespace].debounce = adapter.config.debounce;
2649
- }
2654
+ // changesOnly
2655
+ sqlDPs[id][adapter.namespace].changesOnly = sqlDPs[id][adapter.namespace].changesOnly === 'true' || sqlDPs[id][adapter.namespace].changesOnly === true;
2650
2656
 
2651
- // changesOnly
2652
- sqlDPs[id][adapter.namespace].changesOnly = sqlDPs[id][adapter.namespace].changesOnly === 'true' || sqlDPs[id][adapter.namespace].changesOnly === true;
2657
+ // ignoreZero
2658
+ sqlDPs[id][adapter.namespace].ignoreZero = sqlDPs[id][adapter.namespace].ignoreZero === 'true' || sqlDPs[id][adapter.namespace].ignoreZero === true;
2653
2659
 
2654
- // changesRelogInterval
2655
- if (sqlDPs[id][adapter.namespace].changesRelogInterval !== undefined && sqlDPs[id][adapter.namespace].changesRelogInterval !== null && sqlDPs[id][adapter.namespace].changesRelogInterval !== '') {
2656
- sqlDPs[id][adapter.namespace].changesRelogInterval = parseInt(sqlDPs[id][adapter.namespace].changesRelogInterval, 10) || 0;
2657
- } else {
2658
- sqlDPs[id][adapter.namespace].changesRelogInterval = adapter.config.changesRelogInterval;
2659
- }
2660
+ // ignoreBelowZero
2661
+ sqlDPs[id][adapter.namespace].ignoreBelowZero = sqlDPs[id][adapter.namespace].ignoreBelowZero === 'true' || sqlDPs[id][adapter.namespace].ignoreBelowZero === true;
2660
2662
 
2661
- // changesMinDelta
2662
- if (sqlDPs[id][adapter.namespace].changesMinDelta !== undefined && sqlDPs[id][adapter.namespace].changesMinDelta !== null && sqlDPs[id][adapter.namespace].changesMinDelta !== '') {
2663
- sqlDPs[id][adapter.namespace].changesMinDelta = parseFloat(sqlDPs[id][adapter.namespace].changesMinDelta) || 0;
2664
- } else {
2665
- sqlDPs[id][adapter.namespace].changesMinDelta = adapter.config.changesMinDelta;
2666
- }
2663
+ // changesRelogInterval
2664
+ if (sqlDPs[id][adapter.namespace].changesRelogInterval !== undefined && sqlDPs[id][adapter.namespace].changesRelogInterval !== null && sqlDPs[id][adapter.namespace].changesRelogInterval !== '') {
2665
+ sqlDPs[id][adapter.namespace].changesRelogInterval = parseInt(sqlDPs[id][adapter.namespace].changesRelogInterval, 10) || 0;
2666
+ } else {
2667
+ sqlDPs[id][adapter.namespace].changesRelogInterval = adapter.config.changesRelogInterval;
2668
+ }
2667
2669
 
2668
- // storageType
2669
- if (!sqlDPs[id][adapter.namespace].storageType) {
2670
- sqlDPs[id][adapter.namespace].storageType = false;
2671
- }
2670
+ // changesMinDelta
2671
+ 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;
2673
+ } else {
2674
+ sqlDPs[id][adapter.namespace].changesMinDelta = adapter.config.changesMinDelta;
2675
+ }
2672
2676
 
2673
- // add one day if retention is too small
2674
- if (sqlDPs[id][adapter.namespace].retention && sqlDPs[id][adapter.namespace].retention <= 604800) {
2675
- sqlDPs[id][adapter.namespace].retention += 86400;
2676
- }
2677
+ // storageType
2678
+ if (!sqlDPs[id][adapter.namespace].storageType) {
2679
+ sqlDPs[id][adapter.namespace].storageType = false;
2680
+ }
2677
2681
 
2678
- // relogTimeout
2679
- if (sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].changesRelogInterval > 0) {
2680
- sqlDPs[id].relogTimeout && clearTimeout(sqlDPs[id].relogTimeout);
2681
- sqlDPs[id].relogTimeout = setTimeout(reLogHelper, (sqlDPs[id][adapter.namespace].changesRelogInterval * 500 * Math.random()) + sqlDPs[id][adapter.namespace].changesRelogInterval * 500, id);
2682
- }
2682
+ // add one day if retention is too small
2683
+ if (sqlDPs[id][adapter.namespace].retention && sqlDPs[id][adapter.namespace].retention <= 604800) {
2684
+ sqlDPs[id][adapter.namespace].retention += 86400;
2685
+ }
2683
2686
 
2684
- sqlDPs[id].realId = realId;
2687
+ // relogTimeout
2688
+ if (sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].changesRelogInterval > 0) {
2689
+ sqlDPs[id].relogTimeout && clearTimeout(sqlDPs[id].relogTimeout);
2690
+ sqlDPs[id].relogTimeout = setTimeout(reLogHelper, (sqlDPs[id][adapter.namespace].changesRelogInterval * 500 * Math.random()) + sqlDPs[id][adapter.namespace].changesRelogInterval * 500, id);
2685
2691
  }
2692
+
2693
+ sqlDPs[id].realId = realId;
2686
2694
  }
2687
2695
  }
2688
2696
  }
2697
+ }
2689
2698
 
2690
- adapter.config.writeNulls && writeNulls();
2699
+ adapter.config.writeNulls && writeNulls();
2691
2700
 
2692
- if (count < 200) {
2693
- Object.keys(sqlDPs).forEach(id =>
2694
- sqlDPs[id] && sqlDPs[id][adapter.namespace] && !sqlDPs[id].realId && adapter.log.warn(`No realID found for ${id}`));
2701
+ if (count < 200) {
2702
+ Object.keys(sqlDPs).forEach(id =>
2703
+ sqlDPs[id] && sqlDPs[id][adapter.namespace] && !sqlDPs[id].realId && adapter.log.warn(`No realID found for ${id}`));
2695
2704
 
2696
- // BF (2020.05.20) change it later to
2697
- // adapter.subscribeForeignStates(Object.keys(sqlDPs).forEach(id => sqlDPs[id] && sqlDPs[id][adapter.namespace] && sqlDPs[id].realId).filter(id => id));
2705
+ // BF (2020.05.20) change it later to
2706
+ // adapter.subscribeForeignStates(Object.keys(sqlDPs).forEach(id => sqlDPs[id] && sqlDPs[id][adapter.namespace] && sqlDPs[id].realId).filter(id => id));
2698
2707
 
2699
- Object.keys(sqlDPs).forEach(id =>
2700
- sqlDPs[id] && sqlDPs[id][adapter.namespace] && sqlDPs[id].realId && adapter.subscribeForeignStates(sqlDPs[id].realId));
2701
- } else {
2702
- subscribeAll = true;
2703
- adapter.subscribeForeignStates('*');
2704
- }
2705
- adapter.subscribeForeignObjects('*');
2706
- adapter.log.debug('Initialization done');
2707
- setConnected(true);
2708
- processStartValues();
2709
- });
2708
+ Object.keys(sqlDPs).forEach(id =>
2709
+ sqlDPs[id] && sqlDPs[id][adapter.namespace] && sqlDPs[id].realId && adapter.subscribeForeignStates(sqlDPs[id].realId));
2710
+ } else {
2711
+ subscribeAll = true;
2712
+ adapter.subscribeForeignStates('*');
2713
+ }
2714
+ adapter.subscribeForeignObjects('*');
2715
+ adapter.log.debug('Initialization done');
2716
+ setConnected(true);
2717
+ processStartValues();
2710
2718
  });
2711
2719
  });
2712
2720
  }