use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class YamahaReceiverBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
YamahaReceiverBindingConfig config = getConfigForItemName(itemName);
if (config == null) {
logger.error("Received command for unknown item '" + itemName + "'");
return;
}
YamahaReceiverProxy proxy = proxies.get(config.getDeviceUid());
if (proxy == null) {
logger.error("Received command for unknown device uid '" + config.getDeviceUid() + "'");
return;
}
if (logger.isDebugEnabled()) {
logger.debug(BINDING_NAME + " processing command '" + command + "' of type '" + command.getClass().getSimpleName() + "' for item '" + itemName + "'");
}
try {
Zone zone = config.getZone();
BindingType type = config.getBindingType();
if (type == BindingType.power) {
if (command instanceof OnOffType) {
proxy.setPower(zone, command == OnOffType.ON);
}
} else if (type == BindingType.volumePercent || type == BindingType.volumeDb) {
if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
// increase/decrease dB by .5 dB
float db = proxy.getState(zone).getVolume();
float adjAmt;
if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
adjAmt = .5f;
} else {
adjAmt = -.5f;
}
float newDb = db + adjAmt;
proxy.setVolume(zone, newDb);
// send new value as update
State newState = new DecimalType(newDb);
eventPublisher.postUpdate(itemName, newState);
} else if (command instanceof PercentType) {
// set dB from percent
byte percent = ((PercentType) command).byteValue();
int db = percentToDB(percent);
proxy.setVolume(zone, db);
} else {
// set dB from value
float db = Float.parseFloat(command.toString());
proxy.setVolume(zone, db);
}
// Volume updates multiple values => send update now
sendUpdates(proxy, config.getDeviceUid());
} else if (type == BindingType.mute) {
if (command instanceof OnOffType) {
proxy.setMute(zone, command == OnOffType.ON);
}
} else if (type == BindingType.input) {
proxy.setInput(zone, parseString(command.toString()));
} else if (type == BindingType.surroundProgram) {
proxy.setSurroundProgram(zone, parseString(command.toString()));
} else if (type == BindingType.netRadio) {
if (command instanceof DecimalType) {
proxy.setNetRadio(((DecimalType) command).intValue());
}
}
} catch (IOException e) {
logger.warn("Cannot communicate with " + proxy.getHost() + " (uid: " + config.getDeviceUid() + ")");
} catch (Throwable t) {
logger.error("Error processing command '" + command + "' for item '" + itemName + "'", t);
}
}
use of org.openhab.core.library.types.PercentType 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.PercentType in project openhab1-addons by openhab.
the class JdbcBaseDAO method getState.
/*****************
* H E L P E R S *
*****************/
protected State getState(Item item, Object v) {
String clazz = v.getClass().getSimpleName();
logger.debug("JDBC::ItemResultHandler::handleResult getState value = '{}', getClass = '{}', clazz = '{}'", v.toString(), v.getClass(), clazz);
if (item instanceof NumberItem) {
String it = getSqlTypes().get("NUMBERITEM");
if (it.toUpperCase().contains("DOUBLE")) {
return new DecimalType(((Number) v).doubleValue());
} else if (it.toUpperCase().contains("DECIMAL") || it.toUpperCase().contains("NUMERIC")) {
return new DecimalType((BigDecimal) v);
} else if (it.toUpperCase().contains("INT")) {
return new DecimalType(((Integer) v).intValue());
}
return DecimalType.valueOf(((String) v).toString());
} else if (item instanceof ColorItem) {
return HSBType.valueOf(((String) v).toString());
} else if (item instanceof DimmerItem) {
return new PercentType(objectAsInteger(v));
} else if (item instanceof SwitchItem) {
return OnOffType.valueOf(((String) v).toString().trim());
} else if (item instanceof ContactItem) {
return OpenClosedType.valueOf(((String) v).toString().trim());
} else if (item instanceof RollershutterItem) {
return new PercentType(objectAsInteger(v));
} else if (item instanceof DateTimeItem) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(objectAsLong(v));
return new DateTimeType(calendar);
} else if (item instanceof StringItem) {
return StringType.valueOf(((String) v).toString());
} else {
// Call, Location, String
return StringType.valueOf(((String) v).toString());
}
}
use of org.openhab.core.library.types.PercentType 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);
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class AbstractDynamoDBItemSerializationTest method testUpDownTypeWithRollershutterItem.
@Test
public void testUpDownTypeWithRollershutterItem() throws IOException {
// note: comes back as PercentType instead of the original UpDownType
DynamoDBItem<?> dbItemDown = testStateGeneric(UpDownType.DOWN, BigDecimal.ZERO);
testAsHistoricGeneric(dbItemDown, new RollershutterItem("foo"), new PercentType(BigDecimal.ZERO));
DynamoDBItem<?> dbItemUp = testStateGeneric(UpDownType.UP, BigDecimal.ONE);
testAsHistoricGeneric(dbItemUp, new RollershutterItem("foo"), new PercentType(BigDecimal.ONE));
}
Aggregations