Basic MQTT configuration implementation. Still needs username/pwd and

MQTT screen helper.
This commit is contained in:
Admin
2016-11-11 16:42:20 -06:00
parent 7d39b79e05
commit 3eba9b9245
8 changed files with 216 additions and 1 deletions

11
pom.xml
View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>3.2.2a</version>
<version>3.2.2b</version>
<packaging>jar</packaging>
<name>HA Bridge</name>
@@ -22,6 +22,10 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>Eclipse Paho Repo</id>
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
</repository>
</repositories>
<dependencies>
@@ -106,6 +110,11 @@
<artifactId>smack-core</artifactId>
<version>4.0.7</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>

View File

@@ -34,6 +34,8 @@ public class BridgeSettingsDescriptor {
private boolean settingsChanged;
private String myechourl;
private String webaddress;
private IpList mqttaddress;
private boolean mqttconfigured;
public BridgeSettingsDescriptor() {
super();
@@ -44,6 +46,7 @@ public class BridgeSettingsDescriptor {
this.harmonyconfigured = false;
this.hueconfigured = false;
this.halconfigured = false;
this.mqttconfigured = false;
this.farenheit = true;
this.whitelist = null;
this.settingsChanged = false;
@@ -224,6 +227,18 @@ public class BridgeSettingsDescriptor {
public void setWebaddress(String webaddress) {
this.webaddress = webaddress;
}
public IpList getMqttaddress() {
return mqttaddress;
}
public void setMqttaddress(IpList mqttaddress) {
this.mqttaddress = mqttaddress;
}
public boolean isMqttconfigured() {
return mqttconfigured;
}
public void setMqttconfigured(boolean mqttconfigured) {
this.mqttconfigured = mqttconfigured;
}
public Boolean isValidVera() {
if(this.getVeraAddress() == null || this.getVeraAddress().getDevices().size() <= 0)
return false;
@@ -269,4 +284,12 @@ public class BridgeSettingsDescriptor {
return false;
return true;
}
public Boolean isValidMQTT() {
if(this.getMqttaddress() == null || this.getMqttaddress().getDevices().size() <= 0)
return false;
List<NamedIP> devicesList = this.getMqttaddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
}

View File

@@ -13,6 +13,7 @@ import com.bwssystems.NestBridge.NestHome;
import com.bwssystems.hal.HalHome;
import com.bwssystems.harmony.HarmonyHome;
import com.bwssystems.hue.HueHome;
import com.bwssystems.mqtt.MQTTHome;
import com.bwssystems.util.UDPDatagramSender;
public class HABridge {
@@ -39,6 +40,7 @@ public class HABridge {
NestHome nestHome;
HueHome hueHome;
HalHome halHome;
MQTTHome mqttHome;
HueMulator theHueMulator;
UDPDatagramSender udpSender;
UpnpSettingsResource theSettingResponder;
@@ -72,6 +74,8 @@ public class HABridge {
hueHome = new HueHome(bridgeSettings.getBridgeSettingsDescriptor());
//setup the hal configuration if available
halHome = new HalHome(bridgeSettings.getBridgeSettingsDescriptor());
//setup the mqtt handlers if available
mqttHome = new MQTTHome(bridgeSettings.getBridgeSettingsDescriptor());
// setup the class to handle the resource setup rest api
theResources = new DeviceResource(bridgeSettings.getBridgeSettingsDescriptor(), harmonyHome, nestHome, hueHome, halHome);
// setup the class to handle the upnp response rest api
@@ -104,6 +108,8 @@ public class HABridge {
nestHome = null;
harmonyHome.shutdownHarmonyHubs();
harmonyHome = null;
mqttHome.shutdownMQTTClients();
mqttHome = null;
udpSender.closeResponseSocket();
}
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");

View File

@@ -4,6 +4,9 @@ public class NamedIP {
private String name;
private String ip;
private String port;
private String username;
private String password;
public String getName() {
return name;
}
@@ -22,4 +25,16 @@ public class NamedIP {
public void setPort(String port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@@ -0,0 +1,61 @@
package com.bwssystems.mqtt;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttSecurityException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
public class MQTTHandler {
private static final Logger log = LoggerFactory.getLogger(MQTTHandler.class);
private NamedIP myConfig;
private MqttClient myClient;
private int qos = 1;
public MQTTHandler(NamedIP aConfig) {
super();
log.debug("Setting up handler for name: " + aConfig.getName());
MemoryPersistence persistence = new MemoryPersistence();
myConfig = aConfig;
try {
myClient = new MqttClient("tcp://" + myConfig.getIp(), myConfig.getName(), persistence);
} catch (MqttException e) {
log.error("Could not create MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
try {
myClient.connect(connOpts);
} catch (MqttSecurityException e) {
log.error("Could not connect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
} catch (MqttException e) {
log.error("Could not connect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}
}
public void publishMessage(String topic, String content) {
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
try {
myClient.publish(topic, message);
} catch (MqttPersistenceException e) {
log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
} catch (MqttException e) {
log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}
}
public void shutdown() {
try {
myClient.disconnect();
} catch (MqttException e) {
log.warn("Could not disconnect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp());
}
}
}

View File

@@ -0,0 +1,56 @@
package com.bwssystems.mqtt;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.NamedIP;
public class MQTTHome {
private static final Logger log = LoggerFactory.getLogger(MQTTHome.class);
private Map<String, MQTTHandler> handlers;
public MQTTHome(BridgeSettingsDescriptor bridgeSettings) {
super();
if(!bridgeSettings.isValidMQTT())
return;
handlers = new HashMap<String, MQTTHandler>();
Iterator<NamedIP> theList = bridgeSettings.getMqttaddress().getDevices().iterator();
while(theList.hasNext()) {
NamedIP aClientConfig = theList.next();
MQTTHandler aHandler = new MQTTHandler(aClientConfig);
if(aHandler != null)
handlers.put(aClientConfig.getName(), aHandler);
}
}
public void shutdownMQTTClients() {
log.debug("Shutting down MQTT handlers.");
if(!handlers.isEmpty()) {
Iterator<String> keys = handlers.keySet().iterator();
while(keys.hasNext()) {
String key = keys.next();
handlers.get(key).shutdown();
}
}
}
public MQTTHandler getMQTTHandler(String aName) {
MQTTHandler aHandler;
if(aName == null || aName.equals("")) {
aHandler = null;
log.debug("Cannot get MQTT handler for name as it is empty.");
}
else {
aHandler = handlers.get(aName);
log.debug("Retrieved a MQTT hanlder for name: " + aName);
}
return aHandler;
}
}

View File

@@ -680,6 +680,22 @@ app.controller('SystemController', function ($scope, $location, $http, $window,
}
}
};
$scope.addMQTTtoSettings = function (newmqttname, newmqttip) {
if($scope.bridge.settings.mqttaddress == null) {
$scope.bridge.settings.mqttaddress = { devices: [] };
}
var newmqtt = {name: newmqttname, ip: newmqttip }
$scope.bridge.settings.mqttaddress.devices.push(newmqtt);
$scope.newmqttname = null;
$scope.newmqttip = null;
};
$scope.removeMQTTtoSettings = function (mqttname, mqttip) {
for(var i = $scope.bridge.settings.mqttaddress.devices.length - 1; i >= 0; i--) {
if($scope.bridge.settings.mqttaddress.devices[i].name === mqttname && $scope.bridge.settings.mqttaddress.devices[i].ip === mqttip) {
$scope.bridge.settings.mqttaddress.devices.splice(i, 1);
}
}
};
$scope.bridgeReinit = function () {
bridgeService.reinit();
};

View File

@@ -236,6 +236,35 @@
type="password" ng-model="bridge.settings.haltoken"
placeholder="thetoken"></td>
</tr>
<tr>
<td>MQTT Client IDs and IP Addresses</td>
<td><table
class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Client ID</th>
<th>IP</th>
<th>Manage</th>
</tr>
</thead>
<tr ng-repeat="mqtt in bridge.settings.mqttaddress.devices">
<td>{{mqtt.name}}</td>
<td>{{mqtt.ip}}</td>
<td><button class="btn btn-danger" type="submit"
ng-click="removeMQTTtoSettings(mqtt.name, mqtt.ip)">Del</button></td>
</tr>
<tr>
<td><input id="bridge-settings-next-mqtt-name"
class="form-control" type="text" ng-model="newmqttname"
placeholder="A MQTT Client ID"></td>
<td><input id="bridge-settings-next-mqtt-ip"
class="form-control" type="text" ng-model="newmqttip"
placeholder="MQTT Broker IP and port"></td>
<td><button class="btn btn-success" type="submit"
ng-click="addMQTTtoSettings(newmqttname, newmqttip)">Add</button></td>
</tr>
</table></td>
</tr>
<tr>
<td>Nest Username</td>
<td><input id="bridge-settings-nestuser" class="form-control"