Skip to content

Commit

Permalink
Checks for accepted command typs for HSB and Decimal types as well a…
Browse files Browse the repository at this point in the history
…s reports on HSB types, fixes openhab#1261, fixes openhab#1266

Signed-off-by: Dan Cunningham <dan@digitaldan.com>
  • Loading branch information
digitaldan committed Sep 28, 2016
1 parent 79e9245 commit db810fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.items.events.ItemEventFactory;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.HSBType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.TypeParser;
Expand Down Expand Up @@ -258,15 +260,22 @@ private void apiState(String id, HttpServletRequest req, HttpServletResponse res
// will throw exception if not found
Item item = itemRegistry.getItem(id);
HueState state = gson.fromJson(req.getReader(), HueState.class);

logger.debug("State " + state);

String value;
if (state.bri > -1) {
if (item.getAcceptedCommandTypes().contains(HSBType.class)) {
value = String.format("%d,%d,%d", state.hue, state.sat, state.bri);
} else if (state.bri > -1 && (item.getAcceptedCommandTypes().contains(DecimalType.class)
|| item.getAcceptedCommandTypes().contains(PercentType.class))) {
value = String.valueOf(Math.round(state.bri / 255.0 * 100));
} else {
value = state.on ? "ON" : "OFF";
}

Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), value);
eventPublisher.post(ItemEventFactory.createCommandEvent(id, command));

PrintWriter out = resp.getWriter();
out.write(String.format(STATE_RESP, id, String.valueOf(state.on)));
out.close();
Expand Down Expand Up @@ -434,13 +443,20 @@ public Map<String, String> getHueDeviceNames() {
*/
private HueDevice itemToDevice(Item item) {
State itemState = item.getState();
short bri = 0;
if (itemState instanceof DecimalType) {
bri = (short) ((((DecimalType) itemState).intValue() * 255) / 100);
HueState hueState;
if (itemState instanceof HSBType) {
HSBType color = (HSBType) itemState;
hueState = new HueState(color.getHue().intValue(), color.getSaturation().shortValue(),
color.getBrightness().shortValue());
} else if (itemState instanceof DecimalType) {
short bri = (short) ((((DecimalType) itemState).intValue() * 255) / 100);
hueState = new HueState(bri > 0, bri);
} else if (itemState instanceof OnOffType) {
bri = (short) (((OnOffType) itemState) == OnOffType.ON ? 255 : 0);
short bri = (short) (((OnOffType) itemState) == OnOffType.ON ? 255 : 0);
hueState = new HueState(bri > 0, bri);
} else {
hueState = new HueState(false, (short) 0);
}
HueState hueState = new HueState(bri > 0, bri);
HueDevice d = new HueDevice(hueState, item.getLabel(), item.getName());
return d;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public HueState(boolean on, short bri) {
this.bri = bri;
}

public HueState(int h, short s, short b) {
super();
this.on = b > 0;
this.hue = h;
this.sat = s;
this.bri = b;
}

@Override
public String toString() {
String xyString = "{";
Expand Down

0 comments on commit db810fa

Please sign in to comment.