Search in sources :

Example 6 with Card

use of org.openhab.ui.habot.card.Card in project habot by ghys.

the class HABotResource method setCardBookmark.

@PUT
@Path("/cards/{cardUID}/bookmark")
@ApiOperation(value = "Sets a bookmark on a card.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "The card with the provided UID doesn't exist"), @ApiResponse(code = 500, message = "An error occured") })
public Response setCardBookmark(@PathParam("cardUID") @ApiParam(value = "cardUID", required = true) String cardUID) {
    Card card = this.cardRegistry.get(cardUID);
    if (card == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    card.setBookmark(true);
    this.cardRegistry.update(card);
    return Response.ok().build();
}
Also used : Card(org.openhab.ui.habot.card.Card) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with Card

use of org.openhab.ui.habot.card.Card 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 8 with Card

use of org.openhab.ui.habot.card.Card in project habot by ghys.

the class CardRegistry method add.

@Override
@NonNull
public Card add(@NonNull Card element) {
    // Remove old ephemeral cards
    Comparator<Card> byTimestamp = (e1, e2) -> e2.getTimestamp().compareTo(e1.getTimestamp());
    List<Card> oldCards = getAll().stream().filter(card -> card.isEphemeral()).sorted(byTimestamp).skip(10).collect(Collectors.toList());
    for (Card card : oldCards) {
        logger.info("Removing old ephemeral card {}", card.getUID());
        remove(card.getUID());
    }
    return super.add(element);
}
Also used : Logger(org.slf4j.Logger) Collection(java.util.Collection) AbstractRegistry(org.eclipse.smarthome.core.common.registry.AbstractRegistry) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) List(java.util.List) Component(org.osgi.service.component.annotations.Component) Card(org.openhab.ui.habot.card.Card) Comparator(java.util.Comparator) Reference(org.osgi.service.component.annotations.Reference) NonNull(org.eclipse.jdt.annotation.NonNull) Registry(org.eclipse.smarthome.core.common.registry.Registry) Card(org.openhab.ui.habot.card.Card) NonNull(org.eclipse.jdt.annotation.NonNull)

Aggregations

Card (org.openhab.ui.habot.card.Card)8 ApiOperation (io.swagger.annotations.ApiOperation)5 Path (javax.ws.rs.Path)5 ApiResponses (io.swagger.annotations.ApiResponses)4 PUT (javax.ws.rs.PUT)3 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Produces (javax.ws.rs.Produces)2 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)2 Reference (org.osgi.service.component.annotations.Reference)2 InvalidParameterException (java.security.InvalidParameterException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 List (java.util.List)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1