]> git.basschouten.com Git - openhab-addons.git/blob
4992e02a9161f0a9b6a99624859249e8e7f3b0a3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.ipcamera.internal;
14
15 import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
16
17 import java.net.UnknownHostException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.ipcamera.internal.onvif.OnvifDiscovery;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.DiscoveryService;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.osgi.service.component.annotations.Component;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link IpCameraDiscoveryService} is responsible for auto finding cameras that have Onvif
33  *
34  * @author Matthew Skinner - Initial contribution
35  */
36
37 @NonNullByDefault
38 @Component(service = DiscoveryService.class, configurationPid = "binding.ipcamera")
39 public class IpCameraDiscoveryService extends AbstractDiscoveryService {
40
41     private final Logger logger = LoggerFactory.getLogger(IpCameraDiscoveryService.class);
42
43     public IpCameraDiscoveryService() {
44         super(SUPPORTED_THING_TYPES, 0, false);
45     }
46
47     @Override
48     protected void startBackgroundDiscovery() {
49     }
50
51     @Override
52     protected void deactivate() {
53         super.deactivate();
54     }
55
56     public void newCameraFound(String brand, String hostname, int onvifPort) {
57         ThingTypeUID thingtypeuid = new ThingTypeUID("ipcamera", brand);
58         ThingUID thingUID = new ThingUID(thingtypeuid, hostname.replace(".", ""));
59         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
60                 .withProperty(CONFIG_IPADDRESS, hostname).withProperty(CONFIG_ONVIF_PORT, onvifPort)
61                 .withLabel(brand + " camera @" + hostname).build();
62         thingDiscovered(discoveryResult);
63     }
64
65     @Override
66     protected void startScan() {
67         removeOlderResults(getTimestampOfLastScan());
68         OnvifDiscovery onvifDiscovery = new OnvifDiscovery(this);
69         try {
70             onvifDiscovery.discoverCameras();
71         } catch (UnknownHostException | InterruptedException e) {
72             logger.warn(
73                     "IpCamera Discovery has an issue discovering the network settings to find cameras with. Try setting up the camera manually.");
74         }
75         stopScan();
76     }
77 }