2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ipcamera.internal;
15 import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
17 import java.net.UnknownHostException;
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;
35 * The {@link IpCameraDiscoveryService} is responsible for auto finding cameras that have Onvif
37 * @author Matthew Skinner - Initial contribution
41 @Component(service = DiscoveryService.class, configurationPid = "binding.ipcamera")
42 public class IpCameraDiscoveryService extends AbstractDiscoveryService {
44 private final Logger logger = LoggerFactory.getLogger(IpCameraDiscoveryService.class);
45 private final NetworkAddressService networkAddressService;
48 public IpCameraDiscoveryService(@Reference NetworkAddressService networkAddressService) {
49 super(SUPPORTED_THING_TYPES, 0, false);
50 this.networkAddressService = networkAddressService;
54 protected void startBackgroundDiscovery() {
58 protected void deactivate() {
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);
72 protected void startScan() {
73 removeOlderResults(getTimestampOfLastScan());
74 OnvifDiscovery onvifDiscovery = new OnvifDiscovery(networkAddressService, this);
76 onvifDiscovery.discoverCameras();
77 } catch (UnknownHostException | InterruptedException e) {
79 "IpCamera Discovery has an issue discovering the network settings to find cameras with. Try setting up the camera manually.");