use of org.openhab.core.types.Type in project openhab1-addons by openhab.
the class KNXCoreTypeMapperTest method testTypeMappingB1U3.
/**
* Convenience method: testing KNXCoretypeMapper for type “Datapoint Types B1U3 Controlled"
*
* @throws KNXFormatException
*/
private void testTypeMappingB1U3(DPT dpt, Class<? extends Type> expectedClass, String valueLow, String valueHigh) throws KNXFormatException {
testToTypeClass(dpt, expectedClass);
// Use a too short byte array
assertNull("KNXCoreTypeMapper.toType() should return null (required data length too short)", testToType(dpt, new byte[] {}, expectedClass));
// 3 lsb set to 0 indicate a break.
Type type = testToType(dpt, new byte[] { 0x00 }, expectedClass);
assertEquals("KNXCoreTypeMapper.toType() should return IncreaseDecreaseType.INCREASE for break messages", IncreaseDecreaseType.INCREASE, type);
// 3 lsb set to 8 indicate a break.
type = testToType(dpt, new byte[] { 0x08 }, expectedClass);
assertEquals("KNXCoreTypeMapper.toType() should return IncreaseDecreaseType.INCREASE for break messages", IncreaseDecreaseType.INCREASE, type);
type = testToType(dpt, new byte[] { 0x01 }, expectedClass);
testToDPTValue(dpt, type, valueLow);
type = testToType(dpt, new byte[] { 0x0F }, expectedClass);
testToDPTValue(dpt, type, valueHigh);
// Check that additional bit (4 msb) will be ignored
type = testToType(dpt, new byte[] { (byte) 0xFF }, expectedClass);
testToDPTValue(dpt, type, valueHigh);
// Use a too long byte array expecting that additional bytes will be ignored
type = testToType(dpt, new byte[] { (byte) 0x0F, 0 }, expectedClass);
testToDPTValue(dpt, type, valueHigh);
}
use of org.openhab.core.types.Type in project openhab1-addons by openhab.
the class KNXCoreTypeMapperTest method testTypeMappingTime_10_001.
/**
* KNXCoreTypeMapper tests method typeMapper.toType() for type “Time" KNX ID: 10.001 DPT_TIMEOFDAY
*
* Test case: positive tests
*
* @throws KNXFormatException
*/
@Test
public void testTypeMappingTime_10_001() throws KNXFormatException {
DPT dpt = DPTXlatorTime.DPT_TIMEOFDAY;
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[] { 0x20, 0x00, 0x00, (byte) 0xFF }, DateTimeType.class);
testToDPTValue(dpt, type, "Mon, 00:00:00");
/*
* Set day to no day, 0 hours, 0 minutes and 0 seconds
*
*/
type = testToType(dpt, new byte[] { 0x00, 0x00, 0x00 }, DateTimeType.class);
String today = String.format(Locale.US, "%1$ta", Calendar.getInstance());
testToDPTValue(dpt, type, today + ", 00:00:00");
/*
* Set day to Monday, 0 hours, 0 minutes and 0 seconds January 5th, 1970 was a Monday
*/
type = testToType(dpt, new byte[] { 0x20, 0x00, 0x00 }, DateTimeType.class);
testToDPTValue(dpt, type, "Mon, 00:00:00");
/*
* * Set day to Tuesday, 0 hours, 0 minutes and 0 seconds January 6th, 1970 was a Tuesday
*/
type = testToType(dpt, new byte[] { 0x40, 0, 0 }, DateTimeType.class);
testToDPTValue(dpt, type, "Tue, 00:00:00");
/*
* Set day to Wednesday, 0 hours, 0 minutes and 0 seconds January 7th, 1970 was a Wednesday
*/
type = testToType(dpt, new byte[] { 0x60, 0, 0 }, DateTimeType.class);
testToDPTValue(dpt, type, "Wed, 00:00:00");
/*
* Set day to Thursday, 0 hours, 0 minutes and 0 seconds January 1st, 1970 was a Thursday
*/
type = testToType(dpt, new byte[] { (byte) 0x80, 0, 0 }, DateTimeType.class);
testToDPTValue(dpt, type, "Thu, 00:00:00");
/*
* Set day to Friday, 0 hours, 0 minutes and 0 seconds January 2nd, 1970 was a Friday
*/
type = testToType(dpt, new byte[] { (byte) 0xA0, 0, 0 }, DateTimeType.class);
testToDPTValue(dpt, type, "Fri, 00:00:00");
/*
* Set day to Saturday, 0 hours, 0 minutes and 0 seconds January 3rd, 1970 was a Saturday
*/
type = testToType(dpt, new byte[] { (byte) 0xC0, 0, 0 }, DateTimeType.class);
testToDPTValue(dpt, type, "Sat, 00:00:00");
/*
* Set day to Sunday, 0 hours, 0 minutes and 0 seconds January 4th, 1970 was a Sunday
*/
type = testToType(dpt, new byte[] { (byte) 0xE0, 0, 0 }, DateTimeType.class);
testToDPTValue(dpt, type, "Sun, 00:00:00");
/*
* Set day to Monday, 1 hour, 0 minutes and 0 seconds
*/
type = testToType(dpt, new byte[] { 0x21, 0, 0 }, DateTimeType.class);
testToDPTValue(dpt, type, "Mon, 01:00:00");
/*
* Set day to Monday, 0 hour, 1 minute and 0 seconds
*/
type = testToType(dpt, new byte[] { 0x20, 1, 0 }, DateTimeType.class);
testToDPTValue(dpt, type, "Mon, 00:01:00");
/*
* Set day to Monday, 0 hour, 0 minute and 1 seconds
*/
type = testToType(dpt, new byte[] { 0x20, 0, 1 }, DateTimeType.class);
testToDPTValue(dpt, type, "Mon, 00:00:01");
/*
* Set day to Monday, 23 hours, 59 minutes and 59 seconds
*/
type = testToType(dpt, new byte[] { 0x37, 59, 59 }, DateTimeType.class);
testToDPTValue(dpt, type, "Mon, 23:59:59");
}
use of org.openhab.core.types.Type in project openhab1-addons by openhab.
the class KNXCoreTypeMapperTest method testTypeMapping8BitSigned_6_010.
/**
* KNXCoreTypeMapper tests method typeMapper.toType() for type “8-Bit Signed Value" KNX ID: 6.010
* DPT_VALUE_1_UCOUNT
*
* @throws KNXFormatException
*/
@Test
public void testTypeMapping8BitSigned_6_010() throws KNXFormatException {
DPT dpt = DPTXlator8BitSigned.DPT_VALUE_1_UCOUNT;
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) 0x7F }, DecimalType.class);
testToDPTValue(dpt, type, "127");
type = testToType(dpt, new byte[] { (byte) 0x80 }, DecimalType.class);
testToDPTValue(dpt, type, "-128");
type = testToType(dpt, new byte[] { (byte) 0xFF }, DecimalType.class);
testToDPTValue(dpt, type, "-1");
// 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, "-1");
}
use of org.openhab.core.types.Type in project openhab1-addons by openhab.
the class KNXBinding method readFromKNX.
/**
* Handles the given {@link ProcessEvent}. After finding the corresponding
* Item (by iterating through all known group addresses) this Item is updated.
* Each item is added to a special list to identify and avoid echo's in
* the <code>receiveUpdate</code> and <code>receiveCommand</code> methods.
*
* @param e the {@link ProcessEvent} to handle.
*/
private void readFromKNX(ProcessEvent e) {
try {
GroupAddress destination = e.getDestination();
byte[] asdu = e.getASDU();
if (asdu.length == 0) {
return;
}
String[] itemList = getItemNames(destination);
if (itemList.length == 0) {
logger.debug("Received telegram for unknown group address {}", destination.toString());
}
for (String itemName : itemList) {
Iterable<Datapoint> datapoints = getDatapoints(itemName, destination);
if (datapoints != null) {
for (Datapoint datapoint : datapoints) {
Type type = getType(datapoint, asdu);
if (type != null) {
if (type instanceof Command && isStartStopEnabled(itemName, destination, datapoint)) {
if (isDimmerThreadRunning(itemName) && type == IncreaseDecreaseType.INCREASE) {
stopDimmerThread(itemName);
} else {
startDimmerThread(destination, itemName, (Command) type);
}
} else {
sendTypeToItemButNotToKnx(destination, itemName, type);
}
} else {
final char[] hexCode = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder(2 + asdu.length * 2);
sb.append("0x");
for (byte b : asdu) {
sb.append(hexCode[(b >> 4) & 0xF]);
sb.append(hexCode[(b & 0xF)]);
}
logger.debug("Ignoring KNX bus data: couldn't transform to an openHAB type (not supported). Destination='{}', datapoint='{}', data='{}'", new Object[] { destination.toString(), datapoint.toString(), sb.toString() });
}
}
}
}
} catch (RuntimeException re) {
logger.error("Error while receiving event from KNX bus: " + re.toString());
}
}
use of org.openhab.core.types.Type in project openhab1-addons by openhab.
the class KNXCoreTypeMapperTest method testTypeMapping4ByteUnsigned_12_001.
/**
* KNXCoreTypeMapper tests method typeMapper.toType() for type “4-Octet Unsigned Value" KNX ID: 12.001
* DPT_VALUE_4_UCOUNT
*
* @throws KNXFormatException
*/
@Test
public void testTypeMapping4ByteUnsigned_12_001() throws KNXFormatException {
DPT dpt = DPTXlator4ByteUnsigned.DPT_VALUE_4_UCOUNT;
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));
// Use a too long byte array expecting that additional bytes will be ignored
Type type = testToType(dpt, new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, DecimalType.class);
testToDPTValue(dpt, type, "4294967295");
type = testToType(dpt, new byte[] { 0x00, 0x00, 0x00, 0x00 }, DecimalType.class);
testToDPTValue(dpt, type, "0");
type = testToType(dpt, new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, DecimalType.class);
testToDPTValue(dpt, type, "4294967295");
}
Aggregations