Search in sources :

Example 31 with DecimalType

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);
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.Test)

Example 32 with DecimalType

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);
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.Test)

Example 33 with DecimalType

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);
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.Test)

Example 34 with DecimalType

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));
    }
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.Test)

Example 35 with DecimalType

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);
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.Test)

Aggregations

DecimalType (org.openhab.core.library.types.DecimalType)222 Test (org.junit.Test)70 StringType (org.openhab.core.library.types.StringType)69 OnOffType (org.openhab.core.library.types.OnOffType)63 PercentType (org.openhab.core.library.types.PercentType)43 State (org.openhab.core.types.State)40 Type (org.openhab.core.types.Type)39 NumberItem (org.openhab.core.library.items.NumberItem)38 SHCMessage (org.openhab.binding.smarthomatic.internal.SHCMessage)37 DateTimeType (org.openhab.core.library.types.DateTimeType)29 BigDecimal (java.math.BigDecimal)23 Calendar (java.util.Calendar)23 HSBType (org.openhab.core.library.types.HSBType)20 SwitchItem (org.openhab.core.library.items.SwitchItem)18 IOException (java.io.IOException)16 StringItem (org.openhab.core.library.items.StringItem)16 ConfigurationException (org.osgi.service.cm.ConfigurationException)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ContactItem (org.openhab.core.library.items.ContactItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)14