use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class ReadRegistersTestCase method testReadRegistersInt16.
/**
* Test reading of input/holding registers, uses valuetype=int16
*/
@Test
public void testReadRegistersInt16() throws InterruptedException, UnknownHostException, BindingConfigParseException, ConfigurationException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Modbus server ("modbus slave") has input registers
addRegisterMethod.invoke(spi, constructRegisterInt.newInstance(2));
addRegisterMethod.invoke(spi, constructRegisterInt.newInstance(-4));
addRegisterMethod.invoke(spi, constructRegisterInt.newInstance(99));
binding = new ModbusBinding();
binding.updated(addSlave(newLongPollBindingConfig(), SLAVE_NAME, type, ModbusBindingProvider.VALUE_TYPE_INT16, nonZeroOffset ? 1 : 0, 2));
configureNumberItemBinding(2, 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) {
verify(eventPublisher).postUpdate("Item1", new DecimalType(-4));
verify(eventPublisher).postUpdate("Item2", new DecimalType(99));
} else {
verify(eventPublisher).postUpdate("Item1", new DecimalType(2));
verify(eventPublisher).postUpdate("Item2", new DecimalType(-4));
}
verifyNoMoreInteractions(eventPublisher);
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class ReadRegistersTestCase method testReadRegistersInt32.
/**
* Test reading of input/holding registers, uses valuetype=int32
*
* @throws IOException
*/
@Test
public void testReadRegistersInt32() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ConfigurationException, BindingConfigParseException, IOException {
// Modbus server ("modbus slave") has input registers
// 0x075BCD15
byte[] registerData = int32AsRegisters(123456789);
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance(registerData[0], registerData[1]));
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance(registerData[2], registerData[3]));
// 0xF8A432EB
registerData = int32AsRegisters(-123456789);
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance(registerData[0], registerData[1]));
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance(registerData[2], registerData[3]));
// 0x075BCD14
registerData = int32AsRegisters(123456788);
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance(registerData[0], registerData[1]));
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance(registerData[2], registerData[3]));
binding = new ModbusBinding();
// read 4 registers = 2 uint32 numbers
binding.updated(addSlave(newLongPollBindingConfig(), SLAVE_NAME, type, ModbusBindingProvider.VALUE_TYPE_INT32, nonZeroOffset ? 1 : 0, 4));
configureNumberItemBinding(2, 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) {
// -854198108 = 0xCD15F8A4 = (1st register lo byte, 2nd register hi
// byte)
verify(eventPublisher).postUpdate("Item1", new DecimalType(-854198108));
// 854263643 = 0x32EB 075B = (2nd register lo byte, 3rd register hi
// byte)
verify(eventPublisher).postUpdate("Item2", new DecimalType(854263643));
} else {
verify(eventPublisher).postUpdate("Item1", new DecimalType(123456789));
verify(eventPublisher).postUpdate("Item2", new DecimalType(-123456789));
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class ReadRegistersTestCase method testReadRegistersUint16.
/**
* Test reading of input/holding registers, uses default valuetype
*/
@Test
public void testReadRegistersUint16() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, UnknownHostException, ConfigurationException, BindingConfigParseException {
// Modbus server ("modbus slave") has input registers
addRegisterMethod.invoke(spi, constructRegisterInt.newInstance(2));
addRegisterMethod.invoke(spi, constructRegisterInt.newInstance(-4));
addRegisterMethod.invoke(spi, constructRegisterInt.newInstance(99));
binding = new ModbusBinding();
binding.updated(addSlave(newLongPollBindingConfig(), SLAVE_NAME, type, null, nonZeroOffset ? 1 : 0, 2));
configureNumberItemBinding(2, 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) {
verify(eventPublisher).postUpdate("Item1", new DecimalType(65532));
verify(eventPublisher).postUpdate("Item2", new DecimalType(99));
} else {
verify(eventPublisher).postUpdate("Item1", new DecimalType(2));
verify(eventPublisher).postUpdate("Item2", new DecimalType(65532));
}
verifyNoMoreInteractions(eventPublisher);
}
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;
}
Aggregations