]> git.basschouten.com Git - openhab-addons.git/blob
e4af4b2725c233123b458071e19f1e728e823e4b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.gardena.internal.util;
14
15 import static org.openhab.binding.gardena.internal.GardenaBindingConstants.BINDING_ID;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.openhab.binding.gardena.internal.handler.GardenaDeviceConfig;
21 import org.openhab.binding.gardena.internal.model.Device;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.ThingUID;
26
27 /**
28  * Utility class for converting between a Thing and a Gardena device.
29  *
30  * @author Gerhard Riegler - Initial contribution
31  */
32 public class UidUtils {
33
34     /**
35      * Generates the ThingUID for the given device in the given account.
36      */
37     public static ThingUID generateThingUID(Device device, Bridge account) {
38         ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, device.getCategory());
39         return new ThingUID(thingTypeUID, account.getUID(), device.getId());
40     }
41
42     /**
43      * Returns all ThingUIDs for a given device.
44      */
45     public static List<ThingUID> getThingUIDs(Device device, Bridge account) {
46         List<ThingUID> thingUIDs = new ArrayList<>();
47         for (Thing thing : account.getThings()) {
48             String deviceId = thing.getConfiguration().as(GardenaDeviceConfig.class).deviceId;
49             if (deviceId == null) {
50                 deviceId = thing.getUID().getId();
51             }
52             if (deviceId.equals(device.getId())) {
53                 thingUIDs.add(thing.getUID());
54             }
55         }
56         return thingUIDs;
57     }
58
59     /**
60      * Returns the device id of the Gardena device from the given thing.
61      */
62     public static String getGardenaDeviceId(Thing thing) {
63         String deviceId = thing.getConfiguration().as(GardenaDeviceConfig.class).deviceId;
64         if (deviceId != null) {
65             return deviceId;
66         }
67
68         return thing.getUID().getId();
69     }
70 }