]> git.basschouten.com Git - openhab-addons.git/blob
1705023fbcdbbd45a2efec18cbec81a5a6d36802
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.asuswrt.internal;
14
15 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingConstants.PROPERTY_HOSTNAME;
16 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingConstants.THING_TYPE_ROUTER;
17
18 import java.util.HashMap;
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.jupnp.model.meta.DeviceDetails;
25 import org.jupnp.model.meta.RemoteDevice;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33
34 /**
35  * The {@link AsuswrtDiscoveryParticipant} discovers the ASUS routers using UPnP.
36  *
37  * @author Wouter Born - Initial contribution
38  */
39 @NonNullByDefault
40 @Component(service = UpnpDiscoveryParticipant.class, configurationPid = "discovery.asuswrt")
41 public class AsuswrtDiscoveryParticipant implements UpnpDiscoveryParticipant {
42     @Override
43     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
44         return Set.of(THING_TYPE_ROUTER);
45     }
46
47     @Override
48     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
49         ThingUID uid = getThingUID(device);
50         if (uid == null) {
51             return null;
52         }
53
54         DeviceDetails details = device.getDetails();
55
56         String host = details.getPresentationURI().getHost();
57         String model = details.getModelDetails().getModelNumber();
58
59         Map<String, Object> properties = new HashMap<>();
60         properties.put(PROPERTY_HOSTNAME, host);
61         properties.put(Thing.PROPERTY_VENDOR, details.getManufacturerDetails().getManufacturer());
62         properties.put(Thing.PROPERTY_MODEL_ID, model);
63         properties.put(Thing.PROPERTY_MAC_ADDRESS, details.getSerialNumber());
64
65         String label = String.format("ASUS %s Wireless Router (%s)", model, host);
66
67         return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
68                 .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).build();
69     }
70
71     @Override
72     public @Nullable ThingUID getThingUID(RemoteDevice device) {
73         DeviceDetails details = device.getDetails();
74         String modelName = details.getModelDetails().getModelName();
75         String modelManufacturer = details.getManufacturerDetails().getManufacturer();
76         if (modelManufacturer.toUpperCase().contains("ASUS") && ("ASUS Wireless Router".equalsIgnoreCase(modelName))) {
77             return new ThingUID(THING_TYPE_ROUTER, details.getSerialNumber().replace(':', '-'));
78         }
79         return null;
80     }
81 }