2 * Copyright (c) 2010-2024 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.max.internal.discovery;
15 import java.util.Date;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.max.internal.MaxBindingConstants;
21 import org.openhab.binding.max.internal.device.Device;
22 import org.openhab.binding.max.internal.handler.DeviceStatusListener;
23 import org.openhab.binding.max.internal.handler.MaxCubeBridgeHandler;
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.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link MaxDeviceDiscoveryService} class is used to discover MAX! Cube
39 * devices that are connected to the Lan gateway.
41 * @author Marcel Verpaalen - Initial contribution
44 public class MaxDeviceDiscoveryService extends AbstractDiscoveryService
45 implements DeviceStatusListener, DiscoveryService, ThingHandlerService {
47 private static final int SEARCH_TIME = 60;
48 private final Logger logger = LoggerFactory.getLogger(MaxDeviceDiscoveryService.class);
50 private @Nullable MaxCubeBridgeHandler maxCubeBridgeHandler;
52 public MaxDeviceDiscoveryService() {
53 super(MaxBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS, SEARCH_TIME, true);
57 public void setThingHandler(@NonNullByDefault({}) ThingHandler handler) {
58 if (handler instanceof MaxCubeBridgeHandler maxCubeBridgeHandler) {
59 this.maxCubeBridgeHandler = maxCubeBridgeHandler;
64 public @Nullable ThingHandler getThingHandler() {
65 return maxCubeBridgeHandler;
69 public void activate() {
70 MaxCubeBridgeHandler localMaxCubeBridgeHandler = maxCubeBridgeHandler;
71 if (localMaxCubeBridgeHandler != null) {
72 localMaxCubeBridgeHandler.registerDeviceStatusListener(this);
77 public void deactivate() {
78 MaxCubeBridgeHandler localMaxCubeBridgeHandler = maxCubeBridgeHandler;
79 if (localMaxCubeBridgeHandler != null) {
80 localMaxCubeBridgeHandler.unregisterDeviceStatusListener(this);
81 removeOlderResults(new Date().getTime(), localMaxCubeBridgeHandler.getThing().getUID());
86 public Set<ThingTypeUID> getSupportedThingTypes() {
87 return MaxBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS;
91 public void onDeviceAdded(Bridge bridge, Device device) {
92 logger.trace("Adding new MAX! {} with id '{}' to inbox", device.getType(), device.getSerialNumber());
93 ThingUID thingUID = null;
94 switch (device.getType()) {
95 case WallMountedThermostat:
96 thingUID = new ThingUID(MaxBindingConstants.WALLTHERMOSTAT_THING_TYPE, bridge.getUID(),
97 device.getSerialNumber());
99 case HeatingThermostat:
100 thingUID = new ThingUID(MaxBindingConstants.HEATINGTHERMOSTAT_THING_TYPE, bridge.getUID(),
101 device.getSerialNumber());
103 case HeatingThermostatPlus:
104 thingUID = new ThingUID(MaxBindingConstants.HEATINGTHERMOSTATPLUS_THING_TYPE, bridge.getUID(),
105 device.getSerialNumber());
108 thingUID = new ThingUID(MaxBindingConstants.SHUTTERCONTACT_THING_TYPE, bridge.getUID(),
109 device.getSerialNumber());
112 thingUID = new ThingUID(MaxBindingConstants.ECOSWITCH_THING_TYPE, bridge.getUID(),
113 device.getSerialNumber());
118 if (thingUID != null) {
119 String name = device.getName();
120 if (name.isEmpty()) {
121 name = device.getSerialNumber();
123 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
124 .withProperty(Thing.PROPERTY_SERIAL_NUMBER, device.getSerialNumber()).withBridge(bridge.getUID())
125 .withLabel(device.getType() + ": " + name).withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER)
127 thingDiscovered(discoveryResult);
129 logger.debug("Discovered MAX! device is unsupported: type '{}' with id '{}'", device.getType(),
130 device.getSerialNumber());
135 protected void startScan() {
136 MaxCubeBridgeHandler localMaxCubeBridgeHandler = maxCubeBridgeHandler;
137 if (localMaxCubeBridgeHandler != null) {
138 localMaxCubeBridgeHandler.clearDeviceList();
139 localMaxCubeBridgeHandler.deviceInclusion();
144 public void onDeviceStateChanged(ThingUID bridge, Device device) {
145 // this can be ignored here
149 public void onDeviceRemoved(MaxCubeBridgeHandler bridge, Device device) {
150 // this can be ignored here
154 public void onDeviceConfigUpdate(Bridge bridge, Device device) {
155 // this can be ignored here