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