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