]> git.basschouten.com Git - openhab-addons.git/blob
36e30a756a3e1a06e964dc82041540bdfc2df96f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.sonyprojector.internal.discovery;
14
15 import static org.openhab.binding.sonyprojector.internal.SonyProjectorBindingConstants.THING_TYPE_ETHERNET;
16
17 import java.util.Locale;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.sonyprojector.internal.configuration.SonyProjectorEthernetConfiguration;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.sddp.SddpDevice;
27 import org.openhab.core.config.discovery.sddp.SddpDiscoveryParticipant;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Discovery Service for Sony Projectors that support SDDP.
37  *
38  * @author Laurent Garnier - Initial contribution
39  *
40  */
41 @NonNullByDefault
42 @Component(immediate = true)
43 public class SonyProjectorDiscoveryParticipant implements SddpDiscoveryParticipant {
44     private final Logger logger = LoggerFactory.getLogger(SonyProjectorDiscoveryParticipant.class);
45
46     private static final String SONY = "SONY";
47     private static final String TYPE_PROJECTOR = "PROJECTOR";
48
49     @Override
50     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51         return Set.of(THING_TYPE_ETHERNET);
52     }
53
54     @Override
55     public @Nullable DiscoveryResult createResult(SddpDevice device) {
56         final ThingUID uid = getThingUID(device);
57         if (uid != null) {
58             final String label = device.manufacturer + " " + device.model;
59             final Map<String, Object> properties = Map.of("host", device.ipAddress, //
60                     "port", SonyProjectorEthernetConfiguration.DEFAULT_PORT, //
61                     "model", SonyProjectorEthernetConfiguration.MODEL_AUTO, //
62                     Thing.PROPERTY_MAC_ADDRESS, device.macAddress);
63             logger.debug("Created a DiscoveryResult for device '{}' with UID '{}'", label, uid.getId());
64             return DiscoveryResultBuilder.create(uid).withProperties(properties)
65                     .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).withLabel(label).build();
66         } else {
67             return null;
68         }
69     }
70
71     @Override
72     public @Nullable ThingUID getThingUID(SddpDevice device) {
73         if (device.manufacturer.toUpperCase(Locale.ENGLISH).contains(SONY)
74                 && device.type.toUpperCase(Locale.ENGLISH).contains(TYPE_PROJECTOR) && !device.macAddress.isBlank()
75                 && !device.ipAddress.isBlank()) {
76
77             logger.debug("Sony projector with mac {} found at {}", device.macAddress, device.ipAddress);
78             return new ThingUID(THING_TYPE_ETHERNET, device.macAddress);
79         }
80         return null;
81     }
82 }