-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.js
128 lines (111 loc) · 4.33 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
121
122
123
124
125
126
127
128
'use strict';
const Homey = require('homey');
const HomeMaticDiscovery = require('./lib/HomeMaticDiscovery');
const HomeMaticCCUMQTT = require('./lib/HomeMaticCCUMQTT');
const HomeMaticCCURPC = require('./lib/HomeMaticCCURPC');
const HomeMaticCCUJack = require('./lib/HomeMaticCCUJack');
const Constants = require('./lib/constants');
const Logger = require('./lib/logger')
const connTypeRPC = 'use_rpc'
const connTypeMQTT = 'use_mqtt'
const connTypeCCUJack = 'use_ccu_jack'
class Homematic extends Homey.App {
async onInit() {
this.logger = new Logger(this.homey);
this.logger.log('info', 'Started homematic...');
var self = this;
let address = await this.homey.cloud.getLocalAddress()
self.homeyIP = address.split(':')[0]
self.settings = self.getSettings();
self.discovery = new HomeMaticDiscovery(this.logger, this.homey);
self.bridges = {};
if (this.homey.app.settings.use_stored_bridges) {
self.initializeStoredBridges();
} else {
await self.discovery.discover()
}
}
getSettings() {
return {
"use_mqtt": this.homey.settings.get('use_mqtt'),
"connection_type": this.homey.settings.get('connection_type'),
"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() {
var self = this;
var bridges = {};
this.homey.settings.getKeys().forEach((key) => {
if (key.startsWith(Constants.SETTINGS_PREFIX_BRIDGE)) {
let bridge = this.homey.settings.get(key);
bridges[bridge.serial] = bridge
}
})
return bridges
}
initializeStoredBridges() {
var self = this;
var bridges = this.getStoredBridges();
Object.keys(bridges).forEach((serial) => {
let bridge = bridges[serial];
self.logger.log('info', "Initializing stored ccu:", "Type", bridge.type, "Serial", bridge.serial, "IP", bridge.address);
self.initializeBridge(bridge)
});
}
getConnectionType() {
if (this.homey.app.settings.connection_type) {
return this.homey.app.settings.connection_type
}
if (this.homey.app.settings.use_mqtt === true) {
return connTypeMQTT
}
return connTypeRPC
}
initializeBridge(bridge) {
let self = this;
let connType = self.getConnectionType()
self.logger.log('info', 'Connection type:', connType)
switch (connType) {
case connTypeRPC:
self.logger.log('info', "Initializing RPC CCU");
self.bridges[bridge.serial] = new HomeMaticCCURPC(self.logger, self.homey, bridge.type, bridge.serial, bridge.address);
break;
case connTypeMQTT:
self.logger.log('info', "Initializing MQTT CCU ");
self.bridges[bridge.serial] = new HomeMaticCCUMQTT(self.logger, self.homey, bridge.type, bridge.serial, bridge.address);
break;
case connTypeCCUJack:
self.logger.log('info', "Initializing CCU Jack");
self.bridges[bridge.serial] = new HomeMaticCCUJack(
self.logger,
self.homey,
bridge.type,
bridge.serial,
bridge.address,
this.homey.app.settings.ccu_jack_mqtt_port,
this.homey.app.settings.ccu_jack_user,
this.homey.app.settings.ccu_jack_password,
);
break;
}
return self.bridges[bridge.serial]
}
setBridgeAddress(serial, address) {
var self = this;
self.bridges[serial].address = address;
}
deleteStoredBridges() {
var self = this;
var bridges = this.getStoredBridges()
Object.keys(bridges).forEach((serial) => {
this.homey.settings.unset(Constants.SETTINGS_PREFIX_BRIDGE + serial)
})
}
getLogLines() {
return this.logger.getLogLines();
}
}
module.exports = Homematic;