iobroker.influxdb 2.4.0 → 2.5.2
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/LICENSE +1 -1
- package/README.md +80 -23
- package/io-package.json +38 -44
- package/lib/Database.js +6 -5
- package/lib/DatabaseInfluxDB1x.js +18 -17
- package/lib/DatabaseInfluxDB2x.js +65 -70
- package/lib/aggregate.js +141 -37
- package/main.js +475 -169
- package/package.json +9 -9
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
|
|
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(
|
|
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(
|
|
88
|
+
adapter.log.debug(`Removed Alias: ${id} !-> ${adapter._aliasMap[id]}`);
|
|
89
89
|
delete adapter._aliasMap[id];
|
|
90
90
|
}
|
|
91
91
|
|
|
@@ -229,18 +229,17 @@ 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) {
|
|
@@ -272,9 +271,9 @@ function connect(adapter) {
|
|
|
272
271
|
case '1.x':
|
|
273
272
|
default:
|
|
274
273
|
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;
|
|
274
|
+
return adapter.log.error('Password error: Please re-enter the password in Admin. Stopping');
|
|
277
275
|
}
|
|
276
|
+
|
|
278
277
|
adapter._client = new DatabaseInfluxDB1x(
|
|
279
278
|
adapter.log,
|
|
280
279
|
adapter.config.host,
|
|
@@ -473,9 +472,25 @@ function destroyDB(adapter, msg) {
|
|
|
473
472
|
function processMessage(adapter, msg) {
|
|
474
473
|
adapter.log.debug(`Incoming message ${msg.command} from ${msg.from}`);
|
|
475
474
|
if (msg.command === 'features') {
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
475
|
+
// influxdb 1
|
|
476
|
+
if (adapter.config.dbversion === '1.x') {
|
|
477
|
+
adapter.sendTo(msg.from, msg.command, {supportedFeatures: ['update', 'delete', 'deleteRange', 'deleteAll', 'storeState']}, msg.callback);
|
|
478
|
+
} else {
|
|
479
|
+
// not yet implemented
|
|
480
|
+
adapter.sendTo(msg.from, msg.command, {supportedFeatures: ['storeState']}, msg.callback);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
else if (msg.command === 'update') {
|
|
484
|
+
updateState(adapter, msg);
|
|
485
|
+
} else if (msg.command === 'delete') {
|
|
486
|
+
deleteState(adapter, msg);
|
|
487
|
+
} else if (msg.command === 'deleteAll') {
|
|
488
|
+
deleteStateAll(adapter, msg);
|
|
489
|
+
} else if (msg.command === 'deleteRange') {
|
|
490
|
+
deleteState(adapter, msg);
|
|
491
|
+
} else if (msg.command === 'storeState') {
|
|
492
|
+
storeState(adapter, msg);
|
|
493
|
+
} else if (msg.command === 'getHistory') {
|
|
479
494
|
adapter.config.dbversion === '1.x' ? getHistory(adapter, msg) : getHistoryIflx2(adapter, msg);
|
|
480
495
|
}
|
|
481
496
|
else if (msg.command === 'test') {
|
|
@@ -502,9 +517,6 @@ function processMessage(adapter, msg) {
|
|
|
502
517
|
else if (msg.command === 'resetConflictingPoints') {
|
|
503
518
|
resetConflictingPoints(adapter, msg);
|
|
504
519
|
}
|
|
505
|
-
else if (msg.command === 'storeState') {
|
|
506
|
-
storeState(adapter, msg);
|
|
507
|
-
}
|
|
508
520
|
else if (msg.command === 'enableHistory') {
|
|
509
521
|
enableHistory(adapter, msg);
|
|
510
522
|
}
|
|
@@ -597,10 +609,11 @@ function main(adapter) {
|
|
|
597
609
|
|
|
598
610
|
// read all custom settings
|
|
599
611
|
adapter.getObjectView('system', 'custom', {}, (err, doc) => {
|
|
600
|
-
|
|
612
|
+
err && adapter.log.error('main/getObjectView: ' + err);
|
|
601
613
|
let count = 0;
|
|
602
614
|
if (doc && doc.rows) {
|
|
603
|
-
|
|
615
|
+
const l = doc.rows.length;
|
|
616
|
+
for (let i = 0; i < l; i++) {
|
|
604
617
|
if (doc.rows[i].value) {
|
|
605
618
|
let id = doc.rows[i].id;
|
|
606
619
|
const realId = id;
|
|
@@ -637,7 +650,8 @@ function main(adapter) {
|
|
|
637
650
|
} else {
|
|
638
651
|
adapter._influxDPs[id][adapter.namespace].changesMinDelta = adapter.config.changesMinDelta;
|
|
639
652
|
}
|
|
640
|
-
|
|
653
|
+
|
|
654
|
+
adapter._influxDPs[id][adapter.namespace].storageType = adapter._influxDPs[id][adapter.namespace].storageType || false;
|
|
641
655
|
|
|
642
656
|
adapter._influxDPs[id].realId = realId;
|
|
643
657
|
writeInitialValue(adapter, realId, id);
|
|
@@ -759,6 +773,7 @@ function pushHistory(adapter, id, state, timerRelog) {
|
|
|
759
773
|
// only store state if really changed
|
|
760
774
|
adapter._influxDPs[id].state = state;
|
|
761
775
|
}
|
|
776
|
+
|
|
762
777
|
adapter._influxDPs[id].lastLogTime = state.ts;
|
|
763
778
|
adapter._influxDPs[id].skipped = null;
|
|
764
779
|
|
|
@@ -1008,7 +1023,8 @@ function writeAllSeriesPerID(adapter, series, cb, idList) {
|
|
|
1008
1023
|
return cb && cb();
|
|
1009
1024
|
}
|
|
1010
1025
|
const id = idList.shift();
|
|
1011
|
-
writeSeriesPerID(adapter, id, series[id], () =>
|
|
1026
|
+
writeSeriesPerID(adapter, id, series[id], () =>
|
|
1027
|
+
writeAllSeriesPerID(adapter, series, cb, idList));
|
|
1012
1028
|
}
|
|
1013
1029
|
|
|
1014
1030
|
function writeSeriesPerID(adapter, seriesId, points, cb) {
|
|
@@ -1074,7 +1090,7 @@ function writeOnePointForID(adapter, pointId, point, directWrite, cb) {
|
|
|
1074
1090
|
adapter._client.writePoint(pointId, point, null, (err /* , result */) => {
|
|
1075
1091
|
if (err) {
|
|
1076
1092
|
adapter.log.warn(`Error on writePoint("${JSON.stringify(point)}): ${err} / ${JSON.stringify(err.message)}"`);
|
|
1077
|
-
if (
|
|
1093
|
+
if (!adapter._client.request.getHostsAvailable().length || (err.message && err.message === 'timeout')) {
|
|
1078
1094
|
reconnect(adapter);
|
|
1079
1095
|
addPointToSeriesBuffer(adapter, pointId, point);
|
|
1080
1096
|
} else if (err.message && typeof err.message === 'string' && err.message.includes('field type conflict')) {
|
|
@@ -1135,7 +1151,7 @@ function writeOnePointForID(adapter, pointId, point, directWrite, cb) {
|
|
|
1135
1151
|
}
|
|
1136
1152
|
if (!directWrite || !retry) {
|
|
1137
1153
|
// remember this as a pot. conflicting point and write synchronous
|
|
1138
|
-
adapter._conflictingPoints[pointId]=1;
|
|
1154
|
+
adapter._conflictingPoints[pointId] = 1;
|
|
1139
1155
|
adapter.log.warn(`Add ${pointId} to conflicting Points (${Object.keys(adapter._conflictingPoints).length} now)`);
|
|
1140
1156
|
}
|
|
1141
1157
|
} else {
|
|
@@ -1145,7 +1161,7 @@ function writeOnePointForID(adapter, pointId, point, directWrite, cb) {
|
|
|
1145
1161
|
adapter._errorPoints[pointId]++;
|
|
1146
1162
|
}
|
|
1147
1163
|
if (adapter._errorPoints[pointId] < 10) {
|
|
1148
|
-
// re-add that point to buffer to try write again
|
|
1164
|
+
// re-add that point to buffer to try to write again
|
|
1149
1165
|
adapter.log.info(`Add point that had error for ${pointId} to buffer again, error-count=${adapter._errorPoints[pointId]}`);
|
|
1150
1166
|
addPointToSeriesBuffer(adapter, pointId, point);
|
|
1151
1167
|
} else {
|
|
@@ -1178,7 +1194,334 @@ function writeFileBufferToDisk() {
|
|
|
1178
1194
|
adapter._seriesBufferCounter = null;
|
|
1179
1195
|
}
|
|
1180
1196
|
|
|
1197
|
+
function _delete(adapter, id, state, cb) {
|
|
1198
|
+
if (!adapter._connected) {
|
|
1199
|
+
return cb && cb('not connected');
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
if (adapter.config.dbversion === '1.x') {
|
|
1203
|
+
let query;
|
|
1204
|
+
if (state.ts) {
|
|
1205
|
+
query = `DELETE FROM "${id}" WHERE time = '${new Date(state.ts).toISOString()}'`;
|
|
1206
|
+
} else if (state.start) {
|
|
1207
|
+
query = `DELETE FROM "${id}" WHERE time >= '${new Date(state.start).toISOString()}'${state.end ? ` AND time <= '${new Date(state.end).toISOString()}'` : ''}`;
|
|
1208
|
+
} else if (state.end) {
|
|
1209
|
+
query = `DELETE FROM "${id}" WHERE time <= '${new Date(state.end).toISOString()}`;
|
|
1210
|
+
} else {
|
|
1211
|
+
query = `DELETE FROM "${id}" WHERE time >= '2000-01-01T00:00:00.000Z'`; // delete all
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
adapter._client.query(query, err => {
|
|
1215
|
+
if (err) {
|
|
1216
|
+
adapter.log.warn(`Error on delete("${query}): ${err} / ${JSON.stringify(err.message)}"`);
|
|
1217
|
+
cb(err);
|
|
1218
|
+
} else {
|
|
1219
|
+
setConnected(adapter, true);
|
|
1220
|
+
}
|
|
1221
|
+
cb && cb();
|
|
1222
|
+
});
|
|
1223
|
+
} else {
|
|
1224
|
+
cb && cb('not implemented');
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
function deleteState(adapter, msg) {
|
|
1229
|
+
if (!msg.message) {
|
|
1230
|
+
adapter.log.error('deleteState called with invalid data');
|
|
1231
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
1232
|
+
error: 'Invalid call: ' + JSON.stringify(msg)
|
|
1233
|
+
}, msg.callback);
|
|
1234
|
+
}
|
|
1235
|
+
let id;
|
|
1236
|
+
if (Array.isArray(msg.message)) {
|
|
1237
|
+
adapter.log.debug(`deleteState ${msg.message.length} items`);
|
|
1238
|
+
|
|
1239
|
+
for (let i = 0; i < msg.message.length; i++) {
|
|
1240
|
+
id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
1241
|
+
|
|
1242
|
+
// {id: 'blabla', ts: 892}
|
|
1243
|
+
if (msg.message[i].ts) {
|
|
1244
|
+
_delete(adapter, id, {ts: msg.message[i].ts});
|
|
1245
|
+
} else
|
|
1246
|
+
if (msg.message[i].start) {
|
|
1247
|
+
if (typeof msg.message[i].start === 'string') {
|
|
1248
|
+
msg.message[i].start = new Date(msg.message[i].start).getTime();
|
|
1249
|
+
}
|
|
1250
|
+
if (typeof msg.message[i].end === 'string') {
|
|
1251
|
+
msg.message[i].end = new Date(msg.message[i].end).getTime();
|
|
1252
|
+
}
|
|
1253
|
+
_delete(adapter, id, {start: msg.message[i].start, end: msg.message[i].end || Date.now()});
|
|
1254
|
+
} else
|
|
1255
|
+
if (typeof msg.message[i].state === 'object' && msg.message[i].state && msg.message[i].state.ts) {
|
|
1256
|
+
_delete(adapter, id, {ts: msg.message[i].state.ts});
|
|
1257
|
+
} else
|
|
1258
|
+
if (typeof msg.message[i].state === 'object' && msg.message[i].state && msg.message[i].state.start) {
|
|
1259
|
+
if (typeof msg.message[i].state.start === 'string') {
|
|
1260
|
+
msg.message[i].state.start = new Date(msg.message[i].state.start).getTime();
|
|
1261
|
+
}
|
|
1262
|
+
if (typeof msg.message[i].state.end === 'string') {
|
|
1263
|
+
msg.message[i].state.end = new Date(msg.message[i].state.end).getTime();
|
|
1264
|
+
}
|
|
1265
|
+
_delete(adapter, id, {start: msg.message[i].state.start, end: msg.message[i].state.end || Date.now()});
|
|
1266
|
+
} else {
|
|
1267
|
+
adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message[i]));
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
} else if (msg.message.state && Array.isArray(msg.message.state)) {
|
|
1271
|
+
adapter.log.debug(`deleteState ${msg.message.state.length} items`);
|
|
1272
|
+
id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
|
|
1273
|
+
|
|
1274
|
+
for (let j = 0; j < msg.message.state.length; j++) {
|
|
1275
|
+
if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
|
|
1276
|
+
if (msg.message.state[j].ts) {
|
|
1277
|
+
_delete(adapter, id, {ts: msg.message.state[j].ts});
|
|
1278
|
+
} else if (msg.message.state[j].start) {
|
|
1279
|
+
if (typeof msg.message.state[j].start === 'string') {
|
|
1280
|
+
msg.message.state[j].start = new Date(msg.message.state[j].start).getTime();
|
|
1281
|
+
}
|
|
1282
|
+
if (typeof msg.message.state[j].end === 'string') {
|
|
1283
|
+
msg.message.state[j].end = new Date(msg.message.state[j].end).getTime();
|
|
1284
|
+
}
|
|
1285
|
+
_delete(adapter, id, {start: msg.message.state[j].start, end: msg.message.state[j].end || Date.now()});
|
|
1286
|
+
}
|
|
1287
|
+
} else if (msg.message.state[j] && typeof msg.message.state[j] === 'number') {
|
|
1288
|
+
_delete(adapter, id, {ts: msg.message.state[j]});
|
|
1289
|
+
} else {
|
|
1290
|
+
adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message.state[j]));
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
} else if (msg.message.ts && Array.isArray(msg.message.ts)) {
|
|
1294
|
+
adapter.log.debug(`deleteState ${msg.message.ts.length} items`);
|
|
1295
|
+
id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
|
|
1296
|
+
for (let j = 0; j < msg.message.ts.length; j++) {
|
|
1297
|
+
if (msg.message.ts[j] && typeof msg.message.ts[j] === 'number') {
|
|
1298
|
+
_delete(adapter, id, {ts: msg.message.ts[j]});
|
|
1299
|
+
} else {
|
|
1300
|
+
adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message.ts[j]));
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
} else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
|
|
1304
|
+
adapter.log.debug('deleteState 1 item');
|
|
1305
|
+
id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
|
|
1306
|
+
return _delete(adapter, id, {ts: msg.message.state.ts}, error => adapter.sendTo(msg.from, msg.command, {
|
|
1307
|
+
success: !error,
|
|
1308
|
+
error,
|
|
1309
|
+
connected: !!adapter._connected
|
|
1310
|
+
}, msg.callback));
|
|
1311
|
+
} else if (msg.message.id && msg.message.ts && typeof msg.message.ts === 'number') {
|
|
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.ts}, error => adapter.sendTo(msg.from, msg.command, {
|
|
1315
|
+
success: !error,
|
|
1316
|
+
error,
|
|
1317
|
+
connected: !!adapter._connected
|
|
1318
|
+
}, msg.callback));
|
|
1319
|
+
} else {
|
|
1320
|
+
adapter.log.error('deleteState called with invalid data');
|
|
1321
|
+
return adapter.sendTo(msg.from, msg.command, {error: 'Invalid call: ' + JSON.stringify(msg)}, msg.callback);
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
setTimeout(() => {
|
|
1325
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
1326
|
+
success: true,
|
|
1327
|
+
connected: !!adapter._connected
|
|
1328
|
+
}, msg.callback);
|
|
1329
|
+
}, 300);
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
function deleteStateAll(adapter, msg) {
|
|
1333
|
+
if (!msg.message) {
|
|
1334
|
+
adapter.log.error('deleteState called with invalid data');
|
|
1335
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
1336
|
+
error: 'Invalid call: ' + JSON.stringify(msg)
|
|
1337
|
+
}, msg.callback);
|
|
1338
|
+
}
|
|
1339
|
+
let id;
|
|
1340
|
+
if (Array.isArray(msg.message)) {
|
|
1341
|
+
adapter.log.debug(`deleteStateAll ${msg.message.length} items`);
|
|
1342
|
+
for (let i = 0; i < msg.message.length; i++) {
|
|
1343
|
+
id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
1344
|
+
_delete(adapter, id, {}, error => {
|
|
1345
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
1346
|
+
success: !error,
|
|
1347
|
+
error,
|
|
1348
|
+
connected: !!adapter._connected
|
|
1349
|
+
}, msg.callback);
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
} else if (msg.message.id) {
|
|
1353
|
+
adapter.log.debug('deleteStateAll 1 item');
|
|
1354
|
+
id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
|
|
1355
|
+
_delete(adapter, id, {}, error => {
|
|
1356
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
1357
|
+
success: !error,
|
|
1358
|
+
error,
|
|
1359
|
+
connected: !!adapter._connected
|
|
1360
|
+
}, msg.callback);
|
|
1361
|
+
});
|
|
1362
|
+
} else {
|
|
1363
|
+
adapter.log.error('deleteStateAll called with invalid data');
|
|
1364
|
+
return adapter.sendTo(msg.from, msg.command, {error: 'Invalid call: ' + JSON.stringify(msg)}, msg.callback);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
function update(adapter, id, state, cb) {
|
|
1369
|
+
if (!adapter._connected) {
|
|
1370
|
+
return cb && cb('not connected');
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
if (adapter.config.dbversion === '1.x') {
|
|
1374
|
+
let query = `SELECT * FROM "${id}" WHERE time = '${new Date(state.ts).toISOString()}'`;
|
|
1375
|
+
|
|
1376
|
+
adapter._client.query(query, (err, result) => {
|
|
1377
|
+
if (err) {
|
|
1378
|
+
adapter.log.warn(`Error on update("${query}): ${err} / ${JSON.stringify(err.message)}"`);
|
|
1379
|
+
cb(err);
|
|
1380
|
+
} else {
|
|
1381
|
+
setConnected(adapter, true);
|
|
1382
|
+
|
|
1383
|
+
console.log(JSON.stringify(result));
|
|
1384
|
+
if (result[0] && result[0][0]) {
|
|
1385
|
+
const stored = result[0][0];
|
|
1386
|
+
if (state.val !== undefined) {
|
|
1387
|
+
stored.val = state.val;
|
|
1388
|
+
}
|
|
1389
|
+
if (state.ack !== undefined) {
|
|
1390
|
+
stored.ack = state.ack;
|
|
1391
|
+
}
|
|
1392
|
+
if (state.q !== undefined) {
|
|
1393
|
+
stored.q = state.q;
|
|
1394
|
+
}
|
|
1395
|
+
if (state.from) {
|
|
1396
|
+
stored.from = state.from;
|
|
1397
|
+
}
|
|
1398
|
+
stored.ts = state.ts;
|
|
1399
|
+
delete stored.time;
|
|
1400
|
+
|
|
1401
|
+
_delete(adapter, id, {ts: stored.ts}, error => {
|
|
1402
|
+
if (error) {
|
|
1403
|
+
adapter.log.error(`Cannot delete value for ${id}: ${JSON.stringify(state)}`);
|
|
1404
|
+
cb(err);
|
|
1405
|
+
} else {
|
|
1406
|
+
pushValueIntoDB(adapter, id, stored);
|
|
1407
|
+
cb && cb();
|
|
1408
|
+
}
|
|
1409
|
+
});
|
|
1410
|
+
} else {
|
|
1411
|
+
adapter.log.error(`Cannot find value to delete for ${id}: ${JSON.stringify(state)}`);
|
|
1412
|
+
cb && cb('not found');
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
});
|
|
1416
|
+
} else {
|
|
1417
|
+
cb && cb('not implemented');
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
function updateState(adapter, msg) {
|
|
1422
|
+
if (!msg.message) {
|
|
1423
|
+
adapter.log.error('updateState called with invalid data');
|
|
1424
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
1425
|
+
error: 'Invalid call: ' + JSON.stringify(msg)
|
|
1426
|
+
}, msg.callback);
|
|
1427
|
+
}
|
|
1428
|
+
let id;
|
|
1429
|
+
if (Array.isArray(msg.message)) {
|
|
1430
|
+
adapter.log.debug(`updateState ${msg.message.length} items`);
|
|
1431
|
+
for (let i = 0; i < msg.message.length; i++) {
|
|
1432
|
+
id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
1433
|
+
|
|
1434
|
+
if (msg.message[i].state && typeof msg.message[i].state === 'object') {
|
|
1435
|
+
update(adapter, id, msg.message[i].state);
|
|
1436
|
+
} else {
|
|
1437
|
+
adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message[i]));
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
1441
|
+
success: true,
|
|
1442
|
+
connected: !!adapter._connected
|
|
1443
|
+
}, msg.callback);
|
|
1444
|
+
} else if (msg.message.state && Array.isArray(msg.message.state)) {
|
|
1445
|
+
adapter.log.debug('updateState ' + msg.message.state.length + ' items');
|
|
1446
|
+
id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
|
|
1447
|
+
for (let j = 0; j < msg.message.state.length; j++) {
|
|
1448
|
+
if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
|
|
1449
|
+
update(adapter, id, msg.message.state[j]);
|
|
1450
|
+
} else {
|
|
1451
|
+
adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message.state[j]));
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
1455
|
+
success: true,
|
|
1456
|
+
connected: !!adapter._connected
|
|
1457
|
+
}, msg.callback);
|
|
1458
|
+
} else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
|
|
1459
|
+
adapter.log.debug('updateState 1 item');
|
|
1460
|
+
id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
|
|
1461
|
+
update(adapter, id, msg.message.state, error => adapter.sendTo(msg.from, msg.command, {
|
|
1462
|
+
success: !error,
|
|
1463
|
+
error,
|
|
1464
|
+
connected: !!adapter._connected
|
|
1465
|
+
}, msg.callback));
|
|
1466
|
+
} else {
|
|
1467
|
+
adapter.log.error('updateState called with invalid data');
|
|
1468
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
1469
|
+
error: 'Invalid call: ' + JSON.stringify(msg)
|
|
1470
|
+
}, msg.callback);
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
function storeState(adapter, msg) {
|
|
1475
|
+
if (!msg.message) {
|
|
1476
|
+
adapter.log.error('storeState called with invalid data');
|
|
1477
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
1478
|
+
error: 'Invalid call: ' + JSON.stringify(msg)
|
|
1479
|
+
}, msg.callback);
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
let id;
|
|
1483
|
+
if (Array.isArray(msg.message)) {
|
|
1484
|
+
adapter.log.debug(`storeState ${msg.message.length} items`);
|
|
1485
|
+
for (let i = 0; i < msg.message.length; i++) {
|
|
1486
|
+
id = adapter._aliasMap[msg.message[i].id] ? adapter._aliasMap[msg.message[i].id] : msg.message[i].id;
|
|
1487
|
+
if (msg.message[i].state && typeof msg.message[i].state === 'object') {
|
|
1488
|
+
pushValueIntoDB(adapter, id, msg.message[i].state);
|
|
1489
|
+
} else {
|
|
1490
|
+
adapter.log.warn('Invalid state for ' + JSON.stringify(msg.message[i]));
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
} else if (msg.message.state && Array.isArray(msg.message.state)) {
|
|
1494
|
+
adapter.log.debug(`storeState ${msg.message.state.length} items`);
|
|
1495
|
+
id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
|
|
1496
|
+
for (let j = 0; j < msg.message.state.length; j++) {
|
|
1497
|
+
if (msg.message.state[j] && typeof msg.message.state[j] === 'object') {
|
|
1498
|
+
pushValueIntoDB(adapter, id, msg.message.state[j]);
|
|
1499
|
+
} else {
|
|
1500
|
+
adapter.log.warn(`Invalid state for ${JSON.stringify(msg.message.state[j])}`);
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
} else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
|
|
1504
|
+
adapter.log.debug('storeState 1 item');
|
|
1505
|
+
id = adapter._aliasMap[msg.message.id] ? adapter._aliasMap[msg.message.id] : msg.message.id;
|
|
1506
|
+
pushValueIntoDB(adapter, id, msg.message.state);
|
|
1507
|
+
} else {
|
|
1508
|
+
adapter.log.error('storeState called with invalid data');
|
|
1509
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
1510
|
+
error: `Invalid call: ${JSON.stringify(msg)}`
|
|
1511
|
+
}, msg.callback);
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
1515
|
+
success: true,
|
|
1516
|
+
connected: !!adapter._connected,
|
|
1517
|
+
seriesBufferCounter: adapter._seriesBufferCounter,
|
|
1518
|
+
seriesBufferFlushPlanned: adapter._seriesBufferFlushPlanned
|
|
1519
|
+
}, msg.callback);
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1181
1522
|
function finish(adapter, callback) {
|
|
1523
|
+
adapter.setState && adapter.setState('info.connection', false, true);
|
|
1524
|
+
|
|
1182
1525
|
if (!adapter._subscribeAll) {
|
|
1183
1526
|
// unsubscribe
|
|
1184
1527
|
for (const _id in adapter._influxDPs) {
|
|
@@ -1198,7 +1541,7 @@ function finish(adapter, callback) {
|
|
|
1198
1541
|
adapter._pingInterval = null;
|
|
1199
1542
|
}
|
|
1200
1543
|
if (adapter._finished) {
|
|
1201
|
-
|
|
1544
|
+
callback && callback();
|
|
1202
1545
|
return;
|
|
1203
1546
|
}
|
|
1204
1547
|
adapter._finished = true;
|
|
@@ -1208,7 +1551,9 @@ function finish(adapter, callback) {
|
|
|
1208
1551
|
}
|
|
1209
1552
|
let count = 0;
|
|
1210
1553
|
for (const id in adapter._influxDPs) {
|
|
1211
|
-
if (!adapter._influxDPs.hasOwnProperty(id))
|
|
1554
|
+
if (!adapter._influxDPs.hasOwnProperty(id)) {
|
|
1555
|
+
continue;
|
|
1556
|
+
}
|
|
1212
1557
|
|
|
1213
1558
|
if (adapter._influxDPs[id].relogTimeout) {
|
|
1214
1559
|
clearTimeout(adapter._influxDPs[id].relogTimeout);
|
|
@@ -1251,12 +1596,14 @@ function finish(adapter, callback) {
|
|
|
1251
1596
|
if (callback) {
|
|
1252
1597
|
callback();
|
|
1253
1598
|
} else {
|
|
1254
|
-
adapter.terminate ? adapter.terminate() : process.exit()
|
|
1599
|
+
adapter.terminate ? adapter.terminate() : process.exit();
|
|
1255
1600
|
}
|
|
1256
1601
|
}
|
|
1257
1602
|
}
|
|
1258
1603
|
|
|
1259
1604
|
function getHistory(adapter, msg) {
|
|
1605
|
+
const startTime = Date.now();
|
|
1606
|
+
|
|
1260
1607
|
const options = {
|
|
1261
1608
|
id: msg.message.id === '*' ? null : msg.message.id,
|
|
1262
1609
|
start: msg.message.options.start,
|
|
@@ -1312,7 +1659,7 @@ function getHistory(adapter, msg) {
|
|
|
1312
1659
|
query += ' *';
|
|
1313
1660
|
}
|
|
1314
1661
|
|
|
1315
|
-
query +=
|
|
1662
|
+
query += ` from "${options.id}"`;
|
|
1316
1663
|
|
|
1317
1664
|
if (!adapter._influxDPs[options.id]) {
|
|
1318
1665
|
adapter.sendTo(msg.from, msg.command, {
|
|
@@ -1328,67 +1675,77 @@ function getHistory(adapter, msg) {
|
|
|
1328
1675
|
options.end = options.start;
|
|
1329
1676
|
options.start = _end;
|
|
1330
1677
|
}
|
|
1678
|
+
|
|
1331
1679
|
// if less 2000.01.01 00:00:00
|
|
1332
|
-
if (options.end && options.end < 946681200000)
|
|
1333
|
-
|
|
1680
|
+
if (options.end && options.end < 946681200000) {
|
|
1681
|
+
options.end *= 1000;
|
|
1682
|
+
}
|
|
1683
|
+
if (options.start && options.start < 946681200000) {
|
|
1684
|
+
options.start *= 1000;
|
|
1685
|
+
}
|
|
1334
1686
|
|
|
1335
1687
|
if (!options.start && !options.count) {
|
|
1336
1688
|
options.start = options.end - 86400000; // - 1 day
|
|
1337
1689
|
}
|
|
1338
1690
|
|
|
1339
|
-
// query one timegroup-value more
|
|
1340
|
-
// to make sure to have no 0 values because of the way InfluxDB
|
|
1691
|
+
// query one timegroup-value more than requested originally at start and end
|
|
1692
|
+
// to make sure to have no 0 values because of the way InfluxDB does group by time
|
|
1341
1693
|
if (options.aggregate !== 'onchange' && options.aggregate !== 'none' && options.aggregate !== 'minmax') {
|
|
1342
1694
|
if (!options.step) {
|
|
1343
1695
|
// calculate "step" based on difference between start and end using count
|
|
1344
1696
|
options.step = parseInt((options.end - options.start) / options.count, 10);
|
|
1345
1697
|
}
|
|
1346
|
-
if (options.start)
|
|
1698
|
+
if (options.start) {
|
|
1699
|
+
options.start -= options.step;
|
|
1700
|
+
}
|
|
1347
1701
|
options.end += options.step;
|
|
1348
1702
|
options.limit += 2;
|
|
1349
1703
|
}
|
|
1350
1704
|
|
|
1351
|
-
query +=
|
|
1705
|
+
query += ` WHERE `;
|
|
1352
1706
|
if (options.start) {
|
|
1353
|
-
query +=
|
|
1707
|
+
query += ` time > '${new Date(options.start).toISOString()}' AND `;
|
|
1354
1708
|
}
|
|
1355
|
-
query +=
|
|
1709
|
+
query += ` time < '${new Date(options.end).toISOString()}'`;
|
|
1356
1710
|
|
|
1357
1711
|
if (!options.start && (options.count || options.limit)) {
|
|
1358
|
-
query +=
|
|
1712
|
+
query += ` ORDER BY time DESC`;
|
|
1359
1713
|
}
|
|
1360
1714
|
|
|
1361
1715
|
if (options.aggregate !== 'onchange' && options.aggregate !== 'none' && options.aggregate !== 'minmax') {
|
|
1362
|
-
query +=
|
|
1716
|
+
query += ` GROUP BY time(${options.step}ms) fill(previous) LIMIT ${options.limit}`;
|
|
1363
1717
|
} else if (options.aggregate !== 'minmax') {
|
|
1364
|
-
query +=
|
|
1718
|
+
query += ` LIMIT ${options.count}`;
|
|
1365
1719
|
}
|
|
1366
1720
|
|
|
1367
|
-
// select one datapoint more
|
|
1721
|
+
// select one datapoint more than wanted
|
|
1368
1722
|
if (options.aggregate === 'minmax' || options.aggregate === 'onchange' || options.aggregate === 'none') {
|
|
1369
1723
|
let addQuery = '';
|
|
1370
1724
|
if (options.start) {
|
|
1371
|
-
addQuery =
|
|
1725
|
+
addQuery = `SELECT value from "${options.id}" WHERE time <= '${new Date(options.start).toISOString()}' ORDER BY time DESC LIMIT 1;`;
|
|
1372
1726
|
query = addQuery + query;
|
|
1373
1727
|
}
|
|
1374
|
-
addQuery =
|
|
1728
|
+
addQuery = `;SELECT value from "${options.id}" WHERE time >= '${new Date(options.end).toISOString()}' LIMIT 1`;
|
|
1375
1729
|
query = query + addQuery;
|
|
1376
1730
|
}
|
|
1377
1731
|
|
|
1378
1732
|
adapter.log.debug(query);
|
|
1379
1733
|
|
|
1380
1734
|
// if specific id requested
|
|
1381
|
-
adapter._client.query(query, (
|
|
1382
|
-
if (
|
|
1735
|
+
adapter._client.query(query, (error, rows) => {
|
|
1736
|
+
if (error) {
|
|
1383
1737
|
if (adapter._client.request.getHostsAvailable().length === 0) {
|
|
1384
1738
|
setConnected(adapter, false);
|
|
1385
1739
|
}
|
|
1386
|
-
adapter.log.error('getHistory: ' +
|
|
1740
|
+
adapter.log.error('getHistory: ' + error);
|
|
1387
1741
|
} else {
|
|
1388
1742
|
setConnected(adapter, true);
|
|
1389
1743
|
}
|
|
1744
|
+
|
|
1390
1745
|
adapter.log.debug('ROWS:' + JSON.stringify(rows));
|
|
1746
|
+
|
|
1391
1747
|
let result = [];
|
|
1748
|
+
|
|
1392
1749
|
if (rows && rows.length) {
|
|
1393
1750
|
for (let qr = 0; qr < rows.length; qr++) {
|
|
1394
1751
|
for (let rr = 0; rr < rows[qr].length; rr++) {
|
|
@@ -1407,28 +1764,21 @@ function getHistory(adapter, msg) {
|
|
|
1407
1764
|
}
|
|
1408
1765
|
}
|
|
1409
1766
|
}
|
|
1410
|
-
if (options.addId)
|
|
1767
|
+
if (options.addId) {
|
|
1768
|
+
rows[qr][rr].id = msg.message.id;
|
|
1769
|
+
}
|
|
1411
1770
|
result.push(rows[qr][rr]);
|
|
1412
1771
|
}
|
|
1413
1772
|
}
|
|
1414
1773
|
}
|
|
1415
1774
|
|
|
1416
|
-
|
|
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);
|
|
1775
|
+
Aggregate.sendResponse(adapter, msg, options, (error ? error.toString() : null) || result, startTime);
|
|
1428
1776
|
});
|
|
1429
1777
|
}
|
|
1430
1778
|
|
|
1431
1779
|
function getHistoryIflx2(adapter, msg) {
|
|
1780
|
+
const startTime = Date.now();
|
|
1781
|
+
|
|
1432
1782
|
const options = {
|
|
1433
1783
|
id: msg.message.id === '*' ? null : msg.message.id,
|
|
1434
1784
|
start: msg.message.options.start,
|
|
@@ -1446,17 +1796,16 @@ function getHistoryIflx2(adapter, msg) {
|
|
|
1446
1796
|
options.id = adapter._aliasMap[options.id];
|
|
1447
1797
|
}
|
|
1448
1798
|
const fluxQueries = [];
|
|
1449
|
-
let fluxQuery =
|
|
1799
|
+
let fluxQuery = `from(bucket: "${adapter.config.dbname}") `;
|
|
1450
1800
|
|
|
1451
1801
|
const valueColumn = adapter.config.usetags ? '_value' : 'value';
|
|
1452
1802
|
|
|
1453
1803
|
if (!adapter._influxDPs[options.id]) {
|
|
1454
|
-
adapter.sendTo(msg.from, msg.command, {
|
|
1804
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
1455
1805
|
result: [],
|
|
1456
1806
|
step: 0,
|
|
1457
1807
|
error: 'No connection'
|
|
1458
1808
|
}, msg.callback);
|
|
1459
|
-
return;
|
|
1460
1809
|
}
|
|
1461
1810
|
|
|
1462
1811
|
if (options.start > options.end) {
|
|
@@ -1472,8 +1821,8 @@ function getHistoryIflx2(adapter, msg) {
|
|
|
1472
1821
|
options.start = options.end - 86400000; // - 1 day
|
|
1473
1822
|
}
|
|
1474
1823
|
|
|
1475
|
-
// query one timegroup-value more
|
|
1476
|
-
// to make sure to have no 0 values because of the way InfluxDB
|
|
1824
|
+
// query one timegroup-value more than requested originally at start and end
|
|
1825
|
+
// to make sure to have no 0 values because of the way InfluxDB does group by time
|
|
1477
1826
|
if (options.aggregate !== 'onchange' && options.aggregate !== 'none' && options.aggregate !== 'minmax') {
|
|
1478
1827
|
if (!options.step) {
|
|
1479
1828
|
// calculate "step" based on difference between start and end using count
|
|
@@ -1484,8 +1833,8 @@ function getHistoryIflx2(adapter, msg) {
|
|
|
1484
1833
|
options.limit += 2;
|
|
1485
1834
|
}
|
|
1486
1835
|
|
|
1487
|
-
fluxQuery +=
|
|
1488
|
-
fluxQuery +=
|
|
1836
|
+
fluxQuery += ` |> range(${(options.start) ? "start: " + new Date(options.start).toISOString() + ", " : "start: -" + adapter.config.retention + "ms, "}stop: ${new Date(options.end).toISOString()})`;
|
|
1837
|
+
fluxQuery += ` |> filter(fn: (r) => r["_measurement"] == "${options.id}")`;
|
|
1489
1838
|
|
|
1490
1839
|
if (adapter.config.usetags)
|
|
1491
1840
|
fluxQuery += ' |> duplicate(column: "_value", as: "value")';
|
|
@@ -1493,100 +1842,98 @@ function getHistoryIflx2(adapter, msg) {
|
|
|
1493
1842
|
fluxQuery += ' |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")';
|
|
1494
1843
|
|
|
1495
1844
|
if (!options.start && (options.count || options.limit)) {
|
|
1496
|
-
fluxQuery +=
|
|
1845
|
+
fluxQuery += ` |> sort(columns:["_time"], desc: true)`;
|
|
1497
1846
|
}
|
|
1498
1847
|
|
|
1499
1848
|
if (options.aggregate !== 'onchange' && options.aggregate !== 'none' && options.aggregate !== 'minmax') {
|
|
1500
1849
|
if ((options.step !== null) && (options.step > 0))
|
|
1501
|
-
fluxQuery +=
|
|
1502
|
-
fluxQuery +=
|
|
1850
|
+
fluxQuery += ` |> window(every: ${options.step}ms)`;
|
|
1851
|
+
fluxQuery += `|> fill(column: "${valueColumn}", usePrevious: true)`;
|
|
1503
1852
|
} else if (options.aggregate !== 'minmax') {
|
|
1504
|
-
fluxQuery +=
|
|
1853
|
+
fluxQuery += ` |> group() |> limit(n: ${options.count})`;
|
|
1505
1854
|
}
|
|
1506
1855
|
|
|
1507
|
-
//Workaround to detect if measurement is of type bool (to skip non-
|
|
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: "
|
|
1511
|
-
|> range(
|
|
1512
|
-
|> filter(fn: (r) => r["_measurement"] == "
|
|
1513
|
-
|
|
1856
|
+
// Workaround to detect if measurement is of type bool (to skip non-sensual aggregation options)
|
|
1857
|
+
// There seems to be no officially supported way to detect this, so we check it by forcing a type-conflict
|
|
1858
|
+
const booleanTypeCheckQuery = `
|
|
1859
|
+
from(bucket: "${adapter.config.dbname}")
|
|
1860
|
+
|> range(${(options.start) ? `start: ${new Date(options.start).toISOString()}, ` : `start: -${adapter.config.retention}ms, `}stop: ${new Date(options.end).toISOString()})
|
|
1861
|
+
|> filter(fn: (r) => r["_measurement"] == "${options.id}" and contains(value: r._value, set: [true, false]))
|
|
1862
|
+
`;
|
|
1514
1863
|
|
|
1515
1864
|
adapter._client.query(booleanTypeCheckQuery, (error, _rslt) => {
|
|
1516
1865
|
let isBoolean;
|
|
1517
1866
|
if (error) {
|
|
1518
|
-
if (error.message.match('.*type conflict: bool.*'))
|
|
1867
|
+
if (error.message.match('.*type conflict: bool.*')) {
|
|
1519
1868
|
isBoolean = false;
|
|
1520
|
-
else {
|
|
1521
|
-
adapter.sendTo(msg.from, msg.command, {
|
|
1869
|
+
} else {
|
|
1870
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
1522
1871
|
result: [],
|
|
1523
1872
|
error: error,
|
|
1524
1873
|
sessionId: options.sessionId
|
|
1525
1874
|
}, msg.callback);
|
|
1526
|
-
return;
|
|
1527
1875
|
}
|
|
1528
1876
|
} else {
|
|
1529
1877
|
isBoolean = true;
|
|
1530
1878
|
adapter.log.debug(`Measurement ${options.id} is of type Boolean - skipping aggregation options`);
|
|
1531
1879
|
}
|
|
1532
1880
|
|
|
1533
|
-
|
|
1534
1881
|
if (options.step && !isBoolean) {
|
|
1535
1882
|
|
|
1536
1883
|
switch (options.aggregate) {
|
|
1537
1884
|
case 'average':
|
|
1538
|
-
fluxQuery +=
|
|
1885
|
+
fluxQuery += ` |> mean(column: "${valueColumn}")`;
|
|
1539
1886
|
break;
|
|
1540
1887
|
|
|
1541
1888
|
case 'max':
|
|
1542
|
-
fluxQuery +=
|
|
1889
|
+
fluxQuery += ` |> max(column: "${valueColumn}")`;
|
|
1543
1890
|
break;
|
|
1544
1891
|
|
|
1545
1892
|
case 'min':
|
|
1546
|
-
fluxQuery +=
|
|
1893
|
+
fluxQuery += ` |> min(column: "${valueColumn}")`;
|
|
1547
1894
|
break;
|
|
1548
1895
|
|
|
1549
1896
|
case 'total':
|
|
1550
|
-
fluxQuery +=
|
|
1897
|
+
fluxQuery += ` |> sum(column: "${valueColumn}")`;
|
|
1551
1898
|
break;
|
|
1552
1899
|
|
|
1553
1900
|
case 'count':
|
|
1554
|
-
fluxQuery +=
|
|
1901
|
+
fluxQuery += ` |> count(column: "${valueColumn}")`;
|
|
1555
1902
|
break;
|
|
1556
1903
|
|
|
1557
1904
|
default:
|
|
1558
|
-
fluxQuery +=
|
|
1905
|
+
fluxQuery += ` |> mean(column: "${valueColumn}")`;
|
|
1559
1906
|
break;
|
|
1560
1907
|
}
|
|
1561
1908
|
}
|
|
1562
1909
|
|
|
1563
1910
|
fluxQueries.push(fluxQuery);
|
|
1564
1911
|
|
|
1565
|
-
// select one datapoint more
|
|
1912
|
+
// select one datapoint more than wanted
|
|
1566
1913
|
if (options.aggregate === 'minmax' || options.aggregate === 'onchange' || options.aggregate === 'none') {
|
|
1567
1914
|
let addFluxQuery = '';
|
|
1568
1915
|
if (options.start) {
|
|
1569
1916
|
// get one entry "before" the defined timeframe for displaying purposes
|
|
1570
|
-
addFluxQuery =
|
|
1571
|
-
|> range(start:
|
|
1572
|
-
|> filter(fn: (r) => r["_measurement"] == "
|
|
1573
|
-
|
|
1574
|
-
|> sort(columns: ["_time"], desc: true)
|
|
1575
|
-
|> group()
|
|
1576
|
-
|> limit(n: 1)
|
|
1917
|
+
addFluxQuery = `from(bucket: "${adapter.config.dbname}")
|
|
1918
|
+
|> range(start: ${new Date(options.start - (adapter.config.retention || 31536000) * 1000).toISOString()}, stop: ${new Date(options.start).toISOString()})
|
|
1919
|
+
|> filter(fn: (r) => r["_measurement"] == "${options.id}")
|
|
1920
|
+
${(!adapter.config.usetags) ? '|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")' : ''}
|
|
1921
|
+
|> sort(columns: ["_time"], desc: true)
|
|
1922
|
+
|> group()
|
|
1923
|
+
|> limit(n: 1)`;
|
|
1577
1924
|
|
|
1578
1925
|
const mainQuery = fluxQueries.pop();
|
|
1579
1926
|
fluxQueries.push(addFluxQuery);
|
|
1580
1927
|
fluxQueries.push(mainQuery);
|
|
1581
1928
|
}
|
|
1582
1929
|
// get one entry "after" the defined timeframe for displaying purposes
|
|
1583
|
-
addFluxQuery =
|
|
1584
|
-
|> range(start:
|
|
1585
|
-
|> filter(fn: (r) => r["_measurement"] == "
|
|
1586
|
-
|
|
1587
|
-
|> sort(columns: ["_time"], desc: false)
|
|
1588
|
-
|> group()
|
|
1589
|
-
|> limit(n: 1)
|
|
1930
|
+
addFluxQuery = `from(bucket: "${adapter.config.dbname}")
|
|
1931
|
+
|> range(start: ${new Date(options.end).toISOString()})
|
|
1932
|
+
|> filter(fn: (r) => r["_measurement"] == "${options.id}")
|
|
1933
|
+
${(!adapter.config.usetags) ? '|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")' : ''}
|
|
1934
|
+
|> sort(columns: ["_time"], desc: false)
|
|
1935
|
+
|> group()
|
|
1936
|
+
|> limit(n: 1)`;
|
|
1590
1937
|
//fluxQuery = fluxQuery + addFluxQuery;
|
|
1591
1938
|
fluxQueries.push(addFluxQuery);
|
|
1592
1939
|
}
|
|
@@ -1603,8 +1950,11 @@ function getHistoryIflx2(adapter, msg) {
|
|
|
1603
1950
|
} else {
|
|
1604
1951
|
setConnected(adapter, true);
|
|
1605
1952
|
}
|
|
1953
|
+
|
|
1606
1954
|
adapter.log.debug('Parsing retrieved rows:' + JSON.stringify(rows));
|
|
1955
|
+
|
|
1607
1956
|
let result = [];
|
|
1957
|
+
|
|
1608
1958
|
if (rows && rows.length) {
|
|
1609
1959
|
for (let qr = 0; qr < rows.length; qr++) {
|
|
1610
1960
|
for (let rr = 0; rr < rows[qr].length; rr++) {
|
|
@@ -1612,8 +1962,11 @@ function getHistoryIflx2(adapter, msg) {
|
|
|
1612
1962
|
rows[qr][rr].val = rows[qr][rr].value;
|
|
1613
1963
|
delete rows[qr][rr].value;
|
|
1614
1964
|
}
|
|
1615
|
-
|
|
1965
|
+
|
|
1966
|
+
rows[qr][rr].ts = new Date(rows[qr][rr].time).getTime();
|
|
1967
|
+
|
|
1616
1968
|
delete rows[qr][rr].time;
|
|
1969
|
+
|
|
1617
1970
|
if (rows[qr][rr].val !== null) {
|
|
1618
1971
|
const f = parseFloat(rows[qr][rr].val);
|
|
1619
1972
|
if (f == rows[qr][rr].val) {
|
|
@@ -1623,24 +1976,17 @@ function getHistoryIflx2(adapter, msg) {
|
|
|
1623
1976
|
}
|
|
1624
1977
|
}
|
|
1625
1978
|
}
|
|
1626
|
-
|
|
1979
|
+
|
|
1980
|
+
if (options.addId) {
|
|
1981
|
+
rows[qr][rr].id = msg.message.id;
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1627
1984
|
result.push(rows[qr][rr]);
|
|
1628
1985
|
}
|
|
1629
1986
|
}
|
|
1630
1987
|
}
|
|
1631
1988
|
|
|
1632
|
-
|
|
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);
|
|
1989
|
+
Aggregate.sendResponse(adapter, msg, options, (error ? error.toString() : null) || result, startTime);
|
|
1644
1990
|
});
|
|
1645
1991
|
});
|
|
1646
1992
|
}
|
|
@@ -1736,9 +2082,9 @@ function multiQuery(adapter, msg) {
|
|
|
1736
2082
|
}
|
|
1737
2083
|
adapter.log.error('queries: ' + err);
|
|
1738
2084
|
return adapter.sendTo(msg.from, msg.command, {
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
2085
|
+
result: [],
|
|
2086
|
+
error: 'Invalid call'
|
|
2087
|
+
}, msg.callback);
|
|
1742
2088
|
} else {
|
|
1743
2089
|
setConnected(adapter, true);
|
|
1744
2090
|
}
|
|
@@ -1768,42 +2114,6 @@ function multiQuery(adapter, msg) {
|
|
|
1768
2114
|
}
|
|
1769
2115
|
}
|
|
1770
2116
|
|
|
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
2117
|
function enableHistory(adapter, msg) {
|
|
1808
2118
|
if (!msg.message || !msg.message.id) {
|
|
1809
2119
|
adapter.log.error('enableHistory called with invalid data');
|
|
@@ -1841,17 +2151,13 @@ function disableHistory(adapter, msg) {
|
|
|
1841
2151
|
obj.common.custom = {};
|
|
1842
2152
|
obj.common.custom[adapter.namespace] = {};
|
|
1843
2153
|
obj.common.custom[adapter.namespace].enabled = false;
|
|
1844
|
-
adapter.extendForeignObject(msg.message.id, obj,
|
|
1845
|
-
if (
|
|
1846
|
-
adapter.log.error('disableHistory: ' +
|
|
1847
|
-
adapter.sendTo(msg.from, msg.command, {
|
|
1848
|
-
error: err
|
|
1849
|
-
}, msg.callback);
|
|
2154
|
+
adapter.extendForeignObject(msg.message.id, obj, error => {
|
|
2155
|
+
if (error) {
|
|
2156
|
+
adapter.log.error('disableHistory: ' + error);
|
|
2157
|
+
adapter.sendTo(msg.from, msg.command, {error}, msg.callback);
|
|
1850
2158
|
} else {
|
|
1851
2159
|
adapter.log.info(JSON.stringify(obj));
|
|
1852
|
-
adapter.sendTo(msg.from, msg.command, {
|
|
1853
|
-
success: true
|
|
1854
|
-
}, msg.callback);
|
|
2160
|
+
adapter.sendTo(msg.from, msg.command, {success: true}, msg.callback);
|
|
1855
2161
|
}
|
|
1856
2162
|
});
|
|
1857
2163
|
}
|