use of org.openhab.ui.habot.card.Component in project habot by ghys.
the class HistoryLastChangesSkill method interpret.
@Override
public IntentInterpretation interpret(Intent intent, String language) {
IntentInterpretation interpretation = new IntentInterpretation();
Set<Item> matchedItems = findItems(intent);
if (matchedItems == null || matchedItems.isEmpty()) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("answer_nothing_found"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
return interpretation;
}
Set<String> tags = intent.getEntities().entrySet().stream().map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.toSet());
Card card = new Card("HbCard");
card.addTags(tags);
card.updateTimestamp();
card.setEphemeral(true);
card.setAddToDeckDenied(true);
Component timeline = new Component("HbTimeline");
if (matchedItems.size() == 1) {
Item item = matchedItems.stream().findFirst().get();
// TODO figure out a solution
HistoricItem historicItem = PersistenceExtensions.previousState(item, false);
if (historicItem == null) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("answer_nothing_found"));
interpretation.setHint(answerFormatter.getRandomAnswer("no_historical_data"));
return interpretation;
}
card.setTitle(item.getLabel());
card.setSubtitle(item.getName());
DateFormat dateFormat = new SimpleDateFormat();
Component pastTimelineEntry = new Component("HbTimelineEntry");
pastTimelineEntry.addConfig("title", formatState(item, historicItem.getState()));
pastTimelineEntry.addConfig("subtitle", dateFormat.format(historicItem.getTimestamp()));
timeline.addComponent("main", pastTimelineEntry);
Component nowTimelineEntry = new Component("HbTimelineEntry");
nowTimelineEntry.addConfig("title", formatState(item, historicItem.getState()));
nowTimelineEntry.addConfig("subtitle", dateFormat.format(new Date()));
timeline.addComponent("main", nowTimelineEntry);
} else {
interpretation.setAnswer(answerFormatter.getRandomAnswer("multiple_items_error"));
return interpretation;
}
card.addComponent("main", timeline);
this.cardRegistry.add(card);
interpretation.setAnswer(answerFormatter.getRandomAnswer("info_found_simple"));
interpretation.setCard(card);
return interpretation;
}
Aggregations