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.openhab.binding.max.internal.MaxBindingConstants;
20 import org.openhab.binding.max.internal.device.Device;
21 import org.openhab.binding.max.internal.handler.DeviceStatusListener;
22 import org.openhab.binding.max.internal.handler.MaxCubeBridgeHandler;
23 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Thing;
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.osgi.service.component.annotations.ServiceScope;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link MaxDeviceDiscoveryService} class is used to discover MAX! Cube
37 * devices that are connected to the Lan gateway.
39 * @author Marcel Verpaalen - Initial contribution
41 @Component(scope = ServiceScope.PROTOTYPE, service = MaxDeviceDiscoveryService.class)
43 public class MaxDeviceDiscoveryService extends AbstractThingHandlerDiscoveryService<MaxCubeBridgeHandler>
44 implements DeviceStatusListener {
46 private static final int SEARCH_TIME = 60;
47 private final Logger logger = LoggerFactory.getLogger(MaxDeviceDiscoveryService.class);
49 public MaxDeviceDiscoveryService() {
50 super(MaxCubeBridgeHandler.class, MaxBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS, SEARCH_TIME, true);
54 public void initialize() {
55 thingHandler.registerDeviceStatusListener(this);
60 public void dispose() {
62 thingHandler.unregisterDeviceStatusListener(this);
63 removeOlderResults(new Date().getTime(), thingHandler.getThing().getUID());
67 public Set<ThingTypeUID> getSupportedThingTypes() {
68 return MaxBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS;
72 public void onDeviceAdded(Bridge bridge, Device device) {
73 logger.trace("Adding new MAX! {} with id '{}' to inbox", device.getType(), device.getSerialNumber());
74 ThingUID thingUID = null;
75 switch (device.getType()) {
76 case WallMountedThermostat:
77 thingUID = new ThingUID(MaxBindingConstants.WALLTHERMOSTAT_THING_TYPE, bridge.getUID(),
78 device.getSerialNumber());
80 case HeatingThermostat:
81 thingUID = new ThingUID(MaxBindingConstants.HEATINGTHERMOSTAT_THING_TYPE, bridge.getUID(),
82 device.getSerialNumber());
84 case HeatingThermostatPlus:
85 thingUID = new ThingUID(MaxBindingConstants.HEATINGTHERMOSTATPLUS_THING_TYPE, bridge.getUID(),
86 device.getSerialNumber());
89 thingUID = new ThingUID(MaxBindingConstants.SHUTTERCONTACT_THING_TYPE, bridge.getUID(),
90 device.getSerialNumber());
93 thingUID = new ThingUID(MaxBindingConstants.ECOSWITCH_THING_TYPE, bridge.getUID(),
94 device.getSerialNumber());
99 if (thingUID != null) {
100 String name = device.getName();
101 if (name.isEmpty()) {
102 name = device.getSerialNumber();
104 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
105 .withProperty(Thing.PROPERTY_SERIAL_NUMBER, device.getSerialNumber()).withBridge(bridge.getUID())
106 .withLabel(device.getType() + ": " + name).withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER)
108 thingDiscovered(discoveryResult);
110 logger.debug("Discovered MAX! device is unsupported: type '{}' with id '{}'", device.getType(),
111 device.getSerialNumber());
116 protected void startScan() {
117 thingHandler.clearDeviceList();
118 thingHandler.deviceInclusion();
122 public void onDeviceStateChanged(ThingUID bridge, Device device) {
123 // this can be ignored here
127 public void onDeviceRemoved(MaxCubeBridgeHandler bridge, Device device) {
128 // this can be ignored here
132 public void onDeviceConfigUpdate(Bridge bridge, Device device) {
133 // this can be ignored here