]> git.basschouten.com Git - openhab-addons.git/blob
b2c90cfad9bd96dabcd087c799d2738a034c4527
[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.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;
32
33 /**
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}'
36  *
37  * @author Svilen Valkanov - Initial contribution
38  * @author Wouter Born - Add null annotations
39  */
40 @NonNullByDefault
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";
45
46     private final Logger logger = LoggerFactory.getLogger(SysteminfoDiscoveryService.class);
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_COMPUTER);
49
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 = "_";
53
54     public SysteminfoDiscoveryService() {
55         super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
56     }
57
58     @Override
59     protected void startScan() {
60         logger.debug("Starting system information discovery !");
61         String hostname;
62
63         try {
64             hostname = getHostName();
65             if (hostname.isEmpty()) {
66                 throw new UnknownHostException();
67             }
68             if (!hostname.matches("[" + THING_UID_VALID_CHARS + "]*")) {
69                 hostname = hostname.replaceAll("[^" + THING_UID_VALID_CHARS + "]", HOST_NAME_SEPERATOR);
70             }
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: {}",
74                     DEFAULT_THING_ID);
75         }
76
77         ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
78         ThingUID computer = new ThingUID(computerType, hostname);
79         thingDiscovered(DiscoveryResultBuilder.create(computer).withLabel(DEFAULT_THING_LABEL).build());
80     }
81
82     protected String getHostName() throws UnknownHostException {
83         InetAddress addr = InetAddress.getLocalHost();
84         return addr.getHostName();
85     }
86 }