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:
Clawd
2026-01-29 08:44:48 +00:00
parent 97f8eee507
commit 1c38e729f3
2 changed files with 93 additions and 1 deletions

View File

@@ -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', () => {