Search in sources :

Example 6 with IntentInterpretation

use of org.openhab.ui.habot.nlp.IntentInterpretation 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 7 with IntentInterpretation

use of org.openhab.ui.habot.nlp.IntentInterpretation 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)

Example 8 with IntentInterpretation

use of org.openhab.ui.habot.nlp.IntentInterpretation 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 9 with IntentInterpretation

use of org.openhab.ui.habot.nlp.IntentInterpretation 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 10 with IntentInterpretation

use of org.openhab.ui.habot.nlp.IntentInterpretation 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)

Aggregations

IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)12 Item (org.eclipse.smarthome.core.items.Item)11 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)5 Intent (org.openhab.ui.habot.nlp.Intent)5 Skill (org.openhab.ui.habot.nlp.Skill)5 Reference (org.osgi.service.component.annotations.Reference)5 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)4 AbstractItemIntentInterpreter (org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 List (java.util.List)3 ItemEventFactory (org.eclipse.smarthome.core.items.events.ItemEventFactory)3 CardBuilder (org.openhab.ui.habot.card.CardBuilder)3 Locale (java.util.Locale)2 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)2 Card (org.openhab.ui.habot.card.Card)2 InputStream (java.io.InputStream)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1