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.systeminfo.internal.discovery;
15 import static org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants.THING_TYPE_COMPUTER;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Discovery service implementation for the Systeminfo binding. It creates {@link DiscoveryResult} with
35 * {@link #DEFAULT_THING_LABEL}. The discovered Thing will have id - the hostname or {@link #DEFAULT_THING_ID}'
37 * @author Svilen Valkanov - Initial contribution
38 * @author Wouter Born - Add null annotations
41 @Component(service = DiscoveryService.class, configurationPid = "discovery.systeminfo")
42 public class SysteminfoDiscoveryService extends AbstractDiscoveryService {
43 public static final String DEFAULT_THING_ID = "unknown";
44 public static final String DEFAULT_THING_LABEL = "Local computer";
46 private final Logger logger = LoggerFactory.getLogger(SysteminfoDiscoveryService.class);
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_COMPUTER);
50 private static final int DISCOVERY_TIME_SECONDS = 30;
51 private static final String THING_UID_VALID_CHARS = "A-Za-z0-9_-";
52 private static final String HOST_NAME_SEPERATOR = "_";
54 public SysteminfoDiscoveryService() {
55 super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
59 protected void startScan() {
60 logger.debug("Starting system information discovery !");
64 hostname = getHostName();
65 if (hostname.isEmpty()) {
66 throw new UnknownHostException();
68 if (!hostname.matches("[" + THING_UID_VALID_CHARS + "]*")) {
69 hostname = hostname.replaceAll("[^" + THING_UID_VALID_CHARS + "]", HOST_NAME_SEPERATOR);
71 } catch (UnknownHostException ex) {
72 hostname = DEFAULT_THING_ID;
73 logger.info("Hostname can not be resolved. Computer name will be set to the default one: {}",
77 ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
78 ThingUID computer = new ThingUID(computerType, hostname);
79 thingDiscovered(DiscoveryResultBuilder.create(computer).withLabel(DEFAULT_THING_LABEL).build());
82 protected String getHostName() throws UnknownHostException {
83 InetAddress addr = InetAddress.getLocalHost();
84 return addr.getHostName();