Skip to content

Commit

Permalink
Reconnect to MQTT broker if MQTT setting are updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekadams committed Sep 26, 2018
1 parent a3a365d commit d975cc8
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/renderer/components/assignments/AssignmentEmulator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ export default {
// Monitor MQTT connection.
mqttConnected: function(value) {
console.log("MQTT connected " + value);
},
mqttHostname: function(value) {
this.establishMqttConnection();
},
mqttPort: function(value) {
this.establishMqttConnection();
},
mqttTopic: function(value) {
this.establishMqttConnection();
}
},
Expand Down Expand Up @@ -246,19 +255,23 @@ export default {
// Kill any existing connection.
if (mqttClient && mqttConnected) {
mqttClient.end();
this.$data.mqttConnected = false;
this.killMqttConnection();
}
// Build URL based on form data.
let url = "mqtt://" + this.$data.mqttHostname + ":" + this.$data.mqttPort;
console.log(`Connecting to MQTT via ${url}`);
mqttClient = MQTT.connect(url);
mqttClient.on("connect", function() {
component.$data.mqttConnected = true;
mqttClient.subscribe(component.$data.mqttTopic);
});
this.$data.mqttClient = mqttClient;
},
killMqttConnection: function() {
mqttClient.end();
this.$data.mqttConnected = false;
}
}
};
Expand Down

0 comments on commit d975cc8

Please sign in to comment.