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.nobohub.internal.discovery;
15 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.AUTODISCOVERED_THING_TYPES_UIDS;
16 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_MODEL;
17 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_NAME;
18 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_TEMPERATURE_SENSOR_FOR_ZONE;
19 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_VENDOR_NAME;
20 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_ZONE;
21 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_ZONE_ID;
22 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.THING_TYPE_COMPONENT;
23 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.THING_TYPE_ZONE;
25 import java.util.Collection;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.openhab.binding.nobohub.internal.NoboHubBridgeHandler;
34 import org.openhab.binding.nobohub.internal.model.Component;
35 import org.openhab.binding.nobohub.internal.model.Zone;
36 import org.openhab.core.config.discovery.AbstractDiscoveryService;
37 import org.openhab.core.config.discovery.DiscoveryResult;
38 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingUID;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * This class identifies devices that are available on the Nobø hub and adds discovery results for them.
47 * @author Jørgen Austvik - Initial contribution
48 * @author Espen Fossen - Initial contribution
51 public class NoboThingDiscoveryService extends AbstractDiscoveryService {
52 private final Logger logger = LoggerFactory.getLogger(NoboThingDiscoveryService.class);
54 private final NoboHubBridgeHandler bridgeHandler;
56 public NoboThingDiscoveryService(NoboHubBridgeHandler bridgeHandler) {
57 super(AUTODISCOVERED_THING_TYPES_UIDS, 10, true);
58 this.bridgeHandler = bridgeHandler;
62 protected void startScan() {
63 bridgeHandler.startScan();
67 public synchronized void stopScan() {
69 removeOlderResults(getTimestampOfLastScan());
73 public void deactivate() {
74 removeOlderResults(new Date().getTime());
77 public void detectZones(Collection<Zone> zones) {
78 ThingUID bridge = bridgeHandler.getThing().getUID();
79 List<Thing> things = bridgeHandler.getThing().getThings();
81 for (Zone zone : zones) {
82 ThingUID discoveredThingId = new ThingUID(THING_TYPE_ZONE, bridge, Integer.toString(zone.getId()));
84 boolean addDiscoveredZone = true;
85 for (Thing thing : things) {
86 if (thing.getUID().equals(discoveredThingId)) {
87 addDiscoveredZone = false;
91 if (addDiscoveredZone) {
92 String label = zone.getName();
94 Map<String, Object> properties = new HashMap<>(3);
95 properties.put(PROPERTY_ZONE_ID, Integer.toString(zone.getId()));
96 properties.put(PROPERTY_NAME, zone.getName());
97 properties.put(Thing.PROPERTY_VENDOR, PROPERTY_VENDOR_NAME);
99 logger.debug("Adding device {} to inbox", discoveredThingId);
100 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(discoveredThingId).withBridge(bridge)
101 .withLabel(label).withProperties(properties).withRepresentationProperty("id").build();
102 thingDiscovered(discoveryResult);
107 public void detectComponents(Collection<Component> components) {
108 ThingUID bridge = bridgeHandler.getThing().getUID();
109 List<Thing> things = bridgeHandler.getThing().getThings();
111 for (Component component : components) {
112 ThingUID discoveredThingId = new ThingUID(THING_TYPE_COMPONENT, bridge,
113 component.getSerialNumber().toString());
115 boolean addDiscoveredComponent = true;
116 for (Thing thing : things) {
117 if (thing.getUID().equals(discoveredThingId)) {
118 addDiscoveredComponent = false;
122 if (addDiscoveredComponent) {
123 String label = component.getName();
125 Map<String, Object> properties = new HashMap<>(4);
126 properties.put(Thing.PROPERTY_SERIAL_NUMBER, component.getSerialNumber().toString());
127 properties.put(PROPERTY_NAME, component.getName());
128 properties.put(Thing.PROPERTY_VENDOR, PROPERTY_VENDOR_NAME);
129 properties.put(PROPERTY_MODEL, component.getSerialNumber().getComponentType());
131 String zoneName = getZoneName(component.getZoneId());
132 if (zoneName != null) {
133 properties.put(PROPERTY_ZONE, zoneName);
136 int zoneId = component.getTemperatureSensorForZoneId();
138 String tempForZoneName = getZoneName(zoneId);
139 if (tempForZoneName != null) {
140 properties.put(PROPERTY_TEMPERATURE_SENSOR_FOR_ZONE, tempForZoneName);
144 logger.debug("Adding device {} to inbox", discoveredThingId);
145 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(discoveredThingId).withBridge(bridge)
146 .withLabel(label).withProperties(properties)
147 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).build();
148 thingDiscovered(discoveryResult);
153 private @Nullable String getZoneName(int zoneId) {
154 Zone zone = bridgeHandler.getZone(zoneId);
159 return zone.getName();