forked from LRuesink-WebArray/homey-matic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
120 lines (102 loc) · 3.93 KB
/
app.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
'use strict';
const Homey = require('homey');
const HomeMaticDiscovery = require('./lib/HomeMaticDiscovery');
const HomeMaticCCUJack = require('./lib/HomeMaticCCUJack');
const Constants = require('./lib/constants');
const Logger = require('./lib/logger');
const connTypeCCUJack = 'use_ccu_jack';
class Homematic extends Homey.App {
async onInit() {
this.logger = new Logger(this.homey);
this.logger.log('info', 'Started homematic...');
try {
const address = await this.homey.cloud.getLocalAddress();
this.homeyIP = address.split(':')[0];
this.settings = this.getSettings();
this.discovery = new HomeMaticDiscovery(this.logger, this.homey);
this.bridges = {};
const storedBridges = this.getStoredBridges();
if (Object.keys(storedBridges).length > 0) {
this.logger.log('info', 'Initializing stored bridges...');
await this.initializeStoredBridges(storedBridges);
} else {
await this.discovery.discover();
}
} catch (err) {
this.logger.log('error', 'Initialization failed:', err);
}
}
async onUninit() {
this.logger.log('info', 'Unloading homematic app...');
const cleanupPromises = Object.values(this.bridges).map(bridge => bridge.cleanup?.());
await Promise.all(cleanupPromises);
this.logger.log('info', 'All bridges cleaned up.');
}
getSettings() {
return {
"ccu_jack_mqtt_port": this.homey.settings.get('ccu_jack_mqtt_port'),
"ccu_jack_user": this.homey.settings.get('ccu_jack_user'),
"ccu_jack_password": this.homey.settings.get('ccu_jack_password'),
"use_stored_bridges": this.homey.settings.get('use_stored_bridges'),
};
}
getStoredBridges() {
const bridges = {};
this.homey.settings.getKeys().forEach((key) => {
if (key.startsWith(Constants.SETTINGS_PREFIX_BRIDGE)) {
const bridge = this.homey.settings.get(key);
bridges[bridge.serial] = bridge;
}
});
return bridges;
}
async initializeStoredBridges(bridges) {
for (const serial of Object.keys(bridges)) {
const bridge = bridges[serial];
this.logger.log('info', "Initializing stored CCU:", "Type", bridge.type, "Serial", bridge.serial, "IP", bridge.address);
await this.initializeBridge(bridge);
}
}
getConnectionType() {
return connTypeCCUJack;
}
async initializeBridge(bridge) {
try {
const connType = this.getConnectionType();
this.logger.log('info', 'Connection type:', connType);
this.logger.log('info', "Initializing CCU Jack");
const bridgeInstance = new HomeMaticCCUJack(
this.logger,
this.homey,
bridge.type,
bridge.serial,
bridge.address,
this.settings.ccu_jack_mqtt_port,
this.settings.ccu_jack_user,
this.settings.ccu_jack_password,
);
this.bridges[bridge.serial] = bridgeInstance;
return bridgeInstance;
} catch (err) {
this.logger.log('error', `Failed to initialize bridge ${bridge.serial}:`, err);
}
}
setBridgeAddress(serial, address) {
const bridge = this.bridges[serial];
if (bridge) {
bridge.address = address;
} else {
this.logger.log('error', `No bridge found for serial: ${serial}`);
}
}
deleteStoredBridges() {
const bridges = this.getStoredBridges();
bridges.forEach((serial) => {
this.homey.settings.unset(Constants.SETTINGS_PREFIX_BRIDGE + serial);
});
}
getLogLines() {
return this.logger.getLogLines();
}
}
module.exports = Homematic;