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.mihome.internal.discovery;
15 import static org.openhab.binding.mihome.internal.XiaomiGatewayBindingConstants.*;
17 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.mihome.internal.socket.XiaomiDiscoverySocket;
23 import org.openhab.binding.mihome.internal.socket.XiaomiSocketListener;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
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;
33 import com.google.gson.JsonObject;
36 * Discovery service for the Xiaomi bridge.
38 * @author Patrick Boos - Initial contribution
39 * @author Kuba Wolanin - logger fixes
42 @Component(service = DiscoveryService.class, configurationPid = "discovery.mihome")
43 public class XiaomiBridgeDiscoveryService extends AbstractDiscoveryService implements XiaomiSocketListener {
45 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_BRIDGE);
46 private static final int DISCOVERY_TIMEOUT_SEC = 30;
48 private final Logger logger = LoggerFactory.getLogger(XiaomiBridgeDiscoveryService.class);
49 private final XiaomiDiscoverySocket socket = new XiaomiDiscoverySocket("discovery");
51 public XiaomiBridgeDiscoveryService() {
52 super(SUPPORTED_THING_TYPES, DISCOVERY_TIMEOUT_SEC, false);
56 protected void startScan() {
58 logger.debug("Start scan for bridges");
59 socket.registerListener(this);
64 protected synchronized void stopScan() {
66 logger.debug("Stop scan");
67 removeOlderResults(getTimestampOfLastScan());
68 socket.unregisterListener(this);
72 public void deactivate() {
74 socket.unregisterListener(this);
78 public void onDataReceived(JsonObject data) {
79 logger.debug("Received message {}", data);
80 if ("iam".equals(data.get("cmd").getAsString())) {
86 public int getScanTimeout() {
87 return DISCOVERY_TIMEOUT_SEC;
90 private void discoverGateways() {
91 socket.sendMessage("{\"cmd\":\"whois\"}");
94 private void getGatewayInfo(JsonObject jobject) {
95 Map<String, Object> properties = new HashMap<>(4);
96 String serialNumber = jobject.get("sid").getAsString();
97 String ipAddress = jobject.get("ip").getAsString();
98 int port = jobject.get("port").getAsInt();
100 // It is reported that the gateway is sometimes providing the serial number without the leading 0
101 // This is a workaround for a bug in the gateway
102 if (serialNumber.length() == 11) {
103 serialNumber = "0" + serialNumber;
106 properties.put(SERIAL_NUMBER, serialNumber);
107 properties.put(HOST, ipAddress);
108 properties.put(PORT, port);
110 logger.debug("Discovered Xiaomi Gateway - sid: {} ip: {} port: {}", serialNumber, ipAddress, port);
112 ThingUID thingUID = new ThingUID(THING_TYPE_BRIDGE, serialNumber);
115 DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_BRIDGE).withProperties(properties)
116 .withLabel("Xiaomi Gateway").withRepresentationProperty(SERIAL_NUMBER).build());