From 839b5a1869bd5b2206b1334fdadf833358fdde56 Mon Sep 17 00:00:00 2001 From: Mark Hilbush Date: Thu, 26 Aug 2021 13:09:26 -0400 Subject: [PATCH] API changed location of event counts (#11159) Signed-off-by: Mark Hilbush Signed-off-by: Thomas --- .../internal/dto/EventSummaryDTO.java | 54 +++++++++++++++++++ .../internal/dto/MonitorItemDTO.java | 6 +++ .../internal/handler/ZmBridgeHandler.java | 52 ++++++++++++------ 3 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/EventSummaryDTO.java diff --git a/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/EventSummaryDTO.java b/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/EventSummaryDTO.java new file mode 100644 index 0000000000000..dd8c8ba1c2ad3 --- /dev/null +++ b/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/EventSummaryDTO.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.zoneminder.internal.dto; + +import com.google.gson.annotations.SerializedName; + +/** + * The {@link EventSummaryDTO} contains event counts for the monitor. If this object + * doesn't exist in the JSON response, the event counts will be in the monitor object. + * + * @author Mark Hilbush - Initial contribution + */ +public class EventSummaryDTO { + + /** + * Number of events in last hour + */ + @SerializedName("HourEvents") + public String hourEvents; + + /** + * Number of events in last day + */ + @SerializedName("DayEvents") + public String dayEvents; + + /** + * Number of events in last week + */ + @SerializedName("WeekEvents") + public String weekEvents; + + /** + * Number of events in last month + */ + @SerializedName("MonthEvents") + public String monthEvents; + + /** + * Total number of events + */ + @SerializedName("TotalEvents") + public String totalEvents; +} diff --git a/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/MonitorItemDTO.java b/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/MonitorItemDTO.java index 6e618ec484fb5..3332a4b8f33d6 100644 --- a/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/MonitorItemDTO.java +++ b/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/MonitorItemDTO.java @@ -32,4 +32,10 @@ public class MonitorItemDTO { */ @SerializedName("Monitor_Status") public MonitorStatusDTO monitorStatus; + + /** + * Event counts + */ + @SerializedName("Event_Summary") + public EventSummaryDTO eventSummary; } diff --git a/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/handler/ZmBridgeHandler.java b/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/handler/ZmBridgeHandler.java index fc71e50f13cd6..4fd9a2362e9e5 100644 --- a/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/handler/ZmBridgeHandler.java +++ b/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/handler/ZmBridgeHandler.java @@ -41,6 +41,7 @@ import org.openhab.binding.zoneminder.internal.config.ZmBridgeConfig; import org.openhab.binding.zoneminder.internal.discovery.MonitorDiscoveryService; import org.openhab.binding.zoneminder.internal.dto.EventDTO; +import org.openhab.binding.zoneminder.internal.dto.EventSummaryDTO; import org.openhab.binding.zoneminder.internal.dto.EventsDTO; import org.openhab.binding.zoneminder.internal.dto.MonitorDTO; import org.openhab.binding.zoneminder.internal.dto.MonitorItemDTO; @@ -302,23 +303,20 @@ private synchronized List getMonitors() { } try { String response = executeGet(buildUrl("/api/monitors.json")); - MonitorsDTO monitors = GSON.fromJson(response, MonitorsDTO.class); - if (monitors != null && monitors.monitorItems != null) { + MonitorsDTO monitorsDTO = GSON.fromJson(response, MonitorsDTO.class); + if (monitorsDTO != null && monitorsDTO.monitorItems != null) { List options = new ArrayList<>(); - for (MonitorItemDTO monitorItem : monitors.monitorItems) { - MonitorDTO m = monitorItem.monitor; - MonitorStatusDTO mStatus = monitorItem.monitorStatus; - if (m != null && mStatus != null) { - Monitor monitor = new Monitor(m.id, m.name, m.function, m.enabled, mStatus.status); - monitor.setHourEvents(m.hourEvents); - monitor.setDayEvents(m.dayEvents); - monitor.setWeekEvents(m.weekEvents); - monitor.setMonthEvents(m.monthEvents); - monitor.setTotalEvents(m.totalEvents); - monitor.setImageUrl(buildStreamUrl(m.id, STREAM_IMAGE)); - monitor.setVideoUrl(buildStreamUrl(m.id, STREAM_VIDEO)); + for (MonitorItemDTO monitorItemDTO : monitorsDTO.monitorItems) { + MonitorDTO monitorDTO = monitorItemDTO.monitor; + MonitorStatusDTO monitorStatusDTO = monitorItemDTO.monitorStatus; + if (monitorDTO != null && monitorStatusDTO != null) { + Monitor monitor = new Monitor(monitorDTO.id, monitorDTO.name, monitorDTO.function, + monitorDTO.enabled, monitorStatusDTO.status); + extractEventCounts(monitor, monitorItemDTO); + monitor.setImageUrl(buildStreamUrl(monitorDTO.id, STREAM_IMAGE)); + monitor.setVideoUrl(buildStreamUrl(monitorDTO.id, STREAM_VIDEO)); monitorList.add(monitor); - options.add(new StateOption(m.id, "Monitor " + m.id)); + options.add(new StateOption(monitorDTO.id, "Monitor " + monitorDTO.id)); } stateDescriptionProvider .setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_IMAGE_MONITOR_ID), options); @@ -340,6 +338,30 @@ private synchronized List getMonitors() { return monitorList; } + private void extractEventCounts(Monitor monitor, MonitorItemDTO monitorItemDTO) { + /* + * The Zoneminder API changed in version 1.36.x such that the event counts moved from the + * monitor object to a new event summary object. Therefore, if the event summary object + * exists in the JSON response, pull the event counts from that object, otherwise get the + * counts from the monitor object. + */ + if (monitorItemDTO.eventSummary != null) { + EventSummaryDTO eventSummaryDTO = monitorItemDTO.eventSummary; + monitor.setHourEvents(eventSummaryDTO.hourEvents); + monitor.setDayEvents(eventSummaryDTO.dayEvents); + monitor.setWeekEvents(eventSummaryDTO.weekEvents); + monitor.setMonthEvents(eventSummaryDTO.monthEvents); + monitor.setTotalEvents(eventSummaryDTO.totalEvents); + } else { + MonitorDTO monitorDTO = monitorItemDTO.monitor; + monitor.setHourEvents(monitorDTO.hourEvents); + monitor.setDayEvents(monitorDTO.dayEvents); + monitor.setWeekEvents(monitorDTO.weekEvents); + monitor.setMonthEvents(monitorDTO.monthEvents); + monitor.setTotalEvents(monitorDTO.totalEvents); + } + } + @SuppressWarnings("null") private @Nullable Event getLastEvent(String id) { if (!zmAuth.isAuthorized()) {