Search in sources :

Example 6 with StringType

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

the class OpenPathsBinding method execute.

/**
     * @{inheritDoc
     */
@Override
public void execute() {
    if (!bindingsExist()) {
        logger.debug("There is no existing OpenPaths binding configuration => refresh cycle aborted!");
        return;
    }
    for (OpenPathsBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            logger.trace("try binding provider item: " + itemName);
            OpenPathsBindingConfig bindingConfig = provider.getItemConfig(itemName);
            String bindingConfigName = bindingConfig.getName();
            String[] bindingParts = bindingConfigName.split("\\:");
            if (bindingParts.length < 1) {
                logger.error("Empty OpenPaths binding config");
                continue;
            }
            String name = bindingParts[0];
            if (!openPathsUsers.containsKey(name)) {
                logger.warn("There is no OpenPaths user configured for '" + name + "'. Please add this user to the binding configuration, including both the ACCESS_KEY and SECRET_KEY from the OpenPaths profile.");
                continue;
            }
            Location location = null;
            OpenPathsUser openPathsUser = openPathsUsers.get(name);
            if (openPathsUser.lastUpdateTS + this.refreshInterval < System.currentTimeMillis()) {
                String accessKey = openPathsUser.getAccessKey();
                String secretKey = openPathsUser.getSecretKey();
                if (StringUtils.isEmpty(accessKey)) {
                    logger.warn("There is no ACCESS_KEY configured for '" + name + "'. Please add this user to the binding configuration, including both the ACCESS_KEY and SECRET_KEY from the OpenPaths profile.");
                    continue;
                }
                if (StringUtils.isEmpty(secretKey)) {
                    logger.warn("There is no SECRET_KEY configured for '" + name + "'. Please add this user to the binding configuration, including both the ACCESS_KEY and SECRET_KEY from the OpenPaths profile.");
                    continue;
                }
                logger.debug("Requesting location for '{}'...", name);
                location = getUserLocation(accessKey, secretKey);
                if (location != null) {
                    openPathsUsers.get(name).setLastLocation(location);
                    logger.debug("New location received for '{}': {}", name, location.toString());
                } else {
                    logger.warn("Unable to determine location for '{}'. Skipping.", name);
                    continue;
                }
            } else {
                location = openPathsUsers.get(name).getLastLocation();
                logger.trace("Using cached location for '{}'", openPathsUser.toString());
            }
            String bindingLocationName = bindingParts.length > 1 ? bindingParts[1] : "";
            if (bindingLocationName.startsWith("current")) {
                if (bindingLocationName.equals("currentLocation")) {
                    eventPublisher.postUpdate(itemName, new StringType("" + location.getLatitude() + ", " + location.getLongitude()));
                } else if (bindingLocationName.equals("currentLatitude")) {
                    eventPublisher.postUpdate(itemName, new DecimalType(new BigDecimal(location.getLatitude())));
                } else if (bindingLocationName.equals("currentLongitude")) {
                    eventPublisher.postUpdate(itemName, new DecimalType(new BigDecimal(location.getLongitude())));
                } else {
                    logger.warn("unsupported Binding: " + bindingLocationName);
                }
                continue;
            }
            if (!locations.containsKey(bindingLocationName)) {
                logger.warn("location name " + bindingLocationName + " not configured, falling back to 'home'");
                bindingLocationName = "home";
            }
            logger.debug("OpenPathsUser: " + name + "@" + bindingLocationName);
            LocationBindingType bindingType = LocationBindingType.on;
            if (bindingParts.length == 3) {
                if (bindingParts[2].equals("distance")) {
                    bindingType = LocationBindingType.distance;
                }
            }
            Location bindingLocation = locations.get(bindingLocationName);
            logger.trace("Calculating distance between home ({}) and user location ({}) for '{}'...", new Object[] { bindingLocation.toString(), location.toString(), name });
            double distance = calculateDistance(bindingLocation, location);
            bindingLocation.setDistance(distance);
            logger.trace("Distance calculated as {} for '{}'@'{}'", distance, name, bindingLocationName);
            if (bindingType.equals(LocationBindingType.on)) {
                float fence = bindingLocation.getGeofence() == 0.0 ? geoFence : bindingLocation.getGeofence();
                if (distance <= fence) {
                    logger.trace("Detected that '{}'@'{}' is inside the geofence ({}m)", name, bindingLocationName, fence);
                    eventPublisher.postUpdate(itemName, OnOffType.ON);
                } else {
                    logger.trace("Detected that '{}'@'{}' is outside the geofence ({}m)", name, bindingLocationName, fence);
                    eventPublisher.postUpdate(itemName, OnOffType.OFF);
                }
            } else if (bindingType.equals(LocationBindingType.distance)) {
                eventPublisher.postUpdate(itemName, new DecimalType(new BigDecimal(distance / 1000)));
            }
        }
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) DecimalType(org.openhab.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal) OpenPathsBindingProvider(org.openhab.binding.openpaths.OpenPathsBindingProvider)

Example 7 with StringType

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

the class OnkyoBinding method convertDeviceValueToOpenHabState.

/**
     * Convert receiver value to OpenHAB state.
     *
     * @param itemType
     * @param data
     *
     * @return
     */
private State convertDeviceValueToOpenHabState(Class<? extends Item> itemType, String data) {
    State state = UnDefType.UNDEF;
    try {
        int index;
        String s;
        if (itemType == SwitchItem.class) {
            index = Integer.parseInt(data.substring(3, 5), 16);
            state = index == 0 ? OnOffType.OFF : OnOffType.ON;
        } else if (itemType == NumberItem.class) {
            index = Integer.parseInt(data.substring(3, 5), 16);
            state = new DecimalType(index);
        } else if (itemType == DimmerItem.class) {
            index = Integer.parseInt(data.substring(3, 5), 16);
            state = new PercentType(index);
        } else if (itemType == RollershutterItem.class) {
            index = Integer.parseInt(data.substring(3, 5), 16);
            state = new PercentType(index);
        } else if (itemType == StringItem.class) {
            s = data.substring(3, data.length());
            state = new StringType(s);
        }
    } catch (Exception e) {
        logger.debug("Cannot convert value '{}' to data type {}", data, 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) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 8 with StringType

use of org.openhab.core.library.types.StringType 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 9 with StringType

use of org.openhab.core.library.types.StringType 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 10 with StringType

use of org.openhab.core.library.types.StringType 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

StringType (org.openhab.core.library.types.StringType)90 DecimalType (org.openhab.core.library.types.DecimalType)69 State (org.openhab.core.types.State)30 DateTimeType (org.openhab.core.library.types.DateTimeType)28 PercentType (org.openhab.core.library.types.PercentType)27 NumberItem (org.openhab.core.library.items.NumberItem)25 Calendar (java.util.Calendar)23 StringItem (org.openhab.core.library.items.StringItem)18 OnOffType (org.openhab.core.library.types.OnOffType)15 SwitchItem (org.openhab.core.library.items.SwitchItem)12 ContactItem (org.openhab.core.library.items.ContactItem)10 DimmerItem (org.openhab.core.library.items.DimmerItem)10 RollershutterItem (org.openhab.core.library.items.RollershutterItem)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 DateTimeItem (org.openhab.core.library.items.DateTimeItem)8 HSBType (org.openhab.core.library.types.HSBType)8 ConfigurationException (org.osgi.service.cm.ConfigurationException)8 IOException (java.io.IOException)7 BigDecimal (java.math.BigDecimal)7