Skip to content

Commit

Permalink
Finished implementation for multiple vera support. Tweaks to the UI.
Browse files Browse the repository at this point in the history
Fixes #19
Fixes #21
Fixes #23
Fixes #26
Fixes #28
Fixes #30
Fixes #31
  • Loading branch information
bwssytems committed Jan 28, 2016
1 parent a6bb1ae commit 2565183
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 147 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ java -jar -Dupnp.config.address=A.B.C.D -Dvera.address=E.F.G.H -Dharmony.addres
## Available Arguments
### -Dupnp.config.address=`<ip address>`
The server defaults to the first available address on the host if this is not given. This default may NOT be the correct IP that is your public IP for your host on the network. It is best to set this parameter to not have discovery issues. Replace the -Dupnp.config.address=`<ip address>` value with the server ipv4 address you would like to use as the address that any upnp device will call after discovery.
### -Dvera.address=`<ip address>`
The argument for the vera address should be given as it the system does not have a way to find the address. Supply -Dvera.address=X.Y.Z.A on the command line to provide it. If a vera is not used, do not set it.
### -Dvera.address=`<ip address>` | `<{devices:[{name:avera,ip:x.y.w.z},{name:anothervera,ip:a.b.c.d}]}>`
The argument for the vera address should be given as it the system does not have a way to find the address. Supply -Dvera.address=X.Y.Z.A on the command line to provide it. If a vera is not used, do not set it. To provide multiple veras, use the json style notation outlined above to provide the list. This argument is backwards compatible.
### -Dserver.port=`<port>`
The server defaults to running on port 8080. If you're already running a server (like openHAB) on 8080, -Dserver.port=`<port>` on the command line.
### -Dupnp.device.db=`<filepath>`
Expand Down Expand Up @@ -306,7 +306,9 @@ comment | string | Comment configured with device. Not always present.
"status":"0",
"level":"0",
"state":"-1",
"comment":""
"comment":"",
"veraname":"default",
"veraddress":"192.168.1.2"
},
{
"name":"Couch Right Lamp",
Expand All @@ -320,6 +322,8 @@ comment | string | Comment configured with device. Not always present.
"level":"0",
"state":"-1",
"comment":""
"veraname":"default",
"veraddress":"192.168.1.2"
}]
```
### Get Vera Scenes
Expand All @@ -340,12 +344,16 @@ room | string | Room name assigned to scene.
"name":"AccentLightsOff",
"id":"27",
"room":"no room"
"veraname":"default",
"veraddress":"192.168.1.2"
},
{
"active":"0",
"name":"AccentLightsOn",
"id":"26",
"room":"no room"
"veraname":"default",
"veraddress":"192.168.1.2"
}]
```
### Get Harmony Activities
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>1.3.1c</version>
<version>1.3.5</version>
<packaging>jar</packaging>

<name>HA Bridge</name>
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/bwssystems/HABridge/BridgeSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class BridgeSettings {
private String serverport;
private String upnpresponseport;
private String upnpdevicedb;
private String veraaddress;
private IpList veraaddress;
private IpList harmonyaddress;
private String harmonyuser;
private String harmonypwd;
Expand Down Expand Up @@ -43,10 +43,10 @@ public String getUpnpDeviceDb() {
public void setUpnpDeviceDb(String upnpDeviceDb) {
this.upnpdevicedb = upnpDeviceDb;
}
public String getVeraAddress() {
public IpList getVeraAddress() {
return veraaddress;
}
public void setVeraAddress(String veraAddress) {
public void setVeraAddress(IpList veraAddress) {
this.veraaddress = veraAddress;
}
public IpList getHarmonyAddress() {
Expand Down Expand Up @@ -110,7 +110,8 @@ public void setNestConfigured(boolean isNestConfigured) {
this.nestconfigured = isNestConfigured;
}
public Boolean isValidVera() {
if(this.veraaddress.contains(Configuration.DEFAULT_ADDRESS))
List<NamedIP> devicesList = this.veraaddress.getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/bwssystems/HABridge/HABridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,19 @@ public static void main(String[] args) {

bridgeSettings.setUpnpDeviceDb(System.getProperty("upnp.device.db", Configuration.DEVICE_DB_DIRECTORY));
bridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", Configuration.UPNP_RESPONSE_PORT));
bridgeSettings.setVeraAddress(System.getProperty("vera.address", Configuration.DEFAULT_ADDRESS));
IpList theVeraList;

try {
theVeraList = new Gson().fromJson(System.getProperty("vera.address", Configuration.DEFAULT_HARMONY_ADDRESS_LIST), IpList.class);
} catch (Exception e) {
try {
theVeraList = new Gson().fromJson("{devices:[{name:default,ip:" + System.getProperty("vera.address", Configuration.DEFAULT_ADDRESS) + "}]}", IpList.class);
} catch (Exception et) {
log.error("Cannot parse vera.address, Exiting with message: " + e.getMessage(), e);
return;
}
}
bridgeSettings.setVeraAddress(theVeraList);
IpList theHarmonyList;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.bwssystems.NestBridge.NestHome;
import com.bwssystems.harmony.HarmonyHome;
import com.bwssystems.luupRequests.Sdata;
import com.bwssystems.vera.VeraHome;
import com.bwssystems.vera.VeraInfo;
import com.google.gson.Gson;

Expand All @@ -35,15 +36,20 @@ public class DeviceResource {
private static final Logger log = LoggerFactory.getLogger(DeviceResource.class);

private DeviceRepository deviceRepository;
private VeraInfo veraInfo;
private VeraHome veraHome;
private Version version;
private HarmonyHome myHarmonyHome;
private NestHome nestHome;
private static final Set<String> supportedVerbs = new HashSet<>(Arrays.asList("get", "put", "post"));

public DeviceResource(BridgeSettings theSettings, Version theVersion, HarmonyHome theHarmonyHome, NestHome aNestHome) {
this.deviceRepository = new DeviceRepository(theSettings.getUpnpDeviceDb());
this.veraInfo = new VeraInfo(theSettings.getVeraAddress(), theSettings.isValidVera());

if(theSettings.isValidVera())
this.veraHome = new VeraHome(theSettings);
else
this.veraHome = null;

if(theSettings.isValidHarmony())
this.myHarmonyHome = theHarmonyHome;
else
Expand All @@ -53,6 +59,7 @@ public DeviceResource(BridgeSettings theSettings, Version theVersion, HarmonyHom
this.nestHome = aNestHome;
else
this.nestHome = null;

this.version = theVersion;
setupEndpoints();
}
Expand Down Expand Up @@ -175,25 +182,23 @@ private void setupEndpoints() {

get (API_CONTEXT + "/vera/devices", "application/json", (request, response) -> {
log.debug("Get vera devices");
Sdata sData = veraInfo.getSdata();
if(sData == null){
if(veraHome == null){
response.status(HttpStatus.SC_NOT_FOUND);
return null;
}

response.status(HttpStatus.SC_OK);
return sData.getDevices();
return veraHome.getDevices();
}, new JsonTransformer());

get (API_CONTEXT + "/vera/scenes", "application/json", (request, response) -> {
log.debug("Get vera scenes");
Sdata sData = veraInfo.getSdata();
if(sData == null){
if(veraHome == null){
response.status(HttpStatus.SC_NOT_FOUND);
return null;
}
response.status(HttpStatus.SC_OK);
return sData.getScenes();
return veraHome.getScenes();
}, new JsonTransformer());

get (API_CONTEXT + "/harmony/activities", "application/json", (request, response) -> {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/bwssystems/luupRequests/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class Device {
private String level;
private String state;
private String comment;
private String veraname;
private String veraaddress;
public String getName() {
return name;
}
Expand Down Expand Up @@ -79,5 +81,17 @@ public String getComment() {
public void setComment(String comment) {
this.comment = comment;
}
public String getVeraname() {
return veraname;
}
public void setVeraname(String veraname) {
this.veraname = veraname;
}
public String getVeraaddress() {
return veraaddress;
}
public void setVeraaddress(String veraaddress) {
this.veraaddress = veraaddress;
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/bwssystems/luupRequests/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public class Scene {
private String name;
private String id;
private String room;
private String veraname;
private String veraaddress;
public String getActive() {
return active;
}
Expand All @@ -29,5 +31,17 @@ public String getRoom() {
public void setRoom(String room) {
this.room = room;
}
public String getVeraname() {
return veraname;
}
public void setVeraname(String veraname) {
this.veraname = veraname;
}
public String getVeraaddress() {
return veraaddress;
}
public void setVeraaddress(String veraaddress) {
this.veraaddress = veraaddress;
}

}
58 changes: 58 additions & 0 deletions src/main/java/com/bwssystems/vera/VeraHome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.bwssystems.vera;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bwssystems.HABridge.BridgeSettings;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.luupRequests.Device;
import com.bwssystems.luupRequests.Scene;

public class VeraHome {
private static final Logger log = LoggerFactory.getLogger(VeraHome.class);
private Map<String, VeraInfo> veras;

public VeraHome(BridgeSettings bridgeSettings) {
veras = new HashMap<String, VeraInfo>();
if(!bridgeSettings.isValidVera())
return;
Iterator<NamedIP> theList = bridgeSettings.getVeraAddress().getDevices().iterator();
while(theList.hasNext()) {
NamedIP aVera = theList.next();
veras.put(aVera.getName(), new VeraInfo(aVera, bridgeSettings.isValidVera()));
}
}

public List<Device> getDevices() {
log.debug("consolidating devices for veras");
Iterator<String> keys = veras.keySet().iterator();
ArrayList<Device> deviceList = new ArrayList<Device>();
while(keys.hasNext()) {
String key = keys.next();
Iterator<Device> devices = veras.get(key).getSdata().getDevices().iterator();
while(devices.hasNext()) {
deviceList.add(devices.next());
}
}
return deviceList;
}
public List<Scene> getScenes() {
log.debug("consolidating scenes for veras");
Iterator<String> keys = veras.keySet().iterator();
ArrayList<Scene> sceneList = new ArrayList<Scene>();
while(keys.hasNext()) {
String key = keys.next();
Iterator<Scene> scenes = veras.get(key).getSdata().getScenes().iterator();
while(scenes.hasNext()) {
sceneList.add(scenes.next());
}
}
return sceneList;
}
}
13 changes: 9 additions & 4 deletions src/main/java/com/bwssystems/vera/VeraInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.luupRequests.Categorie;
import com.bwssystems.luupRequests.Device;
import com.bwssystems.luupRequests.Room;
Expand All @@ -25,21 +26,21 @@ public class VeraInfo {
private static final Logger log = LoggerFactory.getLogger(VeraInfo.class);
private HttpClient httpClient;
private static final String SDATA_REQUEST = ":3480/data_request?id=sdata&output_format=json";
private String veraAddressString;
private NamedIP veraAddress;
private Boolean validVera;

public VeraInfo(String addressString, Boolean isValidVera) {
public VeraInfo(NamedIP addressName, Boolean isValidVera) {
super();
httpClient = HttpClients.createDefault();
veraAddressString = addressString;
veraAddress = addressName;
validVera = isValidVera;
}

public Sdata getSdata() {
if(!validVera)
return new Sdata();

String theUrl = "http://" + veraAddressString + SDATA_REQUEST;
String theUrl = "http://" + veraAddress.getIp() + SDATA_REQUEST;
String theData;

theData = doHttpGETRequest(theUrl);
Expand Down Expand Up @@ -71,6 +72,8 @@ private void denormalizeSdata(Sdata theSdata) {
theDevice.setCategory(categoryMap.get(theDevice.getCategory()).getName());
else
theDevice.setCategory("<unknown>");
theDevice.setVeraaddress(veraAddress.getIp());
theDevice.setVeraname(veraAddress.getName());
}

ListIterator<Scene> theSecneIter = theSdata.getScenes().listIterator();
Expand All @@ -81,6 +84,8 @@ private void denormalizeSdata(Sdata theSdata) {
theScene.setRoom(roomMap.get(theScene.getRoom()).getName());
else
theScene.setRoom("no room");
theScene.setVeraaddress(veraAddress.getIp());
theScene.setVeraname(veraAddress.getName());
}
}

Expand Down
Loading

0 comments on commit 2565183

Please sign in to comment.