iobroker.influxdb 2.4.0 → 2.6.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
@@ -20,7 +20,7 @@ function isEqual(a, b) {
20
20
  //console.log('Compare ' + JSON.stringify(a) + ' with ' + JSON.stringify(b));
21
21
  // Create arrays of property names
22
22
  if (a === null || a === undefined || b === null || b === undefined) {
23
- return (a === b);
23
+ return a === b;
24
24
  }
25
25
 
26
26
  const aProps = Object.getOwnPropertyNames(a);
@@ -76,7 +76,7 @@ function startAdapter(options) {
76
76
  if (obj.common.custom && obj.common.custom[adapter.namespace] && obj.common.custom[adapter.namespace].aliasId) {
77
77
  if (obj.common.custom[adapter.namespace].aliasId !== id) {
78
78
  adapter._aliasMap[id] = obj.common.custom[adapter.namespace].aliasId;
79
- adapter.log.debug('Registered Alias: ' + id + ' --> ' + adapter._aliasMap[id]);
79
+ adapter.log.debug(`Registered Alias: ${id} --> ${adapter._aliasMap[id]}`);
80
80
  id = adapter._aliasMap[id];
81
81
  checkForRemove = false;
82
82
  } else {
@@ -85,7 +85,7 @@ function startAdapter(options) {
85
85
  }
86
86
  }
87
87
  if (checkForRemove && adapter._aliasMap[id]) {
88
- adapter.log.debug('Removed Alias: ' + id + ' !-> ' + adapter._aliasMap[id]);
88
+ adapter.log.debug(`Removed Alias: ${id} !-> ${adapter._aliasMap[id]}`);
89
89
  delete adapter._aliasMap[id];
90
90
  }
91
91
 
@@ -229,24 +229,24 @@ function stopPing(adapter) {
229
229
 
230
230
  function ping(adapter) {
231
231
  adapter._client.ping && adapter.config.pingserver !== false && adapter._client.ping(adapter.config.pingInterval - 1000 < 0 ? 1000 : adapter.config.pingInterval - 1000)
232
- .then(
233
- hosts => {
234
- if (!hosts.some(host => host.online)) {
235
- reconnect(adapter);
236
- } else {
237
- adapter.log.debug('PING OK');
238
- }
239
- },
240
- error => {
241
- adapter.log.error(`Error during ping: ${error}. Attempting reconnect.`);
232
+ .then(hosts => {
233
+ if (!hosts.some(host => host.online)) {
242
234
  reconnect(adapter);
243
- });
235
+ } else {
236
+ adapter.log.debug('PING OK');
237
+ }
238
+ },
239
+ error => {
240
+ adapter.log.error(`Error during ping: ${error}. Attempting reconnect.`);
241
+ reconnect(adapter);
242
+ });
244
243
  }
245
244
 
246
245
  function connect(adapter) {
247
246
  adapter.log.info(`Connecting ${adapter.config.protocol}://${adapter.config.host}:${adapter.config.port} ...`);
248
247
 
249
248
  adapter.config.dbname = adapter.config.dbname || appName;
249
+ adapter.config.validateSSL = adapter.config.validateSSL !== undefined ? !!adapter.config.validateSSL : true;
250
250
 
251
251
  adapter.config.seriesBufferMax = parseInt(adapter.config.seriesBufferMax, 10) || 0;
252
252
 
@@ -266,15 +266,17 @@ function connect(adapter) {
266
266
  adapter.config.token,
267
267
  adapter.config.organization,
268
268
  adapter.config.dbname,
269
+ adapter.config.requestTimeout,
270
+ adapter.config.validateSSL,
269
271
  adapter.config.usetags
270
272
  )
271
273
  break;
272
274
  case '1.x':
273
275
  default:
274
276
  if (/[\x00-\x08\x0E-\x1F\x80-\xFF]/.test(adapter.config.password)) {
275
- adapter.log.error('Password error: Please re-enter the password in Admin. Stopping');
276
- return;
277
+ return adapter.log.error('Password error: Please re-enter the password in Admin. Stopping');
277
278
  }
279
+
278
280
  adapter._client = new DatabaseInfluxDB1x(
279
281
  adapter.log,
280
282
  adapter.config.host,
@@ -283,8 +285,8 @@ function connect(adapter) {
283
285
  adapter.config.user,
284
286
  adapter.config.password,
285
287
  adapter.config.dbname,
286
- 'ms',
287
- 30000
288
+ adapter.config.requestTimeout,
289
+ 'ms'
288
290
  );
289
291
  break;
290
292
  }
@@ -384,6 +386,7 @@ function getRetention(adapter, msg) {
384
386
  function testConnection(adapter, msg) {
385
387
  adapter.log.debug('testConnection msg-object: ' + JSON.stringify(msg));
386
388
  msg.message.config.port = parseInt(msg.message.config.port, 10) || 0;
389
+ msg.message.config.requestTimeout = parseInt(msg.message.config.requestTimeout) || 30000;
387
390
 
388
391
  let timeout;
389
392
  try {
@@ -404,7 +407,8 @@ function testConnection(adapter, msg) {
404
407
  msg.message.config.protocol, // optional, default 'http'
405
408
  msg.message.config.token,
406
409
  msg.message.config.organization,
407
- msg.message.config.dbname || appName
410
+ msg.message.config.dbname || appName,
411
+ msg.message.config.requestTimeout
408
412
  )
409
413
  break;
410
414
  default:
@@ -416,7 +420,8 @@ function testConnection(adapter, msg) {
416
420
  msg.message.config.protocol, // optional, default 'http'
417
421
  msg.message.config.user,
418
422
  msg.message.config.password,
419
- msg.message.config.dbname || appName
423
+ msg.message.config.dbname || appName,
424
+ msg.message.config.requestTimeout
420
425
  );
421
426
  break;
422
427
  }
@@ -473,9 +478,25 @@ function destroyDB(adapter, msg) {
473
478
  function processMessage(adapter, msg) {
474
479
  adapter.log.debug(`Incoming message ${msg.command} from ${msg.from}`);
475
480
  if (msg.command === 'features') {
476
- adapter.sendTo(msg.from, msg.command, {supportedFeatures: []}, msg.callback);
477
- } else
478
- if (msg.command === 'getHistory') {
481
+ // influxdb 1
482
+ if (adapter.config.dbversion === '1.x') {
483
+ adapter.sendTo(msg.from, msg.command, {supportedFeatures: ['update', 'delete', 'deleteRange', 'deleteAll', 'storeState']}, msg.callback);
484
+ } else {
485
+ // not yet implemented
486
+ adapter.sendTo(msg.from, msg.command, {supportedFeatures: ['storeState']}, msg.callback);
487
+ }
488
+ }
489
+ else if (msg.command === 'update') {
490
+ updateState(adapter, msg);
491
+ } else if (msg.command === 'delete') {
492
+ deleteState(adapter, msg);
493
+ } else if (msg.command === 'deleteAll') {
494
+ deleteStateAll(adapter, msg);
495
+ } else if (msg.command === 'deleteRange') {
496
+ deleteState(adapter, msg);
497
+ } else if (msg.command === 'storeState') {
498
+ storeState(adapter, msg);
499
+ } else if (msg.command === 'getHistory') {
479
500
  adapter.config.dbversion === '1.x' ? getHistory(adapter, msg) : getHistoryIflx2(adapter, msg);
480
501
  }
481
502
  else if (msg.command === 'test') {
@@ -502,9 +523,6 @@ function processMessage(adapter, msg) {
502
523
  else if (msg.command === 'resetConflictingPoints') {
503
524
  resetConflictingPoints(adapter, msg);
504
525
  }
505
- else if (msg.command === 'storeState') {
506
- storeState(adapter, msg);
507
- }
508
526
  else if (msg.command === 'enableHistory') {
509
527
  enableHistory(adapter, msg);
510
528
  }
@@ -572,6 +590,8 @@ function main(adapter) {
572
590
 
573
591
  adapter.config.seriesBufferFlushInterval = parseInt(adapter.config.seriesBufferFlushInterval, 10) || 600;
574
592
 
593
+ adapter.config.requestTimeout = parseInt(adapter.config.requestTimeout, 10) || 30000;
594
+
575
595
  if (adapter.config.changesMinDelta !== null && adapter.config.changesMinDelta !== undefined) {
576
596
  adapter.config.changesMinDelta = parseFloat(adapter.config.changesMinDelta.toString().replace(/,/g, '.'));
577
597
  } else {
@@ -597,10 +617,11 @@ function main(adapter) {
597
617
 
598
618
  // read all custom settings
599
619
  adapter.getObjectView('system', 'custom', {}, (err, doc) => {
600
- if (err) adapter.log.error('main/getObjectView: ' + err);
620
+ err && adapter.log.error('main/getObjectView: ' + err);
601
621
  let count = 0;
602
622
  if (doc && doc.rows) {
603
- for (let i = 0, l = doc.rows.length; i < l; i++) {
623
+ const l = doc.rows.length;
624
+ for (let i = 0; i < l; i++) {
604
625
  if (doc.rows[i].value) {
605
626
  let id = doc.rows[i].id;
606
627
  const realId = id;
@@ -637,7 +658,8 @@ function main(adapter) {
637
658
  } else {
638
659
  adapter._influxDPs[id][adapter.namespace].changesMinDelta = adapter.config.changesMinDelta;
639
660
  }
640
- if (!adapter._influxDPs[id][adapter.namespace].storageType) adapter._influxDPs[id][adapter.namespace].storageType = false;
661
+
662
+ adapter._influxDPs[id][adapter.namespace].storageType = adapter._influxDPs[id][adapter.namespace].storageType || false;
641
663
 
642
664
  adapter._influxDPs[id].realId = realId;
643
665
  writeInitialValue(adapter, realId, id);
@@ -759,6 +781,7 @@ function pushHistory(adapter, id, state, timerRelog) {
759
781
  // only store state if really changed
760
782
  adapter._influxDPs[id].state = state;
761
783
  }
784
+
762
785
  adapter._influxDPs[id].lastLogTime = state.ts;
763
786
  adapter._influxDPs[id].skipped = null;
764
787
 
@@ -1008,7 +1031,8 @@ function writeAllSeriesPerID(adapter, series, cb, idList) {
1008
1031
  return cb && cb();
1009
1032
  }
1010
1033
  const id = idList.shift();
1011
- writeSeriesPerID(adapter, id, series[id], () => writeAllSeriesPerID(adapter, series, cb, idList));
1034
+ writeSeriesPerID(adapter, id, series[id], () =>
1035
+ writeAllSeriesPerID(adapter, series, cb, idList));
1012
1036
  }
1013
1037
 
1014
1038
  function writeSeriesPerID(adapter, seriesId, points, cb) {
@@ -1074,7 +1098,7 @@ function writeOnePointForID(adapter, pointId, point, directWrite, cb) {
1074
1098
  adapter._client.writePoint(pointId, point, null, (err /* , result */) => {
1075
1099
  if (err) {
1076
1100
  adapter.log.warn(`Error on writePoint("${JSON.stringify(point)}): ${err} / ${JSON.stringify(err.message)}"`);
1077
- if ((adapter._client.request.getHostsAvailable().length === 0) || (err.message && err.message === 'timeout')) {
1101
+ if (!adapter._client.request.getHostsAvailable().length || (err.message && err.message === 'timeout')) {
1078
1102
  reconnect(adapter);
1079
1103
  addPointToSeriesBuffer(adapter, pointId, point);
1080
1104
  } else if (err.message && typeof err.message === 'string' && err.message.includes('field type conflict')) {
@@ -1135,7 +1159,7 @@ function writeOnePointForID(adapter, pointId, point, directWrite, cb) {
1135
1159
  }
1136
1160
  if (!directWrite || !retry) {
1137
1161
  // remember this as a pot. conflicting point and write synchronous
1138
- adapter._conflictingPoints[pointId]=1;
1162
+ adapter._conflictingPoints[pointId] = 1;
1139
1163
  adapter.log.warn(`Add ${pointId} to conflicting Points (${Object.keys(adapter._conflictingPoints).length} now)`);
1140
1164
  }
1141
1165
  } else {
@@ -1145,7 +1169,7 @@ function writeOnePointForID(adapter, pointId, point, directWrite, cb) {
1145
1169
  adapter._errorPoints[pointId]++;
1146
1170
  }
1147
1171
  if (adapter._errorPoints[pointId] < 10) {
1148
- // re-add that point to buffer to try write again
1172
+ // re-add that point to buffer to try to write again
1149
1173
  adapter.log.info(`Add point that had error for ${pointId} to buffer again, error-count=${adapter._errorPoints[pointId]}`);
1150
1174
  addPointToSeriesBuffer(adapter, pointId, point);
1151
1175
  } else {
@@ -1178,7 +1202,334 @@ function writeFileBufferToDisk() {
1178
1202
  adapter._seriesBufferCounter = null;
1179
1203
  }
1180
1204
 
1205
+ function _delete(adapter, id, state, cb) {
1206
+ if (!adapter._connected) {
1207
+ return cb && cb('not connected');
1208
+ }
1209
+
1210
+ if (adapter.config.dbversion === '1.x') {
1211
+ let query;
1212
+ if (state.ts) {
1213
+ query = `DELETE FROM "${id}" WHERE time = '${new Date(state.ts).toISOString()}'`;
1214
+ } else if (state.start) {
1215
+ query = `DELETE FROM "${id}" WHERE time >= '${new Date(state.start).toISOString()}'${state.end ? ` AND time <= '${new Date(state.end).toISOString()}'` : ''}`;
1216
+ } else if (state.end) {
1217
+ query = `DELETE FROM "${id}" WHERE time <= '${new Date(state.end).toISOString()}`;
1218
+ } else {
1219
+ query = `DELETE FROM "${id}" WHERE time >= '2000-01-01T00:00:00.000Z'`; // delete all
1220
+ }
1221
+
1222
+ adapter._client.query(query, err => {
1223
+ if (err) {
1224
+ adapter.log.warn(`Error on delete("${query}): ${err} / ${JSON.stringify(err.message)}"`);
1225
+ cb(err);
1226
+ } else {
1227
+ setConnected(adapter, true);
1228
+ }
1229
+ cb && cb();
1230
+ });
1231
+ } else {
1232
+ cb && cb('not implemented');
1233
+ }
1234
+ }
1235
+
1236
+ function deleteState(adapter, msg) {
1237
+ if (!msg.message) {
1238
+ adapter.log.error('deleteState called with invalid data');
1239
+ return adapter.sendTo(msg.from, msg.command, {
1240
+ error: 'Invalid call: ' + JSON.stringify(msg)
1241
+ }, msg.callback);
1242
+ }
1243
+ let id;
1244
+ if (Array.isArray(msg.message)) {
1245
+ adapter.log.debug(`deleteState ${msg.message.length} items`);
1246
+
1247
+ for (let i = 0; i < msg.message.length; i++) {
1248
+ id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
1249
+
1250
+ // {id: 'blabla', ts: 892}
1251
+ if (msg.message[i].ts) {
1252
+ _delete(adapter, id, {ts: msg.message[i].ts});
1253
+ } else
1254
+ if (msg.message[i].start) {
1255
+ if (typeof msg.message[i].start === 'string') {
1256
+ msg.message[i].start = new Date(msg.message[i].start).getTime();
1257
+ }
1258
+ if (typeof msg.message[i].end === 'string') {
1259
+ msg.message[i].end = new Date(msg.message[i].end).getTime();
1260
+ }
1261
+ _delete(adapter, id, {start: msg.message[i].start, end: msg.message[i].end || Date.now()});
1262
+ } else
1263
+ if (typeof msg.message[i].state === 'object' && msg.message[i].state && msg.message[i].state.ts) {
1264
+ _delete(adapter, id, {ts: msg.message[i].state.ts});
1265
+ } else
1266
+ if (typeof msg.message[i].state === 'object' && msg.message[i].state && msg.message[i].state.start) {
1267
+ if (typeof msg.message[i].state.start === 'string') {
1268
+ msg.message[i].state.start = new Date(msg.message[i].state.start).getTime();
1269
+ }
1270
+ if (typeof msg.message[i].state.end === 'string') {
1271
+ msg.message[i].state.end = new Date(msg.message[i].state.end).getTime();
1272
+ }
1273
+ _delete(adapter, id, {start: msg.message[i].state.start, end: msg.message[i].state.end || Date.now()});
1274
+ } else {
1275
+ adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message[i]));
1276
+ }
1277
+ }
1278
+ } else if (msg.message.state && Array.isArray(msg.message.state)) {
1279
+ adapter.log.debug(`deleteState ${msg.message.state.length} items`);
1280
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1281
+
1282
+ for (let j = 0; j < msg.message.state.length; j++) {
1283
+ if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
1284
+ if (msg.message.state[j].ts) {
1285
+ _delete(adapter, id, {ts: msg.message.state[j].ts});
1286
+ } else if (msg.message.state[j].start) {
1287
+ if (typeof msg.message.state[j].start === 'string') {
1288
+ msg.message.state[j].start = new Date(msg.message.state[j].start).getTime();
1289
+ }
1290
+ if (typeof msg.message.state[j].end === 'string') {
1291
+ msg.message.state[j].end = new Date(msg.message.state[j].end).getTime();
1292
+ }
1293
+ _delete(adapter, id, {start: msg.message.state[j].start, end: msg.message.state[j].end || Date.now()});
1294
+ }
1295
+ } else if (msg.message.state[j] && typeof msg.message.state[j] === 'number') {
1296
+ _delete(adapter, id, {ts: msg.message.state[j]});
1297
+ } else {
1298
+ adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message.state[j]));
1299
+ }
1300
+ }
1301
+ } else if (msg.message.ts && Array.isArray(msg.message.ts)) {
1302
+ adapter.log.debug(`deleteState ${msg.message.ts.length} items`);
1303
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1304
+ for (let j = 0; j < msg.message.ts.length; j++) {
1305
+ if (msg.message.ts[j] && typeof msg.message.ts[j] === 'number') {
1306
+ _delete(adapter, id, {ts: msg.message.ts[j]});
1307
+ } else {
1308
+ adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message.ts[j]));
1309
+ }
1310
+ }
1311
+ } else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
1312
+ adapter.log.debug('deleteState 1 item');
1313
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1314
+ return _delete(adapter, id, {ts: msg.message.state.ts}, error => adapter.sendTo(msg.from, msg.command, {
1315
+ success: !error,
1316
+ error,
1317
+ connected: !!adapter._connected
1318
+ }, msg.callback));
1319
+ } else if (msg.message.id && msg.message.ts && typeof msg.message.ts === 'number') {
1320
+ adapter.log.debug('deleteState 1 item');
1321
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1322
+ return _delete(adapter, id, {ts: msg.message.ts}, error => adapter.sendTo(msg.from, msg.command, {
1323
+ success: !error,
1324
+ error,
1325
+ connected: !!adapter._connected
1326
+ }, msg.callback));
1327
+ } else {
1328
+ adapter.log.error('deleteState called with invalid data');
1329
+ return adapter.sendTo(msg.from, msg.command, {error: 'Invalid call: ' + JSON.stringify(msg)}, msg.callback);
1330
+ }
1331
+
1332
+ setTimeout(() => {
1333
+ adapter.sendTo(msg.from, msg.command, {
1334
+ success: true,
1335
+ connected: !!adapter._connected
1336
+ }, msg.callback);
1337
+ }, 300);
1338
+ }
1339
+
1340
+ function deleteStateAll(adapter, msg) {
1341
+ if (!msg.message) {
1342
+ adapter.log.error('deleteState called with invalid data');
1343
+ return adapter.sendTo(msg.from, msg.command, {
1344
+ error: 'Invalid call: ' + JSON.stringify(msg)
1345
+ }, msg.callback);
1346
+ }
1347
+ let id;
1348
+ if (Array.isArray(msg.message)) {
1349
+ adapter.log.debug(`deleteStateAll ${msg.message.length} items`);
1350
+ for (let i = 0; i < msg.message.length; i++) {
1351
+ id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
1352
+ _delete(adapter, id, {}, error => {
1353
+ adapter.sendTo(msg.from, msg.command, {
1354
+ success: !error,
1355
+ error,
1356
+ connected: !!adapter._connected
1357
+ }, msg.callback);
1358
+ });
1359
+ }
1360
+ } else if (msg.message.id) {
1361
+ adapter.log.debug('deleteStateAll 1 item');
1362
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1363
+ _delete(adapter, id, {}, error => {
1364
+ adapter.sendTo(msg.from, msg.command, {
1365
+ success: !error,
1366
+ error,
1367
+ connected: !!adapter._connected
1368
+ }, msg.callback);
1369
+ });
1370
+ } else {
1371
+ adapter.log.error('deleteStateAll called with invalid data');
1372
+ return adapter.sendTo(msg.from, msg.command, {error: 'Invalid call: ' + JSON.stringify(msg)}, msg.callback);
1373
+ }
1374
+ }
1375
+
1376
+ function update(adapter, id, state, cb) {
1377
+ if (!adapter._connected) {
1378
+ return cb && cb('not connected');
1379
+ }
1380
+
1381
+ if (adapter.config.dbversion === '1.x') {
1382
+ let query = `SELECT * FROM "${id}" WHERE time = '${new Date(state.ts).toISOString()}'`;
1383
+
1384
+ adapter._client.query(query, (err, result) => {
1385
+ if (err) {
1386
+ adapter.log.warn(`Error on update("${query}): ${err} / ${JSON.stringify(err.message)}"`);
1387
+ cb(err);
1388
+ } else {
1389
+ setConnected(adapter, true);
1390
+
1391
+ console.log(JSON.stringify(result));
1392
+ if (result[0] && result[0][0]) {
1393
+ const stored = result[0][0];
1394
+ if (state.val !== undefined) {
1395
+ stored.val = state.val;
1396
+ }
1397
+ if (state.ack !== undefined) {
1398
+ stored.ack = state.ack;
1399
+ }
1400
+ if (state.q !== undefined) {
1401
+ stored.q = state.q;
1402
+ }
1403
+ if (state.from) {
1404
+ stored.from = state.from;
1405
+ }
1406
+ stored.ts = state.ts;
1407
+ delete stored.time;
1408
+
1409
+ _delete(adapter, id, {ts: stored.ts}, error => {
1410
+ if (error) {
1411
+ adapter.log.error(`Cannot delete value for ${id}: ${JSON.stringify(state)}`);
1412
+ cb(err);
1413
+ } else {
1414
+ pushValueIntoDB(adapter, id, stored);
1415
+ cb && cb();
1416
+ }
1417
+ });
1418
+ } else {
1419
+ adapter.log.error(`Cannot find value to delete for ${id}: ${JSON.stringify(state)}`);
1420
+ cb && cb('not found');
1421
+ }
1422
+ }
1423
+ });
1424
+ } else {
1425
+ cb && cb('not implemented');
1426
+ }
1427
+ }
1428
+
1429
+ function updateState(adapter, msg) {
1430
+ if (!msg.message) {
1431
+ adapter.log.error('updateState called with invalid data');
1432
+ return adapter.sendTo(msg.from, msg.command, {
1433
+ error: 'Invalid call: ' + JSON.stringify(msg)
1434
+ }, msg.callback);
1435
+ }
1436
+ let id;
1437
+ if (Array.isArray(msg.message)) {
1438
+ adapter.log.debug(`updateState ${msg.message.length} items`);
1439
+ for (let i = 0; i < msg.message.length; i++) {
1440
+ id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
1441
+
1442
+ if (msg.message[i].state && typeof msg.message[i].state === 'object') {
1443
+ update(adapter, id, msg.message[i].state);
1444
+ } else {
1445
+ adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message[i]));
1446
+ }
1447
+ }
1448
+ adapter.sendTo(msg.from, msg.command, {
1449
+ success: true,
1450
+ connected: !!adapter._connected
1451
+ }, msg.callback);
1452
+ } else if (msg.message.state && Array.isArray(msg.message.state)) {
1453
+ adapter.log.debug('updateState ' + msg.message.state.length + ' items');
1454
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1455
+ for (let j = 0; j < msg.message.state.length; j++) {
1456
+ if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
1457
+ update(adapter, id, msg.message.state[j]);
1458
+ } else {
1459
+ adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message.state[j]));
1460
+ }
1461
+ }
1462
+ adapter.sendTo(msg.from, msg.command, {
1463
+ success: true,
1464
+ connected: !!adapter._connected
1465
+ }, msg.callback);
1466
+ } else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
1467
+ adapter.log.debug('updateState 1 item');
1468
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1469
+ update(adapter, id, msg.message.state, error => adapter.sendTo(msg.from, msg.command, {
1470
+ success: !error,
1471
+ error,
1472
+ connected: !!adapter._connected
1473
+ }, msg.callback));
1474
+ } else {
1475
+ adapter.log.error('updateState called with invalid data');
1476
+ adapter.sendTo(msg.from, msg.command, {
1477
+ error: 'Invalid call: ' + JSON.stringify(msg)
1478
+ }, msg.callback);
1479
+ }
1480
+ }
1481
+
1482
+ function storeState(adapter, msg) {
1483
+ if (!msg.message) {
1484
+ adapter.log.error('storeState called with invalid data');
1485
+ return adapter.sendTo(msg.from, msg.command, {
1486
+ error: 'Invalid call: ' + JSON.stringify(msg)
1487
+ }, msg.callback);
1488
+ }
1489
+
1490
+ let id;
1491
+ if (Array.isArray(msg.message)) {
1492
+ adapter.log.debug(`storeState ${msg.message.length} items`);
1493
+ for (let i = 0; i < msg.message.length; i++) {
1494
+ id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
1495
+ if (msg.message[i].state && typeof msg.message[i].state === 'object') {
1496
+ pushValueIntoDB(adapter, id, msg.message[i].state);
1497
+ } else {
1498
+ adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message[i]));
1499
+ }
1500
+ }
1501
+ } else if (msg.message.state && Array.isArray(msg.message.state)) {
1502
+ adapter.log.debug(`storeState ${msg.message.state.length} items`);
1503
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1504
+ for (let j = 0; j < msg.message.state.length; j++) {
1505
+ if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
1506
+ pushValueIntoDB(adapter, id, msg.message.state[j]);
1507
+ } else {
1508
+ adapter.log.warn(`Invalid state for ${JSON.stringify(msg.message.state[j])}`);
1509
+ }
1510
+ }
1511
+ } else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
1512
+ adapter.log.debug('storeState 1 item');
1513
+ id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1514
+ pushValueIntoDB(adapter, id, msg.message.state);
1515
+ } else {
1516
+ adapter.log.error('storeState called with invalid data');
1517
+ return adapter.sendTo(msg.from, msg.command, {
1518
+ error: `Invalid call: ${JSON.stringify(msg)}`
1519
+ }, msg.callback);
1520
+ }
1521
+
1522
+ adapter.sendTo(msg.from, msg.command, {
1523
+ success: true,
1524
+ connected: !!adapter._connected,
1525
+ seriesBufferCounter: adapter._seriesBufferCounter,
1526
+ seriesBufferFlushPlanned: adapter._seriesBufferFlushPlanned
1527
+ }, msg.callback);
1528
+ }
1529
+
1181
1530
  function finish(adapter, callback) {
1531
+ adapter.setState && adapter.setState('info.connection', false, true);
1532
+
1182
1533
  if (!adapter._subscribeAll) {
1183
1534
  // unsubscribe
1184
1535
  for (const _id in adapter._influxDPs) {
@@ -1198,7 +1549,7 @@ function finish(adapter, callback) {
1198
1549
  adapter._pingInterval = null;
1199
1550
  }
1200
1551
  if (adapter._finished) {
1201
- if (callback) callback();
1552
+ callback && callback();
1202
1553
  return;
1203
1554
  }
1204
1555
  adapter._finished = true;
@@ -1208,7 +1559,9 @@ function finish(adapter, callback) {
1208
1559
  }
1209
1560
  let count = 0;
1210
1561
  for (const id in adapter._influxDPs) {
1211
- if (!adapter._influxDPs.hasOwnProperty(id)) continue;
1562
+ if (!adapter._influxDPs.hasOwnProperty(id)) {
1563
+ continue;
1564
+ }
1212
1565
 
1213
1566
  if (adapter._influxDPs[id].relogTimeout) {
1214
1567
  clearTimeout(adapter._influxDPs[id].relogTimeout);
@@ -1251,12 +1604,14 @@ function finish(adapter, callback) {
1251
1604
  if (callback) {
1252
1605
  callback();
1253
1606
  } else {
1254
- adapter.terminate ? adapter.terminate() : process.exit()
1607
+ adapter.terminate ? adapter.terminate() : process.exit();
1255
1608
  }
1256
1609
  }
1257
1610
  }
1258
1611
 
1259
1612
  function getHistory(adapter, msg) {
1613
+ const startTime = Date.now();
1614
+
1260
1615
  const options = {
1261
1616
  id: msg.message.id === '*' ? null : msg.message.id,
1262
1617
  start: msg.message.options.start,
@@ -1312,7 +1667,7 @@ function getHistory(adapter, msg) {
1312
1667
  query += ' *';
1313
1668
  }
1314
1669
 
1315
- query += ' from "' + options.id + '"';
1670
+ query += ` from "${options.id}"`;
1316
1671
 
1317
1672
  if (!adapter._influxDPs[options.id]) {
1318
1673
  adapter.sendTo(msg.from, msg.command, {
@@ -1328,67 +1683,77 @@ function getHistory(adapter, msg) {
1328
1683
  options.end = options.start;
1329
1684
  options.start = _end;
1330
1685
  }
1686
+
1331
1687
  // if less 2000.01.01 00:00:00
1332
- if (options.end && options.end < 946681200000) options.end *= 1000;
1333
- if (options.start && options.start < 946681200000) options.start *= 1000;
1688
+ if (options.end && options.end < 946681200000) {
1689
+ options.end *= 1000;
1690
+ }
1691
+ if (options.start && options.start < 946681200000) {
1692
+ options.start *= 1000;
1693
+ }
1334
1694
 
1335
1695
  if (!options.start && !options.count) {
1336
1696
  options.start = options.end - 86400000; // - 1 day
1337
1697
  }
1338
1698
 
1339
- // query one timegroup-value more then requested originally at start and end
1340
- // to make sure to have no 0 values because of the way InfluxDB doies group by time
1699
+ // query one timegroup-value more than requested originally at start and end
1700
+ // to make sure to have no 0 values because of the way InfluxDB does group by time
1341
1701
  if (options.aggregate !== 'onchange' && options.aggregate !== 'none' && options.aggregate !== 'minmax') {
1342
1702
  if (!options.step) {
1343
1703
  // calculate "step" based on difference between start and end using count
1344
1704
  options.step = parseInt((options.end - options.start) / options.count, 10);
1345
1705
  }
1346
- if (options.start) options.start -= options.step;
1706
+ if (options.start) {
1707
+ options.start -= options.step;
1708
+ }
1347
1709
  options.end += options.step;
1348
1710
  options.limit += 2;
1349
1711
  }
1350
1712
 
1351
- query += " WHERE ";
1713
+ query += ` WHERE `;
1352
1714
  if (options.start) {
1353
- query += " time > '" + new Date(options.start).toISOString() + "' AND ";
1715
+ query += ` time > '${new Date(options.start).toISOString()}' AND `;
1354
1716
  }
1355
- query += " time < '" + new Date(options.end).toISOString() + "'";
1717
+ query += ` time < '${new Date(options.end).toISOString()}'`;
1356
1718
 
1357
1719
  if (!options.start && (options.count || options.limit)) {
1358
- query += " ORDER BY time DESC";
1720
+ query += ` ORDER BY time DESC`;
1359
1721
  }
1360
1722
 
1361
1723
  if (options.aggregate !== 'onchange' && options.aggregate !== 'none' && options.aggregate !== 'minmax') {
1362
- query += ' GROUP BY time(' + options.step + 'ms) fill(previous) LIMIT ' + options.limit;
1724
+ query += ` GROUP BY time(${options.step}ms) fill(previous) LIMIT ${options.limit}`;
1363
1725
  } else if (options.aggregate !== 'minmax') {
1364
- query += ' LIMIT ' + options.count;
1726
+ query += ` LIMIT ${options.count}`;
1365
1727
  }
1366
1728
 
1367
- // select one datapoint more then wanted
1729
+ // select one datapoint more than wanted
1368
1730
  if (options.aggregate === 'minmax' || options.aggregate === 'onchange' || options.aggregate === 'none') {
1369
1731
  let addQuery = '';
1370
1732
  if (options.start) {
1371
- addQuery = 'SELECT value from "' + options.id + '"' + " WHERE time <= '" + new Date(options.start).toISOString() + "' ORDER BY time DESC LIMIT 1;";
1733
+ addQuery = `SELECT value from "${options.id}" WHERE time <= '${new Date(options.start).toISOString()}' ORDER BY time DESC LIMIT 1;`;
1372
1734
  query = addQuery + query;
1373
1735
  }
1374
- addQuery = ';SELECT value from "' + options.id + '"' + " WHERE time >= '" + new Date(options.end).toISOString() + "' LIMIT 1";
1736
+ addQuery = `;SELECT value from "${options.id}" WHERE time >= '${new Date(options.end).toISOString()}' LIMIT 1`;
1375
1737
  query = query + addQuery;
1376
1738
  }
1377
1739
 
1378
1740
  adapter.log.debug(query);
1379
1741
 
1380
1742
  // if specific id requested
1381
- adapter._client.query(query, (err, rows) => {
1382
- if (err) {
1743
+ adapter._client.query(query, (error, rows) => {
1744
+ if (error) {
1383
1745
  if (adapter._client.request.getHostsAvailable().length === 0) {
1384
1746
  setConnected(adapter, false);
1385
1747
  }
1386
- adapter.log.error('getHistory: ' + err);
1748
+ adapter.log.error('getHistory: ' + error);
1387
1749
  } else {
1388
1750
  setConnected(adapter, true);
1389
1751
  }
1752
+
1390
1753
  adapter.log.debug('ROWS:' + JSON.stringify(rows));
1754
+
1391
1755
  let result = [];
1756
+
1392
1757
  if (rows && rows.length) {
1393
1758
  for (let qr = 0; qr < rows.length; qr++) {
1394
1759
  for (let rr = 0; rr < rows[qr].length; rr++) {
@@ -1407,28 +1772,21 @@ function getHistory(adapter, msg) {
1407
1772
  }
1408
1773
  }
1409
1774
  }
1410
- if (options.addId) rows[qr][rr].id = msg.message.id;
1775
+ if (options.addId) {
1776
+ rows[qr][rr].id = msg.message.id;
1777
+ }
1411
1778
  result.push(rows[qr][rr]);
1412
1779
  }
1413
1780
  }
1414
1781
  }
1415
1782
 
1416
- if ((result.length > 0) && (options.aggregate === 'minmax')) {
1417
- Aggregate.initAggregate(options);
1418
- Aggregate.aggregation(options, result);
1419
- Aggregate.finishAggregation(options);
1420
- result = options.result;
1421
- }
1422
-
1423
- adapter.sendTo(msg.from, msg.command, {
1424
- result: result,
1425
- error: err,
1426
- sessionId: options.sessionId
1427
- }, msg.callback);
1783
+ Aggregate.sendResponse(adapter, msg, options, (error ? error.toString() : null) || result, startTime);
1428
1784
  });
1429
1785
  }
1430
1786
 
1431
1787
  function getHistoryIflx2(adapter, msg) {
1788
+ const startTime = Date.now();
1789
+
1432
1790
  const options = {
1433
1791
  id: msg.message.id === '*' ? null : msg.message.id,
1434
1792
  start: msg.message.options.start,
@@ -1446,17 +1804,16 @@ function getHistoryIflx2(adapter, msg) {
1446
1804
  options.id = adapter._aliasMap[options.id];
1447
1805
  }
1448
1806
  const fluxQueries = [];
1449
- let fluxQuery = 'from(bucket: "' + adapter.config.dbname + '") ';
1807
+ let fluxQuery = `from(bucket: "${adapter.config.dbname}") `;
1450
1808
 
1451
1809
  const valueColumn = adapter.config.usetags ? '_value' : 'value';
1452
1810
 
1453
1811
  if (!adapter._influxDPs[options.id]) {
1454
- adapter.sendTo(msg.from, msg.command, {
1812
+ return adapter.sendTo(msg.from, msg.command, {
1455
1813
  result: [],
1456
1814
  step: 0,
1457
1815
  error: 'No connection'
1458
1816
  }, msg.callback);
1459
- return;
1460
1817
  }
1461
1818
 
1462
1819
  if (options.start > options.end) {
@@ -1472,8 +1829,8 @@ function getHistoryIflx2(adapter, msg) {
1472
1829
  options.start = options.end - 86400000; // - 1 day
1473
1830
  }
1474
1831
 
1475
- // query one timegroup-value more then requested originally at start and end
1476
- // to make sure to have no 0 values because of the way InfluxDB doies group by time
1832
+ // query one timegroup-value more than requested originally at start and end
1833
+ // to make sure to have no 0 values because of the way InfluxDB does group by time
1477
1834
  if (options.aggregate !== 'onchange' && options.aggregate !== 'none' && options.aggregate !== 'minmax') {
1478
1835
  if (!options.step) {
1479
1836
  // calculate "step" based on difference between start and end using count
@@ -1484,8 +1841,8 @@ function getHistoryIflx2(adapter, msg) {
1484
1841
  options.limit += 2;
1485
1842
  }
1486
1843
 
1487
- fluxQuery += " |> range(" + ((options.start) ? "start: " + new Date(options.start).toISOString() + ", " : "start: -" + adapter.config.retention +"ms, ") + "stop: " + new Date(options.end).toISOString() + ")";
1488
- fluxQuery += ' |> filter(fn: (r) => r["_measurement"] == "' + options.id + '")';
1844
+ fluxQuery += ` |> range(${(options.start) ? "start: " + new Date(options.start).toISOString() + ", " : "start: -" + adapter.config.retention + "ms, "}stop: ${new Date(options.end).toISOString()})`;
1845
+ fluxQuery += ` |> filter(fn: (r) => r["_measurement"] == "${options.id}")`;
1489
1846
 
1490
1847
  if (adapter.config.usetags)
1491
1848
  fluxQuery += ' |> duplicate(column: "_value", as: "value")';
@@ -1493,100 +1850,98 @@ function getHistoryIflx2(adapter, msg) {
1493
1850
  fluxQuery += ' |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")';
1494
1851
 
1495
1852
  if (!options.start && (options.count || options.limit)) {
1496
- fluxQuery += " |> sort(columns:[\"_time\"], desc: true)";
1853
+ fluxQuery += ` |> sort(columns:["_time"], desc: true)`;
1497
1854
  }
1498
1855
 
1499
1856
  if (options.aggregate !== 'onchange' && options.aggregate !== 'none' && options.aggregate !== 'minmax') {
1500
1857
  if ((options.step !== null) && (options.step > 0))
1501
- fluxQuery += ' |> window(every: ' + options.step + 'ms)';
1502
- fluxQuery += '|> fill(column: "' + valueColumn +'", usePrevious: true)';
1858
+ fluxQuery += ` |> window(every: ${options.step}ms)`;
1859
+ fluxQuery += `|> fill(column: "${valueColumn}", usePrevious: true)`;
1503
1860
  } else if (options.aggregate !== 'minmax') {
1504
- fluxQuery += ' |> group() |> limit(n: ' + options.count + ')';
1861
+ fluxQuery += ` |> group() |> limit(n: ${options.count})`;
1505
1862
  }
1506
1863
 
1507
- //Workaround to detect if measurement is of type bool (to skip non-sensical aggregation options)
1508
- //There seems to be no officially supported way to detect this, so we check it by forcing a type-conflict
1509
- const booleanTypeCheckQuery = '\
1510
- from(bucket: "' + adapter.config.dbname + '")\
1511
- |> range(' + ((options.start) ? 'start: ' + new Date(options.start).toISOString() + ', ' : 'start: -' + adapter.config.retention +'ms, ') + 'stop: ' + new Date(options.end).toISOString() + ')\
1512
- |> filter(fn: (r) => r["_measurement"] == "' + options.id + '" and contains(value: r._value, set: [true, false]))\
1513
- ';
1864
+ // Workaround to detect if measurement is of type bool (to skip non-sensual aggregation options)
1865
+ // There seems to be no officially supported way to detect this, so we check it by forcing a type-conflict
1866
+ const booleanTypeCheckQuery = `
1867
+ from(bucket: "${adapter.config.dbname}")
1868
+ |> range(${(options.start) ? `start: ${new Date(options.start).toISOString()}, ` : `start: -${adapter.config.retention}ms, `}stop: ${new Date(options.end).toISOString()})
1869
+ |> filter(fn: (r) => r["_measurement"] == "${options.id}" and contains(value: r._value, set: [true, false]))
1870
+ `;
1514
1871
 
1515
1872
  adapter._client.query(booleanTypeCheckQuery, (error, _rslt) => {
1516
1873
  let isBoolean;
1517
1874
  if (error) {
1518
- if (error.message.match('.*type conflict: bool.*'))
1875
+ if (error.message.match('.*type conflict: bool.*')) {
1519
1876
  isBoolean = false;
1520
- else {
1521
- adapter.sendTo(msg.from, msg.command, {
1877
+ } else {
1878
+ return adapter.sendTo(msg.from, msg.command, {
1522
1879
  result: [],
1523
1880
  error: error,
1524
1881
  sessionId: options.sessionId
1525
1882
  }, msg.callback);
1526
- return;
1527
1883
  }
1528
1884
  } else {
1529
1885
  isBoolean = true;
1530
1886
  adapter.log.debug(`Measurement ${options.id} is of type Boolean - skipping aggregation options`);
1531
1887
  }
1532
1888
 
1533
-
1534
1889
  if (options.step && !isBoolean) {
1535
1890
 
1536
1891
  switch (options.aggregate) {
1537
1892
  case 'average':
1538
- fluxQuery += ' |> mean(column: "'+ valueColumn +'")';
1893
+ fluxQuery += ` |> mean(column: "${valueColumn}")`;
1539
1894
  break;
1540
1895
 
1541
1896
  case 'max':
1542
- fluxQuery += ' |> max(column: "'+ valueColumn +'")';
1897
+ fluxQuery += ` |> max(column: "${valueColumn}")`;
1543
1898
  break;
1544
1899
 
1545
1900
  case 'min':
1546
- fluxQuery += ' |> min(column: "'+ valueColumn +'")';
1901
+ fluxQuery += ` |> min(column: "${valueColumn}")`;
1547
1902
  break;
1548
1903
 
1549
1904
  case 'total':
1550
- fluxQuery += ' |> sum(column: "'+ valueColumn +'")';
1905
+ fluxQuery += ` |> sum(column: "${valueColumn}")`;
1551
1906
  break;
1552
1907
 
1553
1908
  case 'count':
1554
- fluxQuery += ' |> count(column: "'+ valueColumn +'")';
1909
+ fluxQuery += ` |> count(column: "${valueColumn}")`;
1555
1910
  break;
1556
1911
 
1557
1912
  default:
1558
- fluxQuery += ' |> mean(column: "'+ valueColumn +'")';
1913
+ fluxQuery += ` |> mean(column: "${valueColumn}")`;
1559
1914
  break;
1560
1915
  }
1561
1916
  }
1562
1917
 
1563
1918
  fluxQueries.push(fluxQuery);
1564
1919
 
1565
- // select one datapoint more then wanted
1920
+ // select one datapoint more than wanted
1566
1921
  if (options.aggregate === 'minmax' || options.aggregate === 'onchange' || options.aggregate === 'none') {
1567
1922
  let addFluxQuery = '';
1568
1923
  if (options.start) {
1569
1924
  // get one entry "before" the defined timeframe for displaying purposes
1570
- addFluxQuery = 'from(bucket: "' + adapter.config.dbname + '") \
1571
- |> range(start: ' + new Date(options.start - (adapter.config.retention || 31536000) * 1000).toISOString() + ', stop: ' + new Date(options.start).toISOString() + ') \
1572
- |> filter(fn: (r) => r["_measurement"] == "' + options.id + '") \
1573
- ' + ((!adapter.config.usetags) ? '|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")' : '') + '\
1574
- |> sort(columns: ["_time"], desc: true) \
1575
- |> group() \
1576
- |> limit(n: 1)';
1925
+ addFluxQuery = `from(bucket: "${adapter.config.dbname}")
1926
+ |> range(start: ${new Date(options.start - (adapter.config.retention || 31536000) * 1000).toISOString()}, stop: ${new Date(options.start).toISOString()})
1927
+ |> filter(fn: (r) => r["_measurement"] == "${options.id}")
1928
+ ${(!adapter.config.usetags) ? '|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")' : ''}
1929
+ |> sort(columns: ["_time"], desc: true)
1930
+ |> group()
1931
+ |> limit(n: 1)`;
1577
1932
 
1578
1933
  const mainQuery = fluxQueries.pop();
1579
1934
  fluxQueries.push(addFluxQuery);
1580
1935
  fluxQueries.push(mainQuery);
1581
1936
  }
1582
1937
  // get one entry "after" the defined timeframe for displaying purposes
1583
- addFluxQuery = 'from(bucket: "' + adapter.config.dbname + '") \
1584
- |> range(start: ' + new Date(options.end).toISOString() + ') \
1585
- |> filter(fn: (r) => r["_measurement"] == "' + options.id + '") \
1586
- ' + ((!adapter.config.usetags) ? '|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")' : '') + '\
1587
- |> sort(columns: ["_time"], desc: false) \
1588
- |> group() \
1589
- |> limit(n: 1)';
1938
+ addFluxQuery = `from(bucket: "${adapter.config.dbname}")
1939
+ |> range(start: ${new Date(options.end).toISOString()})
1940
+ |> filter(fn: (r) => r["_measurement"] == "${options.id}")
1941
+ ${(!adapter.config.usetags) ? '|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")' : ''}
1942
+ |> sort(columns: ["_time"], desc: false)
1943
+ |> group()
1944
+ |> limit(n: 1)`;
1590
1945
  //fluxQuery = fluxQuery + addFluxQuery;
1591
1946
  fluxQueries.push(addFluxQuery);
1592
1947
  }
@@ -1603,8 +1958,11 @@ function getHistoryIflx2(adapter, msg) {
1603
1958
  } else {
1604
1959
  setConnected(adapter, true);
1605
1960
  }
1961
+
1606
1962
  adapter.log.debug('Parsing retrieved rows:' + JSON.stringify(rows));
1963
+
1607
1964
  let result = [];
1965
+
1608
1966
  if (rows && rows.length) {
1609
1967
  for (let qr = 0; qr < rows.length; qr++) {
1610
1968
  for (let rr = 0; rr < rows[qr].length; rr++) {
@@ -1612,8 +1970,11 @@ function getHistoryIflx2(adapter, msg) {
1612
1970
  rows[qr][rr].val = rows[qr][rr].value;
1613
1971
  delete rows[qr][rr].value;
1614
1972
  }
1615
- rows[qr][rr].ts = new Date(rows[qr][rr].time).getTime();
1973
+
1974
+ rows[qr][rr].ts = new Date(rows[qr][rr].time).getTime();
1975
+
1616
1976
  delete rows[qr][rr].time;
1977
+
1617
1978
  if (rows[qr][rr].val !== null) {
1618
1979
  const f = parseFloat(rows[qr][rr].val);
1619
1980
  if (f == rows[qr][rr].val) {
@@ -1623,24 +1984,17 @@ function getHistoryIflx2(adapter, msg) {
1623
1984
  }
1624
1985
  }
1625
1986
  }
1626
- if (options.addId) rows[qr][rr].id = msg.message.id;
1987
+
1988
+ if (options.addId) {
1989
+ rows[qr][rr].id = msg.message.id;
1990
+ }
1991
+
1627
1992
  result.push(rows[qr][rr]);
1628
1993
  }
1629
1994
  }
1630
1995
  }
1631
1996
 
1632
- if ((result.length > 0) && (options.aggregate === 'minmax')) {
1633
- Aggregate.initAggregate(options);
1634
- Aggregate.aggregation(options, result);
1635
- Aggregate.finishAggregation(options);
1636
- result = options.result;
1637
- }
1638
-
1639
- adapter.sendTo(msg.from, msg.command, {
1640
- result: result,
1641
- error: err,
1642
- sessionId: options.sessionId
1643
- }, msg.callback);
1997
+ Aggregate.sendResponse(adapter, msg, options, (error ? error.toString() : null) || result, startTime);
1644
1998
  });
1645
1999
  });
1646
2000
  }
@@ -1736,9 +2090,9 @@ function multiQuery(adapter, msg) {
1736
2090
  }
1737
2091
  adapter.log.error('queries: ' + err);
1738
2092
  return adapter.sendTo(msg.from, msg.command, {
1739
- result: [],
1740
- error: 'Invalid call'
1741
- }, msg.callback);
2093
+ result: [],
2094
+ error: 'Invalid call'
2095
+ }, msg.callback);
1742
2096
  } else {
1743
2097
  setConnected(adapter, true);
1744
2098
  }
@@ -1768,42 +2122,6 @@ function multiQuery(adapter, msg) {
1768
2122
  }
1769
2123
  }
1770
2124
 
1771
- function storeState(adapter, msg) {
1772
- if (!msg.message || !msg.message.id || !msg.message.state) {
1773
- adapter.log.error('storeState called with invalid data');
1774
- adapter.sendTo(msg.from, msg.command, {
1775
- error: 'Invalid call'
1776
- }, msg.callback);
1777
- return;
1778
- }
1779
-
1780
- let id;
1781
- if (Array.isArray(msg.message)) {
1782
- adapter.log.debug(`storeState: store ${msg.message.length} states for multiple ids`);
1783
- for (let i = 0; i < msg.message.length; i++) {
1784
- id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
1785
- pushValueIntoDB(adapter, id, msg.message[i].state);
1786
- }
1787
- } else if (Array.isArray(msg.message.state)) {
1788
- adapter.log.debug(`storeState: store ${msg.message.state.length} states for ${msg.message.id}`);
1789
- for (let j = 0; j < msg.message.state.length; j++) {
1790
- id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1791
- pushValueIntoDB(adapter, id, msg.message.state[j]);
1792
- }
1793
- } else {
1794
- adapter.log.debug(`storeState: store 1 state for ${msg.message.id}`);
1795
- id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
1796
- pushValueIntoDB(adapter, id, msg.message.state);
1797
- }
1798
-
1799
- adapter.sendTo(msg.from, msg.command, {
1800
- success: true,
1801
- connected: adapter._connected,
1802
- seriesBufferCounter: adapter._seriesBufferCounter,
1803
- seriesBufferFlushPlanned: adapter._seriesBufferFlushPlanned
1804
- }, msg.callback);
1805
- }
1806
-
1807
2125
  function enableHistory(adapter, msg) {
1808
2126
  if (!msg.message || !msg.message.id) {
1809
2127
  adapter.log.error('enableHistory called with invalid data');
@@ -1841,17 +2159,13 @@ function disableHistory(adapter, msg) {
1841
2159
  obj.common.custom = {};
1842
2160
  obj.common.custom[adapter.namespace] = {};
1843
2161
  obj.common.custom[adapter.namespace].enabled = false;
1844
- adapter.extendForeignObject(msg.message.id, obj, err => {
1845
- if (err) {
1846
- adapter.log.error('disableHistory: ' + err);
1847
- adapter.sendTo(msg.from, msg.command, {
1848
- error: err
1849
- }, msg.callback);
2162
+ adapter.extendForeignObject(msg.message.id, obj, error => {
2163
+ if (error) {
2164
+ adapter.log.error('disableHistory: ' + error);
2165
+ adapter.sendTo(msg.from, msg.command, {error}, msg.callback);
1850
2166
  } else {
1851
2167
  adapter.log.info(JSON.stringify(obj));
1852
- adapter.sendTo(msg.from, msg.command, {
1853
- success: true
1854
- }, msg.callback);
2168
+ adapter.sendTo(msg.from, msg.command, {success: true}, msg.callback);
1855
2169
  }
1856
2170
  });
1857
2171
  }