Skip to content

Commit

Permalink
Merge pull request #158 from bwssytems/v3-updates
Browse files Browse the repository at this point in the history
V3.1.0 updates
  • Loading branch information
bwssytems authored Sep 1, 2016
2 parents c61e623 + 580c037 commit 20dedec
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
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>3.0.0</version>
<version>3.1.0</version>
<packaging>jar</packaging>

<name>HA Bridge</name>
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/com/bwssystems/HABridge/BridgeSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import org.apache.http.conn.util.InetAddressUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -178,14 +181,15 @@ private void _loadConfig() {

private void _loadConfig(Path aPath) {
String jsonContent = configReader(aPath);
if(jsonContent == null)
return;
try {
theBridgeSettings = new Gson().fromJson(jsonContent, BridgeSettingsDescriptor.class);
theBridgeSettings = new Gson().fromJson(jsonContent, BridgeSettingsDescriptor.class);
} catch (Exception e) {
log.warn("Issue loading values from file: " + aPath.toUri().toString() + ", Gson convert failed.");
theBridgeSettings = new BridgeSettingsDescriptor();
theBridgeSettings.setConfigfile(aPath.toString());
}

}

public void save(BridgeSettingsDescriptor newBridgeSettings) {
Expand Down Expand Up @@ -219,6 +223,19 @@ private void configWriter(String content, Path filePath) {
Files.move(filePath, target);
}
Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);

// set attributes to be for user only
// using PosixFilePermission to set file permissions
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
// add owners permission
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);

try {
Files.setPosixFilePermissions(filePath, perms);
} catch(UnsupportedOperationException e) {
log.info("Cannot set permissions for config file on this system as it is not supported. Continuing");
}
if(target != null)
Files.delete(target);
} catch (IOException e) {
Expand All @@ -233,7 +250,6 @@ private String configReader(Path filePath) {
return null;
}


try {
content = new String(Files.readAllBytes(filePath));
} catch (IOException e) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/bwssystems/HABridge/api/hue/DeviceState.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DeviceState {
private String colormode;
private boolean reachable;
private List<Double> xy;
private int transitiontime;

public boolean isOn() {
return on;
Expand Down Expand Up @@ -97,7 +98,15 @@ public List<Double> getXy() {
public void setXy(List<Double> xy) {
this.xy = xy;
}
public static DeviceState createDeviceState() {
public int getTransitiontime() {
return transitiontime;
}

public void setTransitiontime(int transitiontime) {
this.transitiontime = transitiontime;
}

public static DeviceState createDeviceState() {
DeviceState newDeviceState = new DeviceState();
newDeviceState.fillIn();
// newDeviceState.setColormode("none");
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/com/bwssystems/HABridge/hue/HueMulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,16 @@ else if((state.getBri() + theStateChanges.getBri_inc()) <= 0 && state.isOn())
else
{
if (theStateChanges.isOn()) {
state.setOn(true);
if(state.getBri() <= 0)
state.setBri(255);
} else {
state.setOn(false);
state.setBri(0);
}
}
responseString = this.formatSuccessHueResponse(theStateChanges, request.body(), lightId, device.getDeviceState());
device.getDeviceState().setBri(calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc));
responseString = this.formatSuccessHueResponse(theStateChanges, request.body(), lightId);

return responseString;
});
Expand Down Expand Up @@ -625,9 +627,9 @@ else if(responseString.contains("[{\"error\":") && responseString.contains("unau
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"No HUE configured\", \"parameter\": \"/lights/" + lightId + "state\"}}]";

if(responseString == null || !responseString.contains("[{\"error\":")) {
responseString = this.formatSuccessHueResponse(theStateChanges, request.body(), lightId, state);
state.setBri(calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc));
device.setDeviceState(state);
responseString = this.formatSuccessHueResponse(theStateChanges, request.body(), lightId);
}
return responseString;
}
Expand Down Expand Up @@ -882,9 +884,9 @@ else if(callItems[i].getItem().contains("exec://")) {
}

if(responseString == null || !responseString.contains("[{\"error\":")) {
responseString = this.formatSuccessHueResponse(theStateChanges, request.body(), lightId, state);
state.setBri(calculateIntensity(state, theStateChanges, stateHasBri, stateHasBriInc));
device.setDeviceState(state);
responseString = this.formatSuccessHueResponse(theStateChanges, request.body(), lightId);
}
return responseString;
});
Expand Down Expand Up @@ -1036,7 +1038,7 @@ private String doExecRequest(String anItem, int intensity, String lightId) {
return responseString;
}

private String formatSuccessHueResponse(StateChangeBody state, String body, String lightId) {
private String formatSuccessHueResponse(StateChangeBody state, String body, String lightId, DeviceState deviceState) {

String responseString = "[";
boolean notFirstChange = false;
Expand All @@ -1048,6 +1050,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
} else {
responseString = responseString + "false}}";
}
if(deviceState != null)
deviceState.setOn(state.isOn());
notFirstChange = true;
}

Expand All @@ -1056,6 +1060,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri\":" + state.getBri() + "}}";
if(deviceState != null)
deviceState.setBri(state.getBri());
notFirstChange = true;
}

Expand All @@ -1064,14 +1070,17 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri_inc\":" + state.getBri_inc() + "}}";
notFirstChange = true;
//INFO: Bright inc check for deviceState needs to be outside of this method
notFirstChange = true;
}

if(body.contains("\"ct\""))
{
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct\":" + state.getCt() + "}}";
if(deviceState != null)
deviceState.setCt(state.getCt());
notFirstChange = true;
}

Expand All @@ -1080,6 +1089,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy\":" + state.getXy() + "}}";
if(deviceState != null)
deviceState.setXy(state.getXy());
notFirstChange = true;
}

Expand All @@ -1088,6 +1099,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue\":" + state.getHue() + "}}";
if(deviceState != null)
deviceState.setHue(state.getHue());
notFirstChange = true;
}

Expand All @@ -1096,6 +1109,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat\":" + state.getSat() + "}}";
if(deviceState != null)
deviceState.setSat(state.getSat());
notFirstChange = true;
}

Expand All @@ -1104,6 +1119,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct_inc\":" + state.getCt_inc() + "}}";
if(deviceState != null)
deviceState.setCt(deviceState.getCt() + state.getCt_inc());
notFirstChange = true;
}

Expand All @@ -1112,6 +1129,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy_inc\":" + state.getXy_inc() + "}}";
if(deviceState != null)
deviceState.setXy(state.getXy());
notFirstChange = true;
}

Expand All @@ -1120,6 +1139,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue_inc\":" + state.getHue_inc() + "}}";
if(deviceState != null)
deviceState.setHue(deviceState.getHue() + state.getHue_inc());
notFirstChange = true;
}

Expand All @@ -1128,6 +1149,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat_inc\":" + state.getSat_inc() + "}}";
if(deviceState != null)
deviceState.setSat(deviceState.getSat() + state.getSat_inc());
notFirstChange = true;
}

Expand All @@ -1136,6 +1159,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/effect\":" + state.getEffect() + "}}";
if(deviceState != null)
deviceState.setEffect(state.getEffect());
notFirstChange = true;
}

Expand All @@ -1144,6 +1169,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/transitiontime\":" + state.getTransitiontime() + "}}";
if(deviceState != null)
deviceState.setTransitiontime(state.getTransitiontime());
notFirstChange = true;
}

Expand All @@ -1152,6 +1179,8 @@ private String formatSuccessHueResponse(StateChangeBody state, String body, Stri
if(notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/alert\":" + state.getAlert() + "}}";
if(deviceState != null)
deviceState.setAlert(state.getAlert());
notFirstChange = true;
}

Expand Down

0 comments on commit 20dedec

Please sign in to comment.