]> git.basschouten.com Git - openhab-addons.git/blob
93a73f40e4636de5b15bc435e1c5b9e98d7a9645
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 package org.openhab.binding.systeminfo.internal.discovery;
14
15 import static org.openhab.binding.systeminfo.internal.SystemInfoBindingConstants.THING_TYPE_COMPUTER;
16
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.Set;
20
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;
31
32 /**
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}'
35  *
36  * @author Svilen Valkanov - Initial contribution
37  * @author Wouter Born - Add null annotations
38  */
39 @NonNullByDefault
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";
44
45     private final Logger logger = LoggerFactory.getLogger(SystemInfoDiscoveryService.class);
46
47     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_COMPUTER);
48
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 = "_";
52
53     public SystemInfoDiscoveryService() {
54         super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
55     }
56
57     @Override
58     protected void startScan() {
59         logger.debug("Starting system information discovery !");
60         String hostname;
61
62         try {
63             hostname = getHostName();
64             if (hostname.isEmpty()) {
65                 throw new UnknownHostException();
66             }
67             if (!hostname.matches("[" + THING_UID_VALID_CHARS + "]*")) {
68                 hostname = hostname.replaceAll("[^" + THING_UID_VALID_CHARS + "]", HOST_NAME_SEPERATOR);
69             }
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: {}",
73                     DEFAULT_THING_ID);
74         }
75
76         ThingUID computer = new ThingUID(THING_TYPE_COMPUTER, hostname);
77         thingDiscovered(DiscoveryResultBuilder.create(computer).withLabel(DEFAULT_THING_LABEL).build());
78     }
79
80     protected String getHostName() throws UnknownHostException {
81         InetAddress addr = InetAddress.getLocalHost();
82         return addr.getHostName();
83     }
84 }