Search in sources :

Example 26 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class Thermostat method updateItem.

@Override
public void updateItem(Item item, OmniLinkBindingConfig config, EventPublisher publisher) {
    int setting = 0;
    String mode = null;
    switch(config.getObjectType()) {
        case THERMO_COOL_POINT:
            setting = celsius ? MessageUtils.omniToC(properties.getCoolSetpoint()) : MessageUtils.omniToF(properties.getCoolSetpoint());
            break;
        case THERMO_TEMP:
            setting = celsius ? MessageUtils.omniToC(properties.getTemperature()) : MessageUtils.omniToF(properties.getTemperature());
            break;
        case THERMO_HEAT_POINT:
            setting = celsius ? MessageUtils.omniToC(properties.getHeatSetpoint()) : MessageUtils.omniToF(properties.getHeatSetpoint());
            break;
        case THERMO_FAN_MODE:
            setting = properties.isFan() ? 1 : 0;
            break;
        case THERMO_HOLD_MODE:
            setting = properties.isHold() ? 1 : 0;
            break;
        case THERMO_SYSTEM_MODE:
            setting = properties.getMode();
            if (setting < MODES.length) {
                mode = MODES[setting];
            }
            break;
        default:
            return;
    }
    logger.debug("updating item {} for type {} to {}", item.getName(), config.getObjectType(), setting);
    if (item instanceof NumberItem) {
        publisher.postUpdate(item.getName(), new DecimalType(setting));
    } else if (item instanceof SwitchItem) {
        publisher.postUpdate(item.getName(), setting > 0 ? OnOffType.ON : OnOffType.OFF);
    } else if (item instanceof StringItem && mode != null) {
        publisher.postUpdate(item.getName(), new StringType(mode));
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) DecimalType(org.openhab.core.library.types.DecimalType) StringItem(org.openhab.core.library.items.StringItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 27 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class OneWireBindingConfigFactory method createOneWireDeviceProperty.

/**
     * @param pvItem
     * @param pvBindingConfig
     * @return a new BindingConfig, corresponding to the given
     *         <code><pvItem/code> and <code><pvBindingConfig/code>
     * @throws BindingConfigParseException
     */
public static OneWireBindingConfig createOneWireDeviceProperty(Item pvItem, String pvBindingConfig) throws BindingConfigParseException {
    logger.debug("createOneWireDeviceProperty: {} - bindingConfig:{}", pvItem.getName(), pvBindingConfig);
    OneWireBindingConfig lvNewBindingConfig = null;
    if (OneWireClearCacheControlBindingConfig.isBindingConfigToCreate(pvItem, pvBindingConfig)) {
        lvNewBindingConfig = new OneWireClearCacheControlBindingConfig(pvBindingConfig);
    } else if (OneWireDevicePropertyPushButtonBindingConfig.isBindingConfigToCreate(pvItem, pvBindingConfig)) {
        lvNewBindingConfig = new OneWireDevicePropertyPushButtonBindingConfig(pvBindingConfig);
    } else if (OneWireDevicePropertySwitchMinMaxNumberWarningBindingConfig.isBindingConfigToCreate(pvItem, pvBindingConfig)) {
        lvNewBindingConfig = new OneWireDevicePropertySwitchMinMaxNumberWarningBindingConfig(pvBindingConfig);
    } else if (pvItem instanceof NumberItem) {
        lvNewBindingConfig = new OneWireDevicePropertyNumberBindingConfig(pvBindingConfig);
    } else if (pvItem instanceof ContactItem) {
        lvNewBindingConfig = new OneWireDevicePropertyContactBindingConfig(pvBindingConfig);
    } else if (pvItem instanceof SwitchItem) {
        lvNewBindingConfig = new OneWireDevicePropertySwitchBindingConfig(pvBindingConfig);
    } else if (pvItem instanceof StringItem) {
        lvNewBindingConfig = new OneWireDevicePropertyStringBindingConfig(pvBindingConfig);
    } else {
        throw new UnsupportedOperationException("the item-type " + pvItem.getClass() + " cannot be a onewire device");
    }
    logger.debug("created newBindingConfig: {}", lvNewBindingConfig.toString());
    return lvNewBindingConfig;
}
Also used : OneWireDevicePropertyPushButtonBindingConfig(org.openhab.binding.onewire.internal.deviceproperties.OneWireDevicePropertyPushButtonBindingConfig) OneWireDevicePropertySwitchMinMaxNumberWarningBindingConfig(org.openhab.binding.onewire.internal.deviceproperties.OneWireDevicePropertySwitchMinMaxNumberWarningBindingConfig) OneWireDevicePropertySwitchBindingConfig(org.openhab.binding.onewire.internal.deviceproperties.OneWireDevicePropertySwitchBindingConfig) OneWireDevicePropertyStringBindingConfig(org.openhab.binding.onewire.internal.deviceproperties.OneWireDevicePropertyStringBindingConfig) ContactItem(org.openhab.core.library.items.ContactItem) OneWireClearCacheControlBindingConfig(org.openhab.binding.onewire.internal.control.OneWireClearCacheControlBindingConfig) StringItem(org.openhab.core.library.items.StringItem) OneWireDevicePropertyNumberBindingConfig(org.openhab.binding.onewire.internal.deviceproperties.OneWireDevicePropertyNumberBindingConfig) NumberItem(org.openhab.core.library.items.NumberItem) OneWireDevicePropertyContactBindingConfig(org.openhab.binding.onewire.internal.deviceproperties.OneWireDevicePropertyContactBindingConfig) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 28 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class NetatmoBindingTest method testExecute.

@Test
@Ignore("needs real client credentials")
public void testExecute() throws Exception {
    final Dictionary<String, String> config = new Hashtable<String, String>();
    config.put(NetatmoBinding.CONFIG_CLIENT_ID, CLIENT_ID);
    config.put(NetatmoBinding.CONFIG_CLIENT_SECRET, CLIENT_SECRET);
    config.put(NetatmoBinding.CONFIG_REFRESH_TOKEN, REFRESH_TOKEN);
    final NetatmoBinding binding = new NetatmoBinding();
    binding.updated(config);
    final NetatmoGenericBindingProvider provider = new NetatmoGenericBindingProvider();
    provider.processBindingConfiguration("netatmo.items", new NumberItem("Netatmo_OfficeInParis_Bosssoffice_Temperature"), "70:ee:50:00:02:20#Temperature");
    provider.processBindingConfiguration("netatmo.items", new NumberItem("Netatmo_OfficeInParis_NetatmoHQ_Temperature"), "70:ee:50:00:02:20#02:00:00:00:02:a0#Temperature");
    provider.processBindingConfiguration("netatmo.items", new NumberItem("Netatmo_OfficeInParis_NetatmoHQ_Humidity"), "70:ee:50:00:02:20#02:00:00:00:02:a0#Humidity");
    binding.addBindingProvider(provider);
    binding.execute();
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Hashtable(java.util.Hashtable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 29 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class KM200Comm method parseJSONData.

/**
     * This function parses the receviced JSON Data and return the right state
     *
     */
public State parseJSONData(String decodedData, String type, String item, KM200BindingProvider provider) {
    JSONObject nodeRoot = null;
    State state = null;
    Class<? extends Item> itemType = provider.getItemType(item);
    String service = checkParameterReplacement(provider, item);
    KM200CommObject object = device.serviceMap.get(service);
    logger.debug("parseJSONData service: {}, data: {}", service, decodedData);
    /* Now parsing of the JSON String depending on its type and the type of binding item */
    try {
        if (decodedData.length() > 0) {
            nodeRoot = new JSONObject(decodedData);
        } else {
            logger.warn("Get empty reply");
            return null;
        }
        switch(type) {
            case "stringValue":
                /* Check whether the type is a single value containing a string value */
                logger.debug("initDevice: type string value: {}", decodedData);
                String sVal = nodeRoot.getString("value");
                device.serviceMap.get(service).setValue(sVal);
                /* SwitchItem Binding */
                if (itemType.isAssignableFrom(SwitchItem.class)) {
                    if (provider.getParameter(item).containsKey("on")) {
                        if (sVal.equals(provider.getParameter(item).get("off"))) {
                            state = OnOffType.OFF;
                        } else if (sVal.equals(provider.getParameter(item).get("on"))) {
                            state = OnOffType.ON;
                        }
                    } else {
                        logger.warn("Switch-Item only on configured on/off string values: {}", decodedData);
                        return null;
                    }
                /* NumberItem Binding */
                } else if (itemType.isAssignableFrom(NumberItem.class)) {
                    try {
                        state = new DecimalType(Float.parseFloat(sVal));
                    } catch (NumberFormatException e) {
                        logger.error("Conversion of the string value to Decimal wasn't possible, data: {} error: {}", decodedData, e);
                        return null;
                    }
                /* DateTimeItem Binding */
                } else if (itemType.isAssignableFrom(DateTimeItem.class)) {
                    try {
                        state = new DateTimeType(sVal);
                    } catch (IllegalArgumentException e) {
                        logger.error("Conversion of the string value to DateTime wasn't possible, data: {} error: {}", decodedData, e);
                        return null;
                    }
                /* StringItem Binding */
                } else if (itemType.isAssignableFrom(StringItem.class)) {
                    state = new StringType(sVal);
                } else {
                    logger.warn("Bindingtype not supported for string values: {}", itemType.getClass());
                    return null;
                }
                return state;
            case "floatValue":
                /* Check whether the type is a single value containing a float value */
                logger.debug("state of type float value: {}", decodedData);
                BigDecimal bdVal = nodeRoot.getBigDecimal("value");
                device.serviceMap.get(service).setValue(bdVal);
                /* NumberItem Binding */
                if (itemType.isAssignableFrom(NumberItem.class)) {
                    state = new DecimalType(bdVal.floatValue());
                /* StringItem Binding */
                } else if (itemType.isAssignableFrom(StringItem.class)) {
                    state = new StringType(bdVal.toString());
                } else {
                    logger.warn("Bindingtype not supported for float values: {}", itemType.getClass());
                    return null;
                }
                return state;
            case "switchProgram":
                /* Check whether the type is a switchProgram */
                KM200SwitchProgramService sPService = null;
                logger.debug("state of type switchProgram: {}", decodedData);
                /* Get the KM200SwitchProgramService class object with all specific parameters */
                if (object.getVirtual() == 0) {
                    sPService = ((KM200SwitchProgramService) object.getValueParameter());
                } else {
                    sPService = ((KM200SwitchProgramService) device.serviceMap.get(object.getParent()).getValueParameter());
                }
                /* Update the switches insode the KM200SwitchProgramService */
                sPService.updateSwitches(nodeRoot);
                /* the parsing of switch program-services have to be outside, using json in strings */
                if (object.getVirtual() == 1) {
                    return this.getVirtualState(object, itemType, service);
                } else {
                    /* if access to the parent non virtual service the return the switchPoints jsonarray */
                    if (itemType.isAssignableFrom(StringItem.class)) {
                        state = new StringType(nodeRoot.getJSONArray("switchPoints").toString());
                    } else {
                        logger.warn("Bindingtype not supported for switchProgram, only json over strings supported: {}", itemType.getClass());
                        return null;
                    }
                    return state;
                }
            case "errorList":
                /* Check whether the type is a errorList */
                KM200ErrorService eService = null;
                logger.debug("state of type errorList: {}", decodedData);
                /* Get the KM200ErrorService class object with all specific parameters */
                if (object.getVirtual() == 0) {
                    eService = ((KM200ErrorService) object.getValueParameter());
                } else {
                    eService = ((KM200ErrorService) device.serviceMap.get(object.getParent()).getValueParameter());
                }
                /* Update the switches insode the KM200SwitchProgramService */
                eService.updateErrors(nodeRoot);
                /* the parsing of switch program-services have to be outside, using json in strings */
                if (object.getVirtual() == 1) {
                    return this.getVirtualState(object, itemType, service);
                } else {
                    /* if access to the parent non virtual service the return the switchPoints jsonarray */
                    if (itemType.isAssignableFrom(StringItem.class)) {
                        state = new StringType(nodeRoot.getJSONArray("values").toString());
                    } else {
                        logger.warn("Bindingtype not supported for error list, only json over strings is supported: {}", itemType.getClass());
                        return null;
                    }
                    return state;
                }
            case "yRecording":
                /* Check whether the type is a yRecording */
                logger.info("state of: type yRecording is not supported yet: {}", decodedData);
                /* have to be completed */
                break;
            case "systeminfo":
                /* Check whether the type is a systeminfo */
                logger.info("state of: type systeminfo is not supported yet: {}", decodedData);
                /* have to be completed */
                break;
            case "arrayData":
                /* Check whether the type is a arrayData */
                logger.info("state of: type arrayData is not supported yet: {}", decodedData);
                /* have to be completed */
                break;
        }
    } catch (JSONException e) {
        logger.error("Parsingexception in JSON, data: {} error: {} ", decodedData, e.getMessage());
    }
    return null;
}
Also used : StringType(org.openhab.core.library.types.StringType) JSONException(org.json.JSONException) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) JSONObject(org.json.JSONObject) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType)

Example 30 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class IRtransGenericBindingProvider method parseBindingConfig.

/**
     * Parses the binding config.
     *
     * @param config the config
     * @param item the item
     * @param bindingConfig the binding config
     * @throws BindingConfigParseException the binding config parse exception
     */
private void parseBindingConfig(IRtransBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String commandAsString = null;
    String host = null;
    String port = null;
    String led = null;
    String remote = null;
    String irCommand = null;
    Leds ledType = Leds.DEFAULT;
    if (bindingConfig != null) {
        Matcher actionMatcher = ACTION_CONFIG_PATTERN.matcher(bindingConfig);
        Matcher statusMatcher = STATUS_CONFIG_PATTERN.matcher(bindingConfig);
        if ((!actionMatcher.matches() && !statusMatcher.matches())) {
            throw new BindingConfigParseException(getBindingType() + " binding configuration must consist of five [config=" + statusMatcher.pattern() + "] or six parts [config=" + actionMatcher.pattern() + "]");
        } else {
            if (actionMatcher.matches()) {
                commandAsString = actionMatcher.group(1);
                host = actionMatcher.group(2);
                port = actionMatcher.group(3);
                led = actionMatcher.group(4);
                remote = actionMatcher.group(5);
                irCommand = actionMatcher.group(6);
            } else if (statusMatcher.matches()) {
                host = statusMatcher.group(1);
                port = statusMatcher.group(2);
                led = statusMatcher.group(3);
                remote = statusMatcher.group(4);
                irCommand = statusMatcher.group(5);
            }
            if (led.equals("*")) {
                ledType = Leds.ALL;
            } else {
                ledType = Leds.valueOf(led);
            }
            IRtransBindingConfigElement newElement = new IRtransBindingConfigElement(host, port, ledType, remote, irCommand, item.getAcceptedDataTypes());
            Command command = null;
            if (commandAsString == null) {
                // for those configuration strings that are not really linked to a openHAB command we
                // create a dummy Command to be able to store the configuration information
                // I have choosen to do that with NumberItems
                NumberItem dummy = new NumberItem(Integer.toString(counter));
                command = createCommandFromString(dummy, Integer.toString(counter));
                counter++;
                config.put(command, newElement);
            } else {
                command = createCommandFromString(item, commandAsString);
                config.put(command, newElement);
            }
            config.put(command, newElement);
        }
    } else {
        return;
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Aggregations

NumberItem (org.openhab.core.library.items.NumberItem)55 DecimalType (org.openhab.core.library.types.DecimalType)27 SwitchItem (org.openhab.core.library.items.SwitchItem)24 ContactItem (org.openhab.core.library.items.ContactItem)17 StringType (org.openhab.core.library.types.StringType)17 Test (org.junit.Test)16 DimmerItem (org.openhab.core.library.items.DimmerItem)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 StringItem (org.openhab.core.library.items.StringItem)15 Item (org.openhab.core.items.Item)11 PercentType (org.openhab.core.library.types.PercentType)11 DateTimeType (org.openhab.core.library.types.DateTimeType)10 Calendar (java.util.Calendar)9 DateTimeItem (org.openhab.core.library.items.DateTimeItem)9 ColorItem (org.openhab.core.library.items.ColorItem)8 State (org.openhab.core.types.State)8 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)8 BigDecimal (java.math.BigDecimal)6 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)5 HSBType (org.openhab.core.library.types.HSBType)5