use of org.eclipse.smarthome.core.items.Item in project habot by ghys.
the class DeactivateObjectSkill 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("nothing_deactivated"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
} else {
interpretation.setMatchedItems(matchedItems);
// filter out the items which can't receive an OFF command
List<Item> filteredItems = matchedItems.stream().filter(i -> i.getAcceptedCommandTypes().contains(OnOffType.class)).collect(Collectors.toList());
interpretation.setCard(cardBuilder.buildCard(intent, filteredItems));
if (filteredItems.isEmpty()) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("nothing_deactivated"));
interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
} else if (filteredItems.size() == 1) {
if (filteredItems.get(0).getState().equals(OnOffType.OFF)) {
interpretation.setAnswer(answerFormatter.getRandomAnswer("switch_already_off"));
} else {
eventPublisher.post(ItemEventFactory.createCommandEvent(filteredItems.get(0).getName(), OnOffType.OFF));
interpretation.setAnswer(answerFormatter.getRandomAnswer("switch_deactivated"));
}
} else {
for (Item item : filteredItems) {
eventPublisher.post(ItemEventFactory.createCommandEvent(item.getName(), OnOffType.OFF));
}
interpretation.setAnswer(answerFormatter.getRandomAnswer("switches_deactivated", ImmutableMap.of("count", String.valueOf(filteredItems.size()))));
}
}
return interpretation;
}
use of org.eclipse.smarthome.core.items.Item in project habot by ghys.
the class HistoryDailyGraphSkill 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()));
} else {
interpretation.setMatchedItems(matchedItems);
String period = "D";
if (intent.getEntities().containsKey("period")) {
period = intent.getEntities().get("period").concat(period);
}
interpretation.setCard(this.cardBuilder.buildChartCard(intent, matchedItems, period));
}
interpretation.setAnswer(answerFormatter.getRandomAnswer("info_found_simple"));
return interpretation;
}
use of org.eclipse.smarthome.core.items.Item 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;
}
use of org.eclipse.smarthome.core.items.Item in project habot by ghys.
the class CardBuilder method buildCard.
/**
* Retrieves or build a card for the specified intent and matched items
*
* @param intent the intent including entities
* @param matchedItems the matched items
* @return the card (either retrieved or built)
*/
public Card buildCard(Intent intent, Collection<Item> matchedItems) {
Set<String> tags = intent.getEntities().entrySet().stream().filter(e -> e.getKey().equals("object") || e.getKey().equals("location")).map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.toSet());
Collection<Card> cardsInRegistry = this.cardRegistry.getCardByTags(tags).stream().filter(c -> !c.isNotReuseableInChat() && !c.isEphemeral()).collect(Collectors.toList());
if (cardsInRegistry.size() > 0) {
// don't handle multiple cards, just return the first one
Card existingCard = cardsInRegistry.iterator().next();
existingCard.updateTimestamp();
cardRegistry.update(existingCard);
return existingCard;
}
Card card = new Card("HbCard");
card.addTags(tags);
card.setEphemeral(true);
card.addConfig("bigger", true);
card.updateTimestamp();
if (matchedItems.size() == 1) {
Item item = matchedItems.stream().findFirst().get();
card.setTitle(item.getLabel());
card.setSubtitle(item.getName());
switch(item.getType()) {
case CoreItemFactory.SWITCH:
Component switchComponent = new Component("HbSwitch");
switchComponent.addConfig("item", item.getName());
card.addComponent("right", switchComponent);
break;
case CoreItemFactory.DIMMER:
if (item.hasTag("habot:switchable")) {
Component dimmerSwitchComponent = new Component("HbSwitch");
dimmerSwitchComponent.addConfig("item", item.getName());
card.addComponent("right", dimmerSwitchComponent);
} else if (!item.hasTag("habot:control:knob")) {
Component dimmerValueComponent = new Component("HbSingleItemValue");
dimmerValueComponent.addConfig("item", item.getName());
card.addComponent("right", dimmerValueComponent);
}
Component dimmerContainerComponent = new Component("HbContainer");
dimmerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
if (item.hasTag("habot:control:knob")) {
Component knobComponent = new Component("HbKnob");
knobComponent.addConfig("item", item.getName());
knobComponent.addConfig("size", "200px");
knobComponent.addConfig("textSize", "2rem");
knobComponent.addConfig("color", "primary");
dimmerContainerComponent.addComponent("main", knobComponent);
} else {
Component sliderComponent = new Component("HbSlider");
sliderComponent.addConfig("item", item.getName());
dimmerContainerComponent.addComponent("main", sliderComponent);
}
card.addComponent("main", dimmerContainerComponent);
break;
case CoreItemFactory.ROLLERSHUTTER:
Component shutterValueComponent = new Component("HbSingleItemValue");
shutterValueComponent.addConfig("item", item.getName());
card.addComponent("right", shutterValueComponent);
Component shutterContainerComponent = new Component("HbContainer");
shutterContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
Component shutterControlComponent = new Component("HbShutterControl");
shutterControlComponent.addConfig("item", item.getName());
shutterControlComponent.addConfig("size", "lg");
shutterControlComponent.addConfig("rounded", true);
shutterControlComponent.addConfig("glossy", true);
shutterControlComponent.addConfig("push", true);
shutterControlComponent.addConfig("stopIcon", "close");
shutterContainerComponent.addComponent("main", shutterControlComponent);
card.addComponent("main", shutterContainerComponent);
break;
case CoreItemFactory.PLAYER:
Component playerContainerComponent = new Component("HbContainer");
playerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
Component playerComponent = new Component("HbPlayer");
playerComponent.addConfig("item", item.getName());
playerComponent.addConfig("size", "lg");
playerContainerComponent.addComponent("main", playerComponent);
card.addComponent("main", playerContainerComponent);
break;
case CoreItemFactory.COLOR:
Component colorPickerComponent = new Component("HbColorPicker");
colorPickerComponent.addConfig("item", item.getName());
card.addComponent("right", colorPickerComponent);
Component brightnessDimmerComponent = new Component("HbSwitch");
brightnessDimmerComponent.addConfig("item", item.getName());
card.addComponent("right", brightnessDimmerComponent);
Component brightnessDimmerContainerComponent = new Component("HbContainer");
brightnessDimmerContainerComponent.addConfig("classes", new String[] { "full-width", "text-center" });
Component brightnessSliderComponent = new Component("HbSlider");
brightnessSliderComponent.addConfig("item", item.getName());
brightnessDimmerContainerComponent.addComponent("main", brightnessSliderComponent);
card.addComponent("main", brightnessDimmerContainerComponent);
break;
default:
if (item.getType() == CoreItemFactory.IMAGE || item.getTags().stream().anyMatch(t -> t.startsWith("habot:image:sitemap:"))) {
/*
* If the item is an image (or a String with a tag indicating it's an image), build a
* HbImage component in the "media" slot
*/
Component singleImageComponent = new Component("HbImage");
singleImageComponent.addConfig("item", item.getName());
card.addComponent("media", singleImageComponent);
} else {
/*
* Try to get a formatted state to determine whether it's small enough to display
* in the "right" slot - otherwise add it to the "main" slot
*/
String formattedState = formatState(item, item.getState());
Component singleItemComponent = new Component("HbSingleItemValue");
singleItemComponent.addConfig("item", item.getName());
if (formattedState.length() < 10) {
card.addComponent("right", singleItemComponent);
} else {
card.addComponent("main", singleItemComponent);
}
}
break;
}
} else {
card.setTitle(getCardTitleFromGroupLabels(tags));
// TODO: i18n
card.setSubtitle(matchedItems.size() + " items");
// TODO: detect images and build a HbCarousel with them - for webcams etc.
Component list = new Component("HbList");
for (Item item : matchedItems) {
Component listItem = new Component("HbListItem");
listItem.addConfig("item", item.getName());
listItem.addConfig("label", item.getLabel());
list.addComponent("items", listItem);
}
card.addComponent("list", list);
}
// Adds the (ephemeral) card to the registry anyways so it appears in the "recent cards" page
cardRegistry.add(card);
return card;
}
use of org.eclipse.smarthome.core.items.Item in project habot by ghys.
the class AbstractItemIntentInterpreter method findItems.
/**
* Returns the items matching the entities in the intent by looking for tags prefixed by "object:" or "location:".
* Group items are expanded and the tags, and tags are inherited to members.
*
* The resulting items should match the object AND the location if both are provided.
*
* @param intent the {@link Intent} containing the entities to match to items' tags.
* @return the set of matching items
*/
protected Set<Item> findItems(Intent intent) {
Collection<Item> itemsWithLocationTag = null;
if (intent.entities.containsKey("location")) {
itemsWithLocationTag = itemRegistry.getItemsByTag("location:" + intent.entities.get("location"));
}
Collection<Item> itemsWithObjectTag = null;
if (intent.entities.containsKey("object")) {
itemsWithObjectTag = itemRegistry.getItemsByTag("object:" + intent.entities.get("object"));
}
HashSet<Item> itemsMatchingLocationSlot = null;
if (itemsWithLocationTag != null) {
itemsMatchingLocationSlot = new HashSet<Item>();
for (Item item : itemsWithLocationTag) {
if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
for (Item member : groupItem.getAllMembers()) {
itemsMatchingLocationSlot.add(member);
}
} else {
itemsMatchingLocationSlot.add(item);
}
}
}
HashSet<Item> itemsMatchingObjectSlot = null;
if (itemsWithObjectTag != null) {
itemsMatchingObjectSlot = new HashSet<Item>();
for (Item item : itemsWithObjectTag) {
if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
for (Item member : groupItem.getAllMembers()) {
itemsMatchingObjectSlot.add(member);
}
} else {
itemsMatchingObjectSlot.add(item);
}
}
}
if (itemsMatchingLocationSlot == null && itemsMatchingObjectSlot == null) {
return null;
} else if (itemsMatchingObjectSlot == null) {
return itemsMatchingLocationSlot;
} else if (itemsMatchingLocationSlot == null) {
return itemsMatchingObjectSlot;
} else {
return itemsMatchingLocationSlot.stream().filter(itemsMatchingObjectSlot::contains).collect(Collectors.toSet());
}
}
Aggregations