use of org.openhab.core.library.items.LocationItem in project openhab1-addons by openhab.
the class MqttMessageSubscriberTest method canParseCommand.
@Test
public void canParseCommand() throws Exception {
ColorItem colorItem = new ColorItem("ColorItem");
DimmerItem dimmerItem = new DimmerItem("DimmerItem");
LocationItem locationItem = new LocationItem("LocationItem");
NumberItem numberItem = new NumberItem("NumberItem");
RollershutterItem rollershutterItem = new RollershutterItem("SetpointItem");
StringItem stringItem = new StringItem("StringItem");
SwitchItem switchItem = new SwitchItem("SwitchItem");
MqttMessageSubscriber subscriber = new MqttMessageSubscriber("mybroker:/mytopic:command:default");
assertEquals(StringType.valueOf("test"), subscriber.getCommand("test", stringItem.getAcceptedCommandTypes()));
assertEquals(StringType.valueOf("{\"person\"{\"name\":\"me\"}}"), subscriber.getCommand("{\"person\"{\"name\":\"me\"}}", stringItem.getAcceptedCommandTypes()));
assertEquals(StringType.valueOf(""), subscriber.getCommand("", stringItem.getAcceptedCommandTypes()));
assertEquals(OnOffType.ON, subscriber.getCommand("ON", switchItem.getAcceptedCommandTypes()));
assertEquals(HSBType.valueOf("5,6,5"), subscriber.getCommand("5,6,5", colorItem.getAcceptedCommandTypes()));
assertEquals(DecimalType.ZERO, subscriber.getCommand(DecimalType.ZERO.toString(), numberItem.getAcceptedCommandTypes()));
assertEquals(PercentType.HUNDRED, subscriber.getCommand(PercentType.HUNDRED.toString(), dimmerItem.getAcceptedCommandTypes()));
assertEquals(PercentType.valueOf("80"), subscriber.getCommand(PercentType.valueOf("80").toString(), rollershutterItem.getAcceptedCommandTypes()));
assertEquals(PointType.valueOf("53.3239919,-6.5258807"), subscriber.getCommand(PointType.valueOf("53.3239919,-6.5258807").toString(), locationItem.getAcceptedCommandTypes()));
}
use of org.openhab.core.library.items.LocationItem in project openhab1-addons by openhab.
the class MqttMessageSubscriberTest method canParseState.
@Test
public void canParseState() throws Exception {
LocationItem locationItem = new LocationItem("LocationItem");
StringItem stringItem = new StringItem("StringItem");
SwitchItem switchItem = new SwitchItem("SwitchItem");
MqttMessageSubscriber subscriber = new MqttMessageSubscriber("mybroker:/mytopic:state:default");
assertEquals(OnOffType.ON, subscriber.getState("ON", switchItem.getAcceptedDataTypes()));
assertEquals(StringType.valueOf(""), subscriber.getState("", stringItem.getAcceptedDataTypes()));
assertEquals(StringType.valueOf("test"), subscriber.getState("test", stringItem.getAcceptedDataTypes()));
assertEquals(StringType.valueOf("{\"person\"{\"name\":\"me\"}}"), subscriber.getState("{\"person\"{\"name\":\"me\"}}", stringItem.getAcceptedDataTypes()));
assertEquals(PointType.valueOf("53.3239919,-6.5258807"), subscriber.getState(PointType.valueOf("53.3239919,-6.5258807").toString(), locationItem.getAcceptedDataTypes()));
}
use of org.openhab.core.library.items.LocationItem 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.core.library.items.LocationItem 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.core.library.items.LocationItem in project openhab1-addons by openhab.
the class AbstractDynamoDBItemSerializationTest method testPointTypeWithLocationItem.
@Test
public void testPointTypeWithLocationItem() throws IOException {
final PointType point = new PointType(new DecimalType(60.3), new DecimalType(30.2), new DecimalType(510.90));
String expected = StringUtils.join(new String[] { point.getLatitude().toBigDecimal().toString(), point.getLongitude().toBigDecimal().toString(), point.getAltitude().toBigDecimal().toString() }, ",");
DynamoDBItem<?> dbitem = testStateGeneric(point, expected);
testAsHistoricGeneric(dbitem, new LocationItem("foo"), point);
}
Aggregations