use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class ReadRegistersTestCase method testReadRegistersInt8.
/**
* Test reading of input/holding registers, uses valuetype=int8
*/
@Test
public void testReadRegistersInt8() throws InterruptedException, UnknownHostException, BindingConfigParseException, ConfigurationException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Modbus server ("modbus slave") has input registers
// first register has following bytes (hi byte, lo byte)
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance((byte) 1, (byte) 2));
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance((byte) 3, (byte) -4));
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance((byte) 5, (byte) 6));
addRegisterMethod.invoke(spi, constructRegisterInt.newInstance(99));
binding = new ModbusBinding();
binding.updated(addSlave(newLongPollBindingConfig(), SLAVE_NAME, type, ModbusBindingProvider.VALUE_TYPE_INT8, nonZeroOffset ? 1 : 0, 2));
configureNumberItemBinding(4, SLAVE_NAME, 0);
binding.execute();
// Give the system some time to make the expected connections & requests
waitForConnectionsReceived(1);
waitForRequests(1);
verify(eventPublisher, never()).postCommand(null, null);
verify(eventPublisher, never()).sendCommand(null, null);
if (nonZeroOffset) {
// 2nd register, lo byte
verify(eventPublisher).postUpdate("Item1", new DecimalType(-4));
// 2nd register, hi byte
verify(eventPublisher).postUpdate("Item2", new DecimalType(3));
// 3rd register, lo byte
verify(eventPublisher).postUpdate("Item3", new DecimalType(6));
// 3rd register, hi byte
verify(eventPublisher).postUpdate("Item4", new DecimalType(5));
} else {
// 1st register, lo byte
verify(eventPublisher).postUpdate("Item1", new DecimalType(2));
// 1st register, hi byte
verify(eventPublisher).postUpdate("Item2", new DecimalType(1));
// 2nd register, lo byte
verify(eventPublisher).postUpdate("Item3", new DecimalType(-4));
// 2nd register, hi byte
verify(eventPublisher).postUpdate("Item4", new DecimalType(3));
}
verifyNoMoreInteractions(eventPublisher);
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class PrimareResponse method openHabState.
/**
* Convert received response message containing a Primare device variable value
* to suitable OpenHAB state for the given itemType
*
* @param itemType
*
* @return openHAB state
*/
public State openHabState(Class<? extends Item> itemType) {
State state = UnDefType.UNDEF;
try {
int index;
String s;
if (itemType == SwitchItem.class) {
index = message[2];
state = index == 0 ? OnOffType.OFF : OnOffType.ON;
} else if (itemType == NumberItem.class) {
index = message[2];
state = new DecimalType(index);
} else if (itemType == DimmerItem.class) {
index = message[2];
state = new PercentType(index);
} else if (itemType == RollershutterItem.class) {
index = message[2];
state = new PercentType(index);
} else if (itemType == StringItem.class) {
s = new String(Arrays.copyOfRange(message, 2, message.length - 2));
state = new StringType(s);
}
} catch (Exception e) {
logger.debug("Cannot convert value '{}' to data type {}", message[1], itemType);
}
return state;
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class PulseaudioBinding method internalReceiveCommand.
@Override
public void internalReceiveCommand(String itemName, Command command) {
PulseaudioBindingProvider provider = findFirstMatchingBindingProvider(itemName, command);
if (provider == null) {
logger.warn("doesn't find matching binding provider [itemName={}, command={}]", itemName, command);
return;
}
String audioItemName = provider.getItemName(itemName);
String serverId = provider.getServerId(itemName);
// Item item = provider.getItem(itemName);
String paCommand = provider.getCommand(itemName);
PulseaudioCommandTypeMapping pulseaudioCommandType = null;
if (paCommand != null && !paCommand.isEmpty()) {
try {
pulseaudioCommandType = PulseaudioCommandTypeMapping.valueOf(paCommand.toUpperCase());
} catch (IllegalArgumentException e) {
logger.warn("unknown command specified for the given itemName [itemName={}, audio-item-name={}, serverId={}, command={}] => querying for values aborted!", new Object[] { itemName, audioItemName, serverId, command });
}
}
PulseaudioClient client = clients.get(serverId);
if (client == null) {
// try to reconnect if the server is configured
if (serverConfigCache.containsKey(serverId)) {
connect(serverId, serverConfigCache.get(serverId));
client = clients.get(serverId);
}
}
if (client == null) {
logger.warn("does't find matching pulseaudio client [itemName={}, serverId={}]", itemName, serverId);
return;
}
if (audioItemName != null && !audioItemName.isEmpty()) {
AbstractAudioDeviceConfig audioItem = client.getGenericAudioItem(audioItemName);
if (audioItem == null) {
logger.warn("no corresponding audio-item found [audioItemName={}]", audioItemName);
return;
}
State updateState = UnDefType.UNDEF;
if (command instanceof IncreaseDecreaseType) {
int volume = audioItem.getVolume();
logger.debug(audioItemName + " volume is " + volume);
if (command.equals(IncreaseDecreaseType.INCREASE)) {
volume = Math.min(100, volume + 5);
}
if (command.equals(IncreaseDecreaseType.DECREASE)) {
volume = Math.max(0, volume - 5);
}
logger.debug("setting " + audioItemName + " volume to " + volume);
client.setVolumePercent(audioItem, volume);
updateState = new PercentType(volume);
} else if (command instanceof PercentType) {
client.setVolumePercent(audioItem, Integer.valueOf(command.toString()));
updateState = (PercentType) command;
} else if (command instanceof DecimalType) {
if (pulseaudioCommandType == null || pulseaudioCommandType.equals(PulseaudioCommandTypeMapping.VOLUME)) {
// set volume
client.setVolume(audioItem, Integer.valueOf(command.toString()));
updateState = (DecimalType) command;
}
// all other pulseaudioCommandType's for DecimalTypes are
// read-only and
// therefore we do nothing here
} else if (command instanceof OnOffType) {
if (pulseaudioCommandType == null) {
// Default behaviour when no command is specified => mute
client.setMute(audioItem, ((OnOffType) command).equals(OnOffType.ON));
updateState = (OnOffType) command;
} else {
switch(pulseaudioCommandType) {
case EXISTS:
// we better do nothing here
break;
case MUTED:
client.setMute(audioItem, ((OnOffType) command).equals(OnOffType.ON));
updateState = (OnOffType) command;
break;
case RUNNING:
case CORKED:
case SUSPENDED:
case IDLE:
// the state of an audio-item cannot be changed
break;
case ID:
case MODULE_ID:
// changed
break;
case VOLUME:
if (((OnOffType) command).equals(OnOffType.ON)) {
// Set Volume to 100%
client.setVolume(audioItem, 100);
} else {
// set volume to 0
client.setVolume(audioItem, 100);
}
updateState = (OnOffType) command;
break;
case SLAVE_SINKS:
// also an read-only field
break;
}
}
} else if (command instanceof StringType) {
if (pulseaudioCommandType != null) {
switch(pulseaudioCommandType) {
case CORKED:
case EXISTS:
case ID:
case IDLE:
case MODULE_ID:
case MUTED:
case RUNNING:
case SUSPENDED:
case VOLUME:
// no action here
break;
case SLAVE_SINKS:
if (audioItem instanceof Sink && ((Sink) audioItem).isCombinedSink()) {
// change the slave sinks of the given combined sink
// to the new value
Sink mainSink = (Sink) audioItem;
ArrayList<Sink> slaveSinks = new ArrayList<Sink>();
for (String slaveSinkName : StringUtils.split(command.toString(), ",")) {
Sink slaveSink = client.getSink(slaveSinkName);
if (slaveSink != null) {
slaveSinks.add(slaveSink);
}
}
logger.debug(slaveSinks.size() + " slave sinks");
if (slaveSinks.size() > 0) {
client.setCombinedSinkSlaves(mainSink, slaveSinks);
}
}
break;
}
}
}
if (!updateState.equals(UnDefType.UNDEF)) {
eventPublisher.postUpdate(itemName, updateState);
}
} else if (command instanceof StringType) {
// send the command directly to the pulseaudio server
client.sendCommand(command.toString());
eventPublisher.postUpdate(itemName, (StringType) command);
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class PowerDogLocalApiBinding method internalReceiveCommand.
/**
* @{inheritDoc
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
logger.debug("internalReceiveCommand({},{}) is called!", itemName, command);
State newState = null;
// cast Interfaces
if (command instanceof OnOffType) {
newState = (OnOffType) command;
} else if (command instanceof OpenClosedType) {
newState = (OpenClosedType) command;
} else if (command instanceof PercentType) {
newState = (PercentType) command;
} else if (command instanceof DecimalType) {
newState = (DecimalType) command;
}
if (newState != null) {
eventPublisher.postUpdate(itemName, newState);
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class RFXComLighting4MessageTest method testPirSensorMessages.
@Test
public void testPirSensorMessages() throws RFXComException {
RFXComLighting4Message msg = testMessage("0913000145DD99018870", 1, "286169", 7, ON_9, true, 392);
assertEquals("convert to Contact", OpenClosedType.OPEN, msg.convertToState(CONTACT));
assertEquals("convert to Command", OnOffType.ON, msg.convertToState(COMMAND));
assertEquals("convert to Number", new DecimalType(7), msg.convertToState(SIGNAL_LEVEL));
}
Aggregations