Search in sources :

Example 1 with EventType

use of org.openhab.binding.homeconnect.internal.client.model.EventType in project openhab-addons by openhab.

the class HomeConnectEventSourceListener method mapEventSourceEventToEvent.

private List<Event> mapEventSourceEventToEvent(String haId, EventType type, @Nullable String data) {
    List<Event> events = new ArrayList<>();
    if ((STATUS.equals(type) || EVENT.equals(type) || NOTIFY.equals(type)) && data != null && !data.trim().isEmpty() && !EMPTY_DATA.equals(data)) {
        try {
            JsonObject responseObject = HttpHelper.parseString(data).getAsJsonObject();
            JsonArray items = responseObject.getAsJsonArray("items");
            items.forEach(item -> {
                JsonObject obj = (JsonObject) item;
                String key = getJsonElementAsString(obj, "key").orElse(null);
                String value = getJsonElementAsString(obj, "value").orElse(null);
                String unit = getJsonElementAsString(obj, "unit").orElse(null);
                String name = getJsonElementAsString(obj, "name").orElse(null);
                String uri = getJsonElementAsString(obj, "uri").orElse(null);
                EventLevel level = getJsonElementAsString(obj, "level").map(EventLevel::valueOfLevel).orElse(null);
                EventHandling handling = getJsonElementAsString(obj, "handling").map(EventHandling::valueOfHandling).orElse(null);
                ZonedDateTime creation = getJsonElementAsLong(obj, "timestamp").map(timestamp -> ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), TimeZone.getDefault().toZoneId())).orElse(ZonedDateTime.now());
                events.add(new Event(haId, type, key, name, uri, creation, level, handling, value, unit));
            });
        } catch (IllegalStateException e) {
            logger.error("Could not parse event! haId={}, error={}", haId, e.getMessage());
        }
    } else {
        events.add(new Event(haId, type));
    }
    return events;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) EventLevel(org.openhab.binding.homeconnect.internal.client.model.EventLevel) ScheduledFuture(java.util.concurrent.ScheduledFuture) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) EventType(org.openhab.binding.homeconnect.internal.client.model.EventType) ArrayList(java.util.ArrayList) HomeConnectEventListener(org.openhab.binding.homeconnect.internal.client.listener.HomeConnectEventListener) Nullable(org.eclipse.jdt.annotation.Nullable) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HttpStatus(org.eclipse.jetty.http.HttpStatus) LocalDateTime.now(java.time.LocalDateTime.now) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) TimeZone(java.util.TimeZone) Event(org.openhab.binding.homeconnect.internal.client.model.Event) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Instant(java.time.Instant) EventHandling(org.openhab.binding.homeconnect.internal.client.model.EventHandling) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) JsonArray(com.google.gson.JsonArray) ChronoUnit(java.time.temporal.ChronoUnit) InboundSseEvent(javax.ws.rs.sse.InboundSseEvent) Optional(java.util.Optional) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) EventLevel(org.openhab.binding.homeconnect.internal.client.model.EventLevel) ZonedDateTime(java.time.ZonedDateTime) EventHandling(org.openhab.binding.homeconnect.internal.client.model.EventHandling) ArrayList(java.util.ArrayList) Event(org.openhab.binding.homeconnect.internal.client.model.Event) InboundSseEvent(javax.ws.rs.sse.InboundSseEvent) JsonObject(com.google.gson.JsonObject)

Example 2 with EventType

use of org.openhab.binding.homeconnect.internal.client.model.EventType in project openhab-addons by openhab.

the class HomeConnectEventSourceListener method onEvent.

public void onEvent(InboundSseEvent inboundEvent) {
    String id = inboundEvent.getId();
    String type = inboundEvent.getName();
    String data = inboundEvent.readData();
    lastEventReceived = now();
    EventType eventType = valueOfType(type);
    if (eventType != null) {
        mapEventSourceEventToEvent(haId, eventType, data).forEach(event -> {
            eventQueue.add(event);
            logger.debug("Received event ({}): {}", haId, event);
            try {
                eventListener.onEvent(event);
            } catch (Exception e) {
                logger.error("Could not publish event to Listener!", e);
            }
        });
    } else {
        logger.warn("Received unknown event source type! haId={}, id={}, type={}, data={}", haId, id, type, data);
    }
}
Also used : EventType(org.openhab.binding.homeconnect.internal.client.model.EventType) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotAuthorizedException(javax.ws.rs.NotAuthorizedException)

Aggregations

InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)2 EventType (org.openhab.binding.homeconnect.internal.client.model.EventType)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 LocalDateTime.now (java.time.LocalDateTime.now)1 ZonedDateTime (java.time.ZonedDateTime)1 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 TimeZone (java.util.TimeZone)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 InboundSseEvent (javax.ws.rs.sse.InboundSseEvent)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Nullable (org.eclipse.jdt.annotation.Nullable)1