I have this code:
import Homey from 'homey';
import mqtt from 'mqtt';
class MqttSmartBOB extends Homey.Driver {
private devices: any[]; // Deklaracja właściwości devices
private client: mqtt.MqttClient | null; // Deklaracja właściwości client
constructor() {
super();
this.log('Initializing MqttSmartBOB driver');
this.devices = []; // Inicjalizacja devices
this.client = null; // Inicjalizacja client
}
/**
* onInit is called when the driver is initialized.
*/
async onInit() {
this.log('MQTT Driver has been initialized');
// Połączenie MQTT
this.client = mqtt.connect('mqtts://something.eu.hivemq.cloud:8883', {
username: 'log', // Zmień na swoje dane
password: 'pass' // Zmień na swoje dane
});
// Obsługa połączenia
this.client.on('connect', () => {
this.log('MQTT connected');
if (this.client) {
this.client.subscribe('home/discover', (err) => {
if (!err) {
this.log('Subscribed to home/discover');
} else {
this.error('Failed to subscribe to home/discover:', err);
}
});
}
});
// Obsługa błędów
this.client.on('error', (err) => {
this.error('MQTT error:', err);
});
// Obsługa zamknięcia
this.client.on('close', () => {
this.log('MQTT connection closed');
});
// Obsługa wiadomości
this.client.on('message', (topic, message) => {
this.log(`Received message on topic ${topic}: ${message.toString()}`);
if (topic === 'home/discover') {
let payload;
try {
payload = JSON.parse(message.toString());
} catch (e) {
this.error('Failed to parse message:', e);
return;
}
this.log(`Received device discovery message: ${JSON.stringify(payload)}`);
if (payload) {
this.addDevice(payload);
} else {
this.error('Payload is undefined');
}
}
});
}
async addDevice(payload: { id: string; name: string; type: string }) {
try {
const { id, name, type } = payload;
this.log(`Attempting to add device with ID: ${id}, Name: ${name}, Type: ${type}`);
// Sprawdzenie, czy urządzenie już istnieje
const driverInstance = this.homey.drivers.getDriver('mqtt-driver');
const devices = await driverInstance.getDevices();
if (devices.some(device => device.getData().id === id)) {
this.log(`Device ${id} already exists`);
return;
} else {
this.devices.push({
name: `${name}`,
capabilities: ['measure_temperature'],
icon: '/assets/thermometer-temperature.svg',
data: {
id: `${id}`,
},
});
this.log(`Device ${id} added successfully`);
}
} catch (error) {
// Rzutowanie error na Error
if (error instanceof Error) {
this.error(`Error adding device: ${error.message}`);
} else {
this.error(`Unexpected error: ${error}`);
}
}
}
/**
* onPairListDevices is called when a user is adding a device and the 'list_devices' view is called.
* This should return an array with the data of devices that are available for pairing.
*/
async onPairListDevices() {
return this.devices;
}
}
module.exports = MqttSmartBOB;
When I compile have error:
[err] [ManagerDrivers] Error Initializing Driver smartbob-mqtt: TypeError: Cannot read properties of undefined (reading 'discovery')
at new Driver (/node_modules/@athombv/homey-apps-sdk-v3/lib/Driver.js:75:25)
at new MqttSmartBOB (/app/drivers/smartbob-mqtt/driver.js:10:9)
at /node_modules/@athombv/homey-apps-sdk-v3/manager/drivers.js:81:35
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Where is problem?