feat: improve MQTT reconnection handling + document arrival notifier
- Add keepalive (60s), reconnectPeriod (5s), connectTimeout (30s) - Add offline/close event handlers with proper logging - Document Tesla Arrival Notifier setup in README - Document systemd service installation
This commit is contained in:
@@ -188,12 +188,17 @@ async function main() {
|
||||
username: config.mqtt.username,
|
||||
password: config.mqtt.password,
|
||||
rejectUnauthorized: true,
|
||||
keepalive: 60, // Send ping every 60s
|
||||
reconnectPeriod: 5000, // Retry every 5s on disconnect
|
||||
connectTimeout: 30000, // 30s connection timeout
|
||||
};
|
||||
|
||||
const client = mqtt.connect(config.mqtt.url, mqttOptions);
|
||||
const prefix = `teslamate/cars/${config.carId}`;
|
||||
let isConnected = false;
|
||||
|
||||
client.on('connect', () => {
|
||||
isConnected = true;
|
||||
log.info('Connected to MQTT');
|
||||
|
||||
// Subscribe to relevant topics
|
||||
@@ -235,7 +240,22 @@ async function main() {
|
||||
});
|
||||
|
||||
client.on('error', (err) => log.error('MQTT error:', err.message));
|
||||
client.on('reconnect', () => log.info('Reconnecting...'));
|
||||
|
||||
client.on('reconnect', () => {
|
||||
log.info('Reconnecting to MQTT...');
|
||||
});
|
||||
|
||||
client.on('close', () => {
|
||||
if (isConnected) {
|
||||
log.info('MQTT connection closed');
|
||||
isConnected = false;
|
||||
}
|
||||
});
|
||||
|
||||
client.on('offline', () => {
|
||||
log.info('MQTT client offline');
|
||||
isConnected = false;
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
process.on('SIGINT', () => {
|
||||
|
||||
Reference in New Issue
Block a user