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;
}
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");
}
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");
}
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");
}
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");
}
}
Aggregations