]> git.basschouten.com Git - openhab-addons.git/blob
a7429c16b3ccff9de1079d90896868d06623661f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.zoneminder.internal.discovery;
14
15 import static org.openhab.binding.zoneminder.internal.ZmBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
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;
35
36 /**
37  * The {@link MonitorDiscoveryService} is responsible for discovering the Zoneminder monitors
38  * associated with a Zoneminder server.
39  *
40  * @author Mark Hilbush - Initial contribution
41  */
42 @NonNullByDefault
43 public class MonitorDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
44
45     private final Logger logger = LoggerFactory.getLogger(MonitorDiscoveryService.class);
46
47     private @Nullable ZmBridgeHandler bridgeHandler;
48
49     public MonitorDiscoveryService() {
50         super(30);
51     }
52
53     @Override
54     public void setThingHandler(@Nullable ThingHandler handler) {
55         if (handler instanceof ZmBridgeHandler) {
56             ((ZmBridgeHandler) handler).setDiscoveryService(this);
57             this.bridgeHandler = (ZmBridgeHandler) handler;
58         }
59     }
60
61     @Override
62     public @Nullable ThingHandler getThingHandler() {
63         return bridgeHandler;
64     }
65
66     @Override
67     public void activate() {
68     }
69
70     @Override
71     public void deactivate() {
72     }
73
74     @Override
75     public Set<ThingTypeUID> getSupportedThingTypes() {
76         return SUPPORTED_MONITOR_THING_TYPES_UIDS;
77     }
78
79     @Override
80     public void startBackgroundDiscovery() {
81         logger.trace("Discovery: Performing background discovery scan for {}", getBridgeUID());
82         discoverMonitors();
83     }
84
85     @Override
86     public void startScan() {
87         logger.debug("Discovery: Starting monitor discovery scan for {}", getBridgeUID());
88         discoverMonitors();
89     }
90
91     private @Nullable ThingUID getBridgeUID() {
92         ZmBridgeHandler localBridgeHandler = bridgeHandler;
93         return localBridgeHandler != null ? localBridgeHandler.getThing().getUID() : null;
94     }
95
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);
111                 }
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);
115             }
116         }
117     }
118
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();
123     }
124
125     private String buildLabel(String name) {
126         return String.format("Zoneminder Monitor %s", name);
127     }
128 }