mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-18 00:10:20 +00:00
Merge pull request #324 from CrEaK/master
Added a optional webhook for harmony-activity changes
This commit is contained in:
@@ -2,7 +2,8 @@ package com.bwssystems.HABridge;
|
|||||||
|
|
||||||
public class NamedIP {
|
public class NamedIP {
|
||||||
private String name;
|
private String name;
|
||||||
private String ip;
|
private String ip;
|
||||||
|
private String webhook;
|
||||||
private String port;
|
private String port;
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
@@ -20,7 +21,13 @@ public class NamedIP {
|
|||||||
public void setIp(String ip) {
|
public void setIp(String ip) {
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
}
|
}
|
||||||
public String getPort() {
|
public String getWebhook() {
|
||||||
|
return webhook;
|
||||||
|
}
|
||||||
|
public void setWebhook(final String webhook) {
|
||||||
|
this.webhook = webhook;
|
||||||
|
}
|
||||||
|
public String getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
public void setPort(String port) {
|
public void setPort(String port) {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import static java.lang.String.format;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -18,69 +20,98 @@ import net.whistlingfish.harmony.HarmonyClientModule;
|
|||||||
import net.whistlingfish.harmony.config.Activity;
|
import net.whistlingfish.harmony.config.Activity;
|
||||||
import net.whistlingfish.harmony.protocol.OAReplyProvider;
|
import net.whistlingfish.harmony.protocol.OAReplyProvider;
|
||||||
|
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
public class HarmonyServer {
|
public class HarmonyServer {
|
||||||
|
|
||||||
|
private static final String ACTIVIY_ID = "${activity.id}";
|
||||||
|
private static final String ACTIVIY_LABEL = "${activity.label}";
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private HarmonyClient harmonyClient;
|
private HarmonyClient harmonyClient;
|
||||||
|
|
||||||
private HarmonyHandler myHarmony;
|
private HarmonyHandler myHarmony;
|
||||||
private DevModeResponse devResponse;
|
private DevModeResponse devResponse;
|
||||||
private OAReplyProvider dummyProvider;
|
private OAReplyProvider dummyProvider;
|
||||||
private NamedIP myNameAndIP;
|
private NamedIP myNameAndIP;
|
||||||
private Boolean isDevMode;
|
private Boolean isDevMode;
|
||||||
|
private HTTPHandler httpClient;
|
||||||
private Logger log = LoggerFactory.getLogger(HarmonyServer.class);
|
private Logger log = LoggerFactory.getLogger(HarmonyServer.class);
|
||||||
|
|
||||||
public HarmonyServer(NamedIP theHarmonyAddress) {
|
public HarmonyServer(NamedIP theHarmonyAddress) {
|
||||||
super();
|
super();
|
||||||
myHarmony = null;
|
myHarmony = null;
|
||||||
dummyProvider = null;
|
dummyProvider = null;
|
||||||
myNameAndIP = theHarmonyAddress;
|
myNameAndIP = theHarmonyAddress;
|
||||||
isDevMode = false;
|
isDevMode = false;
|
||||||
}
|
httpClient = new HTTPHandler();
|
||||||
|
}
|
||||||
|
|
||||||
public static HarmonyServer setup(BridgeSettingsDescriptor bridgeSettings, Boolean harmonyDevMode, NamedIP theHarmonyAddress) throws Exception {
|
public static HarmonyServer setup(
|
||||||
if(!bridgeSettings.isValidHarmony() && harmonyDevMode) {
|
BridgeSettingsDescriptor bridgeSettings,
|
||||||
return new HarmonyServer(theHarmonyAddress);
|
Boolean harmonyDevMode,
|
||||||
}
|
NamedIP theHarmonyAddress
|
||||||
Injector injector = null;
|
) throws Exception {
|
||||||
if(!harmonyDevMode)
|
if (!bridgeSettings.isValidHarmony() && harmonyDevMode) {
|
||||||
injector = Guice.createInjector(new HarmonyClientModule());
|
return new HarmonyServer(theHarmonyAddress);
|
||||||
HarmonyServer mainObject = new HarmonyServer(theHarmonyAddress);
|
|
||||||
if(!harmonyDevMode)
|
|
||||||
injector.injectMembers(mainObject);
|
|
||||||
mainObject.execute(bridgeSettings, harmonyDevMode);
|
|
||||||
return mainObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void execute(BridgeSettingsDescriptor mySettings, Boolean harmonyDevMode) throws Exception {
|
|
||||||
Boolean noopCalls = Boolean.parseBoolean(System.getProperty("noop.calls", "false"));
|
|
||||||
isDevMode = harmonyDevMode;
|
|
||||||
String modeString = "";
|
|
||||||
if(dummyProvider != null)
|
|
||||||
log.debug("something is very wrong as dummyProvider is not null...");
|
|
||||||
if(isDevMode)
|
|
||||||
modeString = " (development mode)";
|
|
||||||
else if(noopCalls)
|
|
||||||
modeString = " (no op calls to harmony)";
|
|
||||||
log.info("setup initiated " + modeString + "....");
|
|
||||||
if(isDevMode)
|
|
||||||
{
|
|
||||||
harmonyClient = null;
|
|
||||||
devResponse = new DevModeResponse();
|
|
||||||
}
|
}
|
||||||
else {
|
Injector injector = null;
|
||||||
devResponse = null;
|
if (!harmonyDevMode) {
|
||||||
harmonyClient.addListener(new ActivityChangeListener() {
|
injector = Guice.createInjector(new HarmonyClientModule());
|
||||||
@Override
|
}
|
||||||
public void activityStarted(Activity activity) {
|
HarmonyServer mainObject = new HarmonyServer(theHarmonyAddress);
|
||||||
log.info(format("activity changed: [%d] %s", activity.getId(), activity.getLabel()));
|
if (!harmonyDevMode) {
|
||||||
}
|
injector.injectMembers(mainObject);
|
||||||
});
|
}
|
||||||
harmonyClient.connect(myNameAndIP.getIp());
|
mainObject.execute(bridgeSettings, harmonyDevMode);
|
||||||
|
return mainObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void execute(BridgeSettingsDescriptor mySettings, Boolean harmonyDevMode) throws Exception {
|
||||||
|
Boolean noopCalls = Boolean.parseBoolean(System.getProperty("noop.calls", "false"));
|
||||||
|
isDevMode = harmonyDevMode;
|
||||||
|
String modeString = "";
|
||||||
|
if (dummyProvider != null) {
|
||||||
|
log.debug("something is very wrong as dummyProvider is not null...");
|
||||||
|
}
|
||||||
|
if (isDevMode) {
|
||||||
|
modeString = " (development mode)";
|
||||||
|
} else if (noopCalls) {
|
||||||
|
modeString = " (no op calls to harmony)";
|
||||||
|
}
|
||||||
|
log.info("setup initiated " + modeString + "....");
|
||||||
|
if (isDevMode) {
|
||||||
|
harmonyClient = null;
|
||||||
|
devResponse = new DevModeResponse();
|
||||||
|
} else {
|
||||||
|
devResponse = null;
|
||||||
|
harmonyClient.addListener(new ActivityChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void activityStarted(Activity activity) {
|
||||||
|
String webhook = myNameAndIP.getWebhook();
|
||||||
|
if(webhook != null) {
|
||||||
|
try {
|
||||||
|
// Replacing variables
|
||||||
|
webhook = webhook.replace(ACTIVIY_ID, activity.getId().toString());
|
||||||
|
webhook = webhook.replace(ACTIVIY_LABEL, URLEncoder.encode(activity.getLabel(), "UTF-8"));
|
||||||
|
|
||||||
|
log.info(format("calling webhook: %s", webhook));
|
||||||
|
|
||||||
|
// Calling webhook
|
||||||
|
httpClient.doHttpRequest(webhook, HttpGet.METHOD_NAME, null, null, null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("could not call webhook: " + webhook, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.info(format("activity changed: [%d] %s", activity.getId(), activity.getLabel()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
harmonyClient.connect(myNameAndIP.getIp());
|
||||||
}
|
}
|
||||||
myHarmony = new HarmonyHandler(harmonyClient, noopCalls, devResponse);
|
myHarmony = new HarmonyHandler(harmonyClient, noopCalls, devResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HarmonyHandler getMyHarmony() {
|
public HarmonyHandler getMyHarmony() {
|
||||||
return myHarmony;
|
return myHarmony;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,25 +141,30 @@
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>IP</th>
|
<th>IP</th>
|
||||||
|
<th>Webhook</th>
|
||||||
<th>Manage</th>
|
<th>Manage</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tr ng-repeat="harmony in bridge.settings.harmonyaddress.devices">
|
<tr ng-repeat="harmony in bridge.settings.harmonyaddress.devices">
|
||||||
<td>{{harmony.name}}</td>
|
<td>{{harmony.name}}</td>
|
||||||
<td>{{harmony.ip}}</td>
|
<td>{{harmony.ip}}</td>
|
||||||
|
<td>{{harmony.webhook}}</td>
|
||||||
<td><button class="btn btn-danger" type="submit"
|
<td><button class="btn btn-danger" type="submit"
|
||||||
ng-click="removeHarmonytoSettings(harmony.name, harmony.ip)">Del</button></td>
|
ng-click="removeHarmonytoSettings(harmony.name, harmony.ip, harmony.webhook)">Del</button></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><input id="bridge-settings-next-harmony-name"
|
<td><input id="bridge-settings-next-harmony-name"
|
||||||
class="form-control" type="text" ng-model="newharmonyname"
|
class="form-control" type="text" ng-model="newharmonyname"
|
||||||
placeholder="A Harmony"></td>
|
placeholder="A Harmony"></td>
|
||||||
<td><input id="bridge-settings-next-harmony-ip"
|
<td><input id="bridge-settings-next-harmony-ip"
|
||||||
class="form-control" type="text" ng-model="newharmonyip"
|
class="form-control" type="text" ng-model="newharmonyip"
|
||||||
placeholder="192.168.1.3"></td>
|
placeholder="192.168.1.3"></td>
|
||||||
|
<td><input id="bridge-settings-next-harmony-webhook"
|
||||||
|
class="form-control" type="text" ng-model="newharmonywebhook"
|
||||||
|
placeholder="http://hook?a=${activity.label}"></td>
|
||||||
<td><button class="btn btn-success" type="submit"
|
<td><button class="btn btn-success" type="submit"
|
||||||
ng-click="addHarmonytoSettings(newharmonyname, newharmonyip)">Add</button></td>
|
ng-click="addHarmonytoSettings(newharmonyname, newharmonyip, newharmonywebhook)">Add</button></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table></td>
|
</table></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user