Search in sources :

Example 56 with Item

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;
}
Also used : IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) CardBuilder(org.openhab.ui.habot.card.CardBuilder) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) List(java.util.List) Skill(org.openhab.ui.habot.nlp.Skill) AbstractItemIntentInterpreter(org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) Item(org.eclipse.smarthome.core.items.Item) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation)

Example 57 with Item

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;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation)

Example 58 with Item

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;
}
Also used : IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) CardRegistry(org.openhab.ui.habot.card.internal.CardRegistry) TransformationHelper(org.eclipse.smarthome.core.transform.TransformationHelper) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) Set(java.util.Set) PersistenceExtensions(org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Skill(org.openhab.ui.habot.nlp.Skill) Card(org.openhab.ui.habot.card.Card) AbstractItemIntentInterpreter(org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Component(org.openhab.ui.habot.card.Component) State(org.eclipse.smarthome.core.types.State) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) FrameworkUtil(org.osgi.framework.FrameworkUtil) DateFormat(java.text.DateFormat) Item(org.eclipse.smarthome.core.items.Item) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Component(org.openhab.ui.habot.card.Component) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Card(org.openhab.ui.habot.card.Card)

Example 59 with Item

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;
}
Also used : CardRegistry(org.openhab.ui.habot.card.internal.CardRegistry) TransformationHelper(org.eclipse.smarthome.core.transform.TransformationHelper) Collection(java.util.Collection) Set(java.util.Set) HistoryLastChangesSkill(org.openhab.ui.habot.nlp.internal.skill.HistoryLastChangesSkill) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StateDescription(org.eclipse.smarthome.core.types.StateDescription) State(org.eclipse.smarthome.core.types.State) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) FrameworkUtil(org.osgi.framework.FrameworkUtil) Item(org.eclipse.smarthome.core.items.Item)

Example 60 with Item

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());
    }
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)111 GroupItem (org.eclipse.smarthome.core.items.GroupItem)42 GenericItem (org.eclipse.smarthome.core.items.GenericItem)39 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)37 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)35 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)31 State (org.eclipse.smarthome.core.types.State)26 Test (org.junit.Test)26 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)14 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)14 StringItem (org.eclipse.smarthome.core.library.items.StringItem)14 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)12 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)10 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)10 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)9 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 Thing (org.eclipse.smarthome.core.thing.Thing)9 Set (java.util.Set)8 Widget (org.eclipse.smarthome.model.sitemap.Widget)8