mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-18 16:17:30 +00:00
Basic MQTT configuration implementation. Still needs username/pwd and
MQTT screen helper.
This commit is contained in:
11
pom.xml
11
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>com.bwssystems.HABridge</groupId>
|
<groupId>com.bwssystems.HABridge</groupId>
|
||||||
<artifactId>ha-bridge</artifactId>
|
<artifactId>ha-bridge</artifactId>
|
||||||
<version>3.2.2a</version>
|
<version>3.2.2b</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
@@ -22,6 +22,10 @@
|
|||||||
<id>jitpack.io</id>
|
<id>jitpack.io</id>
|
||||||
<url>https://jitpack.io</url>
|
<url>https://jitpack.io</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>Eclipse Paho Repo</id>
|
||||||
|
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
|
||||||
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -106,6 +110,11 @@
|
|||||||
<artifactId>smack-core</artifactId>
|
<artifactId>smack-core</artifactId>
|
||||||
<version>4.0.7</version>
|
<version>4.0.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.paho</groupId>
|
||||||
|
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||||
|
<version>1.1.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ public class BridgeSettingsDescriptor {
|
|||||||
private boolean settingsChanged;
|
private boolean settingsChanged;
|
||||||
private String myechourl;
|
private String myechourl;
|
||||||
private String webaddress;
|
private String webaddress;
|
||||||
|
private IpList mqttaddress;
|
||||||
|
private boolean mqttconfigured;
|
||||||
|
|
||||||
public BridgeSettingsDescriptor() {
|
public BridgeSettingsDescriptor() {
|
||||||
super();
|
super();
|
||||||
@@ -44,6 +46,7 @@ public class BridgeSettingsDescriptor {
|
|||||||
this.harmonyconfigured = false;
|
this.harmonyconfigured = false;
|
||||||
this.hueconfigured = false;
|
this.hueconfigured = false;
|
||||||
this.halconfigured = false;
|
this.halconfigured = false;
|
||||||
|
this.mqttconfigured = false;
|
||||||
this.farenheit = true;
|
this.farenheit = true;
|
||||||
this.whitelist = null;
|
this.whitelist = null;
|
||||||
this.settingsChanged = false;
|
this.settingsChanged = false;
|
||||||
@@ -224,6 +227,18 @@ public class BridgeSettingsDescriptor {
|
|||||||
public void setWebaddress(String webaddress) {
|
public void setWebaddress(String webaddress) {
|
||||||
this.webaddress = 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() {
|
public Boolean isValidVera() {
|
||||||
if(this.getVeraAddress() == null || this.getVeraAddress().getDevices().size() <= 0)
|
if(this.getVeraAddress() == null || this.getVeraAddress().getDevices().size() <= 0)
|
||||||
return false;
|
return false;
|
||||||
@@ -269,4 +284,12 @@ public class BridgeSettingsDescriptor {
|
|||||||
return false;
|
return false;
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import com.bwssystems.NestBridge.NestHome;
|
|||||||
import com.bwssystems.hal.HalHome;
|
import com.bwssystems.hal.HalHome;
|
||||||
import com.bwssystems.harmony.HarmonyHome;
|
import com.bwssystems.harmony.HarmonyHome;
|
||||||
import com.bwssystems.hue.HueHome;
|
import com.bwssystems.hue.HueHome;
|
||||||
|
import com.bwssystems.mqtt.MQTTHome;
|
||||||
import com.bwssystems.util.UDPDatagramSender;
|
import com.bwssystems.util.UDPDatagramSender;
|
||||||
|
|
||||||
public class HABridge {
|
public class HABridge {
|
||||||
@@ -39,6 +40,7 @@ public class HABridge {
|
|||||||
NestHome nestHome;
|
NestHome nestHome;
|
||||||
HueHome hueHome;
|
HueHome hueHome;
|
||||||
HalHome halHome;
|
HalHome halHome;
|
||||||
|
MQTTHome mqttHome;
|
||||||
HueMulator theHueMulator;
|
HueMulator theHueMulator;
|
||||||
UDPDatagramSender udpSender;
|
UDPDatagramSender udpSender;
|
||||||
UpnpSettingsResource theSettingResponder;
|
UpnpSettingsResource theSettingResponder;
|
||||||
@@ -72,6 +74,8 @@ public class HABridge {
|
|||||||
hueHome = new HueHome(bridgeSettings.getBridgeSettingsDescriptor());
|
hueHome = new HueHome(bridgeSettings.getBridgeSettingsDescriptor());
|
||||||
//setup the hal configuration if available
|
//setup the hal configuration if available
|
||||||
halHome = new HalHome(bridgeSettings.getBridgeSettingsDescriptor());
|
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
|
// setup the class to handle the resource setup rest api
|
||||||
theResources = new DeviceResource(bridgeSettings.getBridgeSettingsDescriptor(), harmonyHome, nestHome, hueHome, halHome);
|
theResources = new DeviceResource(bridgeSettings.getBridgeSettingsDescriptor(), harmonyHome, nestHome, hueHome, halHome);
|
||||||
// setup the class to handle the upnp response rest api
|
// setup the class to handle the upnp response rest api
|
||||||
@@ -104,6 +108,8 @@ public class HABridge {
|
|||||||
nestHome = null;
|
nestHome = null;
|
||||||
harmonyHome.shutdownHarmonyHubs();
|
harmonyHome.shutdownHarmonyHubs();
|
||||||
harmonyHome = null;
|
harmonyHome = null;
|
||||||
|
mqttHome.shutdownMQTTClients();
|
||||||
|
mqttHome = null;
|
||||||
udpSender.closeResponseSocket();
|
udpSender.closeResponseSocket();
|
||||||
}
|
}
|
||||||
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");
|
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ public class NamedIP {
|
|||||||
private String name;
|
private String name;
|
||||||
private String ip;
|
private String ip;
|
||||||
private String port;
|
private String port;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@@ -22,4 +25,16 @@ public class NamedIP {
|
|||||||
public void setPort(String port) {
|
public void setPort(String port) {
|
||||||
this.port = 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
61
src/main/java/com/bwssystems/mqtt/MQTTHandler.java
Normal file
61
src/main/java/com/bwssystems/mqtt/MQTTHandler.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
56
src/main/java/com/bwssystems/mqtt/MQTTHome.java
Normal file
56
src/main/java/com/bwssystems/mqtt/MQTTHome.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 () {
|
$scope.bridgeReinit = function () {
|
||||||
bridgeService.reinit();
|
bridgeService.reinit();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -236,6 +236,35 @@
|
|||||||
type="password" ng-model="bridge.settings.haltoken"
|
type="password" ng-model="bridge.settings.haltoken"
|
||||||
placeholder="thetoken"></td>
|
placeholder="thetoken"></td>
|
||||||
</tr>
|
</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>
|
<tr>
|
||||||
<td>Nest Username</td>
|
<td>Nest Username</td>
|
||||||
<td><input id="bridge-settings-nestuser" class="form-control"
|
<td><input id="bridge-settings-nestuser" class="form-control"
|
||||||
|
|||||||
Reference in New Issue
Block a user