mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-21 09:13:21 +00:00
First Beta of Harmony configuration for testing.
This commit is contained in:
@@ -90,6 +90,10 @@ public class BridgeSettings {
|
||||
public Boolean isValidHarmony() {
|
||||
if(this.harmonyaddress.contains(Configuration.DEFAULT_HARMONY_ADDRESS))
|
||||
return false;
|
||||
if(this.harmonypwd == null || this.harmonypwd == "")
|
||||
return false;
|
||||
if(this.harmonyuser == null || this.harmonyuser == "")
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.bwssystems.HABridge.devicemanagmeent.*;
|
||||
import com.bwssystems.HABridge.hue.HueMulator;
|
||||
import com.bwssystems.HABridge.upnp.UpnpListener;
|
||||
import com.bwssystems.HABridge.upnp.UpnpSettingsResource;
|
||||
import com.bwssystems.harmony.HarmonyServer;
|
||||
|
||||
public class HABridge {
|
||||
|
||||
@@ -33,6 +34,7 @@ public class HABridge {
|
||||
public static void main(String[] args) {
|
||||
Logger log = LoggerFactory.getLogger(HABridge.class);
|
||||
DeviceResource theResources;
|
||||
HarmonyServer myHarmonyServer;
|
||||
HueMulator theHueMulator;
|
||||
UpnpSettingsResource theSettingResponder;
|
||||
UpnpListener theUpnpListener;
|
||||
@@ -69,10 +71,17 @@ public class HABridge {
|
||||
port(Integer.valueOf(bridgeSettings.getServerPort()));
|
||||
// sparkjava config directive to set html static file location for Jetty
|
||||
staticFileLocation("/public");
|
||||
//setup the harmony connection if available
|
||||
try {
|
||||
myHarmonyServer = HarmonyServer.setup(bridgeSettings);
|
||||
} catch (Exception e) {
|
||||
log.error("Cannot get harmony client setup, Exiting with message: " + e.getMessage(), e);
|
||||
return;
|
||||
}
|
||||
// setup the class to handle the resource setup rest api
|
||||
theResources = new DeviceResource(bridgeSettings);
|
||||
theResources = new DeviceResource(bridgeSettings, myHarmonyServer.getMyHarmony());
|
||||
// setup the class to handle the hue emulator rest api
|
||||
theHueMulator = new HueMulator(theResources.getDeviceRepository());
|
||||
theHueMulator = new HueMulator(theResources.getDeviceRepository(), myHarmonyServer.getMyHarmony());
|
||||
theHueMulator.setupServer();
|
||||
// setup the class to handle the upnp response rest api
|
||||
theSettingResponder = new UpnpSettingsResource(bridgeSettings);
|
||||
|
||||
@@ -18,6 +18,7 @@ import com.bwssystems.HABridge.BridgeSettings;
|
||||
import com.bwssystems.HABridge.JsonTransformer;
|
||||
import com.bwssystems.HABridge.dao.DeviceDescriptor;
|
||||
import com.bwssystems.HABridge.dao.DeviceRepository;
|
||||
import com.bwssystems.harmony.HarmonyHandler;
|
||||
import com.bwssystems.luupRequests.Sdata;
|
||||
import com.bwssystems.vera.VeraInfo;
|
||||
import com.google.gson.Gson;
|
||||
@@ -31,12 +32,14 @@ public class DeviceResource {
|
||||
|
||||
private DeviceRepository deviceRepository;
|
||||
private VeraInfo veraInfo;
|
||||
private HarmonyHandler myHarmonyHandler;
|
||||
private static final Set<String> supportedVerbs = new HashSet<>(Arrays.asList("get", "put", "post"));
|
||||
|
||||
public DeviceResource(BridgeSettings theSettings) {
|
||||
public DeviceResource(BridgeSettings theSettings, HarmonyHandler myHarmony) {
|
||||
super();
|
||||
deviceRepository = new DeviceRepository(theSettings.getUpnpDeviceDb());
|
||||
veraInfo = new VeraInfo(theSettings.getVeraAddress(), theSettings.isValidVera());
|
||||
myHarmonyHandler = myHarmony;
|
||||
setupEndpoints();
|
||||
}
|
||||
|
||||
@@ -150,5 +153,13 @@ public class DeviceResource {
|
||||
return sData.getScenes();
|
||||
}, new JsonTransformer());
|
||||
|
||||
get (API_CONTEXT + "/harmony/activities", "application/json", (request, response) -> {
|
||||
log.debug("Get harmony activities");
|
||||
response.status(HttpStatus.SC_OK);
|
||||
if(myHarmonyHandler != null)
|
||||
return myHarmonyHandler.getActivities();
|
||||
return "";
|
||||
}, new JsonTransformer());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.bwssystems.HABridge.api.hue.DeviceResponse;
|
||||
import com.bwssystems.HABridge.api.hue.DeviceState;
|
||||
import com.bwssystems.HABridge.api.hue.HueApiResponse;
|
||||
import com.bwssystems.HABridge.dao.*;
|
||||
import com.bwssystems.harmony.HarmonyHandler;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
@@ -51,15 +52,17 @@ public class HueMulator {
|
||||
private static final String HUE_CONTEXT = "/api";
|
||||
|
||||
private DeviceRepository repository;
|
||||
private HarmonyHandler myHarmony;
|
||||
private HttpClient httpClient;
|
||||
private ObjectMapper mapper;
|
||||
|
||||
|
||||
public HueMulator(DeviceRepository aDeviceRepository){
|
||||
public HueMulator(DeviceRepository aDeviceRepository, HarmonyHandler theHandler){
|
||||
httpClient = HttpClients.createDefault();
|
||||
mapper = new ObjectMapper(); //armzilla: work around Echo incorrect content type and breaking mapping. Map manually
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
repository = aDeviceRepository;
|
||||
myHarmony = theHandler;
|
||||
}
|
||||
|
||||
// This function sets up the sparkjava rest calls for the hue api
|
||||
@@ -208,19 +211,28 @@ public class HueMulator {
|
||||
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/on\":false}}]";
|
||||
url = device.getOffUrl();
|
||||
}
|
||||
|
||||
//quick template
|
||||
String body;
|
||||
url = replaceIntensityValue(url, state.getBri());
|
||||
if (state.isOn())
|
||||
body = replaceIntensityValue(device.getContentBody(), state.getBri());
|
||||
|
||||
if(device.getDeviceType() == "activity")
|
||||
{
|
||||
log.debug("executing activity to Harmony: " + url);
|
||||
myHarmony.startActivity(url);
|
||||
}
|
||||
else
|
||||
body = replaceIntensityValue(device.getContentBodyOff(), state.getBri());
|
||||
//make call
|
||||
if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){
|
||||
response.status(HttpStatus.SC_SERVICE_UNAVAILABLE);
|
||||
log.error("Error on calling url to change device state: " + url);
|
||||
return null;
|
||||
{
|
||||
log.debug("executing activity to Http: " + url);
|
||||
// quick template
|
||||
String body;
|
||||
url = replaceIntensityValue(url, state.getBri());
|
||||
if (state.isOn())
|
||||
body = replaceIntensityValue(device.getContentBody(), state.getBri());
|
||||
else
|
||||
body = replaceIntensityValue(device.getContentBodyOff(), state.getBri());
|
||||
// make call
|
||||
if (!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)) {
|
||||
response.status(HttpStatus.SC_SERVICE_UNAVAILABLE);
|
||||
log.error("Error on calling url to change device state: " + url);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
response.type("application/json; charset=utf-8");
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.bwssystems.harmony;
|
||||
|
||||
public class HarmonyActivities {
|
||||
|
||||
}
|
||||
89
src/main/java/com/bwssystems/harmony/HarmonyHandler.java
Normal file
89
src/main/java/com/bwssystems/harmony/HarmonyHandler.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package com.bwssystems.harmony;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.whistlingfish.harmony.HarmonyClient;
|
||||
import net.whistlingfish.harmony.config.Activity;
|
||||
import net.whistlingfish.harmony.config.Device;
|
||||
import net.whistlingfish.harmony.config.HarmonyConfig;
|
||||
|
||||
public class HarmonyHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(HarmonyHandler.class);
|
||||
private HarmonyClient harmonyClient;
|
||||
private Boolean noopCalls;
|
||||
|
||||
public HarmonyHandler(HarmonyClient theClient, Boolean noopCallsSetting) {
|
||||
super();
|
||||
noopCalls = noopCallsSetting;
|
||||
harmonyClient = theClient;
|
||||
}
|
||||
|
||||
public List<Activity> getActivities() {
|
||||
log.debug("Harmony api activities list requested.");
|
||||
return harmonyClient.getConfig().getActivities();
|
||||
}
|
||||
|
||||
public List<Device> getDevices() {
|
||||
log.debug("Harmony api device list requested.");
|
||||
return harmonyClient.getConfig().getDevices();
|
||||
}
|
||||
|
||||
public HarmonyConfig getConfig() {
|
||||
log.debug("Harmony api config requested.");
|
||||
return harmonyClient.getConfig();
|
||||
}
|
||||
|
||||
public Activity getCurrentActivity() {
|
||||
log.debug("Harmony api current sctivity requested.");
|
||||
return harmonyClient.getCurrentActivity();
|
||||
}
|
||||
|
||||
public Boolean startActivity(String anActivity) {
|
||||
log.debug("Harmony api start activity requested for: " + anActivity + " noop mode: " + noopCalls);
|
||||
if (anActivity != null && !anActivity.isEmpty()) {
|
||||
try {
|
||||
if (!noopCalls)
|
||||
harmonyClient.startActivity(Integer.parseInt(anActivity));
|
||||
} catch (IllegalArgumentException e) {
|
||||
try {
|
||||
if (!noopCalls)
|
||||
harmonyClient.startActivityByName(anActivity);
|
||||
} catch (IllegalArgumentException ei) {
|
||||
log.error("Error in finding activity: " + anActivity);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.error("Error in finding activity: " + anActivity);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Boolean pressButton(String aDevice, String aDeviceButton) {
|
||||
log.debug("Harmony api press a button requested for device: " + aDevice + " and a for button: " + aDeviceButton + " noop mode: " + noopCalls);
|
||||
if (aDeviceButton != null && !aDeviceButton.isEmpty()) {
|
||||
try {
|
||||
if (!noopCalls)
|
||||
harmonyClient.pressButton(Integer.parseInt(aDevice), aDeviceButton);
|
||||
} catch (IllegalArgumentException e) {
|
||||
try {
|
||||
if (!noopCalls)
|
||||
harmonyClient.pressButton(aDevice, aDeviceButton);
|
||||
} catch (IllegalArgumentException ei) {
|
||||
log.error("Error in finding device: " + aDevice +" and a button: " + aDeviceButton);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.error("Error in finding device: " + aDevice +" and a button: " + aDeviceButton);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
60
src/main/java/com/bwssystems/harmony/HarmonyServer.java
Normal file
60
src/main/java/com/bwssystems/harmony/HarmonyServer.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.bwssystems.harmony;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.bwssystems.HABridge.BridgeSettings;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
import net.whistlingfish.harmony.ActivityChangeListener;
|
||||
import net.whistlingfish.harmony.HarmonyClient;
|
||||
import net.whistlingfish.harmony.HarmonyClientModule;
|
||||
import net.whistlingfish.harmony.config.Activity;
|
||||
|
||||
public class HarmonyServer {
|
||||
@Inject
|
||||
private HarmonyClient harmonyClient;
|
||||
|
||||
private HarmonyHandler myHarmony;
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(HarmonyServer.class);
|
||||
|
||||
public HarmonyServer() {
|
||||
super();
|
||||
myHarmony = null;
|
||||
}
|
||||
|
||||
public static HarmonyServer setup(BridgeSettings bridgeSettings) throws Exception {
|
||||
if(!bridgeSettings.isValidHarmony()) {
|
||||
return new HarmonyServer();
|
||||
}
|
||||
Injector injector = null;
|
||||
injector = Guice.createInjector(new HarmonyClientModule());
|
||||
HarmonyServer mainObject = new HarmonyServer();
|
||||
injector.injectMembers(mainObject);
|
||||
mainObject.execute(bridgeSettings);
|
||||
return mainObject;
|
||||
}
|
||||
|
||||
private void execute(BridgeSettings mySettings) throws Exception {
|
||||
log.debug("setup initiated....");
|
||||
harmonyClient.addListener(new ActivityChangeListener() {
|
||||
@Override
|
||||
public void activityStarted(Activity activity) {
|
||||
log.info(format("activity changed: [%d] %s", activity.getId(), activity.getLabel()));
|
||||
}
|
||||
});
|
||||
harmonyClient.connect(mySettings.getHarmonyAddress(), mySettings.getHarmonyUser(), mySettings.getHarmonyPwd());
|
||||
Boolean noopCalls = Boolean.parseBoolean(System.getProperty("noop.calls", "false"));
|
||||
myHarmony = new HarmonyHandler(harmonyClient, noopCalls);
|
||||
}
|
||||
|
||||
public HarmonyHandler getMyHarmony() {
|
||||
return myHarmony;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user