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