]> git.basschouten.com Git - openhab-addons.git/blob
edfb640c312b4d86c87f22075b2cffb11dadde2c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.zoneminder.internal.handler.Monitor;
26 import org.openhab.binding.zoneminder.internal.handler.ZmBridgeHandler;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link MonitorDiscoveryService} is responsible for discovering the Zoneminder monitors
40  * associated with a Zoneminder server.
41  *
42  * @author Mark Hilbush - Initial contribution
43  */
44 @NonNullByDefault
45 public class MonitorDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
46
47     private final Logger logger = LoggerFactory.getLogger(MonitorDiscoveryService.class);
48
49     private static final int DISCOVERY_INTERVAL_SECONDS = 300;
50     private static final int DISCOVERY_INITIAL_DELAY_SECONDS = 10;
51     private static final int DISCOVERY_TIMEOUT_SECONDS = 6;
52
53     private @NonNullByDefault({}) ZmBridgeHandler bridgeHandler;
54
55     private @Nullable Future<?> discoveryJob;
56
57     public MonitorDiscoveryService() {
58         super(SUPPORTED_MONITOR_THING_TYPES_UIDS, DISCOVERY_TIMEOUT_SECONDS, true);
59     }
60
61     @Override
62     public void activate() {
63         super.activate(null);
64     }
65
66     @Override
67     public void deactivate() {
68         super.deactivate();
69     }
70
71     @Override
72     public void setThingHandler(@Nullable ThingHandler handler) {
73         if (handler instanceof ZmBridgeHandler zmBridgeHandler) {
74             bridgeHandler = zmBridgeHandler;
75         }
76     }
77
78     @Override
79     public @Nullable ThingHandler getThingHandler() {
80         return bridgeHandler;
81     }
82
83     @Override
84     public Set<ThingTypeUID> getSupportedThingTypes() {
85         return SUPPORTED_MONITOR_THING_TYPES_UIDS;
86     }
87
88     @Override
89     protected void startBackgroundDiscovery() {
90         Future<?> localDiscoveryJob = discoveryJob;
91         if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
92             logger.debug("ZoneminderDiscovery: Starting background discovery job");
93             discoveryJob = scheduler.scheduleWithFixedDelay(this::backgroundDiscoverMonitors,
94                     DISCOVERY_INITIAL_DELAY_SECONDS, DISCOVERY_INTERVAL_SECONDS, TimeUnit.SECONDS);
95         }
96     }
97
98     @Override
99     protected void stopBackgroundDiscovery() {
100         Future<?> localDiscoveryJob = discoveryJob;
101         if (localDiscoveryJob != null) {
102             logger.debug("ZoneminderDiscovery: Stopping background discovery job");
103             localDiscoveryJob.cancel(true);
104             discoveryJob = null;
105         }
106     }
107
108     @Override
109     public void startScan() {
110         logger.debug("ZoneminderDiscovery: Running discovery scan");
111         discoverMonitors();
112     }
113
114     private void backgroundDiscoverMonitors() {
115         if (!bridgeHandler.isBackgroundDiscoveryEnabled()) {
116             return;
117         }
118         logger.debug("ZoneminderDiscovery: Running background discovery scan");
119         discoverMonitors();
120     }
121
122     private synchronized void discoverMonitors() {
123         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
124         Integer alarmDuration = bridgeHandler.getDefaultAlarmDuration();
125         Integer imageRefreshInterval = bridgeHandler.getDefaultImageRefreshInterval();
126         for (Monitor monitor : bridgeHandler.getSavedMonitors()) {
127             String id = monitor.getId();
128             String name = monitor.getName();
129             ThingUID thingUID = new ThingUID(UID_MONITOR, bridgeUID, monitor.getId());
130             Map<String, Object> properties = new HashMap<>();
131             properties.put(CONFIG_MONITOR_ID, id);
132             properties.put(CONFIG_ALARM_DURATION, alarmDuration);
133             if (imageRefreshInterval != null) {
134                 properties.put(CONFIG_IMAGE_REFRESH_INTERVAL, imageRefreshInterval);
135             }
136             thingDiscovered(createDiscoveryResult(thingUID, bridgeUID, id, name, properties));
137             logger.debug("ZoneminderDiscovery: Monitor with id '{}' and name '{}' added to Inbox with UID '{}'",
138                     monitor.getId(), monitor.getName(), thingUID);
139         }
140     }
141
142     private DiscoveryResult createDiscoveryResult(ThingUID monitorUID, ThingUID bridgeUID, String id, String name,
143             Map<String, Object> properties) {
144         return DiscoveryResultBuilder.create(monitorUID).withProperties(properties).withBridge(bridgeUID)
145                 .withLabel(String.format("Zoneminder Monitor %s", name)).withRepresentationProperty(CONFIG_MONITOR_ID)
146                 .build();
147     }
148 }