Skip to content

Commit

Permalink
API changed location of event counts (openhab#11159)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Hilbush <mark@hilbush.com>
  • Loading branch information
mhilbush authored and thinkingstone committed Nov 7, 2021
1 parent ea9ecb3 commit fc3c2b9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public class MonitorItemDTO {
*/
@SerializedName("Monitor_Status")
public MonitorStatusDTO monitorStatus;

/**
* Event counts
*/
@SerializedName("Event_Summary")
public EventSummaryDTO eventSummary;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -302,23 +303,20 @@ private synchronized List<Monitor> 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<StateOption> 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);
Expand All @@ -340,6 +338,30 @@ private synchronized List<Monitor> 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()) {
Expand Down

0 comments on commit fc3c2b9

Please sign in to comment.