homebridge-evn-smartmeter 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mqtt/mqttClient.js +19 -14
- package/dist/platform.js +37 -17
- package/package.json +1 -1
package/dist/mqtt/mqttClient.js
CHANGED
|
@@ -30,20 +30,25 @@ class EVNMQTTClient extends events_1.EventEmitter {
|
|
|
30
30
|
* Connect to MQTT broker
|
|
31
31
|
*/
|
|
32
32
|
connect() {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
try {
|
|
34
|
+
const brokerUrl = `mqtt://${this.broker}:${this.port}`;
|
|
35
|
+
const options = {
|
|
36
|
+
username: this.username,
|
|
37
|
+
password: this.password,
|
|
38
|
+
reconnectPeriod: 1000, // Start reconnect attempts after 1 second
|
|
39
|
+
connectTimeout: 10000, // 10 second connection timeout
|
|
40
|
+
rejectUnauthorized: false,
|
|
41
|
+
};
|
|
42
|
+
this.client = mqtt_1.default.connect(brokerUrl, options);
|
|
43
|
+
this.client.on('connect', () => this.handleConnect());
|
|
44
|
+
this.client.on('message', (topic, message) => this.handleMessage(topic, message));
|
|
45
|
+
this.client.on('error', (error) => this.handleError(error));
|
|
46
|
+
this.client.on('offline', () => this.handleOffline());
|
|
47
|
+
this.client.on('reconnect', () => this.handleReconnect());
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
this.emit('error', new Error(`Failed to create MQTT client: ${error}`));
|
|
51
|
+
}
|
|
47
52
|
}
|
|
48
53
|
/**
|
|
49
54
|
* Disconnect from MQTT broker
|
package/dist/platform.js
CHANGED
|
@@ -25,6 +25,7 @@ class EVNSmartmeterPlatform {
|
|
|
25
25
|
// Validate configuration
|
|
26
26
|
if (!this.config.mqtt?.broker) {
|
|
27
27
|
this.log.error('MQTT broker not configured! Please configure mqtt.broker in config.json');
|
|
28
|
+
this.log.error('Plugin will not be initialized. Please add MQTT configuration to config.json');
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
30
31
|
this.log.info('EVN Smartmeter platform loaded');
|
|
@@ -33,12 +34,22 @@ class EVNSmartmeterPlatform {
|
|
|
33
34
|
// in order to ensure they weren't added to homebridge already. This event can also be used
|
|
34
35
|
// to start discovery of new accessories.
|
|
35
36
|
this.api.on('didFinishLaunching', () => {
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
try {
|
|
38
|
+
this.log.info('Finished launching, initializing platform');
|
|
39
|
+
this.initializePlatform();
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
this.log.error('Failed to initialize platform:', error);
|
|
43
|
+
}
|
|
38
44
|
});
|
|
39
45
|
// Clean up on shutdown
|
|
40
46
|
this.api.on('shutdown', () => {
|
|
41
|
-
|
|
47
|
+
try {
|
|
48
|
+
this.cleanup();
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
this.log.error('Error during cleanup:', error);
|
|
52
|
+
}
|
|
42
53
|
});
|
|
43
54
|
}
|
|
44
55
|
/**
|
|
@@ -96,21 +107,30 @@ class EVNSmartmeterPlatform {
|
|
|
96
107
|
* Register an accessory with Homebridge
|
|
97
108
|
*/
|
|
98
109
|
registerAccessory(name, AccessoryClass) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
110
|
+
try {
|
|
111
|
+
if (!this.mqttClient) {
|
|
112
|
+
this.log.error(`Cannot register accessory ${name}: MQTT client not initialized`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const uuid = this.api.hap.uuid.generate(name);
|
|
116
|
+
const existingAccessory = this.accessories.find((acc) => acc.UUID === uuid);
|
|
117
|
+
if (existingAccessory) {
|
|
118
|
+
// Accessory already exists, restore it
|
|
119
|
+
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
|
|
120
|
+
new AccessoryClass(this, existingAccessory, this.mqttClient);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// Create new accessory
|
|
124
|
+
this.log.info('Adding new accessory:', name);
|
|
125
|
+
const accessory = new this.api.platformAccessory(name, uuid);
|
|
126
|
+
new AccessoryClass(this, accessory, this.mqttClient);
|
|
127
|
+
// Register the accessory
|
|
128
|
+
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
|
|
129
|
+
this.accessories.push(accessory);
|
|
130
|
+
}
|
|
105
131
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
this.log.info('Adding new accessory:', name);
|
|
109
|
-
const accessory = new this.api.platformAccessory(name, uuid);
|
|
110
|
-
new AccessoryClass(this, accessory, this.mqttClient);
|
|
111
|
-
// Register the accessory
|
|
112
|
-
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
|
|
113
|
-
this.accessories.push(accessory);
|
|
132
|
+
catch (error) {
|
|
133
|
+
this.log.error(`Failed to register accessory ${name}:`, error);
|
|
114
134
|
}
|
|
115
135
|
}
|
|
116
136
|
/**
|
package/package.json
CHANGED