2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.zoneminder.internal.discovery;
15 import static org.openhab.binding.zoneminder.internal.ZmBindingConstants.*;
17 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.zoneminder.internal.handler.Monitor;
24 import org.openhab.binding.zoneminder.internal.handler.ZmBridgeHandler;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link MonitorDiscoveryService} is responsible for discovering the Zoneminder monitors
38 * associated with a Zoneminder server.
40 * @author Mark Hilbush - Initial contribution
43 public class MonitorDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
45 private final Logger logger = LoggerFactory.getLogger(MonitorDiscoveryService.class);
47 private @Nullable ZmBridgeHandler bridgeHandler;
49 public MonitorDiscoveryService() {
54 public void setThingHandler(@Nullable ThingHandler handler) {
55 if (handler instanceof ZmBridgeHandler) {
56 ((ZmBridgeHandler) handler).setDiscoveryService(this);
57 this.bridgeHandler = (ZmBridgeHandler) handler;
62 public @Nullable ThingHandler getThingHandler() {
67 public void activate() {
71 public void deactivate() {
75 public Set<ThingTypeUID> getSupportedThingTypes() {
76 return SUPPORTED_MONITOR_THING_TYPES_UIDS;
80 public void startBackgroundDiscovery() {
81 logger.trace("Discovery: Performing background discovery scan for {}", getBridgeUID());
86 public void startScan() {
87 logger.debug("Discovery: Starting monitor discovery scan for {}", getBridgeUID());
91 private @Nullable ThingUID getBridgeUID() {
92 ZmBridgeHandler localBridgeHandler = bridgeHandler;
93 return localBridgeHandler != null ? localBridgeHandler.getThing().getUID() : null;
96 private synchronized void discoverMonitors() {
97 ZmBridgeHandler localBridgeHandler = bridgeHandler;
98 ThingUID bridgeUID = getBridgeUID();
99 if (localBridgeHandler != null && bridgeUID != null) {
100 Integer alarmDuration = localBridgeHandler.getDefaultAlarmDuration();
101 Integer imageRefreshInterval = localBridgeHandler.getDefaultImageRefreshInterval();
102 for (Monitor monitor : localBridgeHandler.getSavedMonitors()) {
103 String id = monitor.getId();
104 String name = monitor.getName();
105 ThingUID thingUID = new ThingUID(UID_MONITOR, bridgeUID, monitor.getId());
106 Map<String, Object> properties = new HashMap<>();
107 properties.put(CONFIG_MONITOR_ID, id);
108 properties.put(CONFIG_ALARM_DURATION, alarmDuration);
109 if (imageRefreshInterval != null) {
110 properties.put(CONFIG_IMAGE_REFRESH_INTERVAL, imageRefreshInterval);
112 thingDiscovered(createDiscoveryResult(thingUID, bridgeUID, id, name, properties));
113 logger.trace("Discovery: Monitor with id '{}' and name '{}' added to Inbox with UID '{}'",
114 monitor.getId(), monitor.getName(), thingUID);
119 private DiscoveryResult createDiscoveryResult(ThingUID monitorUID, ThingUID bridgeUID, String id, String name,
120 Map<String, Object> properties) {
121 return DiscoveryResultBuilder.create(monitorUID).withProperties(properties).withBridge(bridgeUID)
122 .withLabel(buildLabel(name)).withRepresentationProperty(CONFIG_MONITOR_ID).build();
125 private String buildLabel(String name) {
126 return String.format("Zoneminder Monitor %s", name);