Search in sources :

Example 16 with StringItem

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

the class DigitalSTROMBinding method updateItemState.

private void updateItemState(Item item) {
    if (item != null) {
        Device device = deviceMap.get(item.getName());
        if (device != null) {
            State state = null;
            if (item instanceof DimmerItem) {
                state = new PercentType(getPercent(device.getMaxOutPutValue(), device.getOutputValue()));
            } else if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
                state = device.getOutputValue() > 0 ? OnOffType.ON : OnOffType.OFF;
            } else if (item instanceof NumberItem) {
                DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
                if (confItem != null) {
                    if (confItem.sensor != null) {
                        int value = -1;
                        switch(confItem.sensor) {
                            case TEMPERATURE_INDOORS:
                                value = device.getTemperatureSensorValue();
                                if (value != -1) {
                                    // Celsius
                                    // TODO use resolution
                                    state = new DecimalType((double) (value) / 40 - 43.15);
                                // from sensor
                                }
                                break;
                            case RELATIVE_HUMIDITY_INDOORS:
                                value = device.getHumiditySensorValue();
                                if (value != -1) {
                                    // Percent
                                    // TODO use resolution from
                                    state = new DecimalType((double) (value) / 40);
                                // sensor
                                }
                                break;
                            case TEMPERATURE_OUTDOORS:
                                value = device.getTemperatureSensorValue();
                                if (value != -1) {
                                    // Celsius
                                    // TODO use resolution
                                    state = new DecimalType((double) (value) / 40 - 43.15);
                                // from sensor
                                }
                                break;
                            case RELATIVE_HUMIDITY_OUTDOORS:
                                value = device.getHumiditySensorValue();
                                if (value != -1) {
                                    // Percent
                                    // TODO use resolution from
                                    state = new DecimalType((double) (value) / 40);
                                // sensor
                                }
                                break;
                            default:
                                break;
                        }
                    } else if (confItem.consumption != null) {
                        int value = -1;
                        switch(confItem.consumption) {
                            case ACTIVE_POWER:
                                value = device.getPowerConsumption();
                                break;
                            case OUTPUT_CURRENT:
                                value = device.getEnergyMeterValue();
                                if (value != -1) {
                                    state = new DecimalType(value);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            } else if (item instanceof RollershutterItem) {
                DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
                if (confItem != null) {
                    if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
                        int output = getPercent(device.getMaxSlatPosition(), device.getSlatPosition());
                        state = new PercentType(100 - output);
                    } else if (confItem.context != null && confItem.context.equals(ContextConfig.awning)) {
                        int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
                        state = new PercentType(output);
                    } else {
                        int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
                        state = new PercentType(100 - output);
                    }
                }
            } else if (item instanceof StringItem) {
                DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
                if (confItem != null) {
                    if (confItem.sensor != null) {
                        int value = -1;
                        switch(confItem.sensor) {
                            case TEMPERATURE_INDOORS:
                                value = device.getTemperatureSensorValue();
                                if (value != -1) {
                                    // Celsius
                                    // TODO use resolution
                                    state = new DecimalType((double) (value) / 40 - 43.15);
                                // from sensor
                                }
                                break;
                            case RELATIVE_HUMIDITY_INDOORS:
                                value = device.getHumiditySensorValue();
                                if (value != -1) {
                                    // Percent
                                    // TODO use resolution from
                                    state = new DecimalType((double) (value) / 40);
                                // sensor
                                }
                                break;
                            case TEMPERATURE_OUTDOORS:
                                value = device.getTemperatureSensorValue();
                                if (value != -1) {
                                    // Celsius
                                    // TODO use resolution
                                    state = new DecimalType((double) (value) / 40 - 43.15);
                                // from sensor
                                }
                                break;
                            case RELATIVE_HUMIDITY_OUTDOORS:
                                value = device.getHumiditySensorValue();
                                if (value != -1) {
                                    // Percent
                                    // TODO use resolution from
                                    state = new DecimalType((double) (value) / 40);
                                // sensor
                                }
                                break;
                            default:
                                break;
                        }
                    } else if (confItem.consumption != null) {
                        int value = -1;
                        switch(confItem.consumption) {
                            case ACTIVE_POWER:
                                value = device.getPowerConsumption();
                                if (value != -1) {
                                    state = new DecimalType(value);
                                }
                                break;
                            case OUTPUT_CURRENT:
                                value = device.getEnergyMeterValue();
                                if (value != -1) {
                                    state = new DecimalType(value);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            eventPublisher.postUpdate(item.getName(), state);
        } else {
            logger.error("couldn't update item state, because device is null: " + item.getName());
        }
    } else {
        logger.error("couldn't update item state, because item is null");
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DigitalSTROMBindingConfig(org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 17 with StringItem

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

the class IhcDataConverter method convertResourceValueToState.

/**
     * Convert IHC data type to openHAB data type.
     *
     * @param itemType
     *            OpenHAB data type class
     * @param value
     *            IHC data value
     *
     * @return openHAB {@link State}
     */
public static State convertResourceValueToState(Class<? extends Item> itemType, WSResourceValue value) throws NumberFormatException {
    org.openhab.core.types.State state = UnDefType.UNDEF;
    if (itemType == NumberItem.class) {
        if (value.getClass() == WSFloatingPointValue.class) {
            // state = new
            // DecimalType(((WSFloatingPointValue)value).getFloatingPointValue());
            // Controller might send floating point value with >10 decimals
            // (22.299999237060546875), so round value to have max 2
            // decimals
            double d = ((WSFloatingPointValue) value).getFloatingPointValue();
            BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
            state = new DecimalType(bd);
        } else if (value.getClass() == WSBooleanValue.class) {
            state = new DecimalType(((WSBooleanValue) value).isValue() ? 1 : 0);
        } else if (value.getClass() == WSIntegerValue.class) {
            state = new DecimalType(((WSIntegerValue) value).getInteger());
        } else if (value.getClass() == WSTimerValue.class) {
            state = new DecimalType(((WSTimerValue) value).getMilliseconds());
        } else if (value.getClass() == WSEnumValue.class) {
            state = new DecimalType(((WSEnumValue) value).getEnumValueID());
        } else if (value.getClass() == WSWeekdayValue.class) {
            state = new DecimalType(((WSWeekdayValue) value).getWeekdayNumber());
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
        }
    } else if (itemType == DimmerItem.class) {
        if (value.getClass() == WSIntegerValue.class) {
            state = new PercentType(((WSIntegerValue) value).getInteger());
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
        }
    } else if (itemType == SwitchItem.class) {
        if (value.getClass() == WSBooleanValue.class) {
            if (((WSBooleanValue) value).isValue()) {
                state = OnOffType.ON;
            } else {
                state = OnOffType.OFF;
            }
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to SwitchItem");
        }
    } else if (itemType == ContactItem.class) {
        if (value.getClass() == WSBooleanValue.class) {
            if (((WSBooleanValue) value).isValue()) {
                state = OpenClosedType.OPEN;
            } else {
                state = OpenClosedType.CLOSED;
            }
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to ContactItem");
        }
    } else if (itemType == DateTimeItem.class) {
        if (value.getClass() == WSDateValue.class) {
            Calendar cal = WSDateTimeToCalendar((WSDateValue) value, null);
            state = new DateTimeType(cal);
        } else if (value.getClass() == WSTimeValue.class) {
            Calendar cal = WSDateTimeToCalendar(null, (WSTimeValue) value);
            state = new DateTimeType(cal);
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to DateTimeItem");
        }
    } else if (itemType == StringItem.class) {
        if (value.getClass() == WSEnumValue.class) {
            state = new StringType(((WSEnumValue) value).getEnumName());
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to StringItem");
        }
    } else if (itemType == RollershutterItem.class) {
        if (value.getClass() == WSIntegerValue.class) {
            state = new PercentType(((WSIntegerValue) value).getInteger());
        } else {
            throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
        }
    }
    return state;
}
Also used : WSIntegerValue(org.openhab.binding.ihc.ws.datatypes.WSIntegerValue) WSFloatingPointValue(org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) WSTimeValue(org.openhab.binding.ihc.ws.datatypes.WSTimeValue) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) DateTimeType(org.openhab.core.library.types.DateTimeType) DimmerItem(org.openhab.core.library.items.DimmerItem) DecimalType(org.openhab.core.library.types.DecimalType) WSWeekdayValue(org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue) WSBooleanValue(org.openhab.binding.ihc.ws.datatypes.WSBooleanValue) WSTimerValue(org.openhab.binding.ihc.ws.datatypes.WSTimerValue)

Example 18 with StringItem

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

the class LightwaveRfBindingFunctionalTest method testHeatingInfoResponseReceived.

@Test
public void testHeatingInfoResponseReceived() throws Exception {
    String message = "*!{\"trans\":1506,\"mac\":\"03:02:71\",\"time\":1423850746,\"prod\":\"valve\",\"serial\":\"064402\",\"signal\":54,\"type\":\"temp\",\"batt\":2.99,\"ver\":56,\"state\":\"boost\",\"cTemp\":22.3,\"cTarg\":24.0,\"output\":100,\"nTarg\":20.0,\"nSlot\":\"18:45\",\"prof\":5}";
    List<ItemConfigAndExpectedState> itemConfigAndExpectedStates = new LinkedList<ItemConfigAndExpectedState>();
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("BATTERY"), "serial=064402,type=HEATING_BATTERY", new DecimalType("2.99")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("CURRENT_TEMP"), "serial=064402,type=HEATING_CURRENT_TEMP", new DecimalType("22.3")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("MODE"), "serial=064402,type=HEATING_MODE", new StringType("boost")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("VERSION"), "serial=064402,type=VERSION", new StringType("56")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("SIGNAL"), "serial=064402,type=SIGNAL", new DecimalType("54")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("SET_TEMP"), "serial=064402,type=HEATING_SET_TEMP", new DecimalType("24.0")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("OUTPUT"), "serial=064402,type=HEATING_OUTPUT", new DecimalType("100")));
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(1423850746000L));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new DateTimeItem("TIME"), "serial=064402,type=UPDATETIME", new DateTimeType(cal)));
    testReceivingACommandAndVerify(itemConfigAndExpectedStates, message);
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) DecimalType(org.openhab.core.library.types.DecimalType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) LinkedList(java.util.LinkedList) Date(java.util.Date) Test(org.junit.Test)

Example 19 with StringItem

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

the class Unit method updateItem.

@Override
public void updateItem(Item item, OmniLinkBindingConfig config, EventPublisher publisher) {
    int status = properties.getState();
    logger.debug("Unit state {}", status);
    int level = prevLevel;
    String display = "Off";
    if (status == UNIT_ON) {
        level = 100;
        display = "On";
    } else if ((status >= UNIT_SCENE_A) && (status <= UNIT_SCENE_L)) {
        level = 100;
        display = String.format("Scene %c", status - UNIT_SCENE_A + 'A');
    } else if ((status >= UNIT_DIM_1) && (status <= UNIT_DIM_9)) {
        display = String.format("Dim %d", level);
    } else if ((status >= UNIT_BRIGHTEN_1) && (status <= UNIT_BRIGHTEN_9)) {
        display = String.format("Brighten %d", level);
    } else if ((status >= UNIT_LEVEL_0) && (status <= UNIT_LEVEL_100)) {
        level = status - UNIT_LEVEL_0;
        display = String.format("Level %d", level);
    }
    if (item instanceof DimmerItem) {
        logger.debug("updating percent type {}", level);
        publisher.postUpdate(item.getName(), new PercentType(level));
    } else if (item instanceof SwitchItem) {
        logger.debug("updating switch type {}", level > 0 ? OnOffType.ON : OnOffType.OFF);
        publisher.postUpdate(item.getName(), level > 0 ? OnOffType.ON : OnOffType.OFF);
    } else if (item instanceof StringItem) {
        logger.debug("updating string type {}", display);
        publisher.postUpdate(item.getName(), new StringType(display));
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) DimmerItem(org.openhab.core.library.items.DimmerItem) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 20 with StringItem

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

the class Zone method updateItem.

@Override
public void updateItem(Item item, OmniLinkBindingConfig config, EventPublisher publisher) {
    int current = ((properties.getStatus() >> 0) & 0x03);
    int latched = ((properties.getStatus() >> 2) & 0x03);
    int arming = ((properties.getStatus() >> 4) & 0x03);
    String latchedTxt = (latched < LATCHED_TEXT.length ? LATCHED_TEXT[latched] : "?");
    String armingTxt = (arming < ARMING_TEXT.length ? ARMING_TEXT[arming] : "?");
    String currentTxt = (current < CURRENT_TEXT.length ? CURRENT_TEXT[current] : "?");
    int setting = 0;
    String str = "";
    switch(config.getObjectType()) {
        case ZONE_STATUS_ARMING:
            setting = arming;
            str = armingTxt;
            break;
        case ZONE_STATUS_CURRENT:
            setting = current;
            str = currentTxt;
            break;
        case ZONE_STATUS_LATCHED:
            setting = latched;
            str = latchedTxt;
            break;
        case ZONE_STATUS_ALL:
            str = currentTxt + " | " + latchedTxt + " | " + armingTxt;
            break;
        default:
            break;
    }
    if (item instanceof NumberItem) {
        publisher.postUpdate(item.getName(), new DecimalType(setting));
    } else if (item instanceof StringItem) {
        publisher.postUpdate(item.getName(), new StringType(str));
    } else if (item instanceof ContactItem) {
        publisher.postUpdate(item.getName(), current == 0 ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) DecimalType(org.openhab.core.library.types.DecimalType) StringItem(org.openhab.core.library.items.StringItem)

Aggregations

StringItem (org.openhab.core.library.items.StringItem)27 NumberItem (org.openhab.core.library.items.NumberItem)16 StringType (org.openhab.core.library.types.StringType)16 DecimalType (org.openhab.core.library.types.DecimalType)14 SwitchItem (org.openhab.core.library.items.SwitchItem)12 DimmerItem (org.openhab.core.library.items.DimmerItem)8 DateTimeType (org.openhab.core.library.types.DateTimeType)8 Test (org.junit.Test)7 ContactItem (org.openhab.core.library.items.ContactItem)7 Calendar (java.util.Calendar)6 DateTimeItem (org.openhab.core.library.items.DateTimeItem)5 RollershutterItem (org.openhab.core.library.items.RollershutterItem)5 PercentType (org.openhab.core.library.types.PercentType)5 BigDecimal (java.math.BigDecimal)4 ColorItem (org.openhab.core.library.items.ColorItem)4 Date (java.util.Date)3 Item (org.openhab.core.items.Item)3 State (org.openhab.core.types.State)3 LinkedList (java.util.LinkedList)2 JSONException (org.json.JSONException)2