2 * Copyright (c) 2010-2024 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.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;
33 * Discovery service implementation for the SystemInfo binding. It creates {@link DiscoveryResult} with
34 * {@link #DEFAULT_THING_LABEL}. The discovered Thing will have id - the hostname or {@link #DEFAULT_THING_ID}'
36 * @author Svilen Valkanov - Initial contribution
37 * @author Wouter Born - Add null annotations
40 @Component(service = DiscoveryService.class, configurationPid = "discovery.systeminfo")
41 public class SystemInfoDiscoveryService extends AbstractDiscoveryService {
42 public static final String DEFAULT_THING_ID = "unknown";
43 public static final String DEFAULT_THING_LABEL = "Local computer";
45 private final Logger logger = LoggerFactory.getLogger(SystemInfoDiscoveryService.class);
47 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_COMPUTER);
49 private static final int DISCOVERY_TIME_SECONDS = 30;
50 private static final String THING_UID_VALID_CHARS = "A-Za-z0-9_-";
51 private static final String HOST_NAME_SEPERATOR = "_";
53 public SystemInfoDiscoveryService() {
54 super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
58 protected void startScan() {
59 logger.debug("Starting system information discovery !");
63 hostname = getHostName();
64 if (hostname.isEmpty()) {
65 throw new UnknownHostException();
67 if (!hostname.matches("[" + THING_UID_VALID_CHARS + "]*")) {
68 hostname = hostname.replaceAll("[^" + THING_UID_VALID_CHARS + "]", HOST_NAME_SEPERATOR);
70 } catch (UnknownHostException ex) {
71 hostname = DEFAULT_THING_ID;
72 logger.info("Hostname can not be resolved. Computer name will be set to the default one: {}",
76 ThingUID computer = new ThingUID(THING_TYPE_COMPUTER, hostname);
77 thingDiscovered(DiscoveryResultBuilder.create(computer).withLabel(DEFAULT_THING_LABEL).build());
80 protected String getHostName() throws UnknownHostException {
81 InetAddress addr = InetAddress.getLocalHost();
82 return addr.getHostName();