iobroker.sql 4.0.4 → 4.1.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/README.md +92 -10
- package/admin/custom/assets/Components-nTLElZ7y.js +1 -0
- package/admin/custom/assets/DataBrowser-CyjKP_L-.js +1 -0
- package/admin/custom/assets/_virtual_mf-localSharedImportMap___mfe_internal__SqlComponentsSet__mf_owner__1-BKlHk-3o.js +1 -0
- package/admin/custom/assets/_virtual_mf___mfe_internal__SqlComponentsSet__mf_owner__1__loadShare___mf_0_iobroker_mf_1_gui_mf_2_components__loadShare__.js-1_ecMmwM.js +1243 -0
- package/admin/custom/assets/_virtual_mf___mfe_internal__SqlComponentsSet__mf_owner__1__loadShare___mf_0_iobroker_mf_1_json_mf_2_config__loadShare__.js-BJcL2b_b.js +1494 -0
- package/admin/custom/assets/_virtual_mf___mfe_internal__SqlComponentsSet__mf_owner__1__loadShare__react_mf_2_dom_mf_1_client__loadShare__.js-DOWbo5mU.js +9 -0
- package/admin/custom/assets/hostInit-CJsaB1F7.js +1 -0
- package/admin/custom/assets/index-D4ovp6PK.js +1 -0
- package/admin/custom/assets/lib-B-9Z42f0.js +3 -0
- package/admin/custom/assets/mf-entry-bootstrap-0-9382e792.js +41 -0
- package/admin/custom/assets/rolldown-runtime-C0FnF6B9.js +1 -0
- package/admin/custom/assets/virtualExposes-C4J_LR3_.js +1 -0
- package/admin/custom/assets/virtual_mf-REMOTE_ENTRY_ID___mfe_internal__SqlComponentsSet__customComponents_js-C7xpzcn4.js +1 -0
- package/admin/custom/assets/virtual_mf-exposes___mfe_internal__SqlComponentsSet__customComponents_js-DBI8_iGe.js +2 -0
- package/admin/custom/assets/vite-preload-helper-B7qeedMF.js +1 -0
- package/admin/custom/customComponents.js +12 -0
- package/admin/custom/i18n/de.json +30 -0
- package/admin/custom/i18n/en.json +30 -0
- package/admin/custom/i18n/es.json +30 -0
- package/admin/custom/i18n/fr.json +30 -0
- package/admin/custom/i18n/it.json +30 -0
- package/admin/custom/i18n/nl.json +30 -0
- package/admin/custom/i18n/pl.json +30 -0
- package/admin/custom/i18n/pt.json +30 -0
- package/admin/custom/i18n/ru.json +30 -0
- package/admin/custom/i18n/zh-cn.json +30 -0
- package/admin/custom/mf-manifest.json +1 -0
- package/admin/i18n/{de/translations.json → de.json} +7 -1
- package/admin/i18n/{en/translations.json → en.json} +7 -1
- package/admin/i18n/{es/translations.json → es.json} +7 -1
- package/admin/i18n/{fr/translations.json → fr.json} +7 -1
- package/admin/i18n/{it/translations.json → it.json} +7 -1
- package/admin/i18n/{nl/translations.json → nl.json} +7 -1
- package/admin/i18n/{pl/translations.json → pl.json} +7 -1
- package/admin/i18n/{pt/translations.json → pt.json} +7 -1
- package/admin/i18n/{ru/translations.json → ru.json} +7 -1
- package/admin/i18n/{zh-cn/translations.json → zh-cn.json} +7 -1
- package/admin/jsonConfig.json +14 -0
- package/admin/jsonCustom.json +18 -0
- package/build/lib/mssql.js +22 -0
- package/build/lib/mssql.js.map +1 -1
- package/build/lib/mysql.js +22 -0
- package/build/lib/mysql.js.map +1 -1
- package/build/lib/postgresql.js +22 -0
- package/build/lib/postgresql.js.map +1 -1
- package/build/lib/sqlite.js +22 -0
- package/build/lib/sqlite.js.map +1 -1
- package/build/main.js +308 -83
- package/build/main.js.map +1 -1
- package/io-package.json +27 -27
- package/package.json +14 -7
- package/build/lib/aggregate.js +0 -1490
- package/build/lib/aggregate.js.map +0 -1
package/build/main.js
CHANGED
|
@@ -35,7 +35,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.SqlAdapter = void 0;
|
|
37
37
|
const adapter_core_1 = require("@iobroker/adapter-core"); // Get common adapter utils
|
|
38
|
-
const aggregate_1 = require("
|
|
38
|
+
const aggregate_1 = require("@iobroker/aggregate");
|
|
39
39
|
const node_fs_1 = require("node:fs");
|
|
40
40
|
const node_path_1 = require("node:path");
|
|
41
41
|
const MSSQL = __importStar(require("./lib/mssql"));
|
|
@@ -62,6 +62,8 @@ const SQLFuncs = {
|
|
|
62
62
|
getHistory: MSSQL.getHistory,
|
|
63
63
|
deleteFromTable: MSSQL.deleteFromTable,
|
|
64
64
|
update: MSSQL.update,
|
|
65
|
+
getRawEntries: MSSQL.getRawEntries,
|
|
66
|
+
getRawEntriesCount: MSSQL.getRawEntriesCount,
|
|
65
67
|
},
|
|
66
68
|
mysql: {
|
|
67
69
|
init: MySQL.init,
|
|
@@ -78,6 +80,8 @@ const SQLFuncs = {
|
|
|
78
80
|
getHistory: MySQL.getHistory,
|
|
79
81
|
deleteFromTable: MySQL.deleteFromTable,
|
|
80
82
|
update: MySQL.update,
|
|
83
|
+
getRawEntries: MySQL.getRawEntries,
|
|
84
|
+
getRawEntriesCount: MySQL.getRawEntriesCount,
|
|
81
85
|
},
|
|
82
86
|
postgresql: {
|
|
83
87
|
init: PostgreSQL.init,
|
|
@@ -94,6 +98,8 @@ const SQLFuncs = {
|
|
|
94
98
|
getHistory: PostgreSQL.getHistory,
|
|
95
99
|
deleteFromTable: PostgreSQL.deleteFromTable,
|
|
96
100
|
update: PostgreSQL.update,
|
|
101
|
+
getRawEntries: PostgreSQL.getRawEntries,
|
|
102
|
+
getRawEntriesCount: PostgreSQL.getRawEntriesCount,
|
|
97
103
|
},
|
|
98
104
|
sqlite: {
|
|
99
105
|
init: SQLite.init,
|
|
@@ -110,6 +116,8 @@ const SQLFuncs = {
|
|
|
110
116
|
getHistory: SQLite.getHistory,
|
|
111
117
|
deleteFromTable: SQLite.deleteFromTable,
|
|
112
118
|
update: SQLite.update,
|
|
119
|
+
getRawEntries: SQLite.getRawEntries,
|
|
120
|
+
getRawEntriesCount: SQLite.getRawEntriesCount,
|
|
113
121
|
},
|
|
114
122
|
};
|
|
115
123
|
const clients = {
|
|
@@ -165,6 +173,8 @@ function isEqual(a, b) {
|
|
|
165
173
|
return true;
|
|
166
174
|
}
|
|
167
175
|
const MAX_TASKS = 100;
|
|
176
|
+
/** Maximal number of rows one `getRawEntries` call may return */
|
|
177
|
+
const MAX_RAW_ENTRIES = 2000;
|
|
168
178
|
function sortByTs(a, b) {
|
|
169
179
|
const aTs = a.ts;
|
|
170
180
|
const bTs = b.ts;
|
|
@@ -1200,6 +1210,12 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1200
1210
|
else if (msg.command === 'storeState') {
|
|
1201
1211
|
this.storeState(msg).catch(e => this.log.error(`Cannot store state: ${e}`));
|
|
1202
1212
|
}
|
|
1213
|
+
else if (msg.command === 'getRawEntries') {
|
|
1214
|
+
this.getRawEntries(msg);
|
|
1215
|
+
}
|
|
1216
|
+
else if (msg.command === 'getDatapoints') {
|
|
1217
|
+
this.getDatapoints(msg);
|
|
1218
|
+
}
|
|
1203
1219
|
else if (msg.command === 'getDpOverview') {
|
|
1204
1220
|
this.getDpOverview(msg);
|
|
1205
1221
|
}
|
|
@@ -1720,7 +1736,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1720
1736
|
if (err || !client) {
|
|
1721
1737
|
this.returnClientToPool(client);
|
|
1722
1738
|
this.log.error(err?.toString() || 'No client');
|
|
1723
|
-
cb?.();
|
|
1739
|
+
cb?.(err || new Error('No client'));
|
|
1724
1740
|
}
|
|
1725
1741
|
else {
|
|
1726
1742
|
client.execute(query, (err) => {
|
|
@@ -1728,7 +1744,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1728
1744
|
if (err) {
|
|
1729
1745
|
this.log.error(`Cannot query ${query}: ${err} (id: ${id})`);
|
|
1730
1746
|
}
|
|
1731
|
-
cb?.();
|
|
1747
|
+
cb?.(err);
|
|
1732
1748
|
});
|
|
1733
1749
|
}
|
|
1734
1750
|
});
|
|
@@ -2081,8 +2097,8 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2081
2097
|
if (this.tasks.length) {
|
|
2082
2098
|
if (this.tasks[0].operation === 'query') {
|
|
2083
2099
|
const taskQuery = this.tasks[0];
|
|
2084
|
-
this._executeQuery(taskQuery.query, this.tasks[0].id,
|
|
2085
|
-
taskQuery.callback?.();
|
|
2100
|
+
this._executeQuery(taskQuery.query, this.tasks[0].id, err => {
|
|
2101
|
+
taskQuery.callback?.(err);
|
|
2086
2102
|
this.tasks.shift();
|
|
2087
2103
|
this.lockTasks = false;
|
|
2088
2104
|
if (this.tasks.length) {
|
|
@@ -2778,32 +2794,42 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2778
2794
|
}
|
|
2779
2795
|
}
|
|
2780
2796
|
}
|
|
2781
|
-
if (
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
cb?.(new Error(error));
|
|
2793
|
-
}
|
|
2794
|
-
else {
|
|
2795
|
-
this.tasks.push({ operation: 'query', query, id, callback: cb });
|
|
2796
|
-
this.tasks.length === 1 && this.processTasks();
|
|
2797
|
-
}
|
|
2797
|
+
if (found) {
|
|
2798
|
+
cb?.();
|
|
2799
|
+
return;
|
|
2800
|
+
}
|
|
2801
|
+
if (!this.sqlDPs[id]) {
|
|
2802
|
+
// Logging is not (or no longer) enabled for this ID, so neither the type nor the index are known
|
|
2803
|
+
// in RAM. Read both from the `datapoints` table, so stored values can still be changed.
|
|
2804
|
+
this.#readIdIndexAndType(id, (err, index, type) => {
|
|
2805
|
+
if (err || index === undefined || type === undefined) {
|
|
2806
|
+
cb?.(err || new Error(`Unknown ID: ${id}`));
|
|
2807
|
+
return;
|
|
2798
2808
|
}
|
|
2799
|
-
|
|
2800
|
-
|
|
2809
|
+
// the source is only known if it was used before, otherwise `_from` stays unchanged
|
|
2810
|
+
if (state.from && this.from[state.from] === undefined) {
|
|
2811
|
+
return this.getFrom(state.from, err => {
|
|
2812
|
+
if (err) {
|
|
2813
|
+
this.log.warn(`Cannot get "from" for "${state.from}": ${err}`);
|
|
2814
|
+
}
|
|
2815
|
+
this.#updateInDb(id, dbNames[type], index, state, cb);
|
|
2816
|
+
});
|
|
2801
2817
|
}
|
|
2818
|
+
this.#updateInDb(id, dbNames[type], index, state, cb);
|
|
2802
2819
|
});
|
|
2820
|
+
return;
|
|
2803
2821
|
}
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2822
|
+
this.prepareTaskCheckTypeAndDbId(id, state, false, (err) => {
|
|
2823
|
+
if (err) {
|
|
2824
|
+
return cb?.(err);
|
|
2825
|
+
}
|
|
2826
|
+
this.#updateInDb(id, dbNames[this.sqlDPs[id].type], this.sqlDPs[id].index, state, cb);
|
|
2827
|
+
});
|
|
2828
|
+
}
|
|
2829
|
+
/** Build and execute the UPDATE query for one entry of one datapoint */
|
|
2830
|
+
#updateInDb(id, table, index, state, cb) {
|
|
2831
|
+
const query = this.sqlFuncs.update(this.config.dbname, index, state, this.from[state.from], table);
|
|
2832
|
+
this.#executeQueries(id, [query], cb);
|
|
2807
2833
|
}
|
|
2808
2834
|
#delete(id, state, cb) {
|
|
2809
2835
|
// first try to find the value in not yet saved data
|
|
@@ -2840,41 +2866,114 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2840
2866
|
}
|
|
2841
2867
|
}
|
|
2842
2868
|
}
|
|
2843
|
-
if (
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2869
|
+
if (found) {
|
|
2870
|
+
cb?.();
|
|
2871
|
+
return;
|
|
2872
|
+
}
|
|
2873
|
+
if (!this.sqlDPs[id]) {
|
|
2874
|
+
// Logging is not (or no longer) enabled for this ID, so neither the type nor the index are known
|
|
2875
|
+
// in RAM. Read both from the `datapoints` table, so the already stored values can still be deleted.
|
|
2876
|
+
this.#readIdIndexAndType(id, (err, index, type) => {
|
|
2877
|
+
if (err || index === undefined || type === undefined) {
|
|
2878
|
+
cb?.(err || new Error(`Unknown ID: ${id}`));
|
|
2879
|
+
return;
|
|
2852
2880
|
}
|
|
2853
|
-
|
|
2854
|
-
|
|
2881
|
+
this.#deleteFromDb(id, dbNames[type], index, state, cb);
|
|
2882
|
+
});
|
|
2883
|
+
return;
|
|
2884
|
+
}
|
|
2885
|
+
this.prepareTaskCheckTypeAndDbId(id, state, false, err => {
|
|
2886
|
+
if (err) {
|
|
2887
|
+
return cb?.(err);
|
|
2888
|
+
}
|
|
2889
|
+
this.#deleteFromDb(id, dbNames[this.sqlDPs[id].type], this.sqlDPs[id].index, state, cb);
|
|
2890
|
+
});
|
|
2891
|
+
}
|
|
2892
|
+
/**
|
|
2893
|
+
* Read the index and the stored data type of one datapoint from the `datapoints` table.
|
|
2894
|
+
*
|
|
2895
|
+
* In contrast to `getId`, this never creates or modifies anything and it works for IDs that are not
|
|
2896
|
+
* (or no longer) enabled for logging, i.e., that are not in `sqlDPs`.
|
|
2897
|
+
*/
|
|
2898
|
+
#readIdIndexAndType(id, cb) {
|
|
2899
|
+
if (!this.clientPool) {
|
|
2900
|
+
this.log.warn('No Connection to database');
|
|
2901
|
+
setImmediate(() => cb(new Error('No Connection to database')));
|
|
2902
|
+
return;
|
|
2903
|
+
}
|
|
2904
|
+
const query = this.sqlFuncs.getIdSelect(this.config.dbname, id);
|
|
2905
|
+
this.log.debug(query);
|
|
2906
|
+
this.borrowClientFromPool((err, client) => {
|
|
2907
|
+
if (err || !client) {
|
|
2908
|
+
this.returnClientToPool(client);
|
|
2909
|
+
cb(err || new Error('No client'));
|
|
2910
|
+
return;
|
|
2911
|
+
}
|
|
2912
|
+
client.execute(query, (err, rows) => {
|
|
2913
|
+
this.returnClientToPool(client);
|
|
2914
|
+
if (err) {
|
|
2915
|
+
this.log.error(`Cannot select ${query}: ${err}`);
|
|
2916
|
+
cb(err);
|
|
2855
2917
|
}
|
|
2856
|
-
else {
|
|
2857
|
-
|
|
2858
|
-
query = this.sqlFuncs.deleteFromTable(this.config.dbname, dbNames[type], this.sqlDPs[id].index);
|
|
2918
|
+
else if (!rows?.length) {
|
|
2919
|
+
cb(new Error(`Unknown ID: ${id}`));
|
|
2859
2920
|
}
|
|
2860
|
-
if (!
|
|
2861
|
-
|
|
2862
|
-
const error = `Cannot queue new requests, because more than ${MAX_TASKS}`;
|
|
2863
|
-
this.log.error(error);
|
|
2864
|
-
cb?.(new Error(error));
|
|
2865
|
-
}
|
|
2866
|
-
else {
|
|
2867
|
-
this.tasks.push({ operation: 'query', query, id, callback: cb });
|
|
2868
|
-
this.tasks.length === 1 && this.processTasks();
|
|
2869
|
-
}
|
|
2921
|
+
else if (typeof rows[0].type !== 'number' || !dbNames[rows[0].type]) {
|
|
2922
|
+
cb(new Error(`No valid data type stored for ${id}: ${rows[0].type}`));
|
|
2870
2923
|
}
|
|
2871
2924
|
else {
|
|
2872
|
-
|
|
2925
|
+
cb(null, rows[0].id, rows[0].type);
|
|
2873
2926
|
}
|
|
2874
2927
|
});
|
|
2928
|
+
});
|
|
2929
|
+
}
|
|
2930
|
+
/** Build and execute the DELETE query for one datapoint */
|
|
2931
|
+
#deleteFromDb(id, table, index, state, cb) {
|
|
2932
|
+
const queries = [this.#buildDeleteQuery(table, index, state)];
|
|
2933
|
+
// the counter values of a numeric datapoint are stored in an own table (like in `checkRetention`)
|
|
2934
|
+
if (table === 'ts_number') {
|
|
2935
|
+
queries.push(this.#buildDeleteQuery('ts_counter', index, state));
|
|
2875
2936
|
}
|
|
2876
|
-
|
|
2937
|
+
this.#executeQueries(id, queries, cb);
|
|
2938
|
+
}
|
|
2939
|
+
/** Build the DELETE query for one table of one datapoint */
|
|
2940
|
+
#buildDeleteQuery(table, index, state) {
|
|
2941
|
+
if (state.start && state.end) {
|
|
2942
|
+
return this.sqlFuncs.deleteFromTable(this.config.dbname, table, index, state.start, state.end);
|
|
2943
|
+
}
|
|
2944
|
+
if (state.ts) {
|
|
2945
|
+
return this.sqlFuncs.deleteFromTable(this.config.dbname, table, index, state.ts);
|
|
2946
|
+
}
|
|
2947
|
+
// delete all entries for ID
|
|
2948
|
+
return this.sqlFuncs.deleteFromTable(this.config.dbname, table, index);
|
|
2949
|
+
}
|
|
2950
|
+
/** Execute the given queries one after another and stop at the first error */
|
|
2951
|
+
#executeQueries(id, queries, cb) {
|
|
2952
|
+
const query = queries.shift();
|
|
2953
|
+
if (!query) {
|
|
2877
2954
|
cb?.();
|
|
2955
|
+
return;
|
|
2956
|
+
}
|
|
2957
|
+
const next = (err) => {
|
|
2958
|
+
if (err || !queries.length) {
|
|
2959
|
+
cb?.(err);
|
|
2960
|
+
return;
|
|
2961
|
+
}
|
|
2962
|
+
this.#executeQueries(id, queries, cb);
|
|
2963
|
+
};
|
|
2964
|
+
if (!this.multiRequests) {
|
|
2965
|
+
if (this.tasks.length > MAX_TASKS) {
|
|
2966
|
+
const error = `Cannot queue new requests, because more than ${MAX_TASKS}`;
|
|
2967
|
+
this.log.error(error);
|
|
2968
|
+
cb?.(new Error(error));
|
|
2969
|
+
}
|
|
2970
|
+
else {
|
|
2971
|
+
this.tasks.push({ operation: 'query', query, id, callback: next });
|
|
2972
|
+
this.tasks.length === 1 && this.processTasks();
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
else {
|
|
2976
|
+
this._executeQuery(query, id, next);
|
|
2878
2977
|
}
|
|
2879
2978
|
}
|
|
2880
2979
|
updateState(msg) {
|
|
@@ -2912,10 +3011,9 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2912
3011
|
else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
|
|
2913
3012
|
this.log.debug('updateState 1 item');
|
|
2914
3013
|
id = this.aliasMap[msg.message.id] ? this.aliasMap[msg.message.id] : msg.message.id;
|
|
2915
|
-
return this.update(id, msg.message.state,
|
|
2916
|
-
|
|
2917
|
-
sqlConnected: !!this.clientPool,
|
|
2918
|
-
}, msg.callback));
|
|
3014
|
+
return this.update(id, msg.message.state, err => this.sendTo(msg.from, msg.command, err
|
|
3015
|
+
? { error: err.message, sqlConnected: !!this.clientPool }
|
|
3016
|
+
: { success: true, sqlConnected: !!this.clientPool }, msg.callback));
|
|
2919
3017
|
}
|
|
2920
3018
|
else {
|
|
2921
3019
|
this.log.error('updateState called with invalid data');
|
|
@@ -3021,18 +3119,16 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3021
3119
|
else if (msg.message.id && msg.message.state && typeof msg.message.state === 'object') {
|
|
3022
3120
|
this.log.debug('deleteHistoryEntry 1 item');
|
|
3023
3121
|
id = this.aliasMap[msg.message.id] ? this.aliasMap[msg.message.id] : msg.message.id;
|
|
3024
|
-
return this.#delete(id, { ts: msg.message.state.ts },
|
|
3025
|
-
|
|
3026
|
-
sqlConnected: !!this.clientPool,
|
|
3027
|
-
}, msg.callback));
|
|
3122
|
+
return this.#delete(id, { ts: msg.message.state.ts }, err => this.sendTo(msg.from, msg.command, err
|
|
3123
|
+
? { error: err.message, sqlConnected: !!this.clientPool }
|
|
3124
|
+
: { success: true, sqlConnected: !!this.clientPool }, msg.callback));
|
|
3028
3125
|
}
|
|
3029
3126
|
else if (msg.message.id && msg.message.ts && typeof msg.message.ts === 'number') {
|
|
3030
3127
|
this.log.debug('deleteHistoryEntry 1 item');
|
|
3031
3128
|
id = this.aliasMap[msg.message.id] ? this.aliasMap[msg.message.id] : msg.message.id;
|
|
3032
|
-
return this.#delete(id, { ts: msg.message.ts },
|
|
3033
|
-
|
|
3034
|
-
sqlConnected: !!this.clientPool,
|
|
3035
|
-
}, msg.callback));
|
|
3129
|
+
return this.#delete(id, { ts: msg.message.ts }, err => this.sendTo(msg.from, msg.command, err
|
|
3130
|
+
? { error: err.message, sqlConnected: !!this.clientPool }
|
|
3131
|
+
: { success: true, sqlConnected: !!this.clientPool }, msg.callback));
|
|
3036
3132
|
}
|
|
3037
3133
|
else {
|
|
3038
3134
|
this.log.error('deleteHistoryEntry called with invalid data');
|
|
@@ -3061,10 +3157,9 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3061
3157
|
else if (msg.message.id) {
|
|
3062
3158
|
this.log.debug('deleteStateAll 1 item');
|
|
3063
3159
|
id = this.aliasMap[msg.message.id] ? this.aliasMap[msg.message.id] : msg.message.id;
|
|
3064
|
-
return this.#delete(id, {},
|
|
3065
|
-
|
|
3066
|
-
sqlConnected: !!this.clientPool,
|
|
3067
|
-
}, msg.callback));
|
|
3160
|
+
return this.#delete(id, {}, err => this.sendTo(msg.from, msg.command, err
|
|
3161
|
+
? { error: err.message, sqlConnected: !!this.clientPool }
|
|
3162
|
+
: { success: true, sqlConnected: !!this.clientPool }, msg.callback));
|
|
3068
3163
|
}
|
|
3069
3164
|
else {
|
|
3070
3165
|
this.log.error('deleteStateAll called with invalid data');
|
|
@@ -3087,20 +3182,36 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3087
3182
|
this.sqlDPs[id].realId = id;
|
|
3088
3183
|
}
|
|
3089
3184
|
return new Promise((resolve, reject) => {
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3185
|
+
const push = () => {
|
|
3186
|
+
if (applyRules) {
|
|
3187
|
+
this.pushHistory(id, state);
|
|
3188
|
+
resolve(true);
|
|
3189
|
+
}
|
|
3190
|
+
else {
|
|
3191
|
+
this.pushHelper(id, state, err => {
|
|
3192
|
+
if (err) {
|
|
3193
|
+
reject(new Error(`Error writing state for ${id}: ${err.message}, Data: ${JSON.stringify(state)}`));
|
|
3194
|
+
}
|
|
3195
|
+
else {
|
|
3196
|
+
resolve(true);
|
|
3197
|
+
}
|
|
3198
|
+
});
|
|
3199
|
+
}
|
|
3200
|
+
};
|
|
3201
|
+
// Without a configuration, the type would be guessed from the value and could then even overwrite
|
|
3202
|
+
// the type in the `datapoints` table. Use the stored type of an already known datapoint instead.
|
|
3203
|
+
if (!this.sqlDPs[id].config && this.sqlDPs[id].type === undefined) {
|
|
3204
|
+
this.#readIdIndexAndType(id, (err, index, type) => {
|
|
3205
|
+
if (!err && index !== undefined && type !== undefined) {
|
|
3206
|
+
this.sqlDPs[id].index = index;
|
|
3207
|
+
this.sqlDPs[id].type = type;
|
|
3208
|
+
this.sqlDPs[id].dbType = type;
|
|
3209
|
+
}
|
|
3210
|
+
push();
|
|
3102
3211
|
});
|
|
3212
|
+
return;
|
|
3103
3213
|
}
|
|
3214
|
+
push();
|
|
3104
3215
|
});
|
|
3105
3216
|
}
|
|
3106
3217
|
async storeState(msg) {
|
|
@@ -3170,6 +3281,110 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3170
3281
|
sqlConnected: !!this.clientPool,
|
|
3171
3282
|
}, msg.callback);
|
|
3172
3283
|
}
|
|
3284
|
+
/**
|
|
3285
|
+
* Read the stored values of one datapoint page by page.
|
|
3286
|
+
*
|
|
3287
|
+
* In contrast to `getHistory`, the rows are returned exactly as they are in the database: no aggregation,
|
|
3288
|
+
* no interpolation, no border values and no rounding. Together with the total number of matching entries,
|
|
3289
|
+
* so that a table can page through them. Works for datapoints that are not (or no longer) enabled for
|
|
3290
|
+
* logging too.
|
|
3291
|
+
*/
|
|
3292
|
+
getRawEntries(msg) {
|
|
3293
|
+
if (!msg.message?.id) {
|
|
3294
|
+
this.log.error('getRawEntries called with invalid data');
|
|
3295
|
+
return this.sendTo(msg.from, msg.command, { error: `Invalid call: ${JSON.stringify(msg)}` }, msg.callback);
|
|
3296
|
+
}
|
|
3297
|
+
const toTs = (value) => {
|
|
3298
|
+
if (value === undefined || value === null || value === '') {
|
|
3299
|
+
return undefined;
|
|
3300
|
+
}
|
|
3301
|
+
const ts = typeof value === 'number' ? value : new Date(value).getTime();
|
|
3302
|
+
return isFinite(ts) ? ts : undefined;
|
|
3303
|
+
};
|
|
3304
|
+
const id = this.aliasMap[msg.message.id] || msg.message.id;
|
|
3305
|
+
const options = {
|
|
3306
|
+
start: toTs(msg.message.start),
|
|
3307
|
+
end: toTs(msg.message.end),
|
|
3308
|
+
limit: Math.min(Math.max(parseInt(msg.message.limit, 10) || 100, 1), MAX_RAW_ENTRIES),
|
|
3309
|
+
offset: Math.max(parseInt(msg.message.offset, 10) || 0, 0),
|
|
3310
|
+
sort: msg.message.sort === 'asc' ? 'asc' : 'desc',
|
|
3311
|
+
};
|
|
3312
|
+
this.#readIdIndexAndType(id, (err, index, type) => {
|
|
3313
|
+
if (err || index === undefined || type === undefined) {
|
|
3314
|
+
return this.sendTo(msg.from, msg.command, { error: (err || new Error(`Unknown ID: ${id}`)).message }, msg.callback);
|
|
3315
|
+
}
|
|
3316
|
+
const table = dbNames[type];
|
|
3317
|
+
this.borrowClientFromPool((err, client) => {
|
|
3318
|
+
if (err || !client) {
|
|
3319
|
+
this.returnClientToPool(client);
|
|
3320
|
+
return this.sendTo(msg.from, msg.command, { error: (err || new Error('No client')).message }, msg.callback);
|
|
3321
|
+
}
|
|
3322
|
+
const countQuery = this.sqlFuncs.getRawEntriesCount(this.config.dbname, table, index, options);
|
|
3323
|
+
this.log.debug(countQuery);
|
|
3324
|
+
client.execute(countQuery, (err, rows) => {
|
|
3325
|
+
if (err) {
|
|
3326
|
+
this.returnClientToPool(client);
|
|
3327
|
+
this.log.error(`Cannot select ${countQuery}: ${err}`);
|
|
3328
|
+
return this.sendTo(msg.from, msg.command, { error: err.message }, msg.callback);
|
|
3329
|
+
}
|
|
3330
|
+
// PostgreSQL returns the count of a bigint column as string
|
|
3331
|
+
const total = rows?.[0] ? parseInt(rows[0].total, 10) || 0 : 0;
|
|
3332
|
+
const query = this.sqlFuncs.getRawEntries(this.config.dbname, table, index, options);
|
|
3333
|
+
this.log.debug(query);
|
|
3334
|
+
client.execute(query, (err, rows) => {
|
|
3335
|
+
this.returnClientToPool(client);
|
|
3336
|
+
if (err) {
|
|
3337
|
+
this.log.error(`Cannot select ${query}: ${err}`);
|
|
3338
|
+
return this.sendTo(msg.from, msg.command, { error: err.message }, msg.callback);
|
|
3339
|
+
}
|
|
3340
|
+
this.sendTo(msg.from, msg.command, {
|
|
3341
|
+
id: msg.message.id,
|
|
3342
|
+
index,
|
|
3343
|
+
type: storageTypes[type],
|
|
3344
|
+
table,
|
|
3345
|
+
total,
|
|
3346
|
+
limit: options.limit,
|
|
3347
|
+
offset: options.offset,
|
|
3348
|
+
sort: options.sort,
|
|
3349
|
+
result: rows || [],
|
|
3350
|
+
}, msg.callback);
|
|
3351
|
+
});
|
|
3352
|
+
});
|
|
3353
|
+
});
|
|
3354
|
+
});
|
|
3355
|
+
}
|
|
3356
|
+
/**
|
|
3357
|
+
* Return all datapoints that have an entry in the `datapoints` table - no matter whether their logging is
|
|
3358
|
+
* still enabled or not.
|
|
3359
|
+
*
|
|
3360
|
+
* In contrast to `getDpOverview`, this is only one SELECT and answers immediately: `getDpOverview`
|
|
3361
|
+
* additionally determines the first timestamp of every datapoint and pauses 5 seconds between the types.
|
|
3362
|
+
*/
|
|
3363
|
+
getDatapoints(msg) {
|
|
3364
|
+
const query = this.sqlFuncs.getIdSelect(this.config.dbname);
|
|
3365
|
+
this.log.debug(query);
|
|
3366
|
+
this.borrowClientFromPool((err, client) => {
|
|
3367
|
+
if (err || !client) {
|
|
3368
|
+
this.returnClientToPool(client);
|
|
3369
|
+
return this.sendTo(msg.from, msg.command, { error: (err || new Error('No client')).message }, msg.callback);
|
|
3370
|
+
}
|
|
3371
|
+
client.execute(query, (err, rows) => {
|
|
3372
|
+
this.returnClientToPool(client);
|
|
3373
|
+
if (err) {
|
|
3374
|
+
this.log.error(`Cannot select ${query}: ${err}`);
|
|
3375
|
+
return this.sendTo(msg.from, msg.command, { error: err.message }, msg.callback);
|
|
3376
|
+
}
|
|
3377
|
+
const result = (rows || [])
|
|
3378
|
+
.map(row => ({
|
|
3379
|
+
id: row.name,
|
|
3380
|
+
index: row.id,
|
|
3381
|
+
type: typeof row.type === 'number' ? storageTypes[row.type] : null,
|
|
3382
|
+
}))
|
|
3383
|
+
.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0));
|
|
3384
|
+
this.sendTo(msg.from, msg.command, { success: true, result }, msg.callback);
|
|
3385
|
+
});
|
|
3386
|
+
});
|
|
3387
|
+
}
|
|
3173
3388
|
getDpOverview(msg) {
|
|
3174
3389
|
const result = [];
|
|
3175
3390
|
const query = this.sqlFuncs.getIdSelect(this.config.dbname);
|
|
@@ -3528,7 +3743,18 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3528
3743
|
await this.createUserInDocker();
|
|
3529
3744
|
}
|
|
3530
3745
|
if (config.dbtype === 'sqlite' || this.config.host) {
|
|
3531
|
-
this.connect(() => {
|
|
3746
|
+
this.connect(async () => {
|
|
3747
|
+
// `enableHistory` only writes the object and leaves the activation to `objectChange`, so
|
|
3748
|
+
// subscribing after the view was read left a gap: a message that arrived in between was
|
|
3749
|
+
// answered with `success: true` and then silently lost, because nothing delivered its
|
|
3750
|
+
// object change. Registering the subscription *before* the view is read closes the gap -
|
|
3751
|
+
// every write now either lands in the snapshot below or arrives as an object change.
|
|
3752
|
+
try {
|
|
3753
|
+
await this.subscribeForeignObjectsAsync('*');
|
|
3754
|
+
}
|
|
3755
|
+
catch (e) {
|
|
3756
|
+
this.log.error(`Cannot subscribe to objects: ${e}`);
|
|
3757
|
+
}
|
|
3532
3758
|
// read all custom settings
|
|
3533
3759
|
this.getObjectView('system', 'custom', {}, (err, doc) => {
|
|
3534
3760
|
let count = 0;
|
|
@@ -3604,7 +3830,6 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3604
3830
|
this.subscribeAll = true;
|
|
3605
3831
|
this.subscribeForeignStates('*');
|
|
3606
3832
|
}
|
|
3607
|
-
this.subscribeForeignObjects('*');
|
|
3608
3833
|
this.log.debug('Initialization done');
|
|
3609
3834
|
this.setConnected(true);
|
|
3610
3835
|
this.processStartValues();
|