Search in sources :

Example 96 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 97 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 98 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)

Example 99 with DecimalType

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

Example 100 with DecimalType

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;
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType)

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