use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class ReadRegistersTestCase method testReadRegistersUint8.
/**
* Test reading of input/holding registers, uses valuetype=uint8
*/
@Test
public void testReadRegistersUint8() 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_UINT8, nonZeroOffset ? 1 : 0, 2));
Assert.assertEquals(REFRESH_INTERVAL, binding.getRefreshInterval());
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(256 - 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(256 - 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 ReadRegistersTestCase method testReadRegistersFloat32.
/**
* Test reading of input/holding registers, uses valuetype=float32
*
* @throws IOException
*/
@Test
public void testReadRegistersFloat32() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ConfigurationException, BindingConfigParseException, IOException {
// Modbus server ("modbus slave") has input registers
byte[] registerData = float32AsRegisters(123456789.95623f);
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance(registerData[0], registerData[1]));
addRegisterMethod.invoke(spi, constructRegister2Byte.newInstance(registerData[2], registerData[3]));
registerData = float32AsRegisters(-123456789.1241243f);
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_FLOAT32, 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);
verify(eventPublisher).postUpdate("Item1", new DecimalType(123456789.95623f));
verify(eventPublisher).postUpdate("Item2", new DecimalType(-123456789.1241243f));
verifyNoMoreInteractions(eventPublisher);
}
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);
}
Aggregations