forked from blabond/ioBroker.maxxi-charge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
159 lines (132 loc) · 4.8 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
const { determineRole } = require('./roles');
const FORBIDDEN_CHARS = /[^a-zA-Z0-9_-]/g;
function name2id(pName) {
return (pName || '').replace(FORBIDDEN_CHARS, '_');
}
async function ensureStateExists(adapter, stateCache, statePath, obj) {
const parentPath = statePath.substring(0, statePath.lastIndexOf('.'));
// Überprüfe und erstelle den übergeordneten Ordner
if (parentPath && !stateCache.has(parentPath)) {
const isDevice = parentPath.split('.').pop().startsWith('maxxi-');
const parentType = isDevice ? 'device' : 'channel';
await adapter.setObjectNotExists(parentPath, {
type: parentType,
common: { name: '' },
native: {},
});
stateCache.add(parentPath);
}
// Überprüfe und erstelle den aktuellen State oder Ordner
if (!stateCache.has(statePath)) {
await adapter.setObjectNotExists(statePath, obj);
stateCache.add(statePath);
}
}
async function getActiveDeviceId(adapter) {
const aktivState = await adapter.getStateAsync('info.aktivCCU');
if (!aktivState || !aktivState.val) {
return null;
}
const deviceId = aktivState.val.split(',')[0].trim(); // Nur den ersten Teil des Strings
if (!deviceId || deviceId === 'null') {
adapter.log.warn(`getActiveDeviceId: Invalid deviceId found: ${deviceId}`);
return null;
}
return deviceId;
}
function getDateValue(date) {
return date?.month * 100 + date?.day;
}
async function processNestedData(adapter, basePath, data, stateCache) {
const folderTypes = ['batteriesInfo', 'convertersInfo']; // Definition innerhalb der Funktion
for (const key in data) {
if (!Object.hasOwn(data, key)) {
continue;
}
const value = data[key];
const safeId = name2id(key);
const stateId = `${basePath}.${safeId}`;
// Bestimme den Typ basierend auf dem Filter
const objectType = folderTypes.includes(key) ? 'folder' : 'channel';
if (typeof value === 'object' && value !== null) {
// Sicherstellen, dass der Ordner existiert
await ensureStateExists(adapter, stateCache, stateId, {
type: objectType,
common: { name: key },
native: {},
});
// Rekursiv die nächsten Ebenen verarbeiten
await processNestedData(adapter, stateId, value, stateCache);
} else {
const role = determineRole(key);
await ensureStateExists(adapter, stateCache, stateId, {
type: 'state',
common: {
name: key,
type: typeof value,
role,
read: true,
write: false,
},
native: {},
});
await adapter.setStateAsync(stateId, { val: value, ack: true });
}
}
}
async function applySocValue(adapter, deviceId, value, type) {
const datapoint = `${adapter.namespace}.${deviceId}.sendcommand.${type}`;
try {
await adapter.setStateAsync(datapoint, { val: value, ack: false });
} catch (err) {
adapter.log.error(`applySocValue failed for ${datapoint}: ${err.message}`);
}
}
function validateInterval(value, min = 1000, max = 3600000) {
if (typeof value !== 'number' || isNaN(value)) {
return min;
}
if (value < min) {
return min;
}
if (value > max) {
return max;
}
return value;
}
async function changeSettingAkku(adapter, batteryCalibration, calibrationProgress) {
try {
const adapterConfigPath = `system.adapter.${adapter.namespace}`;
// Lade die Adapterkonfiguration
const obj = await adapter.getForeignObjectAsync(adapterConfigPath);
if (!obj) {
adapter.log.error(`Adapter configuration not found for: ${adapterConfigPath}`);
return;
}
// Ändere die gewünschten Werte
if (typeof batteryCalibration === 'boolean') {
obj.native.batterycalibration = batteryCalibration;
}
if (calibrationProgress === 'down' || calibrationProgress === 'up') {
obj.native.calibrationProgress = calibrationProgress;
}
// Schreibe die Änderungen zurück
await adapter.setForeignObject(adapterConfigPath, obj);
adapter.log.debug(
`Successfully updated batterycalibration to ${batteryCalibration} and calibrationProgress to ${calibrationProgress}.`,
);
} catch (error) {
adapter.log.error(`Error in changeSettingAkku: ${error.message}`);
}
}
module.exports = {
name2id,
ensureStateExists,
getActiveDeviceId,
processNestedData,
getDateValue,
applySocValue,
validateInterval,
changeSettingAkku,
};