-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·435 lines (371 loc) · 15.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// main.js
"use strict";
const utils = require("@iobroker/adapter-core");
const CorrentlyClient = require('corrently-api');
class EnergySchedule extends utils.Adapter {
constructor(options) {
super({
...options,
name: "energy-schedule",
});
this.scheduleCheckInterval = null;
this.currentSchedule = null;
this.client = null;
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("unload", this.onUnload.bind(this));
}
async onReady() {
try {
this.log.info("Starting energy-schedule adapter...");
// Set connection state to false at startup
await this.setStateAsync('info.connection', { val: false, ack: true });
this.log.debug(`Config values: ${JSON.stringify(this.config)}`);
// Validate ZIP code
if (!this.config.zip) {
this.log.error("No ZIP code configured!");
return;
}
// Initialize client configuration
const clientConfig = {
appid: "0xb61DD55F0DDA7C17975a82dd18EAeD8C025a64ea",
zip: this.config.zip // Add ZIP to client config
};
if (this.config.personalToken) {
clientConfig.token = this.config.personalToken;
this.log.debug("Using personal token from config");
} else {
this.log.info("No personal token configured, using default token");
}
try {
// Initialize Corrently client
this.log.debug(`Initializing Corrently client with config: ${JSON.stringify(clientConfig)}`);
this.client = new CorrentlyClient(clientConfig);
// Test the connection with correct method and ZIP
const testConnection = await this.client.strommix.getEnergyMix({
zip: this.config.zip,
timeframe: "last7days"
});
if (!testConnection) {
throw new Error("No response from API");
}
this.log.debug(`API Test response received: ${JSON.stringify(testConnection)}`);
// Mark connection as established
await this.setStateAsync('info.connection', { val: true, ack: true });
this.log.info("Successfully connected to Corrently API");
// Create states after successful connection
await this.createStates();
// Start schedule check interval
this.scheduleCheckInterval = setInterval(() => {
this.checkScheduleStatus();
}, 60000); // Check every minute
// Initial schedule creation if requirements are set
await this.createInitialSchedule();
} catch (apiError) {
this.log.error(`Failed to connect to Corrently API: ${apiError.message}`);
if (apiError.response) {
this.log.error(`API Response: ${JSON.stringify(apiError.response.data)}`);
}
await this.setStateAsync('info.connection', { val: false, ack: true });
return;
}
} catch (error) {
this.log.error(`Initialization error: ${error.message}`);
if (error.stack) this.log.debug(`Stack: ${error.stack}`);
await this.setStateAsync('info.connection', { val: false, ack: true });
}
}
async createStates() {
try {
// Erstelle zuerst die Channels
await this.setObjectNotExistsAsync("requirements", {
type: "channel",
common: {
name: "Schedule Requirements"
},
native: {}
});
await this.setObjectNotExistsAsync("status", {
type: "channel",
common: {
name: "Schedule Status"
},
native: {}
});
await this.setObjectNotExistsAsync("control", {
type: "channel",
common: {
name: "Schedule Control"
},
native: {}
});
// Warte kurz
await new Promise(resolve => setTimeout(resolve, 1000));
// Erstelle dann die States
const states = {
"requirements.energyDemand": {
name: "Energy Demand",
type: "number",
role: "value",
unit: "kWh",
read: true,
write: true
},
"requirements.maxLoad": {
name: "Maximum Load",
type: "number",
role: "value",
unit: "W",
read: true,
write: true
},
"requirements.avgLoad": {
name: "Average Load",
type: "number",
role: "value",
unit: "W",
read: true,
write: true
},
"status.isActive": {
name: "Schedule Active",
type: "boolean",
role: "indicator",
read: true,
write: false
},
"status.nextSwitch": {
name: "Next Switch Time",
type: "string",
role: "text",
read: true,
write: false
},
"status.scheduleId": {
name: "Current Schedule ID",
type: "string",
role: "text",
read: true,
write: false
},
"status.scheduleDetails": {
name: "Complete Schedule Details",
type: "string",
role: "json",
read: true,
write: false
},
"control.createSchedule": {
name: "Create New Schedule",
type: "boolean",
role: "button",
read: false,
write: true
}
};
// Erstelle alle States
for (const [id, common] of Object.entries(states)) {
await this.setObjectNotExistsAsync(id, {
type: "state",
common: common,
native: {}
});
}
this.log.info("All states created successfully");
} catch (error) {
this.log.error(`Error creating states: ${error.message}`);
throw error;
}
}
async createInitialSchedule() {
try {
// Warte kurz, bis alle States erstellt sind
await new Promise(resolve => setTimeout(resolve, 2000));
this.log.info("Checking for existing requirements...");
const energyDemand = await this.getStateAsync("requirements.energyDemand");
const maxLoad = await this.getStateAsync("requirements.maxLoad");
const avgLoad = await this.getStateAsync("requirements.avgLoad");
// Prüfe, ob die States existieren UND Werte haben
if (energyDemand && maxLoad && avgLoad &&
energyDemand.val !== null &&
maxLoad.val !== null &&
avgLoad.val !== null) {
this.log.info(`Found existing requirements: Energy=${energyDemand.val}, Max=${maxLoad.val}, Avg=${avgLoad.val}`);
// Erstelle Schedule mit den gefundenen Werten
await this.createSchedule({
requirements: {
energyDemand: energyDemand.val,
maxLoad: maxLoad.val,
avgLoad: avgLoad.val
}
});
} else {
this.log.info("No complete requirements found for initial schedule");
// Setze Default-Werte
await this.setStateAsync("requirements.energyDemand", { val: 10, ack: true });
await this.setStateAsync("requirements.maxLoad", { val: 3500, ack: true });
await this.setStateAsync("requirements.avgLoad", { val: 2000, ack: true });
// Erstelle Schedule mit Default-Werten
await this.createSchedule({
requirements: {
energyDemand: 10,
maxLoad: 3500,
avgLoad: 2000
}
});
}
} catch (error) {
this.log.error(`Error in initial schedule creation: ${error.message}`);
if (error.stack) this.log.debug(`Stack: ${error.stack}`);
}
}
async createSchedule(requirements) {
try {
this.log.info(`Creating schedule with requirements: ${JSON.stringify(requirements)}`);
if (!this.config.zip) {
this.log.error("No ZIP code configured!");
return;
}
const mergedRequirements = {
zip: this.config.zip,
requirements: {
law: this.config.law || "comfort",
activeHours: this.config.activeHours || 1,
consecutiveHours: this.config.consecutiveHours !== false,
coverageHours: this.config.coverageHours || 23,
...requirements.requirements
}
};
this.log.debug(`Merged requirements: ${JSON.stringify(mergedRequirements)}`);
// Create new schedule
this.log.info('Calling API to create schedule...');
const newSchedule = await this.client.schedule.createSchedule(mergedRequirements);
this.log.info(`Schedule created with ID: ${newSchedule.scheduleId}`);
// Store scheduleId immediately
await this.setStateAsync("status.scheduleId", { val: newSchedule.scheduleId, ack: true });
// Get full schedule details
this.log.info('Fetching schedule details...');
this.currentSchedule = await this.client.schedule.getSchedule(newSchedule.scheduleId);
this.log.info(`Retrieved schedule details: ${JSON.stringify(this.currentSchedule)}`);
// Update all states
await this.updateScheduleStates();
} catch (error) {
this.log.error(`Schedule creation error: ${error.message}`);
if (error.response) {
this.log.error(`API Response: ${JSON.stringify(error.response.data)}`);
}
// Clear scheduleId on error
await this.setStateAsync("status.scheduleId", { val: null, ack: true });
}
}
// Verbesserte checkScheduleStatus Funktion
async checkScheduleStatus() {
if (!this.currentSchedule) {
this.log.debug('No schedule to check');
return;
}
try {
const scheduleId = this.currentSchedule.scheduleId;
this.log.debug(`Checking status for schedule ${scheduleId}`);
// Refresh schedule details
this.currentSchedule = await this.client.schedule.getSchedule(scheduleId);
await this.updateScheduleStates();
} catch (error) {
this.log.error(`Status check error: ${error.message}`);
this.currentSchedule = null;
await this.updateScheduleStates(); // This will clear all states
}
}
isCurrentlyActive(schedule) {
try {
// Prüfe ob schedule und seine Eigenschaften existieren
if (!schedule?.schedule?.currentRecommendation?.currentPowerState) {
this.log.warn('No currentPowerState found in currentRecommendation');
return false;
}
return schedule.schedule.currentRecommendation.currentPowerState === 'on';
} catch (error) {
this.log.error(`Error in isCurrentlyActive: ${error.message}`);
return false;
}
}
findNextSwitchTime(schedule) {
try {
// Prüfe ob schedule und seine Eigenschaften existieren
if (!schedule?.schedule?.currentRecommendation?.nextSwitch) {
this.log.warn('No nextSwitch found in currentRecommendation');
return new Date();
}
return new Date(schedule.schedule.currentRecommendation.nextSwitch);
} catch (error) {
this.log.error(`Error in findNextSwitchTime: ${error.message}`);
return new Date();
}
}
// Verbesserte updateScheduleStates Funktion
async updateScheduleStates() {
try {
if (!this.currentSchedule) {
this.log.warn('No current schedule available for status update');
await this.setStateAsync("status.isActive", { val: null, ack: true });
await this.setStateAsync("status.nextSwitch", { val: null, ack: true });
await this.setStateAsync("status.scheduleDetails", { val: null, ack: true });
return;
}
const isActive = this.isCurrentlyActive(this.currentSchedule);
const nextSwitch = this.findNextSwitchTime(this.currentSchedule);
this.log.debug(`Status update - isActive: ${isActive}, nextSwitch: ${nextSwitch}, currentPowerState: ${this.currentSchedule.schedule.currentRecommendation.currentPowerState}`);
const updatePromises = [
this.setStateAsync("status.isActive", { val: isActive, ack: true }),
this.setStateAsync("status.nextSwitch", { val: nextSwitch.toISOString(), ack: true }),
this.setStateAsync("status.scheduleDetails", {
val: JSON.stringify(this.currentSchedule),
ack: true
}),
this.setStateAsync("status.scheduleId", {
val: this.currentSchedule.scheduleId,
ack: true
})
];
await Promise.all(updatePromises);
this.log.debug('States updated successfully');
} catch (error) {
this.log.error(`Error updating states: ${error.message}`);
}
}
async onStateChange(id, state) {
if (state && !state.ack) {
if (id.endsWith("control.createSchedule") && state.val) {
const energyDemand = await this.getStateAsync("requirements.energyDemand");
const maxLoad = await this.getStateAsync("requirements.maxLoad");
const avgLoad = await this.getStateAsync("requirements.avgLoad");
if (energyDemand && maxLoad && avgLoad) {
await this.createSchedule({
requirements: {
energyDemand: energyDemand.val,
maxLoad: maxLoad.val,
avgLoad: avgLoad.val
}
});
}
}
}
}
onUnload(callback) {
try {
if (this.scheduleCheckInterval) {
clearInterval(this.scheduleCheckInterval);
}
callback();
} catch (e) {
callback();
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
module.exports = (options) => new EnergySchedule(options);
} else {
// otherwise start the instance directly
new EnergySchedule();
}