Search in sources :

Example 1 with DPT

use of tuwien.auto.calimero.dptxlator.DPT in project openhab1-addons by openhab.

the class KNXCoreTypeMapper method toDPTValue.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.binding.knx.config.KNXTypeMapper#toDPTValue(org.openhab.core.types.Type, java.lang.String)
     */
@Override
public String toDPTValue(Type type, String dptID) {
    DPT dpt;
    int mainNumber = getMainNumber(dptID);
    if (mainNumber == -1) {
        logger.error("toDPTValue couldn't identify mainnumber in dptID: {}", dptID);
        return null;
    }
    try {
        DPTXlator translator = TranslatorTypes.createTranslator(mainNumber, dptID);
        dpt = translator.getType();
    } catch (KNXException e) {
        e.printStackTrace();
        return null;
    }
    // check for HSBType first, because it extends PercentType as well
    if (type instanceof HSBType) {
        Color color = ((HSBType) type).toColor();
        return "r:" + Integer.toString(color.getRed()) + " g:" + Integer.toString(color.getGreen()) + " b:" + Integer.toString(color.getBlue());
    } else if (type instanceof OnOffType) {
        return type.equals(OnOffType.OFF) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof UpDownType) {
        return type.equals(UpDownType.UP) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof IncreaseDecreaseType) {
        DPT valueDPT = ((DPTXlator3BitControlled.DPT3BitControlled) dpt).getControlDPT();
        return type.equals(IncreaseDecreaseType.DECREASE) ? valueDPT.getLowerValue() + " 5" : valueDPT.getUpperValue() + " 5";
    } else if (type instanceof OpenClosedType) {
        return type.equals(OpenClosedType.CLOSED) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof StopMoveType) {
        return type.equals(StopMoveType.STOP) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof PercentType) {
        return type.toString();
    } else if (type instanceof DecimalType) {
        switch(mainNumber) {
            case 2:
                DPT valueDPT = ((DPTXlator1BitControlled.DPT1BitControlled) dpt).getValueDPT();
                switch(((DecimalType) type).intValue()) {
                    case 0:
                        return "0 " + valueDPT.getLowerValue();
                    case 1:
                        return "0 " + valueDPT.getUpperValue();
                    case 2:
                        return "1 " + valueDPT.getLowerValue();
                    default:
                        return "1 " + valueDPT.getUpperValue();
                }
            case 18:
                int intVal = ((DecimalType) type).intValue();
                if (intVal > 63) {
                    return "learn " + (intVal - 0x80);
                } else {
                    return "activate " + intVal;
                }
            default:
                return type.toString();
        }
    } else if (type instanceof StringType) {
        return type.toString();
    } else if (type instanceof DateTimeType) {
        return formatDateTime((DateTimeType) type, dptID);
    }
    logger.debug("toDPTValue: Couldn't get value for {} dpt id {} (no mapping).", type, dptID);
    return null;
}
Also used : KNXException(tuwien.auto.calimero.exception.KNXException) StringType(org.openhab.core.library.types.StringType) Color(java.awt.Color) DPT(tuwien.auto.calimero.dptxlator.DPT) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) StopMoveType(org.openhab.core.library.types.StopMoveType) DateTimeType(org.openhab.core.library.types.DateTimeType) DPTXlator(tuwien.auto.calimero.dptxlator.DPTXlator) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DPTXlator3BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) DPTXlator1BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled) HSBType(org.openhab.core.library.types.HSBType)

Example 2 with DPT

use of tuwien.auto.calimero.dptxlator.DPT in project openhab1-addons by openhab.

the class KNXCoreTypeMapperTest method testTypeMappingDate_11_001.

/**
     * KNXCoreTypeMapper tests method typeMapper.toType() for type “Time" KNX ID: 11.001 DPT_DATE
     *
     * Test correct year evaluation according KNX spec.
     *
     * @throws KNXFormatException
     */
@Test
public void testTypeMappingDate_11_001() throws KNXFormatException {
    DPT dpt = DPTXlatorDate.DPT_DATE;
    testToTypeClass(dpt, DateTimeType.class);
    // Use a too short byte array
    assertNull("KNXCoreTypeMapper.toType() should return null (required data length too short)", testToType(dpt, new byte[] {}, DateTimeType.class));
    // Use a too long byte array expecting that additional bytes will be ignored
    Type type = testToType(dpt, new byte[] { 0x01, 0x01, 0x00, (byte) 0xFF }, DateTimeType.class);
    testToDPTValue(dpt, type, "2000-01-01");
    type = testToType(dpt, new byte[] { 0x01, 0x01, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "2000-01-01");
    type = testToType(dpt, new byte[] { 0x01, 0x01, 99 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1999-01-01");
    type = testToType(dpt, new byte[] { 0x01, 0x01, 90 }, DateTimeType.class);
    testToDPTValue(dpt, type, "1990-01-01");
    type = testToType(dpt, new byte[] { 0x01, 0x01, 89 }, DateTimeType.class);
    testToDPTValue(dpt, type, "2089-01-01");
    // Test roll over (which is actually not in the KNX spec)
    type = testToType(dpt, new byte[] { 31, 0x02, 0x00 }, DateTimeType.class);
    testToDPTValue(dpt, type, "2000-03-02");
}
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 3 with DPT

use of tuwien.auto.calimero.dptxlator.DPT in project openhab1-addons by openhab.

the class KNXCoreTypeMapperTest method testTypeMapping8BitUnsigned_5_006.

/**
     * KNXCoreTypeMapper tests method typeMapper.toType() for type “8-Bit Unsigned Value" KNX ID: 5.006 DPT_TARRIF
     *
     * @throws KNXFormatException
     */
@Test
public void testTypeMapping8BitUnsigned_5_006() throws KNXFormatException {
    DPT dpt = DPTXlator8BitUnsigned.DPT_TARIFF;
    testToTypeClass(dpt, DecimalType.class);
    // Use a too short byte array
    assertNull("KNXCoreTypeMapper.toType() should return null (required data length too short)", testToType(dpt, new byte[] {}, DecimalType.class));
    Type type = testToType(dpt, new byte[] { 0 }, DecimalType.class);
    testToDPTValue(dpt, type, "0");
    type = testToType(dpt, new byte[] { (byte) 0xFF }, DecimalType.class);
    testToDPTValue(dpt, type, "255");
    // Use a too long byte array expecting that additional bytes will be ignored
    type = testToType(dpt, new byte[] { (byte) 0xFF, 0 }, DecimalType.class);
    testToDPTValue(dpt, type, "255");
}
Also used : 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) DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.Test)

Example 4 with DPT

use of tuwien.auto.calimero.dptxlator.DPT in project openhab1-addons by openhab.

the class KNXCoreTypeMapperTest method testTypeMapping8BitUnsigned_5_005.

/**
     * KNXCoreTypeMapper tests method typeMapper.toType() for type “8-Bit Unsigned Value" KNX ID: 5.005
     * DPT_DECIMALFACTOR
     *
     * @throws KNXFormatException
     */
@Test
public void testTypeMapping8BitUnsigned_5_005() throws KNXFormatException {
    DPT dpt = DPTXlator8BitUnsigned.DPT_DECIMALFACTOR;
    testToTypeClass(dpt, DecimalType.class);
    // Use a too short byte array
    assertNull("KNXCoreTypeMapper.toType() should return null (required data length too short)", testToType(dpt, new byte[] {}, DecimalType.class));
    Type type = testToType(dpt, new byte[] { 0 }, DecimalType.class);
    testToDPTValue(dpt, type, "0");
    type = testToType(dpt, new byte[] { (byte) 0xFF }, DecimalType.class);
    testToDPTValue(dpt, type, "255");
    // Use a too long byte array expecting that additional bytes will be ignored
    type = testToType(dpt, new byte[] { (byte) 0xFF, 0 }, DecimalType.class);
    testToDPTValue(dpt, type, "255");
}
Also used : 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) DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.Test)

Example 5 with DPT

use of tuwien.auto.calimero.dptxlator.DPT in project openhab1-addons by openhab.

the class KNXCoreTypeMapperTest method testTypeMappingDateTime_19_001_DST.

/**
     * KNXCoreTypeMapper tests method typeMapper.toType() for type “Date Time" KNX ID: 19.001 DPT_DATE_TIME
     * Testcase tests handling of Daylight Savings Time flag (DST).
     * Interpretation of DST is depending on default timezone, hence we're trying to test using
     * different timezones: default, New York, Berlin and Shanghai. Shanghai not having a DST.
     *
     * @throws KNXFormatException
     */
@Test
public void testTypeMappingDateTime_19_001_DST() throws KNXFormatException {
    DPT dpt = DPTXlatorDateTime.DPT_DATE_TIME;
    // 2014-07-31 00:00:00 DST flag set
    byte[] testDataDST = new byte[] { 0x72, 0x07, 0x1F, 0x00, 0x00, 0x00, (byte) 0x05, (byte) 0x00 };
    // 2014-07-31 00:00:00 DST flag cleared
    byte[] testDataNoDST = new byte[] { 0x72, 0x07, 0x1F, 0x00, 0x00, 0x00, (byte) 0x04, (byte) 0x00 };
    testToTypeClass(dpt, DateTimeType.class);
    Calendar c = Calendar.getInstance();
    c.set(2014, 7, 31);
    if (c.get(Calendar.DST_OFFSET) > 0) {
        // Should be null since we have a DST timezone but non-DST data: should be rejected
        assertNull(testToType(dpt, testDataNoDST, DateTimeType.class));
        Type type = testToType(dpt, testDataDST, DateTimeType.class);
        testToDPTValue(dpt, type, "2014-07-31 00:00:00");
    } else {
        // Should be null since we don't have a non-DST timezone but DST data: should be rejected
        assertNull(testToType(dpt, testDataDST, DateTimeType.class));
        Type type = testToType(dpt, testDataNoDST, DateTimeType.class);
        testToDPTValue(dpt, type, "2014-07-31 00:00:00");
    }
}
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) Calendar(java.util.Calendar) DPT(tuwien.auto.calimero.dptxlator.DPT) Test(org.junit.Test)

Aggregations

DateTimeType (org.openhab.core.library.types.DateTimeType)27 DPT (tuwien.auto.calimero.dptxlator.DPT)27 Test (org.junit.Test)26 DecimalType (org.openhab.core.library.types.DecimalType)21 HSBType (org.openhab.core.library.types.HSBType)21 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)21 OnOffType (org.openhab.core.library.types.OnOffType)21 OpenClosedType (org.openhab.core.library.types.OpenClosedType)21 PercentType (org.openhab.core.library.types.PercentType)21 StopMoveType (org.openhab.core.library.types.StopMoveType)21 StringType (org.openhab.core.library.types.StringType)21 UpDownType (org.openhab.core.library.types.UpDownType)21 Type (org.openhab.core.types.Type)20 Color (java.awt.Color)1 Calendar (java.util.Calendar)1 Locale (java.util.Locale)1 Datapoint (tuwien.auto.calimero.datapoint.Datapoint)1 DPTXlator (tuwien.auto.calimero.dptxlator.DPTXlator)1 DPTXlator1BitControlled (tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled)1 DPTXlator3BitControlled (tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled)1