use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.
the class HueBinding method computeCommandForItemOnBridge.
/**
* Checks whether the command is for one of the configured Hue bulbs. If
* this is the case, the command is translated to the corresponding action
* which is then sent to the given bulb.
*
* @param command
* The command from the openHAB bus.
* @param itemName
* The name of the targeted item.
* @param bridge
* The Hue bridge the Hue bulb is connected to
*/
private void computeCommandForItemOnBridge(Command command, String itemName, HueBridge bridge) {
HueBindingConfig deviceConfig = getConfigForItemName(itemName);
if (deviceConfig == null) {
return;
}
HueBulb bulb = bulbCache.get(deviceConfig.getDeviceId());
if (bulb == null) {
bulb = new HueBulb(bridge, deviceConfig.getDeviceId());
bulbCache.put(deviceConfig.getDeviceId(), bulb);
}
if (command instanceof OnOffType) {
bulb.switchOn(OnOffType.ON.equals(command));
}
if (command instanceof HSBType) {
HSBType hsbCommand = (HSBType) command;
DecimalType hue = hsbCommand.getHue();
PercentType sat = hsbCommand.getSaturation();
PercentType bri = hsbCommand.getBrightness();
bulb.colorizeByHSB(hue.doubleValue() / 360, sat.doubleValue() / 100, bri.doubleValue() / 100);
}
if (deviceConfig.getType().equals(BindingType.brightness) || deviceConfig.getType().equals(BindingType.rgb)) {
if (IncreaseDecreaseType.INCREASE.equals(command)) {
int resultingValue = bulb.increaseBrightness(deviceConfig.getStepSize());
eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
} else if (IncreaseDecreaseType.DECREASE.equals(command)) {
int resultingValue = bulb.decreaseBrightness(deviceConfig.getStepSize());
eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
} else if ((command instanceof PercentType) && !(command instanceof HSBType)) {
bulb.setBrightness((int) Math.round((double) HueBulb.MAX_BRIGHTNESS / (double) 100 * ((PercentType) command).intValue()));
}
}
if (deviceConfig.getType().equals(BindingType.colorTemperature)) {
if (IncreaseDecreaseType.INCREASE.equals(command)) {
bulb.increaseColorTemperature(deviceConfig.getStepSize());
} else if (IncreaseDecreaseType.DECREASE.equals(command)) {
bulb.decreaseColorTemperature(deviceConfig.getStepSize());
} else if (command instanceof PercentType) {
bulb.setColorTemperature((int) Math.round((((double) 346 / (double) 100) * ((PercentType) command).intValue()) + 154));
}
}
}
use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.
the class TinkerforgeBinding method internalReceiveCommand.
/**
* {@inheritDoc}
*
* Searches the item with the given {@code itemName} in the {@link TinkerforgeBindingProvider}
* collection and gets the uid and subid of the device. The appropriate device is searched in the
* ecosystem and the command is executed on the device.
*
* {@code OnOffType} commands are executed on {@link MInSwitchActor} objects. {@code StringType}
* commands are executed on {@link MTextActor} objects.
*
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
logger.debug("received command {} for item {}", command, itemName);
for (TinkerforgeBindingProvider provider : providers) {
for (String itemNameP : provider.getItemNames()) {
if (itemNameP.equals(itemName)) {
String deviceUid = provider.getUid(itemName);
String deviceSubId = provider.getSubId(itemName);
String deviceName = provider.getName(itemName);
if (deviceName != null) {
String[] ids = getDeviceIdsForDeviceName(deviceName);
deviceUid = ids[0];
deviceSubId = ids[1];
}
logger.trace("{} found item for command: uid: {}, subid: {}", LoggerConstants.COMMAND, deviceUid, deviceSubId);
MBaseDevice mDevice = tinkerforgeEcosystem.getDevice(deviceUid, deviceSubId);
if (mDevice != null && mDevice.getEnabledA().get()) {
if (command instanceof OnOffType) {
logger.trace("{} found onoff command", LoggerConstants.COMMAND);
OnOffType cmd = (OnOffType) command;
if (mDevice instanceof MSwitchActor) {
OnOffValue state = cmd == OnOffType.OFF ? OnOffValue.OFF : OnOffValue.ON;
((MSwitchActor) mDevice).turnSwitch(state);
} else if (mDevice instanceof DigitalActor) {
HighLowValue state = cmd == OnOffType.OFF ? HighLowValue.LOW : HighLowValue.HIGH;
((DigitalActor) mDevice).turnDigital(state);
} else if (mDevice instanceof ProgrammableSwitchActor) {
OnOffValue state = cmd == OnOffType.OFF ? OnOffValue.OFF : OnOffValue.ON;
((ProgrammableSwitchActor) mDevice).turnSwitch(state, provider.getDeviceOptions(itemName));
} else {
logger.error("{} received OnOff command for non-SwitchActor", LoggerConstants.COMMAND);
}
} else if (command instanceof StringType) {
logger.trace("{} found string command", LoggerConstants.COMMAND);
if (mDevice instanceof MTextActor) {
((MTextActor) mDevice).write(command.toString());
}
} else if (command instanceof DecimalType) {
logger.debug("{} found number command", LoggerConstants.COMMAND);
if (command instanceof HSBType) {
logger.debug("{} found HSBType command", LoggerConstants.COMMAND);
if (mDevice instanceof ProgrammableColorActor) {
logger.debug("{} found ProgrammableColorActor {}", itemName);
((ProgrammableColorActor) mDevice).setSelectedColor((HSBType) command, provider.getDeviceOptions(itemName));
} else if (mDevice instanceof SimpleColorActor) {
logger.debug("{} found SimpleColorActor {}", itemName);
((SimpleColorActor) mDevice).setSelectedColor((HSBType) command);
}
} else if (command instanceof PercentType) {
if (mDevice instanceof SetPointActor) {
((SetPointActor<?>) mDevice).setValue(((PercentType) command), provider.getDeviceOptions(itemName));
logger.debug("found SetpointActor");
} else if (mDevice instanceof PercentTypeActor) {
((PercentTypeActor) mDevice).setValue(((PercentType) command), provider.getDeviceOptions(itemName));
logger.debug("found PercentType actor");
} else {
logger.error("found no percenttype actor");
}
} else {
if (mDevice instanceof NumberActor) {
((NumberActor) mDevice).setNumber(((DecimalType) command).toBigDecimal());
} else if (mDevice instanceof SetPointActor) {
((SetPointActor<?>) mDevice).setValue(((DecimalType) command).toBigDecimal(), provider.getDeviceOptions(itemName));
} else {
logger.error("found no number actor");
}
}
} else if (command instanceof UpDownType) {
UpDownType cmd = (UpDownType) command;
logger.debug("{} UpDownType command {}", itemName, cmd);
if (mDevice instanceof MoveActor) {
((MoveActor) mDevice).move((UpDownType) command, provider.getDeviceOptions(itemName));
}
} else if (command instanceof StopMoveType) {
StopMoveType cmd = (StopMoveType) command;
if (mDevice instanceof MoveActor) {
if (cmd == StopMoveType.STOP) {
((MoveActor) mDevice).stop();
} else {
((MoveActor) mDevice).moveon(provider.getDeviceOptions(itemName));
}
}
logger.debug("{} StopMoveType command {}", itemName, cmd);
} else if (command instanceof IncreaseDecreaseType) {
IncreaseDecreaseType cmd = (IncreaseDecreaseType) command;
if (mDevice instanceof DimmableActor) {
((DimmableActor<?>) mDevice).dimm((IncreaseDecreaseType) command, provider.getDeviceOptions(itemName));
}
logger.debug("{} IncreaseDecreaseType command {}", itemName, cmd);
} else {
logger.error("{} got unknown command type: {}", LoggerConstants.COMMAND, command.toString());
}
} else {
logger.error("{} no tinkerforge device found for command for item uid: {} subId: {}", LoggerConstants.COMMAND, deviceUid, deviceSubId);
}
}
}
}
}
use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.
the class ColorColorImpl method fetchSensorValue.
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated NOT
*/
@Override
public void fetchSensorValue() {
try {
com.tinkerforge.BrickletColor.Color color = tinkerforgeDevice.getColor();
setSensorValue(new HSBValue(new HSBType(new Color(getRGBValue(color.r), getRGBValue(color.g), getRGBValue(color.b)))));
} catch (TimeoutException e) {
TinkerforgeErrorHandler.handleError(this, TinkerforgeErrorHandler.TF_TIMEOUT_EXCEPTION, e);
} catch (NotConnectedException e) {
TinkerforgeErrorHandler.handleError(this, TinkerforgeErrorHandler.TF_NOT_CONNECTION_EXCEPTION, e);
}
}
use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.
the class InfluxDBPersistenceService method objectToState.
/**
* Converts a value to a {@link State} which is suitable for the given {@link Item}. This is
* needed for querying a {@link HistoricState}.
*
* @param value to be converted to a {@link State}
* @param itemName name of the {@link Item} to get the {@link State} for
* @return the state of the item represented by the itemName parameter,
* else the string value of the Object parameter
*/
private State objectToState(Object value, String itemName) {
String valueStr = String.valueOf(value);
if (itemRegistry != null) {
try {
Item item = itemRegistry.getItem(itemName);
if (item instanceof NumberItem) {
return new DecimalType(valueStr);
} else if (item instanceof ColorItem) {
return new HSBType(valueStr);
} else if (item instanceof DimmerItem) {
return new PercentType(valueStr);
} else if (item instanceof SwitchItem) {
return string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF) ? OnOffType.OFF : OnOffType.ON;
} else if (item instanceof ContactItem) {
return (string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF)) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
} else if (item instanceof RollershutterItem) {
return new PercentType(valueStr);
} else if (item instanceof DateTimeItem) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(new BigDecimal(valueStr).longValue());
return new DateTimeType(calendar);
} else {
return new StringType(valueStr);
}
} catch (ItemNotFoundException e) {
logger.warn("Could not find item '{}' in registry", itemName);
}
}
// just return a StringType as a fallback
return new StringType(valueStr);
}
use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.
the class AbstractDynamoDBItemSerializationTest method testHSBTypeWithColorItem.
@Test
public void testHSBTypeWithColorItem() throws IOException {
HSBType hsb = new HSBType(new DecimalType(1.5), new PercentType(new BigDecimal(2.5)), new PercentType(new BigDecimal(3.5)));
DynamoDBItem<?> dbitem = testStateGeneric(hsb, "1.5,2.5,3.5");
testAsHistoricGeneric(dbitem, new ColorItem("foo"), hsb);
}
Aggregations