forked from blabond/ioBroker.maxxi-charge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
215 lines (182 loc) · 7.36 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict';
const utils = require('@iobroker/adapter-core');
const { validateInterval, getActiveDeviceId } = require('./utils'); // utils importieren
const Commands = require('./commands');
const LocalApi = require('./localApi');
const CloudApi = require('./cloudApi');
const EcoMode = require('./ecoMode');
const BatteryMode = require('./batteryMode');
class MaxxiCharge extends utils.Adapter {
constructor(options) {
super({
...options,
name: 'maxxi-charge',
});
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.activeDevices = {}; // Speichert aktive CCUs
this.commands = new Commands(this); // Initialisiere Commands
this.localApi = new LocalApi(this); // Initialisiere LocalApi
this.cloudApi = null; // Platzhalter für CloudApi, wird in onReady initialisiert
this.ecoMode = new EcoMode(this); // Initialisiere EcoMode
this.batteryMode = new BatteryMode(this);
this.maxxiccuname = ''; // Platzhalter, wird in onReady gesetzt
this.stateCache = new Set(); // Cache für vorhandene States
}
async onReady() {
try {
this.subscribeStates('info.connection');
// Setze info.connection und info.aktivCCU auf Standardwerte
await this.setObjectNotExistsAsync('info.connection', {
type: 'state',
common: {
name: {
en: 'Connection active',
de: 'Verbindung aktiv',
},
type: 'boolean',
role: 'indicator.connected',
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync('info.aktivCCU', {
type: 'state',
common: {
name: {
en: 'Active CCUs',
de: 'Aktive CCUs',
},
type: 'string',
role: 'text',
read: true,
write: false,
},
native: {},
});
// Initialisiere APIs basierend auf dem Modus
this.cloudApi = new CloudApi(this);
if (this.config.apimode === 'local') {
await this.localApi.init();
} else if (this.config.apimode === 'cloud') {
await this.cloudApi.init();
}
// Cleanup-Intervall
this.cleanupInterval = this.setInterval(() => this.cleanupActiveDevices(), validateInterval(30 * 1000));
// EcoMode initialisieren, falls aktiviert
if (this.config.enableseasonmode && this.config.batterycalibration === false) {
await this.ecoMode.init();
}
if (this.config.batterycalibration) {
await this.batteryMode.init();
}
} catch (error) {
this.log.error(`Fatal error during initialization: ${error.message}`);
}
}
async onStateChange(id, state) {
// this.log.debug(`State changed: ${id}, Value: ${state.val}, Ack: ${state.ack}`);
if (!state.ack) {
await this.commands.handleCommandChange(id, state);
} else {
if (id.endsWith('.SOC')) {
await this.ecoMode.handleSOCChange(id, state);
await this.batteryMode.handleCalibrationSOCChange(id, state);
}
if (id === `${this.namespace}.info.connection` && !state.val) {
this.ecoMode.cleanup();
const deviceId = await getActiveDeviceId(this);
if (deviceId) {
const socState = `${this.namespace}.${deviceId}.SOC`;
this.unsubscribeStates(socState);
this.log.debug(`Unsubscribed from dynamic state: ${socState}`);
}
}
}
}
async subscribeDynamicStates(deviceId) {
const socState = `${this.namespace}.${deviceId}.SOC`;
this.subscribeStates(socState);
}
async updateActiveCCU(deviceId) {
this.activeDevices[deviceId] = Date.now();
const keys = Object.keys(this.activeDevices);
const csv = keys.join(',');
// Zwischenspeicher für den letzten Zustand von `info.connection`
if (!this.lastConnectionState) {
this.lastConnectionState = false;
}
const isConnected = keys.length > 0;
if (this.lastConnectionState !== isConnected) {
// Zustand hat sich geändert, also aktualisieren
await this.setState('info.connection', { val: isConnected, ack: true });
this.lastConnectionState = isConnected;
if (isConnected) {
await this.subscribeDynamicStates(deviceId);
if (this.config.enableseasonmode && !this.ecoModeInitialized) {
this.ecoModeInitialized = true; // Sicherstellen, dass `EcoMode` nur einmal gestartet wird
await this.ecoMode.startMonitoring();
}
}
}
// `info.aktivCCU` immer aktualisieren
await this.setState('info.aktivCCU', { val: csv, ack: true });
}
async cleanupActiveDevices() {
const now = Date.now();
const fiveMinAgo = now - 90 * 1000;
for (const deviceId in this.activeDevices) {
if (this.activeDevices[deviceId] < fiveMinAgo) {
delete this.activeDevices[deviceId];
this.log.info(`Device ${deviceId} marked as inactive and removed.`);
}
}
const keys = Object.keys(this.activeDevices);
await this.setState('info.aktivCCU', { val: keys.join(','), ack: true });
await this.setState('info.connection', { val: keys.length > 0, ack: true });
}
async onUnload(callback) {
try {
// Prüfen, ob das Objekt vor dem Setzen existiert
const connectionObj = await this.getObjectAsync('info.connection');
if (connectionObj) {
await this.setState('info.connection', { val: false, ack: true });
}
const aktivCcuObj = await this.getObjectAsync('info.aktivCCU');
if (aktivCcuObj) {
await this.setState('info.aktivCCU', { val: '', ack: true });
}
// Andere Bereinigungen
if (this.commands) {
this.commands.cleanup();
}
if (this.ecoMode) {
this.ecoMode.cleanup();
}
if (this.batteryMode) {
this.batteryMode.cleanup();
}
if (this.localApi) {
this.localApi.cleanup();
}
if (this.cloudApi) {
this.cloudApi.cleanup();
}
// Timer/Intervalle entfernen
if (this.cleanupInterval) {
this.clearInterval(this.cleanupInterval);
}
callback();
} catch (e) {
this.log.error(`Error during shutdown: ${e.message}`);
callback();
}
}
}
if (require.main !== module) {
module.exports = options => new MaxxiCharge(options);
} else {
new MaxxiCharge();
}