Search in sources :

Example 36 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 37 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)

Example 38 with DecimalType

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);
    }
}
Also used : PulseaudioBindingProvider(org.openhab.binding.pulseaudio.PulseaudioBindingProvider) StringType(org.openhab.core.library.types.StringType) ArrayList(java.util.ArrayList) PercentType(org.openhab.core.library.types.PercentType) Sink(org.openhab.binding.pulseaudio.internal.items.Sink) OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) AbstractAudioDeviceConfig(org.openhab.binding.pulseaudio.internal.items.AbstractAudioDeviceConfig)

Example 39 with DecimalType

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

Example 40 with DecimalType

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));
}
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