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;
}
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);
}
}
Aggregations