Search in sources :

Example 26 with DateTimeType

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

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

the class KoubachiBinding method createState.

/**
     * Creates an openHAB {@link State} in accordance to the class of the given
     * {@code propertyValue}. Currently {@link Date}, {@link BigDecimal} and
     * {@link Boolean} are handled explicitly. All other {@code dataTypes} are
     * mapped to {@link StringType}.
     * <p>
     * If {@code propertyValue} is {@code null}, {@link UnDefType#NULL} will be
     * returned.
     *
     * @param propertyValue
     *
     * @return the new {@link State} in accordance to {@code dataType}. Will
     *         never be {@code null}.
     */
private State createState(Object propertyValue) {
    if (propertyValue == null) {
        return UnDefType.NULL;
    }
    Class<?> dataType = propertyValue.getClass();
    if (Date.class.isAssignableFrom(dataType)) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime((Date) propertyValue);
        return new DateTimeType(calendar);
    } else if (BigDecimal.class.isAssignableFrom(dataType)) {
        return new DecimalType((BigDecimal) propertyValue);
    } else if (Boolean.class.isAssignableFrom(dataType)) {
        if ((Boolean) propertyValue) {
            return OnOffType.ON;
        } else {
            return OnOffType.OFF;
        }
    } else {
        return new StringType(propertyValue.toString());
    }
}
Also used : 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) BigDecimal(java.math.BigDecimal)

Example 28 with DateTimeType

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

the class KNXCoreTypeMapperTest method testTypeMappingDateTime_19_001.

/**
     * KNXCoreTypeMapper tests method typeMapper.toType() for type “Date Time" KNX ID: 19.001 DPT_DATE_TIME
     *
     * @throws KNXFormatException
     */
@Test
public void testTypeMappingDateTime_19_001() throws KNXFormatException {
    DPT dpt = DPTXlatorDateTime.DPT_DATE_TIME;
    testToTypeClass(dpt, DateTimeType.class);
    assertNull("KNXCoreTypeMapper.toType() should return null (no-day)", testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }, DateTimeType.class));
    assertNull("KNXCoreTypeMapper.toType() should return null (illegal date)", testToType(dpt, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, DateTimeType.class));
    /*
         * Reference testcase
         * Monday, January 1st, 1900 01:02:03, Fault: Normal (no fault), Working Day: Bank day (No working day), Working
         * Day Field: valid,
         * Year Field valid, Months and Day fields valid, Day of week field valid, Hour of day, Minutes and Seconds
         * fields valid,
         * Standard Summer Time: Time = UT+X, Quality of Clock: clock without ext. sync signal
         */
    Type type = testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x21, 0x02, 0x03, 0x00, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1900-01-01 01:02:03");
    /*
         * Reference testcase + Fault: Fault => not supported
         */
    assertNull("KNXCoreTypeMapper.toType() should return null (faulty clock)", testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x20, 0x00, 0x00, (byte) 0x80, 0x00 }, DateTimeType.class));
    /*
         * Reference testcase + Year Field invalid => not supported
         */
    assertNull("KNXCoreTypeMapper.toType() should return null (date but no year)", testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x20, 0x00, 0x00, 0x10, 0x00 }, DateTimeType.class));
    /*
         * Reference testcase + Months and Day fields invalid => not supported
         */
    assertNull("KNXCoreTypeMapper.toType() should return null (date but no day and month)", testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x20, 0x00, 0x00, 0x08, 0x00 }, DateTimeType.class));
    /*
         * Reference testcase + Year, Months and Day fields invalid
         */
    type = testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x21, 0x02, 0x03, 0x18, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1970-01-01 01:02:03");
    /*
         * Reference testcase + Year , Months and Day fields invalid + Day of week field invalid
         */
    type = testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x21, 0x02, 0x03, 0x1C, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1970-01-01 01:02:03");
    /*
         * Reference testcase + Year, Months and Day fields invalid + Day of week field invalid
         * Working day field invalid
         */
    type = testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x21, 0x02, 0x03, 0x3C, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1970-01-01 01:02:03");
    /*
         * Reference testcase + Year Field invalid + Months and Day fields invalid + Day of week field invalid
         * Working day field invalid + Hour of day, Minutes and Seconds fields invalid
         */
    assertNull("KNXCoreTypeMapper.toType() should return null (neither date nor time)", testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x20, 0x00, 0x00, 0x3E, 0x00 }, DateTimeType.class));
    /*
         * Reference testcase + Year, Months and Day fields invalid + Day of week field invalid
         * Working day field invalid + Hour of day, Minutes and Seconds fields invalid, Standard Summer Time: Time =
         * UT+X+1
         */
    assertNull("KNXCoreTypeMapper.toType() should return null (neither date nor time, but summertime flag)", type = testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x20, 0x00, 0x00, 0x3F, 0x00 }, DateTimeType.class));
    /*
         * Reference testcase + day of week=Any day, Day of week field invalid
         */
    type = testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1900-01-01 00:00:00");
    /*
         * Reference testcase + Day of week field invalid
         */
    type = testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x20, 0x00, 0x00, 0x04, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1900-01-01 00:00:00");
    /*
         * Reference testcase + day of week=Any day, Day of week field invalid, working day, working day field invalid
         */
    type = testToType(dpt, new byte[] { 0x00, 0x01, 0x01, 0x20, 0x00, 0x00, (byte) 0x60, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1900-01-01 00:00:00");
    /*
         * December 31st, 2155 day of week=Any day, Day of week field invalid
         */
    type = testToType(dpt, new byte[] { (byte) 0xFF, 0x0C, 0x1F, 0x17, 0x3B, 0x3B, (byte) 0x04, (byte) 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "2155-12-31 23:59:59");
    /*
         * December 31st, 2155, 24:00:00, day of week=Any day, Day of week field invalid
         *
         * TODO: this test case should test for "2155-12-31 24:00:00" since that is what the (valid) KNX bytes
         * represent.
         * Nevertheless, calimero is "cheating" by using the milliseconds such that "23:59:59.999" is interpreted as
         * "23:59:59"
         * OpenHAB's DateTimeType doesn't support milliseconds (at least not when parsing from a String), hence 24:00:00
         * cannot be mapped.
         */
    type = testToType(dpt, new byte[] { (byte) 0xFF, 0x0C, 0x1F, 0x18, 0x00, 0x00, (byte) 0x04, (byte) 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "2155-12-31 23:59:59");
    /*
         * December 31st, 2014 24:00:00, day of week=Any day, Day of week field invalid
         *
         * TODO: this test case should test for "2155-12-31 24:00:00" since that is what the (valid) KNX bytes
         * represent.
         * Nevertheless, calimero is "cheating" by using the milliseconds such that "23:59:59.999" is interpreted as
         * "23:59:59"
         * OpenHAB's DateTimeType doesn't support milliseconds (at least not when parsing from a String), hence 24:00:00
         * cannot be mapped.
         */
    type = testToType(dpt, new byte[] { (byte) 0x72, 0x0C, 0x1F, 0x18, 0x00, 0x00, (byte) 0x04, (byte) 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "2014-12-31 23:59:59");
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) StringType(org.openhab.core.library.types.StringType) OnOffType(org.openhab.core.library.types.OnOffType) DateTimeType(org.openhab.core.library.types.DateTimeType) Type(org.openhab.core.types.Type) DecimalType(org.openhab.core.library.types.DecimalType) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType) StopMoveType(org.openhab.core.library.types.StopMoveType) DPT(tuwien.auto.calimero.dptxlator.DPT) Test(org.junit.Test)

Example 29 with DateTimeType

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

the class NtpBinding method updateTime.

private void updateTime(NtpBindingProvider provider, String itemName, long networkTimeInMillis) {
    TimeZone timeZone = provider.getTimeZone(itemName);
    Locale locale = provider.getLocale(itemName);
    Calendar calendar = Calendar.getInstance(timeZone, locale);
    calendar.setTimeInMillis(networkTimeInMillis);
    eventPublisher.postUpdate(itemName, new DateTimeType(calendar));
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) DateTimeType(org.openhab.core.library.types.DateTimeType) Calendar(java.util.Calendar)

Example 30 with DateTimeType

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

the class PowerMaxBinding method updateItemsFromAlarmState.

/**
     * Post item updates on the bus to match a new alarm system state
     *
     * @param provider
     *            filter on provider; if null, no filter on provider
     * @param name
     *            filter on item name; if null, no filter on item name
     * @param selector
     *            filter on selector type; if null, no filter on selector type
     * @param state
     *            the alarm system state
     */
private synchronized void updateItemsFromAlarmState(PowerMaxBindingProvider provider, String name, PowerMaxSelectorType selector, PowerMaxState state) {
    if (state == null) {
        return;
    }
    if (itemUpdateDisabled) {
        logger.debug("updateItemsFromAlarmState(): items update disabled");
        return;
    }
    if (provider == null) {
        for (PowerMaxBindingProvider prov : providers) {
            if (prov != null) {
                updateItemsFromAlarmState(prov, name, selector, state);
            }
        }
    } else {
        for (String itemName : provider.getItemNames()) {
            if ((name == null) || itemName.equals(name)) {
                String value = null;
                String value2 = null;
                PowerMaxBindingConfig config = provider.getConfig(itemName);
                Integer num = config.getSelectorIntParam();
                if ((selector == null) || (selector == config.getSelectorType())) {
                    switch(config.getSelectorType()) {
                        case PANEL_MODE:
                            value = state.getPanelMode();
                            break;
                        case PARTITION_STATUS:
                            value = state.getStatusStr();
                            break;
                        case PARTITION_READY:
                            if (state.isReady() != null) {
                                value = state.isReady() ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case PARTITION_BYPASS:
                            if (state.isBypass() != null) {
                                value = state.isBypass() ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case PARTITION_ALARM:
                            if (state.isAlarmActive() != null) {
                                value = state.isAlarmActive() ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case PANEL_TROUBLE:
                            if (state.isTrouble() != null) {
                                value = state.isTrouble() ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case PANEL_ALERT_IN_MEMORY:
                            if (state.isAlertInMemory() != null) {
                                value = state.isAlertInMemory() ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case EVENT_LOG:
                            if ((num != null) && (state.getEventLog(num) != null)) {
                                value = state.getEventLog(num);
                            }
                            break;
                        case PARTITION_ARMED:
                            if (state.isArmed() != null) {
                                value = state.isArmed() ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case PARTITION_ARM_MODE:
                            value = state.getShortArmMode();
                            break;
                        case ZONE_STATUS:
                            if ((num != null) && (state.isSensorTripped(num) != null)) {
                                value = state.isSensorTripped(num) ? OpenClosedType.OPEN.toString() : OpenClosedType.CLOSED.toString();
                                value2 = state.isSensorTripped(num) ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case ZONE_LAST_TRIP:
                            if ((num != null) && (state.getSensorLastTripped(num) != null)) {
                                Calendar cal = Calendar.getInstance();
                                cal.setTimeInMillis(state.getSensorLastTripped(num));
                                value = new DateTimeType(cal).toString();
                            }
                            break;
                        case ZONE_BYPASSED:
                            if ((num != null) && (state.isSensorBypassed(num) != null)) {
                                value = state.isSensorBypassed(num) ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case ZONE_ARMED:
                            if ((num != null) && (state.isSensorArmed(num) != null)) {
                                value = state.isSensorArmed(num) ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case ZONE_LOW_BATTERY:
                            if ((num != null) && (state.isSensorLowBattery(num) != null)) {
                                value = state.isSensorLowBattery(num) ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case PGM_STATUS:
                            if (state.getPGMX10DeviceStatus(0) != null) {
                                value = state.getPGMX10DeviceStatus(0) ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        case X10_STATUS:
                            if ((num != null) && (state.getPGMX10DeviceStatus(num) != null)) {
                                value = state.getPGMX10DeviceStatus(num) ? OnOffType.ON.toString() : OnOffType.OFF.toString();
                            }
                            break;
                        default:
                            break;
                    }
                }
                State itemState = null;
                if (value != null) {
                    itemState = TypeParser.parseState(config.getAcceptedDataTypes(), value);
                }
                if ((itemState == null) && (value2 != null)) {
                    itemState = TypeParser.parseState(config.getAcceptedDataTypes(), value2);
                }
                if (itemState != null) {
                    eventPublisher.postUpdate(itemName, itemState);
                }
            }
        }
    }
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) PowerMaxBindingConfig(org.openhab.binding.powermax.PowerMaxBindingConfig) PowerMaxState(org.openhab.binding.powermax.internal.state.PowerMaxState) State(org.openhab.core.types.State) Calendar(java.util.Calendar) PowerMaxBindingProvider(org.openhab.binding.powermax.PowerMaxBindingProvider)

Aggregations

DateTimeType (org.openhab.core.library.types.DateTimeType)49 Calendar (java.util.Calendar)37 DecimalType (org.openhab.core.library.types.DecimalType)30 StringType (org.openhab.core.library.types.StringType)29 State (org.openhab.core.types.State)16 DateTimeItem (org.openhab.core.library.items.DateTimeItem)12 PercentType (org.openhab.core.library.types.PercentType)12 NumberItem (org.openhab.core.library.items.NumberItem)11 BigDecimal (java.math.BigDecimal)10 OnOffType (org.openhab.core.library.types.OnOffType)9 ContactItem (org.openhab.core.library.items.ContactItem)8 DimmerItem (org.openhab.core.library.items.DimmerItem)8 StringItem (org.openhab.core.library.items.StringItem)8 HSBType (org.openhab.core.library.types.HSBType)8 Test (org.junit.Test)7 SwitchItem (org.openhab.core.library.items.SwitchItem)7 Date (java.util.Date)6 ColorItem (org.openhab.core.library.items.ColorItem)6 RollershutterItem (org.openhab.core.library.items.RollershutterItem)6 ArrayList (java.util.ArrayList)5