Updated FHEM integration for testing

This commit is contained in:
bsamuels
2018-01-02 16:20:51 -06:00
parent 4c694cb285
commit f27d905869
33 changed files with 434 additions and 24 deletions

View File

@@ -211,6 +211,7 @@ public class BridgeSettings extends BackupHandler {
theBridgeSettings.setSomfyconfigured(theBridgeSettings.isValidSomfy());
theBridgeSettings.setHomeWizardConfigured(theBridgeSettings.isValidHomeWizard());
theBridgeSettings.setOpenhabconfigured(theBridgeSettings.isValidOpenhab());
theBridgeSettings.setFhemconfigured(theBridgeSettings.isValidFhem());
// Lifx is either configured or not, so it does not need an update.
if(serverPortOverride != null)
theBridgeSettings.setServerPort(serverPortOverride);

View File

@@ -105,6 +105,9 @@ public class BridgeSettingsDescriptor {
@SerializedName("upnpsenddelay")
@Expose
private Integer upnpsenddelay;
@SerializedName("fhemaddress")
@Expose
private IpList fhemaddress;
private boolean settingsChanged;
private boolean veraconfigured;
@@ -120,6 +123,7 @@ public class BridgeSettingsDescriptor {
private boolean lifxconfigured;
private boolean homewizardconfigured;
private boolean openhabconfigured;
private boolean fhemconfigured;
// Deprecated settings
private String haltoken;
@@ -447,6 +451,18 @@ public class BridgeSettingsDescriptor {
public void setUpnpsenddelay(Integer upnpsenddelay) {
this.upnpsenddelay = upnpsenddelay;
}
public IpList getFhemaddress() {
return fhemaddress;
}
public void setFhemaddress(IpList fhemaddress) {
this.fhemaddress = fhemaddress;
}
public boolean isFhemconfigured() {
return fhemconfigured;
}
public void setFhemconfigured(boolean fhemconfigured) {
this.fhemconfigured = fhemconfigured;
}
public Boolean isValidVera() {
if(this.getVeraAddress() == null || this.getVeraAddress().getDevices().size() <= 0)
return false;
@@ -564,4 +580,14 @@ public class BridgeSettingsDescriptor {
return true;
}
public Boolean isValidFhem() {
if(this.getFhemaddress() == null || this.getFhemaddress().getDevices().size() <= 0)
return false;
List<NamedIP> devicesList = this.getFhemaddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
}

View File

@@ -32,6 +32,7 @@ public class DeviceMapTypes {
public final static String[] SOMFY_DEVICE = { "somfyDevice", "Somfy Device"};
public final static String[] LIFX_DEVICE = { "lifxDevice", "LIFX Device"};
public final static String[] OPENHAB_DEVICE = { "openhabDevice", "OpenHAB Device"};
public final static String[] FHEM_DEVICE = { "fhemDevice", "FHEM Device"};
public final static int typeIndex = 0;
public final static int displayIndex = 1;
@@ -65,6 +66,7 @@ public class DeviceMapTypes {
deviceMapTypes.add(FIBARO_SCENE);
deviceMapTypes.add(SOMFY_DEVICE);
deviceMapTypes.add(OPENHAB_DEVICE);
deviceMapTypes.add(FHEM_DEVICE);
}
public static int getTypeIndex() {
return typeIndex;

View File

@@ -11,6 +11,7 @@ import com.bwssystems.HABridge.devicemanagmeent.ResourceHandler;
import com.bwssystems.HABridge.plugins.NestBridge.NestHome;
import com.bwssystems.HABridge.plugins.domoticz.DomoticzHome;
import com.bwssystems.HABridge.plugins.exec.CommandHome;
import com.bwssystems.HABridge.plugins.fhem.FHEMHome;
import com.bwssystems.HABridge.plugins.hal.HalHome;
import com.bwssystems.HABridge.plugins.harmony.HarmonyHome;
import com.bwssystems.HABridge.plugins.hass.HassHome;
@@ -106,7 +107,7 @@ public class HomeManager {
aHome = new DomoticzHome(bridgeSettings);
homeList.put(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex], aHome);
resourceList.put(DeviceMapTypes.DOMOTICZ_DEVICE[DeviceMapTypes.typeIndex], aHome);
//setup the Somfy configuration if available
//setup the Somfy configuration if availableOPENHAB
aHome = new SomfyHome(bridgeSettings);
homeList.put(DeviceMapTypes.SOMFY_DEVICE[DeviceMapTypes.typeIndex], aHome);
resourceList.put(DeviceMapTypes.SOMFY_DEVICE[DeviceMapTypes.typeIndex], aHome);
@@ -118,6 +119,10 @@ public class HomeManager {
aHome = new OpenHABHome(bridgeSettings);
resourceList.put(DeviceMapTypes.OPENHAB_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.OPENHAB_DEVICE[DeviceMapTypes.typeIndex], aHome);
//setup the OpenHAB configuration if available
aHome = new FHEMHome(bridgeSettings);
resourceList.put(DeviceMapTypes.FHEM_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.FHEM_DEVICE[DeviceMapTypes.typeIndex], aHome);
}
public Home findHome(String type) {

View File

@@ -321,6 +321,12 @@ public class DeviceResource {
return homeManager.findResource(DeviceMapTypes.OPENHAB_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.OPENHAB_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/fhem/devices", "application/json", (request, response) -> {
log.debug("Get FHEM devices");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.FHEM_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.FHEM_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/map/types", "application/json", (request, response) -> {
log.debug("Get map types");
return new DeviceMapTypes().getDeviceMapTypes();

View File

@@ -4,7 +4,7 @@ public class FHEMDevice {
private String address;
private String name;
private FHEMItem item;
private Result item;
public String getAddress() {
return address;
}
@@ -17,11 +17,10 @@ public class FHEMDevice {
public void setName(String name) {
this.name = name;
}
public FHEMItem getItem() {
public Result getItem() {
return item;
}
public void setItem(FHEMItem item) {
public void setItem(Result item) {
this.item = item;
}
}

View File

@@ -131,7 +131,7 @@ public class FHEMHome implements Home {
if(!validFhem)
return null;
log.debug("consolidating devices for java.lang.String");
log.debug("consolidating devices for FHEM");
List<FHEMDevice> theResponse = null;
Iterator<String> keys = fhemMap.keySet().iterator();
List<FHEMDevice> deviceList = new ArrayList<FHEMDevice>();
@@ -160,12 +160,12 @@ public class FHEMHome implements Home {
@Override
public Home createHome(BridgeSettings bridgeSettings) {
fhemMap = null;
validFhem = bridgeSettings.getBridgeSettingsDescriptor().isValidOpenhab();
validFhem = bridgeSettings.getBridgeSettingsDescriptor().isValidFhem();
log.info("FHEM Home created." + (validFhem ? "" : " No FHEMs configured."));
if(validFhem) {
fhemMap = new HashMap<String,FHEMInstance>();
httpClient = HTTPHome.getHandler();
Iterator<NamedIP> theList = bridgeSettings.getBridgeSettingsDescriptor().getOpenhabaddress().getDevices().iterator();
Iterator<NamedIP> theList = bridgeSettings.getBridgeSettingsDescriptor().getFhemaddress().getDevices().iterator();
while(theList.hasNext() && validFhem) {
NamedIP aFhem = theList.next();
try {

View File

@@ -49,7 +49,7 @@ public class FHEMInstance {
public List<FHEMDevice> getDevices(HTTPHandler httpClient) {
List<FHEMDevice> deviceList = null;
FHEMItem[] theFhemStates;
FHEMItem theFhemStates;
String theUrl = null;
String theData;
NameValue[] headers = null;
@@ -61,24 +61,25 @@ public class FHEMInstance {
theUrl = theUrl + theFhem.getUsername() + ":" + theFhem.getPassword() + "@";
}
theUrl = theUrl + theFhem.getIp() + ":" + theFhem.getPort() + "/fhem?cmd=jsonlist2";
if(theFhem.getWebhook() != null && !theFhem.getWebhook().trim().isEmpty())
theUrl = theUrl + "%20room=" + theFhem.getWebhook().trim();
theData = httpClient.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers);
if(theData != null) {
log.debug("GET FHEM States - data: " + theData);
theData = getJSONData(theData);
theFhemStates = new Gson().fromJson(theData, FHEMItem[].class);
theFhemStates = new Gson().fromJson(theData, FHEMItem.class);
if(theFhemStates == null) {
log.warn("Cannot get an devices for FHEM " + theFhem.getName() + " as response is not parsable.");
log.warn("Cannot get any devices for FHEM " + theFhem.getName() + " as response is not parsable.");
}
else {
deviceList = new ArrayList<FHEMDevice>();
for (int i = 0; i < theFhemStates.length; i++) {
for (Result aResult:theFhemStates.getResults()) {
FHEMDevice aNewFhemDeviceDevice = new FHEMDevice();
aNewFhemDeviceDevice.setItem(theFhemStates[i]);
aNewFhemDeviceDevice.setItem(aResult);
aNewFhemDeviceDevice.setAddress(theFhem.getIp() + ":" + theFhem.getPort());
aNewFhemDeviceDevice.setName(theFhem.getName());
deviceList.add(aNewFhemDeviceDevice);
}
}
}
@@ -91,7 +92,10 @@ public class FHEMInstance {
String theData;
theData = response.substring(response.indexOf("<pre>") + 4);
theData = theData.substring(1, theData.indexOf("</pre>") - 1);
// TODO Fix stripping out new line chars
theData = theData.replace("\n", "");
theData = theData.replace("\r", "");
theData = theData.replace("<a href=\"", "<a href=\\\"");
theData = theData.replace("\">", "\\\">");
return theData;
}

View File

@@ -66,7 +66,7 @@ public class OpenHABInstance {
log.debug("GET OpenHAB States - data: " + theData);
theOpenhabStates = new Gson().fromJson(theData, OpenHABItem[].class);
if(theOpenhabStates == null) {
log.warn("Cannot get an devices for OpenHAB " + theOpenHAB.getName() + " as response is not parsable.");
log.warn("Cannot get any devices for OpenHAB " + theOpenHAB.getName() + " as response is not parsable.");
}
else {
deviceList = new ArrayList<OpenHABDevice>();