]> git.basschouten.com Git - openhab-addons.git/blob
c064d8a5359acdefcb16ec7e9e1dd82db3faf572
[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.epsonprojector.internal.discovery;
14
15 import static org.openhab.binding.epsonprojector.internal.EpsonProjectorBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Locale;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
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  *
37  * Discovery Service for Epson Projectors that support SDDP.
38  *
39  * @author Michael Lobstein - Initial contribution
40  *
41  */
42 @NonNullByDefault
43 @Component(immediate = true)
44 public class EpsonProjectorDiscoveryParticipant implements SddpDiscoveryParticipant {
45     private final Logger logger = LoggerFactory.getLogger(EpsonProjectorDiscoveryParticipant.class);
46
47     private static final String EPSON = "EPSON";
48     private static final String TYPE_PROJECTOR = "PROJECTOR";
49
50     @Override
51     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52         return SUPPORTED_THING_TYPES_UIDS;
53     }
54
55     @Override
56     public @Nullable DiscoveryResult createResult(SddpDevice device) {
57         final ThingUID uid = getThingUID(device);
58         if (uid != null) {
59             final Map<String, Object> properties = new HashMap<>(3);
60             final String label = device.manufacturer + " " + device.model;
61
62             properties.put(Thing.PROPERTY_MAC_ADDRESS, uid.getId());
63             properties.put(THING_PROPERTY_HOST, device.ipAddress);
64             properties.put(THING_PROPERTY_PORT, DEFAULT_PORT);
65
66             final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
67                     .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).withLabel(label).build();
68
69             logger.debug("Created a DiscoveryResult for device '{}' with UID '{}'", label, uid.getId());
70             return result;
71         } else {
72             return null;
73         }
74     }
75
76     @Override
77     public @Nullable ThingUID getThingUID(SddpDevice device) {
78         if (device.manufacturer.toUpperCase(Locale.ENGLISH).contains(EPSON)
79                 && device.type.toUpperCase(Locale.ENGLISH).contains(TYPE_PROJECTOR) && !device.macAddress.isBlank()) {
80
81             logger.debug("Epson projector with mac {} found at {}", device.macAddress, device.ipAddress);
82             return new ThingUID(THING_TYPE_PROJECTOR_TCP,
83                     device.macAddress.replaceAll("-", "").toUpperCase(Locale.ENGLISH));
84         }
85         return null;
86     }
87 }