use of org.openhab.library.tel.types.CallType in project openhab1-addons by openhab.
the class JpaHistoricItem method fromPersistedItem.
/**
* Converts the string value of the persisted item to the state of a HistoricItem.
*
* @param pItem the persisted JpaPersistentItem
* @param item the source reference Item
* @return historic item
*/
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
State state;
if (item instanceof NumberItem) {
state = new DecimalType(Double.valueOf(pItem.getValue()));
} else if (item instanceof DimmerItem) {
state = new PercentType(Integer.valueOf(pItem.getValue()));
} else if (item instanceof SwitchItem) {
state = OnOffType.valueOf(pItem.getValue());
} else if (item instanceof ContactItem) {
state = OpenClosedType.valueOf(pItem.getValue());
} else if (item instanceof RollershutterItem) {
state = PercentType.valueOf(pItem.getValue());
} else if (item instanceof ColorItem) {
state = new HSBType(pItem.getValue());
} else if (item instanceof DateTimeItem) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(Long.valueOf(pItem.getValue())));
state = new DateTimeType(cal);
} else if (item instanceof LocationItem) {
PointType pType = null;
String[] comps = pItem.getValue().split(";");
if (comps.length >= 2) {
pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
if (comps.length == 3) {
pType.setAltitude(new DecimalType(comps[2]));
}
}
state = pType;
} else if (item instanceof CallItem) {
state = new CallType(pItem.getValue());
} else {
state = new StringType(pItem.getValue());
}
return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
use of org.openhab.library.tel.types.CallType in project openhab1-addons by openhab.
the class AbstractDynamoDBItem method asHistoricItem.
/*
* (non-Javadoc)
*
* @see org.openhab.persistence.dynamodb.internal.DynamoItem#asHistoricItem(org.openhab.core.items.Item)
*/
@Override
public HistoricItem asHistoricItem(final Item item) {
final State[] state = new State[1];
accept(new DynamoDBItemVisitor() {
@Override
public void visit(DynamoDBStringItem dynamoStringItem) {
if (item instanceof ColorItem) {
state[0] = new HSBType(dynamoStringItem.getState());
} else if (item instanceof LocationItem) {
state[0] = new PointType(dynamoStringItem.getState());
} else if (item instanceof DateTimeItem) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
try {
cal.setTime(DATEFORMATTER.parse(dynamoStringItem.getState()));
} catch (ParseException e) {
LOGGER.error("Failed to parse {} as date. Outputting UNDEF instead", dynamoStringItem.getState());
state[0] = UnDefType.UNDEF;
}
state[0] = new DateTimeType(cal);
} else if (dynamoStringItem.getState().equals(UNDEFINED_PLACEHOLDER)) {
state[0] = UnDefType.UNDEF;
} else if (item instanceof CallItem) {
String parts = dynamoStringItem.getState();
String[] strings = parts.split("##");
String dest = strings[0];
String orig = strings[1];
state[0] = new CallType(orig, dest);
} else {
state[0] = new StringType(dynamoStringItem.getState());
}
}
@Override
public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
if (item instanceof NumberItem) {
state[0] = new DecimalType(dynamoBigDecimalItem.getState());
} else if (item instanceof DimmerItem) {
state[0] = new PercentType(dynamoBigDecimalItem.getState());
} else if (item instanceof SwitchItem) {
state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OnOffType.ON : OnOffType.OFF;
} else if (item instanceof ContactItem) {
state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
} else if (item instanceof RollershutterItem) {
state[0] = new PercentType(dynamoBigDecimalItem.getState());
} else {
LOGGER.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
state[0] = new StringType(dynamoBigDecimalItem.getState().toString());
}
}
});
return new DynamoDBHistoricItem(getName(), state[0], getTime());
}
use of org.openhab.library.tel.types.CallType in project openhab1-addons by openhab.
the class AbstractDynamoDBItemSerializationTest method testAsHistoricGeneric.
/**
* Test state deserialization, that is DynamoDBItem conversion to HistoricItem
*
* @param dbItem dynamo db item
* @param item parameter for DynamoDBItem.asHistoricItem
* @param expectedState Expected state of the historic item. DecimalTypes are compared with reduced accuracy
* @return
* @throws IOException
*/
public HistoricItem testAsHistoricGeneric(DynamoDBItem<?> dbItem, Item item, Object expectedState) throws IOException {
HistoricItem historicItem = dbItem.asHistoricItem(item);
assertEquals("item1", historicItem.getName());
assertEquals(date, historicItem.getTimestamp());
assertEquals(expectedState.getClass(), historicItem.getState().getClass());
if (expectedState instanceof DecimalType) {
// serialization loses accuracy, take this into consideration
assertTrue(DynamoDBBigDecimalItem.loseDigits(((DecimalType) expectedState).toBigDecimal()).compareTo(((DecimalType) historicItem.getState()).toBigDecimal()) == 0);
} else if (expectedState instanceof CallType) {
// CallType has buggy equals, let's compare strings instead
assertEquals(expectedState.toString(), historicItem.getState().toString());
} else {
assertEquals(expectedState, historicItem.getState());
}
return historicItem;
}
use of org.openhab.library.tel.types.CallType in project openhab1-addons by openhab.
the class AbstractDynamoDBItemSerializationTest method testCallTypeWithCallItem.
@Test
public void testCallTypeWithCallItem() throws IOException {
final DynamoDBItem<?> dbitem = testStateGeneric(new CallType("origNum", "destNum"), "destNum##origNum");
testAsHistoricGeneric(dbitem, new CallItem("foo"), new CallType("origNum", "destNum"));
}
use of org.openhab.library.tel.types.CallType in project openhab1-addons by openhab.
the class FreeswitchBinding method endCallItemUpdate.
/**
* update items on call end
*
* @param config
*/
private void endCallItemUpdate(FreeswitchBindingConfig config) {
OnOffType activeState = OnOffType.OFF;
;
CallType callType = (CallType) CallType.EMPTY;
StringType callerId = StringType.EMPTY;
/*
* A channel has ended that has this item associated with it
* We still need to check if this item is associated with other
* channels.
* We are going to iterate backwards to get the last added channel;
*/
ListIterator<String> it = new ArrayList<String>(itemMap.keySet()).listIterator(itemMap.size());
// if we get a match we will stop processing
boolean match = false;
while (it.hasPrevious()) {
String uuid = it.previous();
for (FreeswitchBindingConfig c : itemMap.get(uuid)) {
if (c.getItemName().equals(config.getItemName())) {
Channel channel = eventCache.get(uuid);
activeState = OnOffType.ON;
callType = channel.getCall();
callerId = new StringType(String.format("%s : %s", channel.getEventHeader(CID_NAME), channel.getEventHeader(CID_NUMBER)));
match = true;
break;
}
}
if (match) {
break;
}
}
if (config.getItemType().isAssignableFrom(SwitchItem.class)) {
eventPublisher.postUpdate(config.getItemName(), activeState);
} else if (config.getItemType().isAssignableFrom(CallItem.class)) {
eventPublisher.postUpdate(config.getItemName(), callType);
} else if (config.getItemType().isAssignableFrom(StringItem.class)) {
eventPublisher.postUpdate(config.getItemName(), callerId);
} else {
logger.warn("handleHangupCall - postUpdate for itemType '{}' is undefined", config.getItemName());
}
}
Aggregations