use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class PioneerAvrBinding method convertDeviceValueToOpenHabState.
/**
* Convert receiver value to OpenHAB state.
*
* @param itemType
* @param data
*
* @return
*/
private State convertDeviceValueToOpenHabState(Class<? extends Item> itemType, String data, IpControlCommand cmdType) {
State state = UnDefType.UNDEF;
try {
int index;
// cut off the leading response identifier to get the payload string
String payloadSubstring = data.substring(cmdType.getResponse().length());
// the selected source will then be set to "ON"
if (payloadSubstring.length() == 0) {
payloadSubstring = "1";
}
// special case for display info query: convert to human readable string
if (cmdType.getCommandRef() == IpControlCommandRef.DISPLAY_INFO_QUERY) {
IpControlDisplayInformation displayInfo = new IpControlDisplayInformation(payloadSubstring);
payloadSubstring = displayInfo.getInfoText();
logger.debug("DisplayInfo: converted value '{}' to string '{}'", data, payloadSubstring);
}
if (itemType == SwitchItem.class) {
index = Integer.parseInt(payloadSubstring);
// according to Spec: 0=ON, 1=OFF!
state = (index == 0) ? OnOffType.ON : OnOffType.OFF;
} else if (itemType == DimmerItem.class) {
if (cmdType.getCommandRef().getCommand() == IpControlCommandRef.VOLUME_QUERY.getCommand() || cmdType.getCommandRef().getCommand() == IpControlCommandRef.VOLUME_SET.getCommand()) {
index = convertVolumeToPercent(Integer.parseInt(payloadSubstring));
} else {
index = Integer.parseInt(payloadSubstring);
}
state = new PercentType(index);
} else if (itemType == NumberItem.class) {
index = Integer.parseInt(payloadSubstring);
state = new DecimalType(index);
} else if (itemType == RollershutterItem.class) {
index = Integer.parseInt(payloadSubstring);
state = new PercentType(index);
} else if (itemType == StringItem.class) {
state = new StringType(payloadSubstring);
}
} catch (Exception e) {
logger.debug("Cannot convert value '{}' to data type {}", data, itemType);
}
return state;
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class PilightGenericBindingProviderTest method testNumberItemScale.
@Test
public void testNumberItemScale() throws BindingConfigParseException {
String bindingConfig = "kaku#weather,property=temperature";
PilightBindingConfig config = provider.parseBindingConfig(testItem, bindingConfig);
Assert.assertNotNull(config);
DecimalType number0 = (DecimalType) binding.getState("23", config);
Assert.assertEquals(number0.toBigDecimal().compareTo(new BigDecimal("23")), 0);
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class ZWaveCentralSceneConverter method handleEvent.
@Override
void handleEvent(ZWaveCommandClassValueEvent event, Item item, Map<String, String> arguments) {
org.openhab.core.types.State itemState = null;
// get the central scene command value event properties
Properties properties = (Properties) event.getValue();
int event_scene = (Integer) properties.get("scene");
int event_key = (Integer) properties.get("key");
// then only forward the event for the specified scene number
if (arguments.get("scene") != null) {
// get the item specified scene number
Integer scene = null;
try {
scene = Integer.parseInt(arguments.get("scene"));
} catch (NumberFormatException e) {
logger.error("NODE {}: Number format exception scene={}", event.getNodeId(), arguments.get("scene"));
return;
}
// from the received command event
if (scene != null && scene != event_scene) {
return;
}
// then only forward the event for the specified matching key state
if (arguments.get("key") != null) {
// get the item specified key (state)
Integer key = null;
try {
key = Integer.parseInt(arguments.get("key"));
} catch (NumberFormatException e) {
logger.error("NODE {}: Number format exception key={}", event.getNodeId(), arguments.get("key"));
return;
}
// from the received command event
if (key != null && key != event_key) {
return;
}
// get state converter
ZWaveStateConverter<?, ?> converter = this.getStateConverter(item, event_scene);
if (converter == null) {
logger.warn("No converter found for item = {}, node = {} endpoint = {}, ignoring event.", item.getName(), event.getNodeId(), event.getEndpoint());
return;
}
// convert the scene number and key state to a binary value on a
// successful match
itemState = converter.convertFromValueToState((event_key == key) ? 1 : 0);
} else {
// get state converter
ZWaveStateConverter<?, ?> converter = this.getStateConverter(item, event_key);
if (converter == null) {
logger.warn("No converter found for item = {}, node = {} endpoint = {}, ignoring event.", item.getName(), event.getNodeId(), event.getEndpoint());
return;
}
// convert the scene's key number to an acceptable conversion
// type and then publish the event
itemState = converter.convertFromValueToState(event_key);
}
} else {
// no scene argument provide, so we will convert the central scene
// number
itemState = new DecimalType(event_scene);
}
// publish the central scene update
this.getEventPublisher().postUpdate(item.getName(), itemState);
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class YamahaReceiverBinding method sendUpdates.
private void sendUpdates(YamahaReceiverProxy receiverProxy, String deviceUid) {
// Get all item configurations belonging to this proxy
Collection<YamahaReceiverBindingConfig> configs = getDeviceConfigs(deviceUid);
try {
for (Zone zone : Zone.values()) {
// Poll the state from the device
YamahaReceiverState state = receiverProxy.getState(zone);
// Create state updates
State powerUpdate = state.isPower() ? OnOffType.ON : OnOffType.OFF;
State muteUpdate = state.isMute() ? OnOffType.ON : OnOffType.OFF;
State inputUpdate = new StringType(state.getInput());
State surroundUpdate = new StringType(state.getSurroundProgram());
State updateVolumeDb = new DecimalType(state.getVolume());
State updateVolumePercent = new PercentType((int) dbToPercent(state.getVolume()));
// Send updates
sendUpdate(configs, zone, BindingType.power, powerUpdate);
sendUpdate(configs, zone, BindingType.mute, muteUpdate);
sendUpdate(configs, zone, BindingType.input, inputUpdate);
sendUpdate(configs, zone, BindingType.surroundProgram, surroundUpdate);
sendUpdate(configs, zone, BindingType.volumePercent, updateVolumePercent);
sendUpdate(configs, zone, BindingType.volumeDb, updateVolumeDb);
}
} catch (IOException e) {
logger.warn("Cannot communicate with " + receiverProxy.getHost());
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class XplBinding method handleXPLMessage.
@Override
public void handleXPLMessage(xPL_MessageI theMessage) {
for (XplBindingProvider provider : providers) {
List<String> matchingItems = provider.hasMessage(theMessage);
for (String itemName : matchingItems) {
XplBindingConfig config = provider.getConfig(itemName);
if (config == null) {
continue;
}
String current = theMessage.getNamedValue(config.NamedParameter);
Item item = provider.getItem(itemName);
if (item != null) {
if (item instanceof SwitchItem) {
OnOffType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OnOffType.ON : OnOffType.OFF;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((SwitchItem) item).setState(status);
}
}
} else if (item instanceof ContactItem) {
OpenClosedType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((ContactItem) item).setState(status);
}
}
} else if (item instanceof NumberItem) {
DecimalType value = new DecimalType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((NumberItem) item).setState(value);
}
}
} else if (item instanceof StringItem) {
StringType value = new StringType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((StringItem) item).setState(value);
}
}
}
}
}
}
}
Aggregations