Search in sources :

Example 61 with DecimalType

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

the class FatekColorItem method reg2HSB.

private HSBType reg2HSB(RegValue r1, RegValue r2, RegValue r3) {
    int v1 = r1.intValueUnsigned();
    int v2 = r2.intValueUnsigned();
    int v3 = r3.intValueUnsigned();
    if (isColorRGB) {
        v1 = Math.min(v1, 255);
        v2 = Math.min(v2, 255);
        v3 = Math.min(v3, 255);
        return new HSBType(new Color(v1, v2, v3));
    } else {
        v1 = Math.min(v1, 360);
        v2 = Math.min(v2, 100);
        v3 = Math.min(v3, 100);
        return new HSBType(new DecimalType(v1), new PercentType(v2), new PercentType(v3));
    }
}
Also used : Color(java.awt.Color) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 62 with DecimalType

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

the class FatekNumberItem method getState.

@Override
public State getState(Map<Reg, RegValue> response) {
    BigDecimal val;
    if (floatValue) {
        val = new BigDecimal(response.get(reg1).floatValue());
    } else {
        long longVal;
        if (signed) {
            longVal = response.get(reg1).longValue();
        } else {
            longVal = response.get(reg1).longValueUnsigned();
        }
        val = new BigDecimal(longVal);
    }
    if (factor != null) {
        val = factor.multiply(val);
    }
    if (transType != null) {
        return getStateWithTransformation(val);
    } else {
        return new DecimalType(val);
    }
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal)

Example 63 with DecimalType

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

the class FatekNumberItem method command.

@Override
public void command(FatekPLC fatekPLC, Command command) throws CommandException {
    if (command instanceof DecimalType) {
        BigDecimal val = ((DecimalType) command).toBigDecimal();
        if (factor != null) {
            val = val.divide(factor);
        }
        try {
            FatekWriteMixDataCmd cmd = new FatekWriteMixDataCmd(fatekPLC);
            if (floatValue) {
                cmd.addReg(reg1, val.floatValue());
            } else {
                cmd.addReg(reg1, val.longValue());
            }
            cmd.send();
        } catch (FatekIOException | FatekException e) {
            throw new CommandException(this, command, e);
        }
    } else {
        throw new UnsupportedCommandException(this, command);
    }
}
Also used : FatekWriteMixDataCmd(org.simplify4u.jfatek.FatekWriteMixDataCmd) FatekIOException(org.simplify4u.jfatek.io.FatekIOException) FatekException(org.simplify4u.jfatek.FatekException) DecimalType(org.openhab.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal)

Example 64 with DecimalType

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

the class FritzahaWebserviceUpdateXmlCallback method execute.

/**
     * {@inheritDoc}
     */
@Override
public void execute(int status, String response) {
    super.execute(status, response);
    if (validRequest) {
        logger.trace("Received State response " + response + " for item " + itemName);
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(DevicelistModel.class);
            Unmarshaller jaxbUM = jaxbContext.createUnmarshaller();
            DevicelistModel model = (DevicelistModel) jaxbUM.unmarshal(new StringReader(response));
            ArrayList<DeviceModel> list = model.getDevicelist();
            for (DeviceModel device : list) {
                if (device.getIdentifier().equals(this.deviceAin)) {
                    BigDecimal meterValueScaled = new BigDecimal(0);
                    switch(type) {
                        case POWER:
                            meterValueScaled = device.getPowermeter().getPower().scaleByPowerOfTen(-3);
                            break;
                        case ENERGY:
                            meterValueScaled = device.getPowermeter().getEnergy();
                            break;
                        case TEMPERATURE:
                            meterValueScaled = device.getTemperature().getCelsius().scaleByPowerOfTen(-1);
                            break;
                        default:
                            logger.warn("unknown meter type: " + type);
                            break;
                    }
                    logger.debug(device.toString());
                    webIface.postUpdate(itemName, new DecimalType(meterValueScaled));
                } else {
                    logger.trace("device " + device.getIdentifier() + " was not requested");
                }
            }
        } catch (JAXBException e) {
            logger.error(e.getLocalizedMessage(), e);
        }
    }
}
Also used : DeviceModel(org.openhab.binding.fritzaha.internal.model.DeviceModel) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) DecimalType(org.openhab.core.library.types.DecimalType) JAXBContext(javax.xml.bind.JAXBContext) DevicelistModel(org.openhab.binding.fritzaha.internal.model.DevicelistModel) Unmarshaller(javax.xml.bind.Unmarshaller) BigDecimal(java.math.BigDecimal)

Example 65 with DecimalType

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

the class KNXCoreTypeMapper method toType.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.binding.knx.config.KNXTypeMapper#toType(tuwien.auto.calimero.datapoint.Datapoint, byte[])
     */
@Override
public Type toType(Datapoint datapoint, byte[] data) {
    try {
        DPTXlator translator = TranslatorTypes.createTranslator(datapoint.getMainNumber(), datapoint.getDPT());
        translator.setData(data);
        String value = translator.getValue();
        String id = translator.getType().getID();
        logger.trace("toType datapoint DPT = " + datapoint.getDPT());
        int mainNumber = getMainNumber(id);
        if (mainNumber == -1) {
            logger.debug("toType: couldn't identify mainnumber in dptID: {}.", id);
            return null;
        }
        int subNumber = getSubNumber(id);
        if (subNumber == -1) {
            logger.debug("toType: couldn't identify sub number in dptID: {}.", id);
            return null;
        }
        /*
             * Following code section deals with specific mapping of values from KNX to openHAB types were the String
             * received from the DPTXlator is not sufficient to set the openHAB type or has bugs
             */
        switch(mainNumber) {
            case 1:
                DPTXlatorBoolean translatorBoolean = (DPTXlatorBoolean) translator;
                switch(subNumber) {
                    case 8:
                        return translatorBoolean.getValueBoolean() ? UpDownType.DOWN : UpDownType.UP;
                    case 9:
                        return translatorBoolean.getValueBoolean() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                    case 10:
                        return translatorBoolean.getValueBoolean() ? StopMoveType.MOVE : StopMoveType.STOP;
                    case 19:
                        return translatorBoolean.getValueBoolean() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                    case 22:
                        return DecimalType.valueOf(translatorBoolean.getValueBoolean() ? "1" : "0");
                    default:
                        return translatorBoolean.getValueBoolean() ? OnOffType.ON : OnOffType.OFF;
                }
            case 2:
                DPTXlator1BitControlled translator1BitControlled = (DPTXlator1BitControlled) translator;
                int decValue = (translator1BitControlled.getControlBit() ? 2 : 0) + (translator1BitControlled.getValueBit() ? 1 : 0);
                return new DecimalType(decValue);
            case 3:
                DPTXlator3BitControlled translator3BitControlled = (DPTXlator3BitControlled) translator;
                if (translator3BitControlled.getStepCode() == 0) {
                    /*
                         * there is no STOP for a IncreaseDecreaseType, so we are just using an INCREASE.
                         * It is up to the binding to recognize that a start/stop-dimming is in progress and
                         * stop the dimming accordingly.
                         */
                    logger.debug("toType: KNX DPT_Control_Dimming: break received.");
                    return IncreaseDecreaseType.INCREASE;
                }
                switch(subNumber) {
                    case 7:
                        return translator3BitControlled.getControlBit() ? IncreaseDecreaseType.INCREASE : IncreaseDecreaseType.DECREASE;
                    case 8:
                        return translator3BitControlled.getControlBit() ? UpDownType.DOWN : UpDownType.UP;
                }
            case 14:
                /*
                     * FIXME: Workaround for a bug in Calimero / Openhab DPTXlator4ByteFloat.makeString(): is using a
                     * locale when
                     * translating a Float to String. It could happen the a ',' is used as separator, such as
                     * 3,14159E20.
                     * Openhab's DecimalType expects this to be in US format and expects '.': 3.14159E20.
                     * There is no issue with DPTXlator2ByteFloat since calimero is using a non-localized translation
                     * there.
                     */
                DPTXlator4ByteFloat translator4ByteFloat = (DPTXlator4ByteFloat) translator;
                Float f = translator4ByteFloat.getValueFloat();
                if (Math.abs(f) < 100000) {
                    value = String.valueOf(f);
                } else {
                    NumberFormat dcf = NumberFormat.getInstance(Locale.US);
                    if (dcf instanceof DecimalFormat) {
                        ((DecimalFormat) dcf).applyPattern("0.#####E0");
                    }
                    value = dcf.format(f);
                }
                break;
            case 18:
                DPTXlatorSceneControl translatorSceneControl = (DPTXlatorSceneControl) translator;
                int decimalValue = translatorSceneControl.getSceneNumber();
                if (value.startsWith("learn")) {
                    decimalValue += 0x80;
                }
                value = String.valueOf(decimalValue);
                break;
            case 19:
                DPTXlatorDateTime translatorDateTime = (DPTXlatorDateTime) translator;
                if (translatorDateTime.isFaultyClock()) {
                    // Not supported: faulty clock
                    logger.debug("toType: KNX clock msg ignored: clock faulty bit set, which is not supported");
                    return null;
                } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR) && translatorDateTime.isValidField(DPTXlatorDateTime.DATE)) {
                    // Not supported: "/1/1" (month and day without year)
                    logger.debug("toType: KNX clock msg ignored: no year, but day and month, which is not supported");
                    return null;
                } else if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR) && !translatorDateTime.isValidField(DPTXlatorDateTime.DATE)) {
                    // Not supported: "1900" (year without month and day)
                    logger.debug("toType: KNX clock msg ignored: no day and month, but year, which is not supported");
                    return null;
                } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR) && !translatorDateTime.isValidField(DPTXlatorDateTime.DATE) && !translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
                    // Not supported: No year, no date and no time
                    logger.debug("toType: KNX clock msg ignored: no day and month or year, which is not supported");
                    return null;
                }
                Calendar cal = Calendar.getInstance();
                if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR) && !translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
                    // Pure date format, no time information
                    cal.setTimeInMillis(translatorDateTime.getValueMilliseconds());
                    value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
                    return DateTimeType.valueOf(value);
                } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR) && translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
                    // Pure time format, no date information
                    cal.clear();
                    cal.set(Calendar.HOUR_OF_DAY, translatorDateTime.getHour());
                    cal.set(Calendar.MINUTE, translatorDateTime.getMinute());
                    cal.set(Calendar.SECOND, translatorDateTime.getSecond());
                    value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
                    return DateTimeType.valueOf(value);
                } else if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR) && translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
                    // Date format and time information
                    cal.setTimeInMillis(translatorDateTime.getValueMilliseconds());
                    value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
                    return DateTimeType.valueOf(value);
                }
                break;
        }
        Class<? extends Type> typeClass = toTypeClass(id);
        if (typeClass == null) {
            return null;
        }
        if (typeClass.equals(PercentType.class)) {
            return PercentType.valueOf(value.split(" ")[0]);
        }
        if (typeClass.equals(DecimalType.class)) {
            return DecimalType.valueOf(value.split(" ")[0]);
        }
        if (typeClass.equals(StringType.class)) {
            return StringType.valueOf(value);
        }
        if (typeClass.equals(DateTimeType.class)) {
            String date = formatDateTime(value, datapoint.getDPT());
            if ((date == null) || (date.isEmpty())) {
                logger.debug("toType: KNX clock msg ignored: date object null or empty {}.", date);
                return null;
            } else {
                return DateTimeType.valueOf(date);
            }
        }
        if (typeClass.equals(HSBType.class)) {
            // value has format of "r:<red value> g:<green value> b:<blue value>"
            int r = Integer.parseInt(value.split(" ")[0].split(":")[1]);
            int g = Integer.parseInt(value.split(" ")[1].split(":")[1]);
            int b = Integer.parseInt(value.split(" ")[2].split(":")[1]);
            Color color = new Color(r, g, b);
            return new HSBType(color);
        }
    } catch (KNXFormatException kfe) {
        logger.info("Translator couldn't parse data for datapoint type '{}' (KNXFormatException).", datapoint.getDPT());
    } catch (KNXIllegalArgumentException kiae) {
        logger.info("Translator couldn't parse data for datapoint type '{}' (KNXIllegalArgumentException).", datapoint.getDPT());
    } catch (KNXException e) {
        logger.warn("Failed creating a translator for datapoint type '{}'.", datapoint.getDPT(), e);
    }
    return null;
}
Also used : KNXException(tuwien.auto.calimero.exception.KNXException) DPTXlatorDateTime(tuwien.auto.calimero.dptxlator.DPTXlatorDateTime) KNXIllegalArgumentException(tuwien.auto.calimero.exception.KNXIllegalArgumentException) DecimalFormat(java.text.DecimalFormat) Calendar(java.util.Calendar) Color(java.awt.Color) DPTXlatorString(tuwien.auto.calimero.dptxlator.DPTXlatorString) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) DPTXlatorBoolean(tuwien.auto.calimero.dptxlator.DPTXlatorBoolean) DPTXlator2ByteFloat(tuwien.auto.calimero.dptxlator.DPTXlator2ByteFloat) DPTXlator4ByteFloat(tuwien.auto.calimero.dptxlator.DPTXlator4ByteFloat) DPTXlatorSceneControl(tuwien.auto.calimero.dptxlator.DPTXlatorSceneControl) DPTXlator(tuwien.auto.calimero.dptxlator.DPTXlator) DecimalType(org.openhab.core.library.types.DecimalType) DPTXlator3BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled) DPTXlator4ByteFloat(tuwien.auto.calimero.dptxlator.DPTXlator4ByteFloat) KNXFormatException(tuwien.auto.calimero.exception.KNXFormatException) DPTXlator1BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled) SimpleDateFormat(java.text.SimpleDateFormat) HSBType(org.openhab.core.library.types.HSBType) NumberFormat(java.text.NumberFormat)

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