use of org.openhab.binding.homeconnect.internal.client.model.Event 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;
}
Aggregations