iobroker.sql 1.15.4 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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);
@@ -595,6 +603,9 @@ function destroyDB(msg) {
595
603
 
596
604
  function _userQuery(msg, callback) {
597
605
  try {
606
+ if (typeof msg.message !== 'string' || !msg.message.length) {
607
+ throw new Error('No query provided');
608
+ }
598
609
  adapter.log.debug(msg.message);
599
610
 
600
611
  borrowClientFromPool((err, client) => {
@@ -619,8 +630,8 @@ function _userQuery(msg, callback) {
619
630
  // execute custom query
620
631
  function query(msg) {
621
632
  if (!multiRequests) {
622
- if (tasks.length > 100) {
623
- 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;
624
635
  adapter.log.error(error);
625
636
  adapter.sendTo(msg.from, msg.command, {error}, msg.callback);
626
637
  } else {
@@ -671,7 +682,7 @@ function oneScript(script, cb) {
671
682
  else if (err.code === '42P07') {
672
683
  const match = script.match(/CREATE\s+TABLE\s+(\w*)\s+\(/);
673
684
  if (match) {
674
- adapter.log.debug('OK. Table "' + match[1] + '" yet exists');
685
+ adapter.log.debug(`OK. Table "${match[1]}" yet exists`);
675
686
  err = null;
676
687
  } else {
677
688
  adapter.log.error(script);
@@ -680,7 +691,7 @@ function oneScript(script, cb) {
680
691
  } else if (script.startsWith('CREATE INDEX')) {
681
692
  adapter.log.info('Ignore Error on Create index. You might want to create the index yourself!');
682
693
  adapter.log.info(script);
683
- adapter.log.info(err.code + ': ' + err);
694
+ adapter.log.info(`${err.code}: ${err}`);
684
695
  err = null;
685
696
  } else {
686
697
  adapter.log.error(script);
@@ -768,10 +779,10 @@ function finish(callback) {
768
779
  _state.from = 'system.adapter.' + adapter.namespace;
769
780
  nullValue.ts += 4;
770
781
  nullValue.lc += 4; // because of MS SQL
771
- adapter.log.debug('Write 1/2 "' + _state.val + '" _id: ' + _id);
782
+ adapter.log.debug(`Write 1/2 "${_state.val}" _id: ${_id}`);
772
783
  pushValueIntoDB(_id, _state, () => {
773
784
  // terminate values with null to indicate adapter stop. timestamp + 1#
774
- adapter.log.debug('Write 2/2 "null" _id: ' + _id);
785
+ adapter.log.debug(`Write 2/2 "null" _id: ${_id}`);
775
786
  pushValueIntoDB(_id, _nullValue, () => {
776
787
  if (!--count) {
777
788
  if (clientPool) {
@@ -925,26 +936,6 @@ function processMessage(msg) {
925
936
  }
926
937
  }
927
938
 
928
- function fixSelector(callback) {
929
- // fix _design/custom object
930
- adapter.getForeignObject('_design/custom', (err, obj) => {
931
- if (!obj || !obj.views.state.map.includes('common.custom')) {
932
- obj = {
933
- _id: '_design/custom',
934
- language: 'javascript',
935
- views: {
936
- state: {
937
- map: 'function(doc) { doc.type === \'state\' && doc.common.custom && emit(doc._id, doc.common.custom) }'
938
- }
939
- }
940
- };
941
- adapter.setForeignObject('_design/custom', obj, err => callback && callback(err));
942
- } else {
943
- callback && callback(err);
944
- }
945
- });
946
- }
947
-
948
939
  function processStartValues(callback) {
949
940
  if (tasksStart && tasksStart.length) {
950
941
  const task = tasksStart.shift();
@@ -1163,7 +1154,7 @@ function pushHelper(_id, timeoutTriggered) {
1163
1154
  }
1164
1155
 
1165
1156
  if (sqlDPs[_id].state.val !== null && sqlDPs[_id].state.val !== undefined) {
1166
- 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}`);
1167
1158
 
1168
1159
  if (typeof sqlDPs[_id].state.val === 'string' && _settings.storageType !== 'String') {
1169
1160
  adapter.log.debug('Do Automatic Datatype conversion for ' + _id);
@@ -1288,8 +1279,8 @@ function checkRetention(id) {
1288
1279
  const query = SQLFuncs.retention(adapter.config.dbname, sqlDPs[id].index, dbNames[sqlDPs[id].type], sqlDPs[id][adapter.namespace].retention);
1289
1280
 
1290
1281
  if (!multiRequests) {
1291
- if (tasks.length > 100) {
1292
- 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);
1293
1284
  }
1294
1285
 
1295
1286
  let start = tasks.length === 1;
@@ -1322,7 +1313,7 @@ function _insertValueIntoDB(query, id, cb) {
1322
1313
  borrowClientFromPool((err, client) => {
1323
1314
  if (err) {
1324
1315
  adapter.log.error(err);
1325
- cb && cb();
1316
+ cb && cb(); // BF asked (2021.12.14): may be return here err?
1326
1317
  } else {
1327
1318
  client.execute(query, (err /* , rows, fields */) => {
1328
1319
  returnClientToPool(client);
@@ -1331,7 +1322,7 @@ function _insertValueIntoDB(query, id, cb) {
1331
1322
  } else {
1332
1323
  checkRetention(id);
1333
1324
  }
1334
- cb && cb();
1325
+ cb && cb(); // BF asked (2021.12.14): may be return here err?
1335
1326
  });
1336
1327
  }
1337
1328
  });
@@ -1581,6 +1572,15 @@ function pushValueIntoDB(id, state, isCounter, cb) {
1581
1572
  isCounter = false;
1582
1573
  }
1583
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
+
1584
1584
  adapter.log.debug(`pushValueIntoDB called for ${id} (type: ${sqlDPs[id].type}, ID: ${sqlDPs[id].index}) and state: ${JSON.stringify(state)}`);
1585
1585
 
1586
1586
  prepareTaskCheckTypeAndDbId(id, state, isCounter, err => {
@@ -1602,8 +1602,8 @@ function pushValueIntoDB(id, state, isCounter, cb) {
1602
1602
  const query = SQLFuncs.insert(adapter.config.dbname, sqlDPs[id].index, state, from[state.from] || 0, isCounter ? 'ts_counter' : dbNames[type]);
1603
1603
 
1604
1604
  if (!multiRequests) {
1605
- if (tasks.length > 100) {
1606
- 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;
1607
1607
  adapter.log.error(error);
1608
1608
  cb && cb(error);
1609
1609
  } else {
@@ -1876,9 +1876,9 @@ function getDataFromDB(db, options, callback) {
1876
1876
  const query = SQLFuncs.getHistory(adapter.config.dbname, db, options);
1877
1877
  adapter.log.debug(query);
1878
1878
  if (!multiRequests) {
1879
- if (tasks.length > 100) {
1880
- adapter.log.error('Cannot queue new requests, because more than 100');
1881
- 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);
1882
1882
  } else {
1883
1883
  tasks.push({operation: 'select', query, options, callback});
1884
1884
  tasks.length === 1 && processTasks();
@@ -1894,8 +1894,8 @@ function getCounterDataFromDB(options, callback) {
1894
1894
  adapter.log.debug(query);
1895
1895
 
1896
1896
  if (!multiRequests) {
1897
- if (tasks.length > 100) {
1898
- 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;
1899
1899
  adapter.log.error(error);
1900
1900
  callback && callback(error);
1901
1901
  } else {
@@ -2040,8 +2040,8 @@ function update(id, state, cb) {
2040
2040
  const query = SQLFuncs.update(adapter.config.dbname, sqlDPs[id].index, state, from[state.from], dbNames[type]);
2041
2041
 
2042
2042
  if (!multiRequests) {
2043
- if (tasks.length > 100) {
2044
- 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;
2045
2045
  adapter.log.error(error);
2046
2046
  cb && cb(error);
2047
2047
  } else {
@@ -2072,8 +2072,8 @@ function _delete(id, state, cb) {
2072
2072
  }
2073
2073
 
2074
2074
  if (!multiRequests) {
2075
- if (tasks.length > 100) {
2076
- 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;
2077
2077
  adapter.log.error(error);
2078
2078
  cb && cb(error);
2079
2079
  } else {
@@ -2285,7 +2285,7 @@ function storeState(msg) {
2285
2285
  if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
2286
2286
  pushFunc(id, msg.message.state[j]);
2287
2287
  } else {
2288
- 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])}`);
2289
2289
  }
2290
2290
  }
2291
2291
  } else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
@@ -2295,7 +2295,7 @@ function storeState(msg) {
2295
2295
  } else {
2296
2296
  adapter.log.error('storeState called with invalid data');
2297
2297
  return adapter.sendTo(msg.from, msg.command, {
2298
- error: 'Invalid call: ' + JSON.stringify(msg)
2298
+ error: `Invalid call: ${JSON.stringify(msg)}`
2299
2299
  }, msg.callback);
2300
2300
  }
2301
2301
 
@@ -2313,7 +2313,7 @@ function getDpOverview(msg) {
2313
2313
  borrowClientFromPool((err, client) => {
2314
2314
  if (err) {
2315
2315
  return adapter.sendTo(msg.from, msg.command, {
2316
- error: 'Cannot select ' + query + ': ' + err
2316
+ error: `Cannot select ${query}: ${err}`
2317
2317
  }, msg.callback);
2318
2318
  }
2319
2319
  client.execute(query, (err, rows /* , fields */) => {
@@ -2323,9 +2323,10 @@ function getDpOverview(msg) {
2323
2323
  if (err) {
2324
2324
  returnClientToPool(client);
2325
2325
 
2326
- adapter.log.error('Cannot select ' + query + ': ' + err);
2326
+ adapter.log.error(`Cannot select ${query}: ${err}`);
2327
+
2327
2328
  adapter.sendTo(msg.from, msg.command, {
2328
- error: 'Cannot select ' + query + ': ' + err
2329
+ error: `Cannot select ${query}: ${err}`
2329
2330
  }, msg.callback);
2330
2331
  return;
2331
2332
  }
@@ -2334,11 +2335,15 @@ function getDpOverview(msg) {
2334
2335
 
2335
2336
  if (rows.length) {
2336
2337
  for (let r = 0; r < rows.length; r++) {
2337
- if (!result[rows[r].type]) result[rows[r].type] = {};
2338
+ if (!result[rows[r].type]) {
2339
+ result[rows[r].type] = {};
2340
+ }
2341
+
2338
2342
  result[rows[r].type][rows[r].id] = {};
2339
2343
 
2340
2344
  result[rows[r].type][rows[r].id].name = rows[r].name;
2341
- switch(dbNames[rows[r].type]) {
2345
+
2346
+ switch (dbNames[rows[r].type]) {
2342
2347
  case 'ts_number':
2343
2348
  result[rows[r].type][rows[r].id].type = 'number';
2344
2349
  break;
@@ -2376,14 +2381,14 @@ function getFirstTsForIds(dbClient, typeId, resultData, msg) {
2376
2381
  if (err) {
2377
2382
  returnClientToPool(dbClient);
2378
2383
 
2379
- adapter.log.error('Cannot select ' + query + ': ' + err);
2384
+ adapter.log.error(`Cannot select ${query}: ${err}`);
2380
2385
  adapter.sendTo(msg.from, msg.command, {
2381
- error: 'Cannot select ' + query + ': ' + err
2386
+ error: `Cannot select ${query}: ${err}`
2382
2387
  }, msg.callback);
2383
2388
  return;
2384
2389
  }
2385
2390
 
2386
- adapter.log.info('Query result ' + JSON.stringify(rows));
2391
+ adapter.log.info(`Query result ${JSON.stringify(rows)}`);
2387
2392
 
2388
2393
  if (rows.length) {
2389
2394
  for (let r = 0; r < rows.length; r++) {
@@ -2393,7 +2398,7 @@ function getFirstTsForIds(dbClient, typeId, resultData, msg) {
2393
2398
  }
2394
2399
  }
2395
2400
 
2396
- adapter.log.info('enhanced result (' + typeId + '): ' + JSON.stringify(resultData));
2401
+ adapter.log.info(`enhanced result (${typeId}): ${JSON.stringify(resultData)}`);
2397
2402
  setTimeout(getFirstTsForIds, 5000, dbClient, typeId + 1, resultData, msg);
2398
2403
  });
2399
2404
  }
@@ -2405,7 +2410,9 @@ function getFirstTsForIds(dbClient, typeId, resultData, msg) {
2405
2410
  for (let ti = 0; ti < dbNames.length; ti++ ) {
2406
2411
  if (resultData[ti]) {
2407
2412
  for (let index in resultData[ti]) {
2408
- if (!resultData[ti].hasOwnProperty(index)) continue;
2413
+ if (!resultData[ti].hasOwnProperty(index)) {
2414
+ continue;
2415
+ }
2409
2416
 
2410
2417
  const id = resultData[ti][index].name;
2411
2418
  if (!result[id]) {
@@ -2433,7 +2440,7 @@ function enableHistory(msg) {
2433
2440
  if (!msg.message || !msg.message.id) {
2434
2441
  adapter.log.error('enableHistory called with invalid data');
2435
2442
  adapter.sendTo(msg.from, msg.command, {
2436
- error: 'Invalid call'
2443
+ error: 'Invalid call'
2437
2444
  }, msg.callback);
2438
2445
  return;
2439
2446
  }
@@ -2469,7 +2476,7 @@ function disableHistory(msg) {
2469
2476
  if (!msg.message || !msg.message.id) {
2470
2477
  adapter.log.error('disableHistory called with invalid data');
2471
2478
  return adapter.sendTo(msg.from, msg.command, {
2472
- error: 'Invalid call'
2479
+ error: 'Invalid call'
2473
2480
  }, msg.callback);
2474
2481
  }
2475
2482
 
@@ -2514,9 +2521,9 @@ function main() {
2514
2521
  obj.common.defaultHistory = adapter.namespace;
2515
2522
  adapter.setForeignObject('system.config', obj, err => {
2516
2523
  if (err) {
2517
- adapter.log.error('Cannot set default history instance: ' + err);
2524
+ adapter.log.error(`Cannot set default history instance: ${err}`);
2518
2525
  } else {
2519
- adapter.log.info('Set default history instance to "' + adapter.namespace + '"');
2526
+ adapter.log.info(`Set default history instance to "${adapter.namespace}"`);
2520
2527
  }
2521
2528
  });
2522
2529
  }
@@ -2539,7 +2546,7 @@ function main() {
2539
2546
  }
2540
2547
 
2541
2548
  if (!clients[adapter.config.dbtype]) {
2542
- adapter.log.error('Unknown DB type: ' + adapter.config.dbtype);
2549
+ adapter.log.error(`Unknown DB type: ${adapter.config.dbtype}`);
2543
2550
  adapter.stop();
2544
2551
  }
2545
2552
  if (adapter.config.multiRequests !== undefined && adapter.config.dbtype !== 'SQLite3Client' && adapter.config.dbtype !== 'sqlite') {
@@ -2593,117 +2600,121 @@ function main() {
2593
2600
 
2594
2601
  if (adapter.config.dbtype === 'sqlite' || adapter.config.host) {
2595
2602
  connect(() => {
2596
- fixSelector(() => {
2597
- // read all custom settings
2598
- adapter.getObjectView('custom', 'state', {}, (err, doc) => {
2599
- let count = 0;
2600
- if (doc && doc.rows) {
2601
- for (let i = 0, l = doc.rows.length; i < l; i++) {
2602
- if (doc.rows[i].value) {
2603
- let id = doc.rows[i].id;
2604
- const realId = id;
2605
- if (doc.rows[i].value[adapter.namespace] && doc.rows[i].value[adapter.namespace].aliasId) {
2606
- aliasMap[id] = doc.rows[i].value[adapter.namespace].aliasId;
2607
- adapter.log.debug('Found Alias: ' + id + ' --> ' + aliasMap[id]);
2608
- id = aliasMap[id];
2609
- }
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
+ }
2610
2616
 
2611
- let storedIndex = null;
2612
- let storedType = null;
2613
- if (sqlDPs[id] && sqlDPs[id].index !== undefined) {
2614
- storedIndex = sqlDPs[id].index;
2615
- }
2616
- if (sqlDPs[id] && sqlDPs[id].dbtype !== undefined) {
2617
- storedType = sqlDPs[id].dbtype;
2618
- }
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
+ }
2619
2625
 
2620
- sqlDPs[id] = doc.rows[i].value;
2621
- if (storedIndex !== null) {
2622
- sqlDPs[id].index = storedIndex;
2623
- }
2624
- if (storedType !== null) {
2625
- 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;
2626
2645
  }
2627
2646
 
2628
- if (!sqlDPs[id][adapter.namespace] || typeof sqlDPs[id][adapter.namespace] !== 'object' || sqlDPs[id][adapter.namespace].enabled === false) {
2629
- 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;
2630
2650
  } else {
2631
- count++;
2632
- adapter.log.info('enabled logging of ' + id + ', Alias=' + (id !== realId) + ', ' + count + ' points now activated');
2633
-
2634
- // retention
2635
- if (sqlDPs[id][adapter.namespace].retention !== undefined && sqlDPs[id][adapter.namespace].retention !== null && sqlDPs[id][adapter.namespace].retention !== '') {
2636
- sqlDPs[id][adapter.namespace].retention = parseInt(sqlDPs[id][adapter.namespace].retention, 10) || 0;
2637
- } else {
2638
- sqlDPs[id][adapter.namespace].retention = adapter.config.retention;
2639
- }
2651
+ sqlDPs[id][adapter.namespace].debounce = adapter.config.debounce;
2652
+ }
2640
2653
 
2641
- // debounce
2642
- if (sqlDPs[id][adapter.namespace].debounce !== undefined && sqlDPs[id][adapter.namespace].debounce !== null && sqlDPs[id][adapter.namespace].debounce !== '') {
2643
- sqlDPs[id][adapter.namespace].debounce = parseInt(sqlDPs[id][adapter.namespace].debounce, 10) || 0;
2644
- } else {
2645
- sqlDPs[id][adapter.namespace].debounce = adapter.config.debounce;
2646
- }
2654
+ // changesOnly
2655
+ sqlDPs[id][adapter.namespace].changesOnly = sqlDPs[id][adapter.namespace].changesOnly === 'true' || sqlDPs[id][adapter.namespace].changesOnly === true;
2647
2656
 
2648
- // changesOnly
2649
- 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;
2650
2659
 
2651
- // changesRelogInterval
2652
- if (sqlDPs[id][adapter.namespace].changesRelogInterval !== undefined && sqlDPs[id][adapter.namespace].changesRelogInterval !== null && sqlDPs[id][adapter.namespace].changesRelogInterval !== '') {
2653
- sqlDPs[id][adapter.namespace].changesRelogInterval = parseInt(sqlDPs[id][adapter.namespace].changesRelogInterval, 10) || 0;
2654
- } else {
2655
- sqlDPs[id][adapter.namespace].changesRelogInterval = adapter.config.changesRelogInterval;
2656
- }
2660
+ // ignoreBelowZero
2661
+ sqlDPs[id][adapter.namespace].ignoreBelowZero = sqlDPs[id][adapter.namespace].ignoreBelowZero === 'true' || sqlDPs[id][adapter.namespace].ignoreBelowZero === true;
2657
2662
 
2658
- // changesMinDelta
2659
- if (sqlDPs[id][adapter.namespace].changesMinDelta !== undefined && sqlDPs[id][adapter.namespace].changesMinDelta !== null && sqlDPs[id][adapter.namespace].changesMinDelta !== '') {
2660
- sqlDPs[id][adapter.namespace].changesMinDelta = parseFloat(sqlDPs[id][adapter.namespace].changesMinDelta) || 0;
2661
- } else {
2662
- sqlDPs[id][adapter.namespace].changesMinDelta = adapter.config.changesMinDelta;
2663
- }
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
+ }
2664
2669
 
2665
- // storageType
2666
- if (!sqlDPs[id][adapter.namespace].storageType) {
2667
- sqlDPs[id][adapter.namespace].storageType = false;
2668
- }
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
+ }
2669
2676
 
2670
- // add one day if retention is too small
2671
- if (sqlDPs[id][adapter.namespace].retention && sqlDPs[id][adapter.namespace].retention <= 604800) {
2672
- sqlDPs[id][adapter.namespace].retention += 86400;
2673
- }
2677
+ // storageType
2678
+ if (!sqlDPs[id][adapter.namespace].storageType) {
2679
+ sqlDPs[id][adapter.namespace].storageType = false;
2680
+ }
2674
2681
 
2675
- // relogTimeout
2676
- if (sqlDPs[id][adapter.namespace] && sqlDPs[id][adapter.namespace].changesRelogInterval > 0) {
2677
- sqlDPs[id].relogTimeout && clearTimeout(sqlDPs[id].relogTimeout);
2678
- sqlDPs[id].relogTimeout = setTimeout(reLogHelper, (sqlDPs[id][adapter.namespace].changesRelogInterval * 500 * Math.random()) + sqlDPs[id][adapter.namespace].changesRelogInterval * 500, id);
2679
- }
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
+ }
2680
2686
 
2681
- 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);
2682
2691
  }
2692
+
2693
+ sqlDPs[id].realId = realId;
2683
2694
  }
2684
2695
  }
2685
2696
  }
2697
+ }
2686
2698
 
2687
- adapter.config.writeNulls && writeNulls();
2699
+ adapter.config.writeNulls && writeNulls();
2688
2700
 
2689
- if (count < 200) {
2690
- Object.keys(sqlDPs).forEach(id =>
2691
- 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}`));
2692
2704
 
2693
- // BF (2020.05.20) change it later to
2694
- // 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));
2695
2707
 
2696
- Object.keys(sqlDPs).forEach(id =>
2697
- sqlDPs[id] && sqlDPs[id][adapter.namespace] && sqlDPs[id].realId && adapter.subscribeForeignStates(sqlDPs[id].realId));
2698
- } else {
2699
- subscribeAll = true;
2700
- adapter.subscribeForeignStates('*');
2701
- }
2702
- adapter.subscribeForeignObjects('*');
2703
- adapter.log.debug('Initialization done');
2704
- setConnected(true);
2705
- processStartValues();
2706
- });
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();
2707
2718
  });
2708
2719
  });
2709
2720
  }