Search in sources :

Example 51 with Item

use of org.eclipse.smarthome.core.items.Item in project habot by ghys.

the class HistoryWeeklyGraphSkill 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 = "W";
        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 52 with Item

use of org.eclipse.smarthome.core.items.Item in project habot by ghys.

the class CardBuilder method buildChartCard.

/**
 * Builds a card with a chart from an intent and matched items
 *
 * @param intent the intent
 * @param matchedItems the matched items
 * @param period the chart period
 * @return the card
 */
public Card buildChartCard(Intent intent, Collection<Item> matchedItems, String period) {
    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.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());
    } else {
        card.setTitle(getCardTitleFromGroupLabels(tags));
        // TODO: i18n
        card.setSubtitle(matchedItems.size() + " items");
    }
    Component chart = new Component("HbChartImage");
    chart.addConfig("items", matchedItems.stream().map(i -> i.getName()).collect(Collectors.toList()).toArray(new String[0]));
    chart.addConfig("period", period);
    Component analyzeButton = new Component("HbAnalyzeActionButton");
    analyzeButton.addConfig("items", chart.getConfig().get("items"));
    analyzeButton.addConfig("period", chart.getConfig().get("period"));
    card.addComponent("media", chart);
    card.addComponent("actions", analyzeButton);
    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 53 with Item

use of org.eclipse.smarthome.core.items.Item in project habot by ghys.

the class SetValueSkill 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);
        if (intent.getEntities().containsKey("color")) {
            interpretSetColor(intent, language, interpretation, matchedItems);
        } else if (intent.getEntities().containsKey("value")) {
            interpretSetValue(intent, language, interpretation, matchedItems);
        } else {
            interpretation.setAnswer(answerFormatter.getRandomAnswer("value_misunderstood"));
        }
    }
    return interpretation;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation)

Example 54 with Item

use of org.eclipse.smarthome.core.items.Item in project habot by ghys.

the class HistoryMonthlyGraphSkill 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 = "M";
        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 55 with Item

use of org.eclipse.smarthome.core.items.Item in project habot by ghys.

the class SetValueSkill method interpretSetColor.

private void interpretSetColor(Intent intent, String language, IntentInterpretation interpretation, Set<Item> matchedItems) {
    String colorString = intent.getEntities().get("color");
    // filter out the items which can't receive an HSB command
    List<Item> filteredItems = matchedItems.stream().filter(i -> i.getAcceptedCommandTypes().contains(HSBType.class)).collect(Collectors.toList());
    String hsbValue;
    try {
        ResourceBundle colors = ResourceBundle.getBundle("colors", new Locale(language));
        hsbValue = colors.getString("color_" + colorString);
    } catch (MissingResourceException e) {
        interpretation.setAnswer(answerFormatter.getRandomAnswer("set_color_unknown", ImmutableMap.of("color", colorString)));
        return;
    }
    if (filteredItems.isEmpty()) {
        interpretation.setAnswer(answerFormatter.getRandomAnswer("set_color_no_item", ImmutableMap.of("color", colorString)));
        interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
    } else if (filteredItems.size() == 1) {
        interpretation.setCard(cardBuilder.buildCard(intent, filteredItems));
        eventPublisher.post(ItemEventFactory.createCommandEvent(filteredItems.get(0).getName(), new HSBType(hsbValue)));
        interpretation.setAnswer(answerFormatter.getRandomAnswer("set_color_single", ImmutableMap.of("color", colorString)));
    } else {
        interpretation.setCard(cardBuilder.buildCard(intent, filteredItems));
        for (Item item : filteredItems) {
            eventPublisher.post(ItemEventFactory.createCommandEvent(item.getName(), new HSBType(hsbValue)));
        }
        interpretation.setAnswer(answerFormatter.getRandomAnswer("set_color_multiple", ImmutableMap.of("count", String.valueOf(filteredItems.size()), "color", colorString)));
    }
}
Also used : IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) ImmutableMap(com.google.common.collect.ImmutableMap) MissingResourceException(java.util.MissingResourceException) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) CardBuilder(org.openhab.ui.habot.card.CardBuilder) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) List(java.util.List) HSBType(org.eclipse.smarthome.core.library.types.HSBType) ResourceBundle(java.util.ResourceBundle) Locale(java.util.Locale) PercentType(org.eclipse.smarthome.core.library.types.PercentType) 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) Locale(java.util.Locale) Item(org.eclipse.smarthome.core.items.Item) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

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