Search in sources :

Example 16 with DecimalType

use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.

the class PioneerAvrBinding method convertDeviceValueToOpenHabState.

/**
     * Convert receiver value to OpenHAB state.
     * 
     * @param itemType
     * @param data
     * 
     * @return
     */
private State convertDeviceValueToOpenHabState(Class<? extends Item> itemType, String data, IpControlCommand cmdType) {
    State state = UnDefType.UNDEF;
    try {
        int index;
        // cut off the leading response identifier to get the payload string
        String payloadSubstring = data.substring(cmdType.getResponse().length());
        // the selected source will then be set to "ON"
        if (payloadSubstring.length() == 0) {
            payloadSubstring = "1";
        }
        // special case for display info query: convert to human readable string
        if (cmdType.getCommandRef() == IpControlCommandRef.DISPLAY_INFO_QUERY) {
            IpControlDisplayInformation displayInfo = new IpControlDisplayInformation(payloadSubstring);
            payloadSubstring = displayInfo.getInfoText();
            logger.debug("DisplayInfo: converted value '{}' to string '{}'", data, payloadSubstring);
        }
        if (itemType == SwitchItem.class) {
            index = Integer.parseInt(payloadSubstring);
            // according to Spec: 0=ON, 1=OFF!
            state = (index == 0) ? OnOffType.ON : OnOffType.OFF;
        } else if (itemType == DimmerItem.class) {
            if (cmdType.getCommandRef().getCommand() == IpControlCommandRef.VOLUME_QUERY.getCommand() || cmdType.getCommandRef().getCommand() == IpControlCommandRef.VOLUME_SET.getCommand()) {
                index = convertVolumeToPercent(Integer.parseInt(payloadSubstring));
            } else {
                index = Integer.parseInt(payloadSubstring);
            }
            state = new PercentType(index);
        } else if (itemType == NumberItem.class) {
            index = Integer.parseInt(payloadSubstring);
            state = new DecimalType(index);
        } else if (itemType == RollershutterItem.class) {
            index = Integer.parseInt(payloadSubstring);
            state = new PercentType(index);
        } else if (itemType == StringItem.class) {
            state = new StringType(payloadSubstring);
        }
    } catch (Exception e) {
        logger.debug("Cannot convert value '{}' to data type {}", data, itemType);
    }
    return state;
}
Also used : IpControlDisplayInformation(org.openhab.binding.pioneeravr.internal.ipcontrolprotocol.IpControlDisplayInformation) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 17 with DecimalType

use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.

the class PilightGenericBindingProviderTest method testNumberItemScale.

@Test
public void testNumberItemScale() throws BindingConfigParseException {
    String bindingConfig = "kaku#weather,property=temperature";
    PilightBindingConfig config = provider.parseBindingConfig(testItem, bindingConfig);
    Assert.assertNotNull(config);
    DecimalType number0 = (DecimalType) binding.getState("23", config);
    Assert.assertEquals(number0.toBigDecimal().compareTo(new BigDecimal("23")), 0);
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 18 with DecimalType

use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.

the class ZWaveCentralSceneConverter method handleEvent.

@Override
void handleEvent(ZWaveCommandClassValueEvent event, Item item, Map<String, String> arguments) {
    org.openhab.core.types.State itemState = null;
    // get the central scene command value event properties
    Properties properties = (Properties) event.getValue();
    int event_scene = (Integer) properties.get("scene");
    int event_key = (Integer) properties.get("key");
    // then only forward the event for the specified scene number
    if (arguments.get("scene") != null) {
        // get the item specified scene number
        Integer scene = null;
        try {
            scene = Integer.parseInt(arguments.get("scene"));
        } catch (NumberFormatException e) {
            logger.error("NODE {}: Number format exception scene={}", event.getNodeId(), arguments.get("scene"));
            return;
        }
        // from the received command event
        if (scene != null && scene != event_scene) {
            return;
        }
        // then only forward the event for the specified matching key state
        if (arguments.get("key") != null) {
            // get the item specified key (state)
            Integer key = null;
            try {
                key = Integer.parseInt(arguments.get("key"));
            } catch (NumberFormatException e) {
                logger.error("NODE {}: Number format exception key={}", event.getNodeId(), arguments.get("key"));
                return;
            }
            // from the received command event
            if (key != null && key != event_key) {
                return;
            }
            // get state converter
            ZWaveStateConverter<?, ?> converter = this.getStateConverter(item, event_scene);
            if (converter == null) {
                logger.warn("No converter found for item = {}, node = {} endpoint = {}, ignoring event.", item.getName(), event.getNodeId(), event.getEndpoint());
                return;
            }
            // convert the scene number and key state to a binary value on a
            // successful match
            itemState = converter.convertFromValueToState((event_key == key) ? 1 : 0);
        } else {
            // get state converter
            ZWaveStateConverter<?, ?> converter = this.getStateConverter(item, event_key);
            if (converter == null) {
                logger.warn("No converter found for item = {}, node = {} endpoint = {}, ignoring event.", item.getName(), event.getNodeId(), event.getEndpoint());
                return;
            }
            // convert the scene's key number to an acceptable conversion
            // type and then publish the event
            itemState = converter.convertFromValueToState(event_key);
        }
    } else {
        // no scene argument provide, so we will convert the central scene
        // number
        itemState = new DecimalType(event_scene);
    }
    // publish the central scene update
    this.getEventPublisher().postUpdate(item.getName(), itemState);
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) Properties(java.util.Properties)

Example 19 with DecimalType

use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.

the class YamahaReceiverBinding method sendUpdates.

private void sendUpdates(YamahaReceiverProxy receiverProxy, String deviceUid) {
    // Get all item configurations belonging to this proxy
    Collection<YamahaReceiverBindingConfig> configs = getDeviceConfigs(deviceUid);
    try {
        for (Zone zone : Zone.values()) {
            // Poll the state from the device
            YamahaReceiverState state = receiverProxy.getState(zone);
            // Create state updates
            State powerUpdate = state.isPower() ? OnOffType.ON : OnOffType.OFF;
            State muteUpdate = state.isMute() ? OnOffType.ON : OnOffType.OFF;
            State inputUpdate = new StringType(state.getInput());
            State surroundUpdate = new StringType(state.getSurroundProgram());
            State updateVolumeDb = new DecimalType(state.getVolume());
            State updateVolumePercent = new PercentType((int) dbToPercent(state.getVolume()));
            // Send updates
            sendUpdate(configs, zone, BindingType.power, powerUpdate);
            sendUpdate(configs, zone, BindingType.mute, muteUpdate);
            sendUpdate(configs, zone, BindingType.input, inputUpdate);
            sendUpdate(configs, zone, BindingType.surroundProgram, surroundUpdate);
            sendUpdate(configs, zone, BindingType.volumePercent, updateVolumePercent);
            sendUpdate(configs, zone, BindingType.volumeDb, updateVolumeDb);
        }
    } catch (IOException e) {
        logger.warn("Cannot communicate with " + receiverProxy.getHost());
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) Zone(org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.Zone) YamahaReceiverState(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverState) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) IOException(java.io.IOException) YamahaReceiverState(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverState)

Example 20 with DecimalType

use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.

the class XplBinding method handleXPLMessage.

@Override
public void handleXPLMessage(xPL_MessageI theMessage) {
    for (XplBindingProvider provider : providers) {
        List<String> matchingItems = provider.hasMessage(theMessage);
        for (String itemName : matchingItems) {
            XplBindingConfig config = provider.getConfig(itemName);
            if (config == null) {
                continue;
            }
            String current = theMessage.getNamedValue(config.NamedParameter);
            Item item = provider.getItem(itemName);
            if (item != null) {
                if (item instanceof SwitchItem) {
                    OnOffType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OnOffType.ON : OnOffType.OFF;
                    synchronized (item) {
                        if (!item.getState().equals(status)) {
                            eventPublisher.postUpdate(itemName, status);
                            ((SwitchItem) item).setState(status);
                        }
                    }
                } else if (item instanceof ContactItem) {
                    OpenClosedType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                    synchronized (item) {
                        if (!item.getState().equals(status)) {
                            eventPublisher.postUpdate(itemName, status);
                            ((ContactItem) item).setState(status);
                        }
                    }
                } else if (item instanceof NumberItem) {
                    DecimalType value = new DecimalType(current);
                    synchronized (item) {
                        if (!item.getState().equals(value)) {
                            eventPublisher.postUpdate(itemName, value);
                            ((NumberItem) item).setState(value);
                        }
                    }
                } else if (item instanceof StringItem) {
                    StringType value = new StringType(current);
                    synchronized (item) {
                        if (!item.getState().equals(value)) {
                            eventPublisher.postUpdate(itemName, value);
                            ((StringItem) item).setState(value);
                        }
                    }
                }
            }
        }
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) XplBindingConfig(org.openhab.binding.xpl.XplBindingConfig) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem) XplBindingProvider(org.openhab.binding.xpl.XplBindingProvider)

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