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.Collections;
18 import java.util.HashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.mihome.internal.socket.XiaomiDiscoverySocket;
24 import org.openhab.binding.mihome.internal.socket.XiaomiSocketListener;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
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;
34 import com.google.gson.JsonObject;
37 * Discovery service for the Xiaomi bridge.
39 * @author Patrick Boos - Initial contribution
40 * @author Kuba Wolanin - logger fixes
43 @Component(service = DiscoveryService.class, configurationPid = "discovery.mihome")
44 public class XiaomiBridgeDiscoveryService extends AbstractDiscoveryService implements XiaomiSocketListener {
46 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
47 private static final int DISCOVERY_TIMEOUT_SEC = 30;
49 private final Logger logger = LoggerFactory.getLogger(XiaomiBridgeDiscoveryService.class);
50 private final XiaomiDiscoverySocket socket = new XiaomiDiscoverySocket("discovery");
52 public XiaomiBridgeDiscoveryService() {
53 super(SUPPORTED_THING_TYPES, DISCOVERY_TIMEOUT_SEC, false);
57 protected void startScan() {
59 logger.debug("Start scan for bridges");
60 socket.registerListener(this);
65 protected synchronized void stopScan() {
67 logger.debug("Stop scan");
68 removeOlderResults(getTimestampOfLastScan());
69 socket.unregisterListener(this);
73 public void deactivate() {
75 socket.unregisterListener(this);
79 public void onDataReceived(JsonObject data) {
80 logger.debug("Received message {}", data);
81 if (data.get("cmd").getAsString().equals("iam")) {
87 public int getScanTimeout() {
88 return DISCOVERY_TIMEOUT_SEC;
91 private void discoverGateways() {
92 socket.sendMessage("{\"cmd\":\"whois\"}");
95 private void getGatewayInfo(JsonObject jobject) {
96 Map<String, Object> properties = new HashMap<>(4);
97 String serialNumber = jobject.get("sid").getAsString();
98 String ipAddress = jobject.get("ip").getAsString();
99 int port = jobject.get("port").getAsInt();
101 // It is reported that the gateway is sometimes providing the serial number without the leading 0
102 // This is a workaround for a bug in the gateway
103 if (serialNumber.length() == 11) {
104 serialNumber = "0" + serialNumber;
107 properties.put(SERIAL_NUMBER, serialNumber);
108 properties.put(HOST, ipAddress);
109 properties.put(PORT, port);
111 logger.debug("Discovered Xiaomi Gateway - sid: {} ip: {} port: {}", serialNumber, ipAddress, port);
113 ThingUID thingUID = new ThingUID(THING_TYPE_BRIDGE, serialNumber);
116 DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_BRIDGE).withProperties(properties)
117 .withLabel("Xiaomi Gateway").withRepresentationProperty(SERIAL_NUMBER).build());