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