Skip to content

Commit

Permalink
Update harmony java client to new version v1.1.0. Add enhanced button
Browse files Browse the repository at this point in the history
push parameters of delay and count. Updated Readme.md for deprecated
username and password and added delay and count for button presses.

Fixes #5
Fixes #6
  • Loading branch information
bwssytems committed Nov 18, 2016
1 parent 7f88831 commit cb50101
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 26 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ java -jar -Dserver.port=8081 restful-harmony-0.X.Y.jar <harmony hub ip> <harmony
Optional: The server defaults to using 100 milliseconds between button presses. If this is not recognized very well by your device, use -Dbutton.sleep=`<milliseconds>` on the command line.
### `<harmony hub ip>`
Required: This is the IP address of your harmony hub.
### `<harmony user name>`
Required: This is your username that you registered at MyHarmony.com.
### `<harmony password>`
Required: This is your password that you registered at MyHarmony.com.
### `<harmony user name>` deprecated

### `<harmony password>` deprecated

## Api usage
This application exposes a restful api using the constructs for GET/PUT/POST. The following are the commands in the api that are available. The api address is: http://<ip address>:<port>/harmony and the context we will use below for examples is http://host:8081/harmony.

Expand Down Expand Up @@ -143,8 +143,10 @@ PUT http://host:8081/harmony/press
The body arguments can be one pair of device and button or an array to simulate multiple presses in one call.
Name | Type | Description | Required
-----|-------|--------------|------------
device | string | A name or id for the device to send the button press.
button | string | A name for the button that is pressed.
device | string | A name or id for the device to send the button press. | Yes
button | string | A name for the button that is pressed. | Yes
delay | number | A number of milliseconds to delay to the next call. | Optional
count | number | A number of times to execute this button push. | Optional
```
{
"device" : "TV",
Expand All @@ -158,7 +160,7 @@ OR
"device":"TV","button":"1"
},
{
"device":"TV","button:"2"
"device":"TV","button:"2","delay":150,"count":5
},
{
"device":"TV","button:"3"
Expand Down
20 changes: 18 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.bwssystems</groupId>
<artifactId>restful-harmony</artifactId>
<version>0.1.6</version>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>com.github.bwssytems</groupId>
<artifactId>harmony-java-client</artifactId>
<version>1.0.9</version>
<version>1.1.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
Expand Down Expand Up @@ -91,6 +91,22 @@
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>version.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>version.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ public class HarmonyRestServer {

public static void main(String[] args) throws Exception {
log = LoggerFactory.getLogger(HarmonyRestServer.class);
Version theVersion;

theVersion = new Version();
devMode = Boolean.parseBoolean(System.getProperty("dev.mode", "false"));
noopCalls = Boolean.parseBoolean(System.getProperty("noop.calls", "false"));
String modeString = "";
if(devMode)
modeString = " (development mode)";
if(noopCalls)
modeString = " (no op calls to harmony)";
log.info("Harmony v0.1.5 rest server running" + modeString + "....");
log.info("Harmony v" + theVersion.getVersion() + " rest server running" + modeString + "....");
Injector injector = null;
if(!devMode)
injector = Guice.createInjector(new HarmonyClientModule());
Expand All @@ -63,7 +66,7 @@ public void activityStarted(Activity activity) {
log.info(format("activity changed: [%d] %s", activity.getId(), activity.getLabel()));
}
});
harmonyClient.connect(args[0], args[1], args[2]);
harmonyClient.connect(args[0]);
}
port(Integer.valueOf(System.getProperty("server.port", "8081")));
int sleepTime = Integer.parseInt(System.getProperty("button.sleep", "100"));
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/bwssystems/restfulharmony/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.bwssystems.restfulharmony;

import java.io.InputStream;
import java.util.Properties;


public final class Version {

private String version;
private String groupId;
private String artifactId;
private Properties prop;

public Version()
{
InputStream resourceAsStream =
(InputStream) this.getClass().getResourceAsStream(
"/version.properties"
);
this.prop = new Properties();

try
{
this.prop.load( resourceAsStream );
this.version = this.prop.getProperty("version");
this.groupId = this.prop.getProperty("groupId");
this.artifactId = this.prop.getProperty("artifactId");
}
catch (Exception e)
{
this.version = "0.0.0";
this.groupId = "no group";
this.artifactId = "no artifact";
}

}

public String getVersion() {
return version;
}

public String getGroupId() {
return groupId;
}

public String getArtifactId() {
return artifactId;
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/bwssystems/restfulharmony/api/DeviceButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public class DeviceButton {
private String device;
private String button;
private Integer delay;
private Integer count;
public String getDevice() {
return device;
}
Expand All @@ -15,4 +17,16 @@ public String getButton() {
public void setButton(String button) {
this.button = button;
}
public Integer getDelay() {
return delay;
}
public void setDelay(Integer delay) {
this.delay = delay;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
42 changes: 27 additions & 15 deletions src/main/java/com/bwssystems/restfulharmony/api/HarmonyRest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,40 @@ public void setupServer() {
DeviceButton aDeviceButtons[];
String theArguments = request.body();
String aResponse = "{\"status\": \"OK\"}";
Integer theDelay = sleepTime;
int status_code = HttpStatus.SC_OK;
if(request.body() != null && !request.body().isEmpty()) {
if(theArguments.substring(0, 1).equalsIgnoreCase("{")) {
theArguments = "[" + theArguments +"]";
}
aDeviceButtons = new Gson().fromJson(theArguments, DeviceButton[].class);
Integer setCount = 1;
for(int i = 0; i < aDeviceButtons.length; i++) {
if( i > 0)
Thread.sleep(sleepTime);
try {
if(noopCalls || devMode)
log.info("Noop: press call would be device: " + aDeviceButtons[i].getDevice() + " for button: " + aDeviceButtons[i].getButton());
else
harmonyClient.pressButton(Integer.parseInt(aDeviceButtons[i].getDevice()), aDeviceButtons[i].getButton());
} catch (IllegalArgumentException e) {
try {
harmonyClient.pressButton(aDeviceButtons[i].getDevice(), aDeviceButtons[i].getButton());
} catch (IllegalArgumentException ei) {
aResponse = "{\"status\": \"" + ei.getMessage() + "\"}";
status_code = HttpStatus.SC_NOT_FOUND;
}
}
if(aDeviceButtons[i].getCount() != null && aDeviceButtons[i].getCount() > 0)
setCount = aDeviceButtons[i].getCount();
else
setCount = 1;
for(int x = 0; x < setCount; x++) {
if( x > 0 || i > 0)
Thread.sleep(theDelay);
if(aDeviceButtons[i].getDelay() != null && aDeviceButtons[i].getDelay() > 0)
theDelay = aDeviceButtons[i].getDelay();
else
theDelay = sleepTime;
try {
if(noopCalls || devMode)
log.info("Noop: press call would be device: " + aDeviceButtons[i].getDevice() + " for button: " + aDeviceButtons[i].getButton());
else
harmonyClient.pressButton(Integer.parseInt(aDeviceButtons[i].getDevice()), aDeviceButtons[i].getButton());
} catch (IllegalArgumentException e) {
try {
harmonyClient.pressButton(aDeviceButtons[i].getDevice(), aDeviceButtons[i].getButton());
} catch (IllegalArgumentException ei) {
aResponse = "{\"status\": \"" + ei.getMessage() + "\"}";
status_code = HttpStatus.SC_NOT_FOUND;
}
}
}
}
}
else
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version=${project.version}
groupId=${project.groupId}
artifactId=${project.artifactId}

0 comments on commit cb50101

Please sign in to comment.